Search in sources :

Example 1 with IVagrantConnection

use of org.eclipse.linuxtools.vagrant.core.IVagrantConnection 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 2 with IVagrantConnection

use of org.eclipse.linuxtools.vagrant.core.IVagrantConnection 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 3 with IVagrantConnection

use of org.eclipse.linuxtools.vagrant.core.IVagrantConnection 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 4 with IVagrantConnection

use of org.eclipse.linuxtools.vagrant.core.IVagrantConnection in project linuxtools by eclipse.

the class StartVMCommandHandler method executeInJob.

@Override
void executeInJob(IVagrantVM vm, IProgressMonitor monitor) {
    IVagrantConnection connection = VagrantService.getInstance();
    connection.up(vm.directory(), vm.provider());
    connection.getVMs(true);
}
Also used : IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection)

Example 5 with IVagrantConnection

use of org.eclipse.linuxtools.vagrant.core.IVagrantConnection in project linuxtools by eclipse.

the class VagrantBoxContentProvider method getElements.

@Override
public Object[] getElements(final Object inputElement) {
    if (inputElement instanceof IVagrantConnection) {
        final IVagrantConnection connection = (IVagrantConnection) inputElement;
        if (connection.isBoxesLoaded()) {
            return connection.getBoxes().toArray();
        }
        loadImages(connection);
        return EMPTY;
    }
    return EMPTY;
}
Also used : IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection)

Aggregations

IVagrantConnection (org.eclipse.linuxtools.vagrant.core.IVagrantConnection)9 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 Job (org.eclipse.core.runtime.jobs.Job)5 VagrantException (org.eclipse.linuxtools.vagrant.core.VagrantException)4 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 IVagrantBox (org.eclipse.linuxtools.vagrant.core.IVagrantBox)1