use of org.eclipse.linuxtools.internal.docker.ui.wizards.DataVolumeModel in project linuxtools by eclipse.
the class LaunchConfigurationUtils method createRunImageLaunchConfiguration.
/**
* Creates a new {@link ILaunchConfiguration} for the given
* {@link IDockerContainer}.
*
* @param baseConfigurationName
* the base configuration name to use when creating the
* {@link ILaunchConfiguration}.
* @param image
* the {@link IDockerImage} used to create the container
* @param containerName
* the actual container name (given by the user or generated by
* the Docker daemon)
* @param containerConfig
* @param hostConfig
* the user-provided {@link IDockerHostConfig} (created
* container's one)
* @param removeWhenExits
* flag to indicate if container should be removed when exited
* @return the generated {@link ILaunchConfiguration}
*/
public static ILaunchConfiguration createRunImageLaunchConfiguration(final IDockerImage image, final IDockerContainerConfig containerConfig, final IDockerHostConfig hostConfig, final String containerName, final boolean removeWhenExits) {
try {
final ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
final ILaunchConfigurationType type = manager.getLaunchConfigurationType(IRunDockerImageLaunchConfigurationConstants.CONFIG_TYPE_ID);
final String imageName = createRunImageLaunchConfigurationName(image);
// using the image repo + first tag
final ILaunchConfigurationWorkingCopy workingCopy = getLaunchConfigurationWorkingCopy(type, imageName);
workingCopy.setAttribute(CREATION_DATE, DATE_FORMAT.format(new Date()));
workingCopy.setAttribute(CONNECTION_NAME, image.getConnection().getName());
workingCopy.setAttribute(IMAGE_ID, image.id());
workingCopy.setAttribute(IMAGE_NAME, createRunImageLaunchConfigurationName(image));
if (containerName != null && !containerName.isEmpty()) {
workingCopy.setAttribute(CONTAINER_NAME, containerName);
}
// if we know the raw command string, use it since the container
// config will remove quotes to split up command properly
DockerContainerConfig config = (DockerContainerConfig) containerConfig;
if (config.rawcmd() != null) {
workingCopy.setAttribute(COMMAND, config.rawcmd());
} else {
workingCopy.setAttribute(COMMAND, toString(containerConfig.cmd()));
}
workingCopy.setAttribute(ENTRYPOINT, toString(containerConfig.entrypoint()));
// selected ports
workingCopy.setAttribute(PUBLISH_ALL_PORTS, hostConfig.publishAllPorts());
// format: <containerPort><type>:<hostIP>:<hostPort>
if (hostConfig.publishAllPorts()) {
final IDockerImageInfo imageInfo = image.getConnection().getImageInfo(image.id());
if (imageInfo != null) {
workingCopy.setAttribute(PUBLISHED_PORTS, serializePortBindings(imageInfo.containerConfig().exposedPorts()));
}
} else {
workingCopy.setAttribute(PUBLISHED_PORTS, serializePortBindings(hostConfig.portBindings()));
}
// links (with format being: "<containerName>:<containerAlias>")
workingCopy.setAttribute(LINKS, hostConfig.links());
// env variables
workingCopy.setAttribute(ENV_VARIABLES, containerConfig.env());
// labels
workingCopy.setAttribute(LABELS, containerConfig.labels());
// volumes
final List<String> volumes = new ArrayList<>();
// volumes from other containers
for (String volumeFrom : hostConfig.volumesFrom()) {
final DataVolumeModel volume = DataVolumeModel.parseVolumeFrom(volumeFrom);
if (volume != null) {
volumes.add(volume.toString());
}
}
// bindings to host directory or file
for (String bind : hostConfig.binds()) {
final DataVolumeModel volume = DataVolumeModel.parseHostBinding(bind);
if (volume != null) {
volumes.add(volume.toString());
}
}
// TODO: container path declaration
workingCopy.setAttribute(DATA_VOLUMES, volumes);
// options
workingCopy.setAttribute(AUTO_REMOVE, removeWhenExits);
workingCopy.setAttribute(ALLOCATE_PSEUDO_CONSOLE, containerConfig.tty());
workingCopy.setAttribute(INTERACTIVE, containerConfig.openStdin());
workingCopy.setAttribute(PRIVILEGED, hostConfig.privileged());
workingCopy.setAttribute(READONLY, ((DockerHostConfig) hostConfig).readonlyRootfs());
if (hostConfig.networkMode() != null)
workingCopy.setAttribute(NETWORK_MODE, hostConfig.networkMode());
// resources limitations
if (containerConfig.memory() != null) {
workingCopy.setAttribute(ENABLE_LIMITS, true);
// memory in containerConfig is expressed in bytes
workingCopy.setAttribute(MEMORY_LIMIT, Long.toString(containerConfig.memory().longValue() / MB));
}
if (containerConfig.cpuShares() != null) {
workingCopy.setAttribute(ENABLE_LIMITS, true);
workingCopy.setAttribute(CPU_PRIORITY, containerConfig.cpuShares().toString());
}
return workingCopy.doSave();
} catch (CoreException e) {
Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, LaunchMessages.getString(// $NON-NLS-1$
"RunDockerImageLaunchConfiguration.creation.failure"), e));
}
return null;
}
use of org.eclipse.linuxtools.internal.docker.ui.wizards.DataVolumeModel in project linuxtools by eclipse.
the class RunImageVolumesTab method onRemoveDataVolumes.
private SelectionListener onRemoveDataVolumes(final TableViewer dataVolumesTableViewer) {
return SelectionListener.widgetSelectedAdapter(e -> {
final IStructuredSelection selection = dataVolumesTableViewer.getStructuredSelection();
for (@SuppressWarnings("unchecked") Iterator<DataVolumeModel> iterator = selection.iterator(); iterator.hasNext(); ) {
final DataVolumeModel volume = iterator.next();
model.removeDataVolume(volume);
model.getSelectedDataVolumes().remove(volume);
}
updateLaunchConfigurationDialog();
});
}
use of org.eclipse.linuxtools.internal.docker.ui.wizards.DataVolumeModel in project linuxtools by eclipse.
the class RunImageVolumesTab method performApply.
@Override
public void performApply(ILaunchConfigurationWorkingCopy configuration) {
if (model == null)
return;
WritableList<DataVolumeModel> volumes = model.getDataVolumes();
Set<DataVolumeModel> selectedVolumes = model.getSelectedDataVolumes();
ArrayList<String> binds = new ArrayList<>();
ArrayList<String> volumesFrom = new ArrayList<>();
ArrayList<String> volumesList = new ArrayList<>();
Set<String> selectedVolumesSet = new TreeSet<>();
for (Iterator<DataVolumeModel> iterator = volumes.iterator(); iterator.hasNext(); ) {
DataVolumeModel volume = iterator.next();
StringBuffer buffer = new StringBuffer();
volumesList.add(volume.toString());
switch(volume.getMountType()) {
case HOST_FILE_SYSTEM:
buffer.append(volume.getHostPathMount() + "," + volume.getHostPathMount() + "," + volume.isReadOnly());
if (selectedVolumes.contains(volume)) {
selectedVolumesSet.add(volume.toString());
String bind = LaunchConfigurationUtils.convertToUnixPath(volume.getHostPathMount()) + ':' + volume.getContainerPath() + ':' + 'Z';
if (volume.isReadOnly()) {
// $NON-NLS-1$
bind += ",ro";
}
binds.add(bind);
}
break;
case CONTAINER:
if (selectedVolumes.contains(volume)) {
selectedVolumesSet.add(volume.toString());
volumesFrom.add(volume.getContainerMount());
}
break;
default:
break;
}
}
configuration.setAttribute(IRunDockerImageLaunchConfigurationConstants.BINDS, binds);
configuration.setAttribute(IRunDockerImageLaunchConfigurationConstants.VOLUMES_FROM, volumesFrom);
configuration.setAttribute(IRunDockerImageLaunchConfigurationConstants.DATA_VOLUMES, volumesList);
}
use of org.eclipse.linuxtools.internal.docker.ui.wizards.DataVolumeModel in project linuxtools by eclipse.
the class RunImageVolumesTab method createVolumeSettingsContainer.
private void createVolumeSettingsContainer(final Composite container) {
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).span(3, 1).applyTo(new Label(container, SWT.NONE));
final Label volumesLabel = new Label(container, SWT.NONE);
volumesLabel.setText(WizardMessages.getString(// $NON-NLS-1$
"ImageRunResourceVolVarPage.dataVolumesLabel"));
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).span(COLUMNS, 1).applyTo(volumesLabel);
final CheckboxTableViewer dataVolumesTableViewer = createVolumesTable(container);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).hint(200, 100).applyTo(dataVolumesTableViewer.getTable());
// update table content when selected image changes
bind(dataVolumesTableViewer, model.getDataVolumes(), BeanProperties.values(DataVolumeModel.class, DataVolumeModel.CONTAINER_PATH, DataVolumeModel.MOUNT, DataVolumeModel.READ_ONLY_VOLUME));
final IObservableSet selectedVolumesObservable = BeanProperties.set(ImageRunResourceVolumesVariablesModel.SELECTED_DATA_VOLUMES).observe(model);
dbc.bindSet(ViewersObservables.observeCheckedElements(dataVolumesTableViewer, DataVolumeModel.class), selectedVolumesObservable);
dataVolumesTableViewer.addCheckStateListener(onCheckStateChanged());
// initializes the checkboxes selection upon loading the model.
// remove ?
// selectedVolumesObservable.addChangeListener(new
// IChangeListener() {
//
// @Override
// public void handleChange(ChangeEvent event) {
// final IObservableSet observable = (IObservableSet) event
// .getObservable();
// for (Iterator<?> iterator = observable.iterator(); iterator
// .hasNext();) {
// final DataVolumeModel volume = (DataVolumeModel) iterator
// .next();
// dataVolumesTableViewer.setChecked(volume, true);
// }
// updateLaunchConfigurationDialog();
// }
// });
// buttons
final Composite buttonsContainers = new Composite(container, SWT.NONE);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(false, false).applyTo(buttonsContainers);
GridLayoutFactory.fillDefaults().numColumns(1).margins(0, 0).spacing(SWT.DEFAULT, 0).applyTo(buttonsContainers);
final Button addButton = new Button(buttonsContainers, SWT.NONE);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).applyTo(addButton);
addButton.setText(WizardMessages.getString(// $NON-NLS-1$
"ImageRunResourceVolVarPage.addButton"));
addButton.addSelectionListener(onAddDataVolume(dataVolumesTableViewer));
final Button editButton = new Button(buttonsContainers, SWT.NONE);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).applyTo(editButton);
editButton.setText(WizardMessages.getString(// $NON-NLS-1$
"ImageRunResourceVolVarPage.editButton"));
editButton.addSelectionListener(onEditDataVolume(dataVolumesTableViewer));
editButton.setEnabled(false);
final Button removeButton = new Button(buttonsContainers, SWT.NONE);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).applyTo(removeButton);
removeButton.setText(WizardMessages.getString(// $NON-NLS-1$
"ImageRunResourceVolVarPage.removeButton"));
removeButton.addSelectionListener(onRemoveDataVolumes(dataVolumesTableViewer));
removeButton.setEnabled(false);
// disable the edit and removeButton if the table is empty
dataVolumesTableViewer.addSelectionChangedListener(onSelectionChanged(editButton, removeButton));
}
use of org.eclipse.linuxtools.internal.docker.ui.wizards.DataVolumeModel in project linuxtools by eclipse.
the class RunImageVolumesTab method onEditDataVolume.
private SelectionListener onEditDataVolume(final CheckboxTableViewer dataVolumesTableViewer) {
return SelectionListener.widgetSelectedAdapter(e -> {
final IStructuredSelection selection = (IStructuredSelection) dataVolumesTableViewer.getSelection();
if (selection.isEmpty()) {
return;
}
final DataVolumeModel selectedDataVolume = (DataVolumeModel) selection.getFirstElement();
final ContainerDataVolumeDialog dialog = new ContainerDataVolumeDialog(getShell(), model.getConnection(), selectedDataVolume);
dialog.create();
if (dialog.open() == IDialogConstants.OK_ID) {
final DataVolumeModel dialogDataVolume = dialog.getDataVolume();
selectedDataVolume.setContainerMount(dialogDataVolume.getContainerMount());
selectedDataVolume.setMountType(dialogDataVolume.getMountType());
selectedDataVolume.setHostPathMount(dialogDataVolume.getHostPathMount());
selectedDataVolume.setContainerMount(dialogDataVolume.getContainerMount());
selectedDataVolume.setReadOnly(dialogDataVolume.isReadOnly());
model.getSelectedDataVolumes().add(selectedDataVolume);
dataVolumesTableViewer.setChecked(selectedDataVolume, true);
updateLaunchConfigurationDialog();
}
});
}
Aggregations