2424
2525import com .microsoft .azuretools .container .ConsoleLogger ;
2626import 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 ;
2830import com .microsoft .azuretools .core .utils .AzureAbstractHandler ;
2931import com .microsoft .azuretools .core .utils .PluginUtil ;
3032import com .microsoft .tooling .msservices .components .DefaultLoader ;
3133import com .spotify .docker .client .DefaultDockerClient .Builder ;
3234import 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
5236import org .eclipse .core .commands .ExecutionEvent ;
5337import org .eclipse .core .commands .ExecutionException ;
5438import org .eclipse .core .resources .IProject ;
55- import org .eclipse .core .resources .IncrementalProjectBuilder ;
5639import 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 ;
5940import org .eclipse .ui .IWorkbenchWindow ;
6041import 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
6444public 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}
0 commit comments