Search in sources :

Example 56 with Job

use of org.eclipse.core.runtime.jobs.Job in project linuxtools by eclipse.

the class CreateVmCommandHandler method performCreateVM.

private void performCreateVM(String vmName, String boxRef, String vmFile, Map<String, String> environment) {
    final Job createVMJob = new Job(DVMessages.getFormattedString(CREATE_VM_MSG)) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(DVMessages.getFormattedString(CRATE_VM_TITLE, vmName), IProgressMonitor.UNKNOWN);
            IVagrantConnection connection = VagrantService.getInstance();
            if (findVM(connection, vmName) != null) {
                Display.getDefault().syncExec(() -> MessageDialog.openError(Display.getCurrent().getActiveShell(), // $NON-NLS-1$
                WizardMessages.getString("CreateVmCommandHandler.VMExists.title"), // $NON-NLS-1$
                WizardMessages.getString("CreateVmCommandHandler.VMExists.msg")));
                return Status.CANCEL_STATUS;
            }
            IVagrantBox box = null;
            File vagrantDir;
            String boxName = boxRef;
            if (vmFile == null) {
                // The boxRef is a reference to an actual box file/url
                boolean isValidURL = true;
                try {
                    new URL(boxRef);
                } catch (MalformedURLException e1) {
                    isValidURL = false;
                }
                if (Paths.get(boxRef).toFile().canRead() || isValidURL) {
                    try {
                        String boxPath = boxRef;
                        // Generate the box name from the file name (basename)
                        boxName = boxRef.substring(boxRef.lastIndexOf(File.separator) + 1).replace(".box", // $NON-NLS-1$ //$NON-NLS-2$
                        "");
                        connection.addBox(boxName, boxPath, isValidURL);
                    } catch (VagrantException e) {
                    } catch (InterruptedException e) {
                    }
                }
                // Init a new vagrant folder inside plugin metadata
                vagrantDir = performInit(vmName, boxName, connection);
                // box download job might not be complete so we wait
                box = findBox(connection, boxName);
                while (box == null && isValidURL) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    connection.getVMs(true);
                    box = findBox(connection, boxName);
                    if (monitor.isCanceled()) {
                        CommandUtils.delete(vagrantDir);
                        return Status.CANCEL_STATUS;
                    }
                }
            } else {
                vagrantDir = Paths.get(vmFile).getParent().toFile();
            }
            EnvironmentsManager.getSingleton().setEnvironment(vagrantDir, environment);
            String provider = (box == null ? null : box.getProvider());
            connection.up(vagrantDir, provider);
            connection.getVMs(true);
            return Status.OK_STATUS;
        }
    };
    createVMJob.setUser(true);
    createVMJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MalformedURLException(java.net.MalformedURLException) VagrantException(org.eclipse.linuxtools.vagrant.core.VagrantException) IVagrantBox(org.eclipse.linuxtools.vagrant.core.IVagrantBox) IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection) Job(org.eclipse.core.runtime.jobs.Job) File(java.io.File) URL(java.net.URL)

Example 57 with Job

use of org.eclipse.core.runtime.jobs.Job in project linuxtools by eclipse.

the class PackageVMCommandHandler method performPackageVM.

private void performPackageVM(IVagrantVM vm, String name, Path dest) {
    final Job packageVMJob = new Job(Messages.PackageVMCommandHandler_title) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(NLS.bind(Messages.PackageVMCommandHandler_msg, vm.id()), IProgressMonitor.UNKNOWN);
            IVagrantConnection connection = VagrantService.getInstance();
            try {
                connection.packageVM(vm, name);
                // Wait for the box to be created
                Path vmBox = Paths.get(vm.directory().getAbsolutePath(), name);
                while (!vmBox.toFile().exists()) {
                    try {
                        Thread.sleep(1000);
                        if (monitor.isCanceled()) {
                            return Status.CANCEL_STATUS;
                        }
                    } catch (InterruptedException e) {
                    }
                }
                /*
					 * Manually move the new box to the desired destination
					 * since 'virtualbox' and 'libvirt' providers differ
					 * slightly in their support for this option.
					 */
                Files.move(Paths.get(vm.directory().getAbsolutePath(), name), dest.resolve(name), StandardCopyOption.REPLACE_EXISTING);
            } catch (VagrantException | InterruptedException | IOException e) {
                try {
                    Files.delete(Paths.get(vm.directory().getAbsolutePath(), name));
                } catch (IOException e1) {
                }
                Display.getDefault().syncExec(() -> MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PackageVMCommandHandler_failed, NLS.bind(Messages.PackageVMCommandHandler_failed_desc, new String[] { vm.id(), dest.toString(), name })));
            } finally {
                connection.getVMs(true);
            }
            return Status.OK_STATUS;
        }
    };
    packageVMJob.setUser(true);
    packageVMJob.schedule();
}
Also used : Path(java.nio.file.Path) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VagrantException(org.eclipse.linuxtools.vagrant.core.VagrantException) IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection) IOException(java.io.IOException) Job(org.eclipse.core.runtime.jobs.Job)

Example 58 with Job

use of org.eclipse.core.runtime.jobs.Job in project linuxtools by eclipse.

the class RefreshBoxesCommandHandler method execute.

@Override
public Object execute(ExecutionEvent event) {
    final IVagrantConnection connection = VagrantService.getInstance();
    if (connection == null) {
        return null;
    }
    final Job job = new Job(DVMessages.getString(BOX_REFRESH_MSG)) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(DVMessages.getString(BOX_REFRESH_MSG), 1);
            connection.getBoxes(true);
            monitor.worked(1);
            monitor.done();
            return Status.OK_STATUS;
        }
    };
    job.setPriority(Job.LONG);
    job.schedule();
    return null;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection) Job(org.eclipse.core.runtime.jobs.Job)

Example 59 with Job

use of org.eclipse.core.runtime.jobs.Job in project linuxtools by eclipse.

the class VagrantBoxContentProvider method loadImages.

/**
 * Call the {@link IVagrantConnection#getImages(boolean)} in a background
 * job to avoid blocking the UI.
 *
 * @param connection
 *            the selected {@link VagrantConnection}
 */
private void loadImages(final IVagrantConnection connection) {
    final Job loadImagesJob = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    DVMessages.getString("BoxesLoadJob.msg")) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            connection.getBoxes(true);
            Display.getDefault().asyncExec(() -> viewer.refresh());
            return Status.OK_STATUS;
        }
    };
    loadImagesJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 60 with Job

use of org.eclipse.core.runtime.jobs.Job in project linuxtools by eclipse.

the class VagrantVMContentProvider method loadContainers.

/**
 * Call the {@link IVagrantConnection#getContainers(boolean)} in a background job to avoid blocking the UI.
 * @param containersCategory the selected {@link VagrantVMCategory}
 */
private void loadContainers(final IVagrantConnection connection) {
    final Job loadContainersJob = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    DVMessages.getString("VMLoadJob.msg")) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            connection.getVMs(true);
            Display.getDefault().asyncExec(() -> viewer.refresh());
            return Status.OK_STATUS;
        }
    };
    loadContainersJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

Job (org.eclipse.core.runtime.jobs.Job)177 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)134 IStatus (org.eclipse.core.runtime.IStatus)33 IOException (java.io.IOException)27 File (java.io.File)20 Status (org.eclipse.core.runtime.Status)20 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)20 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)20 CoreException (org.eclipse.core.runtime.CoreException)17 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)17 JobFamily (com.cubrid.common.ui.spi.progress.JobFamily)14 DockerException (org.eclipse.linuxtools.docker.core.DockerException)14 IFile (org.eclipse.core.resources.IFile)13 URL (java.net.URL)11 ArrayList (java.util.ArrayList)11 IPath (org.eclipse.core.runtime.IPath)10 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)9 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)9 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)8 Display (org.eclipse.swt.widgets.Display)8