Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa09375b7aec90383177dc19803a3ed08c5eb4a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
 * Copyright (c) 2015 Red Hat.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.ui.commands;

import java.io.OutputStream;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerContainerConfig;
import org.eclipse.linuxtools.docker.core.IDockerContainerInfo;
import org.eclipse.linuxtools.docker.core.IDockerHostConfig;
import org.eclipse.linuxtools.docker.core.IDockerImage;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
import org.eclipse.linuxtools.internal.docker.ui.RunConsole;
import org.eclipse.linuxtools.internal.docker.ui.views.DVMessages;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerImagesView;
import org.eclipse.linuxtools.internal.docker.ui.wizards.ContainerCreate;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;

public class CreateContainerCommandHandler extends AbstractHandler {

	private final static String CREATE_CONTAINER_JOB_TITLE = "ContainerCreateTitle.msg"; //$NON-NLS-1$
	private final static String CREATE_CONTAINER_MSG = "ContainerCreate.msg"; //$NON-NLS-1$
	private static final String ERROR_CREATING_IMAGE = "ContainerCreateError.msg"; //$NON-NLS-1$
	
	private IDockerConnection connection;
	private IDockerImage image;

	@Override
	public Object execute(final ExecutionEvent event) {
		final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
		List<IDockerImage> selectedImages = CommandUtils
				.getSelectedImages(activePart);
		if (activePart instanceof DockerImagesView) {
			connection = ((DockerImagesView) activePart).getConnection();
		}
		if (selectedImages.size() != 1 || connection == null)
			return null;
		image = selectedImages.get(0);
		final ContainerCreate wizard;
		if (!image.isDangling() && !image.isIntermediateImage())
			wizard = new ContainerCreate(connection, image.repoTags().get(0));
		else
			wizard = new ContainerCreate(connection, image.id()
					.substring(0, 12));
		final boolean tagImage = CommandUtils.openWizard(wizard,
				HandlerUtil.getActiveShell(event));
		if (tagImage) {
			if (activePart instanceof DockerImagesView) {
				connection = ((DockerImagesView) activePart)
						.getConnection();
			}
			performCreateContainer(wizard);
		}
		return null;
	}
	
	private void performCreateContainer(final ContainerCreate wizard) {
		final Job createContainerJob = new Job(
				DVMessages.getString(CREATE_CONTAINER_JOB_TITLE)) {

			@Override
			protected IStatus run(final IProgressMonitor monitor) {
				final IDockerContainerConfig config = wizard.getConfig();
				final IDockerHostConfig hostConfig = wizard.getHostConfig();
				final String image = wizard.getImageId();
				monitor.beginTask(DVMessages.getString(CREATE_CONTAINER_MSG), 4);
				// pull the image and let the progress
				// handler refresh the images when done
				try {
					final String containerId = ((DockerConnection) connection)
							.createContainer(config);
					monitor.worked(1);
					IDockerContainerInfo info = ((DockerConnection) connection)
							.getContainerInfo(containerId);
					String name = info.name();
					if (name.startsWith("/")) //$NON-NLS-1$
						name = name.replaceFirst("/", ""); //$NON-NLS-1$ //$NON-NLS-2$
					monitor.worked(1);
					OutputStream stream = null;
					RunConsole rc = RunConsole.findConsole(containerId,
							RunConsole.DEFAULT_ID, name);
					rc.attachToConsole(connection);
					monitor.worked(1);
					if (rc != null) {
						stream = rc.getOutputStream();
					}
					final OutputStream logstream = stream;
					((DockerConnection) connection).startContainer(containerId,
							hostConfig, logstream);
					monitor.worked(1);
				} catch (final DockerException e) {
					Display.getDefault().syncExec(new Runnable() {

						@Override
						public void run() {
							MessageDialog.openError(Display.getCurrent()
									.getActiveShell(), DVMessages
									.getFormattedString(ERROR_CREATING_IMAGE,
											image), e.getMessage());

						}

					});
					// for now
				} catch (InterruptedException e) {
					// do nothing
				} finally {
					monitor.done();
				}
				return Status.OK_STATUS;
			}

		};

		createContainerJob.schedule();

	}

}

Back to the top