use of org.ballerinax.kubernetes.handlers.DockerHandler in project kubernetes by ballerinax.
the class KubernetesAnnotationProcessor method generateDocker.
/**
* Create docker artifacts.
*
* @param deploymentModel Deployment model
* @param balxFilePath output ballerina file path
* @param outputDir output directory which data should be written
* @throws KubernetesPluginException If an error occurs while generating artifacts
*/
private void generateDocker(DeploymentModel deploymentModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
DockerModel dockerModel = new DockerModel();
String dockerImage = deploymentModel.getImage();
String imageTag = dockerImage.substring(dockerImage.lastIndexOf(":") + 1, dockerImage.length());
dockerModel.setBaseImage(deploymentModel.getBaseImage());
dockerModel.setName(dockerImage);
dockerModel.setTag(imageTag);
dockerModel.setEnableDebug(false);
dockerModel.setUsername(deploymentModel.getUsername());
dockerModel.setPassword(deploymentModel.getPassword());
dockerModel.setPush(deploymentModel.isPush());
dockerModel.setBalxFileName(KubernetesUtils.extractBalxName(balxFilePath) + BALX);
dockerModel.setPorts(deploymentModel.getPorts());
dockerModel.setService(true);
dockerModel.setDockerHost(deploymentModel.getDockerHost());
dockerModel.setDockerCertPath(deploymentModel.getDockerCertPath());
dockerModel.setBuildImage(deploymentModel.isBuildImage());
dockerModel.setCommandArg(deploymentModel.getCommandArgs());
DockerHandler dockerArtifactHandler = new DockerHandler(dockerModel);
String dockerContent = dockerArtifactHandler.generate();
try {
out.print("@kubernetes:Docker \t\t\t - complete 0/3 \r");
KubernetesUtils.writeToFile(dockerContent, outputDir + File.separator + "Dockerfile");
out.print("@kubernetes:Docker \t\t\t - complete 1/3 \r");
String balxDestination = outputDir + File.separator + KubernetesUtils.extractBalxName(balxFilePath) + BALX;
KubernetesUtils.copyFile(balxFilePath, balxDestination);
// check image build is enabled.
if (dockerModel.isBuildImage()) {
dockerArtifactHandler.buildImage(dockerModel, outputDir);
out.print("@kubernetes:Docker \t\t\t - complete 2/3 \r");
Files.delete(Paths.get(balxDestination));
// push only if image build is enabled.
if (dockerModel.isPush()) {
dockerArtifactHandler.pushImage(dockerModel);
}
out.print("@kubernetes:Docker \t\t\t - complete 3/3");
}
} catch (IOException e) {
throw new KubernetesPluginException("Unable to write Dockerfile content to " + outputDir);
} catch (InterruptedException e) {
throw new KubernetesPluginException("Unable to create docker images " + e.getMessage());
}
}
Aggregations