Search in sources :

Example 66 with DockerClient

use of com.spotify.docker.client.DockerClient 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 67 with DockerClient

use of com.spotify.docker.client.DockerClient 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;
    }
}
Also used : Path(java.nio.file.Path) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) DockerProgressHandler(com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) FileNotFoundException(java.io.FileNotFoundException) AzureUIRefreshEvent(com.microsoft.azuretools.utils.AzureUIRefreshEvent) IWebApp(com.microsoft.azure.toolkit.lib.appservice.service.IWebApp) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 68 with DockerClient

use of com.spotify.docker.client.DockerClient 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;
}
Also used : Path(java.nio.file.Path) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) DockerProgressHandler(com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) FileNotFoundException(java.io.FileNotFoundException)

Example 69 with DockerClient

use of com.spotify.docker.client.DockerClient in project azure-tools-for-java by Microsoft.

the class ContainerRegistryExplorerEditor method pullImage.

private void pullImage() {
    Job job = new Job(PULL_IMAGE) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            monitor.beginTask(PULL_IMAGE, IProgressMonitor.UNKNOWN);
            String deploymentName = UUID.randomUUID().toString();
            try {
                if (Utils.isEmptyString(currentRepo) || Utils.isEmptyString(currentTag)) {
                    throw new Exception(REPO_TAG_NOT_AVAILABLE);
                }
                final String image = String.format("%s:%s", currentRepo, currentTag);
                String jobDescription = String.format("Pulling: %s", image);
                AzureDeploymentProgressNotification.createAzureDeploymentProgressNotification(deploymentName, jobDescription);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, "", 5, "Getting Registry...");
                final Registry registry = ContainerRegistryMvpModel.getInstance().getContainerRegistry(subscriptionId, registryId);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, "", 5, "Getting Credential...");
                final PrivateRegistryImageSetting setting = ContainerRegistryMvpModel.getInstance().createImageSettingWithRegistry(registry);
                final String fullImageTagName = String.format("%s/%s", registry.loginServerUrl(), image);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, "", 10, "Pulling image...");
                DockerClient docker = DefaultDockerClient.fromEnv().build();
                DockerUtil.pullImage(docker, registry.loginServerUrl(), setting.getUsername(), setting.getPassword(), fullImageTagName);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, null, 100, "Finish.");
                sendTelemetry(true, subscriptionId, null);
            } catch (Exception ex) {
                monitor.done();
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, null, -1, ex.getMessage());
                sendTelemetry(false, subscriptionId, ex.getMessage());
                return Status.CANCEL_STATUS;
            }
            monitor.done();
            return Status.OK_STATUS;
        }
    };
    job.schedule();
}
Also used : PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Registry(com.microsoft.azure.management.containerregistry.Registry) Job(org.eclipse.core.runtime.jobs.Job) PartInitException(org.eclipse.ui.PartInitException)

Example 70 with DockerClient

use of com.spotify.docker.client.DockerClient in project helios by spotify.

the class MultiplePortJobTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    // 'Reserve' a 2 port wide range of ports.
    final Range<Integer> portRange = temporaryPorts.localPortRange("agent1", 2);
    // Start an agent using the aforementioned 2 port wide range.
    final AgentMain agent1 = startDefaultAgent(testHost(), "--port-range=" + portRange.lowerEndpoint() + ":" + portRange.upperEndpoint());
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final HeliosClient client = defaultClient();
        awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        // foo is a mapping of 4711 -> A port dynamically allocated by the agent's PortAllocator
        // bar is a mapping of 4712 -> A static port randomly selected by temporaryPorts
        final Map<String, PortMapping> ports1 = ImmutableMap.of("foo", PortMapping.of(4711), "bar", staticMapping1);
        // foo is a mapping of 4711 -> A port dynamically allocated by the agent's PortAllocator
        // bar is a mapping of 4712 -> A static port randomly selected by temporaryPorts
        final Map<String, PortMapping> ports2 = ImmutableMap.of("foo", PortMapping.of(4711), "bar", staticMapping2);
        final JobId jobId1 = createJob(testJobName + 1, testJobVersion, BUSYBOX, IDLE_COMMAND, EMPTY_ENV, ports1);
        assertNotNull(jobId1);
        deployJob(jobId1, testHost());
        final TaskStatus firstTaskStatus1 = awaitJobState(client, testHost(), jobId1, RUNNING, LONG_WAIT_SECONDS, SECONDS);
        final JobId jobId2 = createJob(testJobName + 2, testJobVersion, BUSYBOX, IDLE_COMMAND, EMPTY_ENV, ports2);
        assertNotNull(jobId2);
        deployJob(jobId2, testHost());
        final TaskStatus firstTaskStatus2 = awaitJobState(client, testHost(), jobId2, RUNNING, LONG_WAIT_SECONDS, SECONDS);
        // Verify we allocated dynamic ports from within the specified range.
        assertTrue(portRange.contains(firstTaskStatus1.getPorts().get("foo").getExternalPort()));
        assertTrue(portRange.contains(firstTaskStatus2.getPorts().get("foo").getExternalPort()));
        // Verify we allocated the static ports we asked for.
        assertEquals(staticMapping1, firstTaskStatus1.getPorts().get("bar"));
        assertEquals(staticMapping2, firstTaskStatus2.getPorts().get("bar"));
        // Verify we didn't allocate the same dynamic port to both jobs.
        assertNotEquals(firstTaskStatus1.getPorts().get("foo"), firstTaskStatus2.getPorts().get("foo"));
        // TODO (dano): the supervisor should report the allocated ports at all times
        // Verify that port allocation is kept across container restarts
        dockerClient.killContainer(firstTaskStatus1.getContainerId());
        final TaskStatus restartedTaskStatus1 = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<TaskStatus>() {

            @Override
            public TaskStatus call() throws Exception {
                final HostStatus hostStatus = client.hostStatus(testHost()).get();
                final TaskStatus taskStatus = hostStatus.getStatuses().get(jobId1);
                return (taskStatus != null && taskStatus.getState() == RUNNING && !Objects.equals(taskStatus.getContainerId(), firstTaskStatus1.getContainerId())) ? taskStatus : null;
            }
        });
        assertEquals(firstTaskStatus1.getPorts(), restartedTaskStatus1.getPorts());
        // Verify that port allocation is kept across agent restarts
        agent1.stopAsync().awaitTerminated();
        dockerClient.killContainer(firstTaskStatus2.getContainerId());
        startDefaultAgent(testHost());
        final TaskStatus restartedTaskStatus2 = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<TaskStatus>() {

            @Override
            public TaskStatus call() throws Exception {
                final HostStatus hostStatus = client.hostStatus(testHost()).get();
                final TaskStatus taskStatus = hostStatus.getStatuses().get(jobId2);
                return (taskStatus != null && taskStatus.getState() == RUNNING && !Objects.equals(taskStatus.getContainerId(), firstTaskStatus2.getContainerId())) ? taskStatus : null;
            }
        });
        assertEquals(firstTaskStatus2.getPorts(), restartedTaskStatus2.getPorts());
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) HeliosClient(com.spotify.helios.client.HeliosClient) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) AgentMain(com.spotify.helios.agent.AgentMain) HostStatus(com.spotify.helios.common.descriptors.HostStatus) PortMapping(com.spotify.helios.common.descriptors.PortMapping) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

DockerClient (com.spotify.docker.client.DockerClient)185 Test (org.junit.Test)102 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)75 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)38 SWTBotTreeItem (org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem)20 JobId (com.spotify.helios.common.descriptors.JobId)19 DockerException (com.spotify.docker.client.exceptions.DockerException)18 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)18 LogStream (com.spotify.docker.client.LogStream)17 SWTBotMenu (org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu)15 Container (com.spotify.docker.client.messages.Container)14 Path (java.nio.file.Path)12 DockerException (org.eclipse.linuxtools.docker.core.DockerException)12 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)12 Matchers.containsString (org.hamcrest.Matchers.containsString)11 HostConfig (com.spotify.docker.client.messages.HostConfig)10 HeliosClient (com.spotify.helios.client.HeliosClient)10 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)9 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)9 RunWithProject (org.eclipse.linuxtools.internal.docker.ui.testutils.RunWithProject)9