Search in sources :

Example 1 with VagrantException

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

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

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

the class DestroyVMCommandHandler method executeInJob.

@Override
void executeInJob(IVagrantVM vm, IProgressMonitor monitor) {
    IVagrantConnection connection = VagrantService.getInstance();
    try {
        connection.destroyVM(vm);
        String stateLoc = Activator.getDefault().getStateLocation().toOSString();
        File vagrantDir = Paths.get(stateLoc, vm.name()).toFile();
        CommandUtils.delete(vagrantDir);
    } catch (VagrantException | InterruptedException e) {
        final String errorMessage = Messages.DestroyVMCommandHandler_error + vm.id();
        openError(errorMessage, e);
    } finally {
        // always get images as we sometimes get errors on intermediate
        // images
        // being removed but we will remove some top ones successfully
        connection.getVMs(true);
    }
}
Also used : VagrantException(org.eclipse.linuxtools.vagrant.core.VagrantException) IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection) File(java.io.File)

Example 4 with VagrantException

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

the class AddBoxCommandHandler method performPullImage.

private void performPullImage(final String boxName, final String boxLoc) {
    final Job pullImageJob = new Job(DVMessages.getFormattedString(PULL_IMAGE_JOB_TITLE, boxName)) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(DVMessages.getString(PULL_IMAGE_JOB_TASK), IProgressMonitor.UNKNOWN);
            // handler refresh the images when done
            try {
                IVagrantConnection connection = VagrantService.getInstance();
                boolean isValidURL = true;
                try {
                    new URL(boxLoc);
                } catch (MalformedURLException e) {
                    isValidURL = false;
                }
                connection.addBox(boxName, boxLoc, isValidURL);
                connection.getBoxes(true);
            } catch (final VagrantException e) {
                Display.getDefault().syncExec(() -> MessageDialog.openError(Display.getCurrent().getActiveShell(), DVMessages.getFormattedString(ERROR_PULLING_IMAGE, boxName), e.getMessage()));
            // for now
            } catch (InterruptedException e) {
            // do nothing
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    pullImageJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MalformedURLException(java.net.MalformedURLException) VagrantException(org.eclipse.linuxtools.vagrant.core.VagrantException) IVagrantConnection(org.eclipse.linuxtools.vagrant.core.IVagrantConnection) Job(org.eclipse.core.runtime.jobs.Job) URL(java.net.URL)

Aggregations

IVagrantConnection (org.eclipse.linuxtools.vagrant.core.IVagrantConnection)4 VagrantException (org.eclipse.linuxtools.vagrant.core.VagrantException)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 Job (org.eclipse.core.runtime.jobs.Job)3 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