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