org.appops.service.deployment.docker.DockerDeploymentManager Maven / Gradle / Ivy
/*
* AppOps is a Java framework to develop, deploy microservices with ease and is available for free
* and common use developed by AinoSoft ( www.ainosoft.com )
*
* AppOps and AinoSoft are registered trademarks of Aino Softwares private limited, India.
*
* Copyright (C) <2016>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version along with applicable additional terms as
* provisioned by GPL 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License and applicable additional terms
* along with this program.
*
* If not, see and
*/
package org.appops.service.deployment.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.command.BuildImageResultCallback;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.appops.configuration.ContainerConfig;
import org.appops.core.deployment.ServiceConfiguration;
import org.appops.service.exception.DeploymentException;
/**
* Class to create and configure docker based deployment environment. The class facilitates
* creation, management and removal of docker container based on image provided.
*
* @author deba
* @version $Id: $Id
*/
public class DockerDeploymentManager {
private DockerClient dockerClient;
/**
*
* Constructor for DockerDeploymentManager.
*
*/
public DockerDeploymentManager() {
initialize();
}
private void initialize() {
setDockerClient(DockerClientBuilder.getInstance().build());
}
/**
*
* Setter for the field dockerClient
.
*
*
* @param dockerClient a {@link com.github.dockerjava.api.DockerClient} object.
*/
public void setDockerClient(DockerClient dockerClient) {
this.dockerClient = dockerClient;
}
/**
* Deploys appops services in docker environment by a creating docker container per service.
*
* @param config Docker deployment configuration. It contains basic deployment details along with
* list of service container configurations.
* @return Set of docker container ids.
*/
public Set performDeployment(ServiceConfiguration config) {
Set containerIds = new HashSet<>();
ContainerConfig container = config.getContainerConfig();
String baseDir = container.getDockerFileOrBaseDirectory().getAbsolutePath();
try (BuildImageCmd cmd = getDockerClient().buildImageCmd(new File(baseDir))) {
String imageId = cmd.exec(createBuildImageCallback()).awaitImageId();
try (CreateContainerCmd createContainerCmd = getDockerClient().createContainerCmd(imageId)) {
String contaienerId = createContainerCmd.withName(imageId)
.withHostConfig(HostConfig.newHostConfig().withNetworkMode("host")).exec().getId();
getDockerClient().startContainerCmd(contaienerId).exec();
containerIds.add(contaienerId);
} catch (Exception e) {
throw e;
}
} catch (Exception e) {
throw new DeploymentException(
"Unable to perform docker deployment for ->" + config.serviceUrl());
}
return containerIds;
}
/**
*
* createBuildImageCallback.
*
*
* @return a {@link com.github.dockerjava.api.command.BuildImageResultCallback} object.
*/
public BuildImageResultCallback createBuildImageCallback() {
return new BuildImageResultCallback();
}
/**
*
* Getter for the field dockerClient
.
*
*
* @return a {@link com.github.dockerjava.api.DockerClient} object.
*/
public DockerClient getDockerClient() {
return dockerClient;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy