diff --git a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/DockerRunDialog.java b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/DockerRunDialog.java index 7a853af40d..4a09fb5cfa 100644 --- a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/DockerRunDialog.java +++ b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/DockerRunDialog.java @@ -25,7 +25,6 @@ import static com.microsoft.azuretools.telemetry.TelemetryConstants.DEPLOY_WEBAPP_DOCKERLOCAL; import static com.microsoft.azuretools.telemetry.TelemetryConstants.WEBAPP; -import com.microsoft.azure.common.exceptions.AzureExecutionException; import com.microsoft.azuretools.telemetrywrapper.ErrorType; import com.microsoft.azuretools.telemetrywrapper.EventUtil; import com.microsoft.azuretools.telemetrywrapper.Operation; @@ -42,8 +41,6 @@ import java.util.HashMap; import java.util.Map; -import com.microsoft.tooling.msservices.components.DefaultLoader; -import com.spotify.docker.client.exceptions.DockerException; import org.apache.commons.io.FilenameUtils; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; @@ -107,7 +104,6 @@ public class DockerRunDialog extends AzureTitleAreaDialogWrapper { private static final String IMAGE_NAME_PREFIX = "localimage"; private static final String DEFAULT_TAG_NAME = "latest"; private static final String SELECT_DOCKER_FILE = "Browse..."; - private static final String DOCKER_PING_ERROR = "Failed to connect docker host: %s\nIs Docker installed and running?"; private DockerHostRunSetting dataModel; private Text txtDockerHost; @@ -349,13 +345,7 @@ private void execute() { ConsoleLogger.info(String.format("Building image ... [%s]", imageNameWithTag)); DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath()); - try { - docker.ping(); - } catch (DockerException | InterruptedException e) { - final String msg = String.format(DOCKER_PING_ERROR, dataModel.getDockerHost()); - DefaultLoader.getUIHelper().showError(msg, "Failed to connect docker host"); - throw new AzureExecutionException(String.format("Failed to connect docker host: %s", dataModel.getDockerHost())); - } + DockerUtil.ping(docker); DockerUtil.buildImage(docker, imageNameWithTag, targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler()); diff --git a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PublishWebAppOnLinuxDialog.java b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PublishWebAppOnLinuxDialog.java index ba2b066591..a15e548722 100644 --- a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PublishWebAppOnLinuxDialog.java +++ b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PublishWebAppOnLinuxDialog.java @@ -486,7 +486,7 @@ private void execute() { PrivateRegistryImageSetting acrInfo = model.getPrivateRegistryImageSetting(); ConsoleLogger.info(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl())); DockerClient docker = DefaultDockerClient.fromEnv().build(); - + DockerUtil.ping(docker); DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler()); diff --git a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PushImageDialog.java b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PushImageDialog.java index 7051ea7d30..52f80d70f7 100644 --- a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PushImageDialog.java +++ b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/ui/PushImageDialog.java @@ -294,6 +294,7 @@ private void execute() { PrivateRegistryImageSetting acrInfo = model.getPrivateRegistryImageSetting(); ConsoleLogger.info(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl())); DockerClient docker = DefaultDockerClient.fromEnv().build(); + DockerUtil.ping(docker); DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler()); diff --git a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/utils/DockerUtil.java b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/utils/DockerUtil.java index 4063ebaa2f..5f078dd0f9 100644 --- a/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/utils/DockerUtil.java +++ b/PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.container/src/main/java/com/microsoft/azuretools/container/utils/DockerUtil.java @@ -34,9 +34,11 @@ import java.util.Map; import java.util.Optional; +import com.microsoft.azure.common.exceptions.AzureExecutionException; import com.microsoft.azuretools.azurecommons.helpers.NotNull; import com.microsoft.azuretools.azurecommons.util.Utils; import com.microsoft.azuretools.container.Constant; +import com.microsoft.tooling.msservices.components.DefaultLoader; import com.spotify.docker.client.DefaultDockerClient; import com.spotify.docker.client.DockerCertificates; import com.spotify.docker.client.DockerClient; @@ -51,6 +53,8 @@ import com.spotify.docker.client.messages.RegistryAuth; public class DockerUtil { + private static final String DOCKER_PING_ERROR = "Failed to connect docker host: %s\nIs Docker installed and running?"; + /** * create a docker file in specified folder. */ @@ -186,4 +190,14 @@ public static String getDefaultDockerFilePathIfExist(String basePath) { } return ""; } + + public static void ping(DockerClient docker) throws AzureExecutionException { + try { + docker.ping(); + } catch (DockerException | InterruptedException e) { + final String msg = String.format(DOCKER_PING_ERROR, docker.getHost()); + DefaultLoader.getUIHelper().showError(msg, "Failed to connect docker host"); + throw new AzureExecutionException(String.format("Failed to connect docker host: %s", docker.getHost())); + } + } } diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/dockerhost/DockerHostRunState.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/dockerhost/DockerHostRunState.java index 3aa9560b58..55bd91df7b 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/dockerhost/DockerHostRunState.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/dockerhost/DockerHostRunState.java @@ -30,7 +30,6 @@ import com.intellij.execution.process.ProcessOutputTypes; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; -import com.microsoft.azure.common.exceptions.AzureExecutionException; import com.microsoft.azuretools.core.mvp.model.container.pojo.DockerHostRunSetting; import com.microsoft.azuretools.telemetrywrapper.Operation; import com.microsoft.azuretools.telemetrywrapper.TelemetryManager; @@ -40,9 +39,7 @@ import com.microsoft.intellij.runner.container.utils.DockerProgressHandler; import com.microsoft.intellij.runner.container.utils.DockerUtil; import com.microsoft.intellij.util.MavenRunTaskUtil; -import com.microsoft.tooling.msservices.components.DefaultLoader; import com.spotify.docker.client.DockerClient; -import com.spotify.docker.client.exceptions.DockerException; import com.spotify.docker.client.messages.Container; import com.spotify.docker.client.shaded.com.google.common.collect.ImmutableList; @@ -134,13 +131,7 @@ public void onTextAvailable(ProcessEvent processEvent, Key key) { dataModel.isTlsEnabled(), dataModel.getDockerCertPath() ); - try { - docker.ping(); - } catch (DockerException | InterruptedException e) { - final String msg = String.format(DOCKER_PING_ERROR, dataModel.getDockerHost()); - DefaultLoader.getUIHelper().showError(msg, "Failed to connect docker host"); - throw new AzureExecutionException(String.format("Failed to connect docker host: %s", dataModel.getDockerHost())); - } + DockerUtil.ping(docker); DockerUtil.buildImage(docker, imageNameWithTag, targetDockerfile.getParent(), diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/pushimage/PushImageRunState.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/pushimage/PushImageRunState.java index 21351a2059..0ad64cd766 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/pushimage/PushImageRunState.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/pushimage/PushImageRunState.java @@ -90,6 +90,7 @@ public String executeSteps(@NotNull RunProcessHandler processHandler, PrivateRegistryImageSetting acrInfo = dataModel.getPrivateRegistryImageSetting(); processHandler.setText(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl())); DockerClient docker = DefaultDockerClient.fromEnv().build(); + DockerUtil.ping(docker); String image = DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/utils/DockerUtil.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/utils/DockerUtil.java index ac6ea63004..d619375bb0 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/utils/DockerUtil.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/utils/DockerUtil.java @@ -22,7 +22,9 @@ package com.microsoft.intellij.runner.container.utils; +import com.microsoft.azure.common.exceptions.AzureExecutionException; import com.microsoft.azuretools.azurecommons.util.Utils; +import com.microsoft.tooling.msservices.components.DefaultLoader; import com.spotify.docker.client.DefaultDockerClient; import com.spotify.docker.client.DockerCertificates; import com.spotify.docker.client.DockerClient; @@ -50,6 +52,8 @@ public class DockerUtil { + private static final String DOCKER_PING_ERROR = "Failed to connect docker host: %s\nIs Docker installed and running?"; + /** * create a docker file in specified folder. */ @@ -177,14 +181,25 @@ public static DockerClient getDockerClient(String dockerHost, boolean tlsEnabled * Else return an empty String. */ public static String getDefaultDockerFilePathIfExist(String basePath) { - try{ + try { if (!Utils.isEmptyString(basePath)) { Path targetDockerfile = Paths.get(basePath, Constant.DOCKERFILE_NAME); if (targetDockerfile != null && targetDockerfile.toFile().exists()) { return targetDockerfile.toString(); } } - } catch (RuntimeException ignored) {} + } catch (RuntimeException ignored) { + } return ""; } + + public static void ping(DockerClient docker) throws AzureExecutionException { + try { + docker.ping(); + } catch (DockerException | InterruptedException e) { + final String msg = String.format(DOCKER_PING_ERROR, docker.getHost()); + DefaultLoader.getUIHelper().showError(msg, "Failed to connect docker host"); + throw new AzureExecutionException(String.format("Failed to connect docker host: %s", docker.getHost())); + } + } } diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/webapponlinux/WebAppOnLinuxDeployState.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/webapponlinux/WebAppOnLinuxDeployState.java index 54b8ebcfbf..fd8ba9fdd4 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/webapponlinux/WebAppOnLinuxDeployState.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/container/webapponlinux/WebAppOnLinuxDeployState.java @@ -94,6 +94,7 @@ public WebApp executeSteps(@NotNull RunProcessHandler processHandler, processHandler.setText(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl())); DockerClient docker = DefaultDockerClient.fromEnv().build(); + DockerUtil.ping(docker); DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), @@ -144,7 +145,7 @@ protected void onSuccess(WebApp result, @NotNull RunProcessHandler processHandle processHandler.setText("Job done"); processHandler.notifyComplete(); if (deployModel.isCreatingNewWebAppOnLinux() && AzureUIRefreshCore.listeners != null) { - AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH,null)); + AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null)); } }