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();
}
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();
}
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);
}
}
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();
}
Aggregations