refactor devops_docker_build to arch

This commit is contained in:
Michael Jerger 2023-03-12 17:57:31 +01:00
parent d95827e1e0
commit eec7392470
4 changed files with 64 additions and 60 deletions

View file

@ -22,30 +22,31 @@ class DockerBuildService:
def __copy_build_resources_from_dir__(self, build: DockerBuild):
self.file_api.cp_force(build.docker_build_commons_path(), build.build_path())
def initialize_build_dir(self, build: DockerBuild):
self.file_api.clean_dir(build.build_path() + "/image/resources")
if build.use_package_common_files:
self.__copy_build_resources_from_package__(build)
def initialize_build_dir(self, build: Build, docker_build: DockerBuild):
build_path = build.build_path()
self.file_api.clean_dir(f"{build_path}/image/resources")
if docker_build.use_package_common_files:
self.__copy_build_resources_from_package__(docker_build)
else:
self.__copy_build_resources_from_dir__(build)
self.file_api.cp_recursive("image", build.build_path())
self.file_api.cp_recursive("test", build.build_path())
self.__copy_build_resources_from_dir__(docker_build)
self.file_api.cp_recursive("image", build_path)
self.file_api.cp_recursive("test", build_path)
def image(self, build: DockerBuild):
def image(self, build: Build):
self.docker_api.image(build.name(), build.build_path())
def drun(self, build: DockerBuild):
self.docker_api.drun(build.name())
def dockerhub_login(self, build: DockerBuild):
self.docker_api.dockerhub_login(build.dockerhub_user, build.dockerhub_password)
def dockerhub_login(self, docker_build: DockerBuild):
self.docker_api.dockerhub_login(docker_build.dockerhub_user, docker_build.dockerhub_password)
def dockerhub_publish(self, build: DockerBuild):
def dockerhub_publish(self, build: Build, docker_build: DockerBuild):
self.docker_api.dockerhub_publish(
build.name(), build.dockerhub_user, build.docker_publish_tag
build.name(), docker_build.dockerhub_user, docker_build.docker_publish_tag
)
def test(self, build: DockerBuild):
def test(self, build: Build):
self.docker_api.test(build.name(), build.build_path())

View file

@ -2,64 +2,63 @@ from .domain import DockerBuild
from .application import DockerBuildService
from .devops_build import DevopsBuild, create_devops_build_config
def create_devops_docker_build_config(stage,
project_root_path,
module,
dockerhub_user,
dockerhub_password,
build_dir_name='target',
use_package_common_files=True,
build_commons_path=None,
docker_build_commons_dir_name='docker',
docker_publish_tag=None):
ret = create_devops_build_config(
stage, project_root_path, module, build_dir_name)
ret.update({'dockerhub_user': dockerhub_user,
'dockerhub_password': dockerhub_password,
'use_package_common_files': use_package_common_files,
'docker_build_commons_dir_name': docker_build_commons_dir_name,
'build_commons_path': build_commons_path,
'docker_publish_tag': docker_publish_tag, })
def create_devops_docker_build_config(
stage,
project_root_path,
module,
dockerhub_user,
dockerhub_password,
build_dir_name="target",
use_package_common_files=True,
build_commons_path=None,
docker_build_commons_dir_name="docker",
docker_publish_tag=None,
):
ret = create_devops_build_config(stage, project_root_path, module, build_dir_name)
ret.update(
{
"dockerhub_user": dockerhub_user,
"dockerhub_password": dockerhub_password,
"use_package_common_files": use_package_common_files,
"docker_build_commons_dir_name": docker_build_commons_dir_name,
"build_commons_path": build_commons_path,
"docker_publish_tag": docker_publish_tag,
}
)
return ret
class DevopsDockerBuild(DevopsBuild):
def __init__(self, project, config):
super().__init__(project, config)
self.docker_build = DockerBuild(project, config)
self.docker_build_service = DockerBuildService()
docker_build = DockerBuild(config)
self.repo.set_docker_build(self.project, docker_build)
def initialize_build_dir(self):
super().initialize_build_dir()
self.docker_build_service.initialize_build_dir(self.docker_build)
build = self.repo.get_build(self.project)
docker_build = self.repo.get_docker_build(self.project)
self.docker_build_service.initialize_build_dir(build, docker_build)
# TODO: use application from here on
def image(self):
run('docker build -t ' + self.name() +
' --file ' + self.build_path() + '/image/Dockerfile '
+ self.build_path() + '/image', shell=True, check=True)
build = self.repo.get_build(self.project)
self.docker_build_service.image(build)
def drun(self):
run('docker run --expose 8080 -it --entrypoint="" ' +
self.name() + ' /bin/bash', shell=True, check=True)
build = self.repo.get_build(self.project)
self.docker_build_service.drun(build)
def dockerhub_login(self):
run('docker login --username ' + self.dockerhub_user +
' --password ' + self.dockerhub_password, shell=True, check=True)
docker_build = self.repo.get_docker_build(self.project)
self.docker_build_service.dockerhub_login(docker_build)
def dockerhub_publish(self):
if self.docker_publish_tag is not None:
run('docker tag ' + self.name() + ' ' + self.dockerhub_user +
'/' + self.name() + ':' + self.docker_publish_tag, shell=True, check=True)
run('docker push ' + self.dockerhub_user +
'/' + self.name() + ':' + self.docker_publish_tag, shell=True, check=True)
run('docker tag ' + self.name() + ' ' + self.dockerhub_user +
'/' + self.name() + ':latest', shell=True, check=True)
run('docker push ' + self.dockerhub_user +
'/' + self.name() + ':latest', shell=True, check=True)
build = self.repo.get_build(self.project)
docker_build = self.repo.get_docker_build(self.project)
self.docker_build_service.dockerhub_publish(build, docker_build)
def test(self):
run('docker build -t ' + self.name() + '-test ' +
'--file ' + self.build_path() + '/test/Dockerfile '
+ self.build_path() + '/test', shell=True, check=True)
build = self.repo.get_build(self.project)
self.test.dockerhub_publish(build)

View file

@ -57,9 +57,7 @@ class Build(Validateable):
class DockerBuild(Validateable):
def __init__(self, project, config):
project.build_depends_on("dda-python-terraform")
self.build = Build(project, config)
def __init__(self, config):
self.dockerhub_user = config["dockerhub_user"]
self.dockerhub_password = config["dockerhub_password"]
self.use_package_common_files = config["use_package_common_files"]

View file

@ -3,16 +3,22 @@ from sys import stdout
from pkg_resources import resource_string
from os import chmod
import yaml
from .domain import Build
from .domain import Build, DockerBuild
from .python_util import execute
class ProjectRepository:
def get_build(self, project) -> Build:
return project.get_property("devops_build")
return project.get_property("build")
def set_build(self, project, build: Build):
project.set_property("devops_build", build)
project.set_property("build", build)
def get_docker_build(self, project) -> DockerBuild:
return project.get_property("docker_build")
def set_docker_build(self, project, build: DockerBuild):
project.set_property("docker_build", build)
class ResourceApi: