From 51304f3b85d1119186ef1008b3344df7b3af8fb3 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 17 Mar 2023 16:05:39 +0100 Subject: [PATCH] docker_build now works --- build.py | 2 +- infrastructure/clojure/build.py | 44 +++++++----- src/main/python/ddadevops/application.py | 2 +- src/main/python/ddadevops/devops_build.py | 5 +- .../python/ddadevops/devops_docker_build.py | 2 +- src/main/python/ddadevops/infrastructure.py | 67 +++++++++++-------- src/main/python/ddadevops/python_util.py | 5 +- 7 files changed, 72 insertions(+), 55 deletions(-) diff --git a/build.py b/build.py index 696332d..dd24c1a 100644 --- a/build.py +++ b/build.py @@ -28,7 +28,7 @@ use_plugin("python.distutils") default_task = "publish" name = "ddadevops" -version = "3.2.0-dev" +version = "4.0.0-dev9" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" description = __doc__ authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] diff --git a/infrastructure/clojure/build.py b/infrastructure/clojure/build.py index be4a788..786a61a 100644 --- a/infrastructure/clojure/build.py +++ b/infrastructure/clojure/build.py @@ -4,30 +4,39 @@ from pybuilder.core import task, init from ddadevops import * import logging -name = 'clojure' -MODULE = 'docker' -PROJECT_ROOT_PATH = '../..' +name = "clojure" +MODULE = "docker" +PROJECT_ROOT_PATH = "../.." -class MyBuild(DevopsDockerBuild): - pass - @init def initialize(project): - project.build_depends_on('ddadevops>=0.13.0') - stage = 'notused' - dockerhub_user = environ.get('DOCKERHUB_USER') + project.build_depends_on("ddadevops>=4.0.0-dev") + stage = "notused" + dockerhub_user = environ.get("DOCKERHUB_USER") if not dockerhub_user: - dockerhub_user = gopass_field_from_path('meissa/web/docker.com', 'login') - dockerhub_password = environ.get('DOCKERHUB_PASSWORD') + dockerhub_user = gopass_field_from_path("meissa/web/docker.com", "login") + dockerhub_password = environ.get("DOCKERHUB_PASSWORD") if not dockerhub_password: - dockerhub_password = gopass_password_from_path('meissa/web/docker.com') - tag = environ.get('CI_COMMIT_TAG') + dockerhub_password = gopass_password_from_path("meissa/web/docker.com") + tag = environ.get("CI_COMMIT_TAG") if not tag: tag = get_tag_from_latest_commit() - config = create_devops_docker_build_config( - stage, PROJECT_ROOT_PATH, MODULE, dockerhub_user, dockerhub_password, docker_publish_tag=tag) - build = MyBuild(project, config) + + devops = Devops( + stage=stage, + project_root_path=PROJECT_ROOT_PATH, + module=MODULE, + name=name, + ) + docker = Docker( + dockerhub_user=dockerhub_user, + dockerhub_password=dockerhub_password, + docker_publish_tag=tag, + devops=devops, + ) + + build = DevopsDockerBuild(project, docker=docker) build.initialize_build_dir() @@ -36,16 +45,19 @@ def image(project): build = get_devops_build(project) build.image() + @task def drun(project): build = get_devops_build(project) build.drun() + @task def test(project): build = get_devops_build(project) build.test() + @task def publish(project): build = get_devops_build(project) diff --git a/src/main/python/ddadevops/application.py b/src/main/python/ddadevops/application.py index ae97f07..9a7b7a4 100644 --- a/src/main/python/ddadevops/application.py +++ b/src/main/python/ddadevops/application.py @@ -9,7 +9,7 @@ class DockerBuildService: self.docker_api = DockerApi() def __copy_build_resource_file_from_package__(self, resource_name, docker: Docker): - data = self.resource_api.read_resource(f"../../resources/docker/{resource_name}") + data = self.resource_api.read_resource(f"src/main/resources/docker/{resource_name}") self.file_api.write_data_to_file( f"{docker.devops.build_path()}/{resource_name}", data ) diff --git a/src/main/python/ddadevops/devops_build.py b/src/main/python/ddadevops/devops_build.py index 02140db..4f88a0e 100644 --- a/src/main/python/ddadevops/devops_build.py +++ b/src/main/python/ddadevops/devops_build.py @@ -1,4 +1,5 @@ import deprecation +from subprocess import run, CalledProcessError from .domain import Devops from .infrastructure import ProjectRepository, FileApi @@ -14,9 +15,6 @@ def create_devops_build_config( "build_dir_name": build_dir_name, } - -@deprecation.deprecated(deprecated_in="3.2") -# Do not expose build to outside def get_devops_build(project): return project.get_property("devops_build") @@ -50,6 +48,7 @@ class DevopsBuild: build_dir_name=config["build_dir_name"], ) self.repo.set_devops(self.project, devops) + self.repo.set_build(self.project, self) def name(self): devops = self.repo.get_devops(self.project) diff --git a/src/main/python/ddadevops/devops_docker_build.py b/src/main/python/ddadevops/devops_docker_build.py index 5ce1feb..9f5c262 100644 --- a/src/main/python/ddadevops/devops_docker_build.py +++ b/src/main/python/ddadevops/devops_docker_build.py @@ -71,4 +71,4 @@ class DevopsDockerBuild(DevopsBuild): def test(self): docker = self.repo.get_docker(self.project) - self.test.dockerhub_publish(docker) + self.docker_build_service.test(docker) diff --git a/src/main/python/ddadevops/infrastructure.py b/src/main/python/ddadevops/infrastructure.py index 9056583..bf1fe49 100644 --- a/src/main/python/ddadevops/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure.py @@ -2,22 +2,26 @@ from pathlib import Path from sys import stdout from pkg_resources import resource_string from os import chmod +from subprocess import run import yaml from .domain import Devops, Docker, C4k -from .python_util import execute +from .python_util import execute, execute_live class ProjectRepository: + def set_build(self, project, build): + project.set_property("devops_build", build) + def get_devops(self, project) -> Devops: return project.get_property("build") def set_devops(self, project, build: Devops): project.set_property("build", build) - def get_docker(self, project) -> Docker: + def get_docker(self, project) -> Docker: return project.get_property("docker_build") - def set_docker(self, project, build: Docker): + def set_docker(self, project, build: Docker): project.set_property("docker_build", build) def get_c4k(self, project) -> C4k: @@ -55,49 +59,54 @@ class FileApi: class DockerApi: def image(self, name: str, path: Path): - execute( - "docker build -t " - + name - + " --file " - + path - + "/image/Dockerfile " - + path - + "/image", + run( + f"docker build -t {name} --file {path}/image/Dockerfile {path}/image", shell=True, + check=True, ) def drun(self, name: str): - execute('docker run -it --entrypoint="" ' + name + " /bin/bash", shell=True) + run( + f'docker run -it --entrypoint="{name}" /bin/bash', + shell=True, + check=True, + ) def dockerhub_login(self, username: str, password: str): - execute( - "docker login --username " + username + " --password " + password, + run( + f"docker login --username {username} --password {password}", shell=True, + check=True, ) def dockerhub_publish(self, name: str, username: str, tag=None): if tag is not None: - execute( - "docker tag " + name + " " + username + "/" + name + ":" + tag, + run( + f"docker tag {name} {username} /{name}:{tag}", shell=True, + check=True, ) - execute("docker push " + username + "/" + name + ":" + tag, shell=True) - execute( - "docker tag " + name + " " + username + "/" + name + ":latest", shell=True + run( + f"docker push {username} / {name}:{tag}", + shell=True, + check=True, + ) + run( + f"docker tag {name} {username}/{name} :latest", + shell=True, + check=True, + ) + run( + f"docker push {username}/{name}:latest", + shell=True, + check=True, ) - execute("docker push " + username + "/" + name + ":latest", shell=True) def test(self, name: str, path: Path): - execute( - "docker build -t " - + name - + "-test " - + "--file " - + path - + "/test/Dockerfile " - + path - + "/test", + run( + f"docker build -t {name} -test --file {path}/test/Dockerfile {path}/test", shell=True, + check=True, ) diff --git a/src/main/python/ddadevops/python_util.py b/src/main/python/ddadevops/python_util.py index e90f7e3..3b06fee 100644 --- a/src/main/python/ddadevops/python_util.py +++ b/src/main/python/ddadevops/python_util.py @@ -2,10 +2,7 @@ from subprocess import check_output, Popen, PIPE import sys def execute(cmd, shell=False): - if sys.version_info.major == 3: - output = check_output(cmd, encoding='UTF-8', shell=shell) - else: - output = check_output(cmd, shell=shell) + output = check_output(cmd, encoding='UTF-8', shell=shell) return output.rstrip() def execute_live(cmd):