use of com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler in project azure-tools-for-java by Microsoft.
the class WebAppOnLinuxDeployState method executeSteps.
@Override
@AzureOperation(name = "docker.deploy_image.state", type = AzureOperation.Type.ACTION)
public IAppService executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
processHandler.setText("Starting job ... ");
final String basePath = project.getBasePath();
if (basePath == null) {
processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
final String targetFilePath = deployModel.getTargetPath();
processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
final Path targetDockerfile = Paths.get(deployModel.getDockerFilePath());
processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
final PrivateRegistryImageSetting acrInfo = deployModel.getPrivateRegistryImageSetting();
processHandler.setText(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl()));
final DockerClient docker = DefaultDockerClient.fromEnv().build();
DockerUtil.ping(docker);
DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
// push to ACR
processHandler.setText(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler(processHandler));
// deploy
if (deployModel.isCreatingNewWebAppOnLinux()) {
// create new WebApp
processHandler.setText(String.format("Creating new WebApp ... [%s]", deployModel.getWebAppName()));
final IWebApp app = AzureWebAppMvpModel.getInstance().createAzureWebAppWithPrivateRegistryImage(deployModel);
if (app != null && app.name() != null) {
processHandler.setText(String.format("URL: http://%s.azurewebsites.net/", app.name()));
updateConfigurationDataModel(app);
AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null));
}
return app;
} else {
// update WebApp
processHandler.setText(String.format("Updating WebApp ... [%s]", deployModel.getWebAppName()));
final IWebApp app = AzureWebAppMvpModel.getInstance().updateWebAppOnDocker(deployModel.getWebAppId(), acrInfo);
if (app != null && app.name() != null) {
processHandler.setText(String.format("URL: http://%s.azurewebsites.net/", app.name()));
}
return app;
}
}
use of com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler in project azure-tools-for-java by Microsoft.
the class PushImageRunState method executeSteps.
@Override
public String executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
processHandler.setText("Starting job ... ");
String basePath = project.getBasePath();
if (basePath == null) {
processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
String targetFilePath = dataModel.getTargetPath();
processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
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(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
// push to ACR
processHandler.setText(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler(processHandler));
return image;
}
use of com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler in project azure-tools-for-java by Microsoft.
the class DockerHostRunState method executeSteps.
@Override
public String executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
final String[] runningContainerId = { null };
processHandler.addProcessListener(new ProcessListener() {
@Override
public void startNotified(ProcessEvent processEvent) {
}
@Override
public void processTerminated(ProcessEvent processEvent) {
}
@Override
public void processWillTerminate(ProcessEvent processEvent, boolean b) {
try {
DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
DockerUtil.stopContainer(docker, runningContainerId[0]);
} catch (Exception e) {
// ignore
}
}
@Override
public void onTextAvailable(ProcessEvent processEvent, Key key) {
}
});
processHandler.setText("Starting job ... ");
String basePath = project.getBasePath();
if (basePath == null) {
processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
String targetFilePath = dataModel.getTargetPath();
processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
String imageNameWithTag = String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName());
processHandler.setText(String.format("Building image ... [%s]", imageNameWithTag));
DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
DockerUtil.ping(docker);
DockerUtil.buildImage(docker, imageNameWithTag, targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
// docker run
String containerServerPort = getPortFromDockerfile(content);
if (StringUtils.isBlank(containerServerPort)) {
if (StringUtils.endsWith(targetFilePath, MavenConstants.TYPE_WAR)) {
containerServerPort = "80";
} else {
containerServerPort = "8080";
}
}
String containerId = DockerUtil.createContainer(docker, String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName()), containerServerPort);
runningContainerId[0] = containerId;
Container container = DockerUtil.runContainer(docker, containerId);
// props
String hostname = new URI(dataModel.getDockerHost()).getHost();
String publicPort = null;
ImmutableList<Container.PortMapping> ports = container.ports();
if (ports != null) {
for (Container.PortMapping portMapping : ports) {
if (StringUtils.equals(containerServerPort, String.valueOf(portMapping.privatePort()))) {
publicPort = String.valueOf(portMapping.publicPort());
}
}
}
processHandler.setText(String.format(Constant.MESSAGE_CONTAINER_STARTED, (hostname != null ? hostname : "localhost") + (publicPort != null ? ":" + publicPort : "")));
return null;
}
Aggregations