refactor container feature · microsoft/azure-tools-for-java@c81a8da · GitHub
Skip to content

Commit c81a8da

Browse files
committed
refactor container feature
1 parent 25232a3 commit c81a8da

7 files changed

Lines changed: 228 additions & 129 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions

‎PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/com/microsoft/azuretools/container/Activator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void start(BundleContext context) throws Exception {
5757
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
5858
*/
5959
public void stop(BundleContext context) throws Exception {
60-
Runtime.getInstance().cleanRuningContainer();
60+
DockerRuntime.getInstance().cleanRuningContainer();
6161
plugin = null;
6262
super.stop(context);
6363
}

PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/com/microsoft/azuretools/container/Runtime.java renamed to PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/com/microsoft/azuretools/container/DockerRuntime.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,57 @@
2828
import com.spotify.docker.client.exceptions.DockerCertificateException;
2929
import com.spotify.docker.client.exceptions.DockerException;
3030

31-
public class Runtime {
32-
private static final Runtime INSTANCE = new Runtime();
31+
public class DockerRuntime {
32+
private static final DockerRuntime INSTANCE = new DockerRuntime();
3333
private String runningContainerId = null;
3434
private Builder dockerBuilder = null;
3535

36-
private Runtime() {
36+
private String registryUrl = null;
37+
private String registryUsername = null;
38+
private String registryPassword = null;
39+
private String latestImageName = null;
40+
41+
public synchronized String getLatestImageName() {
42+
return latestImageName;
43+
}
44+
45+
public synchronized void setLatestImageName(String latestImageName) {
46+
this.latestImageName = latestImageName;
47+
}
48+
49+
public synchronized String getRegistryUrl() {
50+
return registryUrl;
51+
}
52+
53+
public synchronized void setRegistryUrl(String registryUrl) {
54+
this.registryUrl = registryUrl;
55+
}
56+
57+
public synchronized String getRegistryUsername() {
58+
return registryUsername;
59+
}
60+
61+
public synchronized void setRegistryUsername(String registryUsername) {
62+
this.registryUsername = registryUsername;
63+
}
64+
65+
public synchronized String getRegistryPassword() {
66+
return registryPassword;
67+
}
68+
69+
public synchronized void setRegistryPassword(String registryPassword) {
70+
this.registryPassword = registryPassword;
71+
}
72+
73+
private DockerRuntime() {
3774
try {
3875
dockerBuilder = DefaultDockerClient.fromEnv();
3976
} catch (DockerCertificateException e) {
4077
e.printStackTrace();
4178
}
4279
}
4380

44-
public static Runtime getInstance() {
81+
public static DockerRuntime getInstance() {
4582
return INSTANCE;
4683
}
4784

‎PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/com/microsoft/azuretools/container/handlers/DockerRunHandler.java‎

Lines changed: 13 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,22 @@
2424

2525
import com.microsoft.azuretools.container.ConsoleLogger;
2626
import com.microsoft.azuretools.container.Constant;
27-
import com.microsoft.azuretools.container.Runtime;
27+
import com.microsoft.azuretools.container.DockerRuntime;
28+
import com.microsoft.azuretools.container.utils.DockerUtil;
29+
import com.microsoft.azuretools.container.utils.WarUtil;
2830
import com.microsoft.azuretools.core.utils.AzureAbstractHandler;
2931
import com.microsoft.azuretools.core.utils.PluginUtil;
3032
import com.microsoft.tooling.msservices.components.DefaultLoader;
3133
import com.spotify.docker.client.DefaultDockerClient.Builder;
3234
import com.spotify.docker.client.DockerClient;
33-
import com.spotify.docker.client.ProgressHandler;
34-
import com.spotify.docker.client.exceptions.DockerCertificateException;
35-
import com.spotify.docker.client.exceptions.DockerException;
36-
import com.spotify.docker.client.messages.Container;
37-
import com.spotify.docker.client.messages.ContainerConfig;
38-
import com.spotify.docker.client.messages.ContainerCreation;
39-
import com.spotify.docker.client.messages.HostConfig;
40-
import com.spotify.docker.client.messages.PortBinding;
41-
import com.spotify.docker.client.messages.ProgressMessage;
42-
43-
import java.io.IOException;
44-
import java.nio.file.Paths;
45-
import java.util.ArrayList;
46-
import java.util.HashMap;
47-
import java.util.List;
48-
import java.util.Map;
49-
import java.util.Optional;
50-
import java.util.concurrent.atomic.AtomicReference;
5135

5236
import org.eclipse.core.commands.ExecutionEvent;
5337
import org.eclipse.core.commands.ExecutionException;
5438
import org.eclipse.core.resources.IProject;
55-
import org.eclipse.core.resources.IncrementalProjectBuilder;
5639
import org.eclipse.jface.dialogs.MessageDialog;
57-
import org.eclipse.jst.j2ee.datamodel.properties.IJ2EEComponentExportDataModelProperties;
58-
import org.eclipse.jst.j2ee.internal.web.archive.operations.WebComponentExportDataModelProvider;
5940
import org.eclipse.ui.IWorkbenchWindow;
6041
import org.eclipse.ui.handlers.HandlerUtil;
61-
import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory;
62-
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
42+
6343

6444
public class DockerRunHandler extends AzureAbstractHandler {
6545

@@ -79,15 +59,15 @@ public Object onExecute(ExecutionEvent event) throws ExecutionException {
7959
try {
8060
// Initialize docker client according to env DOCKER_HOST & DOCKER_CERT_PATH
8161
ConsoleLogger.info(Constant.MESSAGE_DOCKER_CONNECTING);
82-
Builder dockerBuilder = Runtime.getInstance().getDockerBuilder();
62+
Builder dockerBuilder = DockerRuntime.getInstance().getDockerBuilder();
8363
docker = dockerBuilder.build();
8464
// Stop running container
85-
String runningContainerId = Runtime.getInstance().getRunningContainerId();
86-
if (containerExists(docker, runningContainerId)) {
87-
boolean stop = MessageDialog.openConfirm(window.getShell(), "Stop",
65+
String runningContainerId = DockerRuntime.getInstance().getRunningContainerId();
66+
if (DockerUtil.containerExists(docker, runningContainerId)) {
67+
boolean stop = MessageDialog.openConfirm(window.getShell(), "Confirmation",
8868
Constant.MESSAGE_CONFIRM_STOP_CONTAINER);
8969
if (stop) {
90-
Runtime.getInstance().cleanRuningContainer();
70+
DockerRuntime.getInstance().cleanRuningContainer();
9171
} else {
9272
return null;
9373
}
@@ -105,20 +85,20 @@ public Object onExecute(ExecutionEvent event) throws ExecutionException {
10585
DefaultLoader.getIdeHelper().invokeAndWait(() -> {
10686
ConsoleLogger.info(String.format(Constant.MESSAGE_EXPORTING_PROJECT, destinationPath));
10787
});
108-
export(project, destinationPath);
88+
WarUtil.export(project, destinationPath);
10989

11090
// build image based on WAR file
11191
DefaultLoader.getIdeHelper().invokeAndWait(() -> {
11292
ConsoleLogger.info(Constant.MESSAGE_BUILDING_IMAGE);
11393
});
114-
String imageName = build(docker, project,
94+
String imageName = DockerUtil.buildImage(docker, project,
11595
project.getLocation() + Constant.DOCKER_CONTEXT_FOLDER);
11696
DefaultLoader.getIdeHelper().invokeAndWait(() -> {
11797
ConsoleLogger.info(String.format(Constant.MESSAGE_IMAGE_INFO, imageName));
11898
});
11999

120100
// create a container
121-
String containerId = createContainer(docker, project, imageName);
101+
String containerId = DockerUtil.createContainer(docker, project, imageName);
122102
DefaultLoader.getIdeHelper().invokeAndWait(() -> {
123103
ConsoleLogger.info(Constant.MESSAGE_CREATING_CONTAINER);
124104
ConsoleLogger.info(String.format(Constant.MESSAGE_CONTAINER_INFO, containerId));
@@ -128,7 +108,7 @@ public Object onExecute(ExecutionEvent event) throws ExecutionException {
128108
DefaultLoader.getIdeHelper().invokeAndWait(() -> {
129109
ConsoleLogger.info(Constant.MESSAGE_STARTING_CONTAINER);
130110
});
131-
String webappUrl = runContainer(docker, containerId);
111+
String webappUrl = DockerUtil.runContainer(docker, containerId);
132112
DefaultLoader.getIdeHelper().invokeAndWait(() -> {
133113
ConsoleLogger.info(
134114
String.format(Constant.MESSAGE_CONTAINER_STARTED, webappUrl, project.getName()));
@@ -142,68 +122,4 @@ public Object onExecute(ExecutionEvent event) throws ExecutionException {
142122
});
143123
return null;
144124
}
145-
146-
private String createContainer(DockerClient docker, IProject project, String imageName)
147-
throws DockerException, InterruptedException {
148-
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
149-
List<PortBinding> randomPort = new ArrayList<>();
150-
PortBinding randomBinding = PortBinding.randomPort("0.0.0.0");
151-
randomPort.add(randomBinding);
152-
portBindings.put(Constant.TOMCAT_SERVICE_PORT, randomPort);
153-
154-
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
155-
156-
final ContainerConfig config = ContainerConfig.builder().hostConfig(hostConfig).image(imageName)
157-
.exposedPorts(Constant.TOMCAT_SERVICE_PORT).build();
158-
final ContainerCreation container = docker.createContainer(config);
159-
return container.id();
160-
}
161-
162-
private String runContainer(DockerClient docker, String containerId) throws Exception {
163-
docker.startContainer(containerId);
164-
final List<Container> containers = docker.listContainers();
165-
final Optional<Container> res = containers.stream().filter(item -> item.id().equals(containerId)).findFirst();
166-
167-
if (res.isPresent()) {
168-
Runtime.getInstance().setRunningContainerId(res.get().id());
169-
return String.format("http://%s:%s", docker.getHost(), res.get().ports().stream()
170-
.filter(item -> item.privatePort().equals(8080)).findFirst().get().publicPort());
171-
} else {
172-
String errorMsg = String.format(Constant.ERROR_STARTING_CONTAINER, containerId);
173-
ConsoleLogger.error(errorMsg);
174-
throw new Exception(errorMsg);
175-
}
176-
}
177-
178-
private String build(DockerClient docker, IProject project, String dockerDirectory)
179-
throws DockerCertificateException, DockerException, InterruptedException, IOException {
180-
final AtomicReference<String> imageIdFromMessage = new AtomicReference<>();
181-
final String imageName = String.format("%s-%s:%tY%<tm%<td%<tH%<tM%<tS", Constant.IMAGE_PREFIX,
182-
project.getName().toLowerCase(), new java.util.Date());
183-
docker.build(Paths.get(dockerDirectory), imageName, new ProgressHandler() {
184-
@Override
185-
public void progress(ProgressMessage message) throws DockerException {
186-
final String imageId = message.buildImageId();
187-
if (imageId != null) {
188-
imageIdFromMessage.set(imageId);
189-
}
190-
}
191-
});
192-
return imageName;
193-
}
194-
195-
private void export(IProject project, String destinationPath) throws Exception {
196-
String projectName = project.getName();
197-
project.build(IncrementalProjectBuilder.FULL_BUILD, null);
198-
IDataModel dataModel = DataModelFactory.createDataModel(new WebComponentExportDataModelProvider());
199-
dataModel.setProperty(IJ2EEComponentExportDataModelProperties.PROJECT_NAME, projectName);
200-
dataModel.setProperty(IJ2EEComponentExportDataModelProperties.ARCHIVE_DESTINATION, destinationPath);
201-
dataModel.getDefaultOperation().execute(null, null);
202-
}
203-
204-
private boolean containerExists(DockerClient docker, String containerId)
205-
throws DockerException, InterruptedException {
206-
long count = docker.listContainers().stream().filter(item -> item.id().equals(containerId)).count();
207-
return (count > 0);
208-
}
209125
}

‎PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/com/microsoft/azuretools/container/handlers/DockerizeHandler.java‎

Lines changed: 5 additions & 27 deletions

0 commit comments

Comments
 (0)