Search in sources :

Example 1 with DockerProgressHandler

use of com.microsoft.azuretools.container.DockerProgressHandler in project azure-tools-for-java by Microsoft.

the class DockerRunDialog method execute.

private void execute() {
    Operation operation = TelemetryManager.createOperation(WEBAPP, DEPLOY_WEBAPP_DOCKERLOCAL);
    Observable.fromCallable(() -> {
        operation.start();
        ConsoleLogger.info("Starting job ...  ");
        if (basePath == null) {
            ConsoleLogger.error("Project base path is null.");
            throw new FileNotFoundException("Project base path is null.");
        }
        // locate artifact to specified location
        String targetFilePath = dataModel.getTargetPath();
        ConsoleLogger.info(String.format("Locating artifact ... [%s]", targetFilePath));
        // validate dockerfile
        Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
        ConsoleLogger.info(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());
        ConsoleLogger.info(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());
        // create a container
        final String containerServerPort = StringUtils.firstNonEmpty(getPortFromDockerfile(content), getPortByArtifact(targetFilePath));
        ConsoleLogger.info(Constant.MESSAGE_CREATING_CONTAINER);
        String containerId = DockerUtil.createContainer(docker, String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName()), containerServerPort);
        ConsoleLogger.info(String.format(Constant.MESSAGE_CONTAINER_INFO, containerId));
        // start container
        ConsoleLogger.info(Constant.MESSAGE_STARTING_CONTAINER);
        Container container = DockerUtil.runContainer(docker, containerId);
        DockerRuntime.getInstance().setRunningContainerId(basePath, container.id(), dataModel);
        // props
        String hostname = new URI(dataModel.getDockerHost()).getHost();
        ImmutableList<PortMapping> ports = container.ports();
        String publicPort = null;
        if (ports != null) {
            for (Container.PortMapping portMapping : ports) {
                if (StringUtils.equalsIgnoreCase(containerServerPort, String.valueOf(portMapping.privatePort()))) {
                    publicPort = String.valueOf(portMapping.publicPort());
                }
            }
        }
        ConsoleLogger.info(String.format(Constant.MESSAGE_CONTAINER_STARTED, (hostname != null ? hostname : "localhost") + (publicPort != null ? ":" + publicPort : "")));
        return null;
    }).subscribeOn(SchedulerProviderFactory.getInstance().getSchedulerProvider().io()).subscribe(ret -> {
        ConsoleLogger.info("Container started.");
        sendTelemetry(true, null);
        operation.complete();
    }, e -> {
        e.printStackTrace();
        ConsoleLogger.error(e.getMessage());
        sendTelemetry(false, e.getMessage());
        EventUtil.logError(operation, ErrorType.systemError, new Exception(e), null, null);
        operation.complete();
    });
}
Also used : Path(java.nio.file.Path) DockerProgressHandler(com.microsoft.azuretools.container.DockerProgressHandler) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) FileNotFoundException(java.io.FileNotFoundException) PortMapping(com.spotify.docker.client.messages.Container.PortMapping) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) PortMapping(com.spotify.docker.client.messages.Container.PortMapping) URI(java.net.URI) InvalidFormDataException(com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException) FileNotFoundException(java.io.FileNotFoundException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException)

Example 2 with DockerProgressHandler

use of com.microsoft.azuretools.container.DockerProgressHandler in project azure-tools-for-java by Microsoft.

the class PublishWebAppOnLinuxDialog method execute.

private void execute() {
    Operation operation = TelemetryManager.createOperation(WEBAPP, DEPLOY_WEBAPP_CONTAINER);
    Observable.fromCallable(() -> {
        ConsoleLogger.info("Starting job ...  ");
        operation.start();
        if (basePath == null) {
            ConsoleLogger.error("Project base path is null.");
            throw new FileNotFoundException("Project base path is null.");
        }
        // locate artifact to specified location
        String targetFilePath = model.getTargetPath();
        ConsoleLogger.info(String.format("Locating artifact ... [%s]", targetFilePath));
        // validate dockerfile
        Path targetDockerfile = Paths.get(model.getDockerFilePath());
        ConsoleLogger.info(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 = 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());
        // push to ACR
        ConsoleLogger.info(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
        DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler());
        // deploy
        if (model.isCreatingNewWebAppOnLinux()) {
            // create new WebApp
            ConsoleLogger.info(String.format("Creating new WebApp ... [%s]", model.getWebAppName()));
            IWebApp app = AzureWebAppMvpModel.getInstance().createAzureWebAppWithPrivateRegistryImage(model);
            if (app != null && app.name() != null) {
                ConsoleLogger.info(String.format("URL:  http://%s.azurewebsites.net/", app.name()));
                AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null));
            }
        } else {
            // update WebApp
            ConsoleLogger.info(String.format("Updating WebApp ... [%s]", model.getWebAppName()));
            IWebApp app = AzureWebAppMvpModel.getInstance().updateWebAppOnDocker(model.getWebAppId(), acrInfo);
            if (app != null && app.name() != null) {
                ConsoleLogger.info(String.format("URL:  http://%s.azurewebsites.net/", app.name()));
            }
        }
        return null;
    }).subscribeOn(SchedulerProviderFactory.getInstance().getSchedulerProvider().io()).subscribe(ret -> {
        ConsoleLogger.info("Job done");
        if (model.isCreatingNewWebAppOnLinux() && AzureUIRefreshCore.listeners != null) {
            AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null));
        }
        sendTelemetry(true, null);
        operation.complete();
    }, err -> {
        err.printStackTrace();
        ConsoleLogger.error(err.getMessage());
        EventUtil.logError(operation, ErrorType.systemError, new Exception(err), null, null);
        operation.complete();
        sendTelemetry(false, err.getMessage());
    });
}
Also used : Path(java.nio.file.Path) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) DockerProgressHandler(com.microsoft.azuretools.container.DockerProgressHandler) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) FileNotFoundException(java.io.FileNotFoundException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AzureUIRefreshEvent(com.microsoft.azuretools.utils.AzureUIRefreshEvent) InvalidFormDataException(com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException) FileNotFoundException(java.io.FileNotFoundException) IWebApp(com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)

Example 3 with DockerProgressHandler

use of com.microsoft.azuretools.container.DockerProgressHandler in project azure-tools-for-java by Microsoft.

the class PushImageDialog method execute.

private void execute() {
    Operation operation = TelemetryManager.createOperation(ACR, ACR_PUSHIMAGE);
    Observable.fromCallable(() -> {
        ConsoleLogger.info("Starting job ...  ");
        operation.start();
        if (basePath == null) {
            ConsoleLogger.error("Project base path is null.");
            throw new FileNotFoundException("Project base path is null.");
        }
        // locate artifact to specified location
        String targetFilePath = model.getTargetPath();
        ConsoleLogger.info(String.format("Locating artifact ... [%s]", targetFilePath));
        // validate dockerfile
        Path targetDockerfile = Paths.get(model.getDockerFilePath());
        ConsoleLogger.info(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 = 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());
        // push to ACR
        ConsoleLogger.info(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
        DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler());
        return null;
    }).subscribeOn(SchedulerProviderFactory.getInstance().getSchedulerProvider().io()).subscribe(props -> {
        ConsoleLogger.info("pushed.");
        sendTelemetry(true, null);
        operation.complete();
    }, err -> {
        err.printStackTrace();
        ConsoleLogger.error(err.getMessage());
        EventUtil.logError(operation, ErrorType.systemError, new Exception(err), null, null);
        operation.complete();
        sendTelemetry(false, err.getMessage());
    });
}
Also used : Path(java.nio.file.Path) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) DockerProgressHandler(com.microsoft.azuretools.container.DockerProgressHandler) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) FileNotFoundException(java.io.FileNotFoundException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) InvalidFormDataException(com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

InvalidFormDataException (com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException)3 DockerProgressHandler (com.microsoft.azuretools.container.DockerProgressHandler)3 Operation (com.microsoft.azuretools.telemetrywrapper.Operation)3 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)3 DockerClient (com.spotify.docker.client.DockerClient)3 FileNotFoundException (java.io.FileNotFoundException)3 Path (java.nio.file.Path)3 PrivateRegistryImageSetting (com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting)2 IWebApp (com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)1 AzureUIRefreshEvent (com.microsoft.azuretools.utils.AzureUIRefreshEvent)1 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)1 Container (com.spotify.docker.client.messages.Container)1 PortMapping (com.spotify.docker.client.messages.Container.PortMapping)1 URI (java.net.URI)1