refactor to dedicated parameters

This commit is contained in:
Michael Jerger 2023-03-14 19:11:34 +01:00
parent 4eeaefc1a3
commit 7ba7cce801
6 changed files with 107 additions and 51 deletions

View file

@ -10,7 +10,15 @@ classDiagram
build_dir_name
}
class Docker
class Docker {
dockerhub_user
dockerhub_password
build_dir_name
use_package_common_files
build_commons_path
docker_build_commons_dir_name
docker_publish_tag
}
class C4k {
executabel_name
@ -25,6 +33,7 @@ classDiagram
}
C4k *-- DnsRecord
Docker *-- Devops
```

View file

@ -8,43 +8,47 @@ class DockerBuildService:
self.resource_api = ResourceApi()
self.docker_api = DockerApi()
def __copy_build_resource_file_from_package__(self, build: Docker):
data = self.resource_api.read_resource(
"src/main/resources/docker/" + build.name
def __copy_build_resource_file_from_package__(self, resource_name, docker: Docker):
data = self.resource_api.read_resource(f"../../resources/docker/{resource_name}")
self.file_api.write_data_to_file(
f"{docker.devops.build_path()}/{resource_name}", data
)
self.file_api.write_data_to_file(build.build_path() + "/" + build.name, data)
def __copy_build_resources_from_package__(self, build: Docker):
def __copy_build_resources_from_package__(self, docker: Docker):
self.__copy_build_resource_file_from_package__(
"image/resources/install_functions.sh"
"image/resources/install_functions.sh", docker
)
def __copy_build_resources_from_dir__(self, build: Docker):
self.file_api.cp_force(build.docker_build_commons_path(), build.build_path())
def __copy_build_resources_from_dir__(self, docker: Docker):
self.file_api.cp_force(
docker.docker_build_commons_path(), docker.devops.build_path()
)
def initialize_build_dir(self, build: Devops, docker_build: Docker):
build_path = build.build_path()
def initialize_build_dir(self, docker: Docker):
build_path = docker.devops.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)
if docker.use_package_common_files:
self.__copy_build_resources_from_package__(docker)
else:
self.__copy_build_resources_from_dir__(docker_build)
self.__copy_build_resources_from_dir__(docker)
self.file_api.cp_recursive("image", build_path)
self.file_api.cp_recursive("test", build_path)
def image(self, build: Devops):
self.docker_api.image(build.name(), build.build_path())
def image(self, docker: Docker):
self.docker_api.image(docker.devops.name, docker.devops.build_path())
def drun(self, build: Docker):
self.docker_api.drun(build.name())
def drun(self, docker: Devops):
self.docker_api.drun(docker.devops.name)
def dockerhub_login(self, docker_build: Docker):
self.docker_api.dockerhub_login(docker_build.dockerhub_user, docker_build.dockerhub_password)
def dockerhub_publish(self, build: Devops, docker_build: Docker):
self.docker_api.dockerhub_publish(
build.name(), docker_build.dockerhub_user, docker_build.docker_publish_tag
def dockerhub_login(self, docker: Docker):
self.docker_api.dockerhub_login(
docker.dockerhub_user, docker.dockerhub_password
)
def test(self, build: Devops):
self.docker_api.test(build.name(), build.build_path())
def dockerhub_publish(self, docker: Docker):
self.docker_api.dockerhub_publish(
docker.devops.name, docker.dockerhub_user, docker.docker_publish_tag
)
def test(self, docker: Docker):
self.docker_api.test(docker.devops.name, docker.devops.build_path())

View file

@ -33,7 +33,6 @@ def create_devops_docker_build_config(
class DevopsDockerBuild(DevopsBuild):
def __init__(self, project, config: map = None, docker: Docker = None):
super().__init__(project, config)
self.docker_build_service = DockerBuildService()
if not docker:
docker = Docker(
@ -44,31 +43,32 @@ class DevopsDockerBuild(DevopsBuild):
docker_build_commons_dir_name=config["docker_build_commons_dir_name"],
docker_publish_tag=config["docker_publish_tag"],
)
super().__init__(project, config=config)
else:
super().__init__(project, devops=docker.devops)
self.repo.set_docker(self.project, docker)
def initialize_build_dir(self):
super().initialize_build_dir()
build = self.repo.get_devops(self.project)
docker_build = self.repo.get_docker(self.project)
self.docker_build_service.initialize_build_dir(build, docker_build)
docker = self.repo.get_docker(self.project)
self.docker_build_service.initialize_build_dir(docker)
def image(self):
build = self.repo.get_devops(self.project)
self.docker_build_service.image(build)
docker = self.repo.get_docker(self.project)
self.docker_build_service.image(docker)
def drun(self):
build = self.repo.get_devops(self.project)
self.docker_build_service.drun(build)
docker = self.repo.get_docker(self.project)
self.docker_build_service.drun(docker)
def dockerhub_login(self):
docker_build = self.repo.get_docker(self.project)
self.docker_build_service.dockerhub_login(docker_build)
docker = self.repo.get_docker(self.project)
self.docker_build_service.dockerhub_login(docker)
def dockerhub_publish(self):
build = self.repo.get_devops(self.project)
docker_build = self.repo.get_docker(self.project)
self.docker_build_service.dockerhub_publish(build, docker_build)
docker = self.repo.get_docker(self.project)
self.docker_build_service.dockerhub_publish(docker)
def test(self):
build = self.repo.get_devops(self.project)
self.test.dockerhub_publish(build)
docker = self.repo.get_docker(self.project)
self.test.dockerhub_publish(docker)

View file

@ -73,18 +73,20 @@ class Docker(Validateable):
self,
dockerhub_user,
dockerhub_password,
devops: Devops,
build_dir_name="target",
use_package_common_files=True,
build_commons_path=None,
docker_build_commons_dir_name="docker",
docker_publish_tag=None,
):
self.dockerhub_user = (dockerhub_user,)
self.dockerhub_password = (dockerhub_password,)
self.use_package_common_files = (use_package_common_files,)
self.build_commons_path = (build_commons_path,)
self.docker_build_commons_dir_name = (docker_build_commons_dir_name,)
self.docker_publish_tag = (docker_publish_tag,)
self.dockerhub_user = dockerhub_user
self.dockerhub_password = dockerhub_password
self.use_package_common_files = use_package_common_files
self.build_commons_path = build_commons_path
self.docker_build_commons_dir_name = docker_build_commons_dir_name
self.docker_publish_tag = docker_publish_tag
self.devops = devops
def docker_build_commons_path(self):
list = [self.build_commons_path, self.docker_build_commons_dir_name]

View file

@ -0,0 +1,24 @@
import os
from pybuilder.core import Project
from src.main.python.ddadevops.domain import Docker, Devops
from src.main.python.ddadevops.devops_docker_build import DevopsDockerBuild
def test_devops_docker_build(tmp_path):
build_dir = "build"
project_name = "testing-project"
module_name = "docker-test"
tmp_path_str = str(tmp_path)
project = Project(tmp_path_str, name=project_name)
devops = Devops(
stage="test",
project_root_path=tmp_path_str,
module=module_name,
name=project_name,
)
docker = Docker(dockerhub_user="user", dockerhub_password="password", devops=devops)
docker_build = DevopsDockerBuild(project, docker=docker)
# docker_build.initialize_build_dir()
# assert os.path.exists(f"{docker_build.build_path()}")

View file

@ -1,5 +1,11 @@
from pybuilder.core import Project
from src.main.python.ddadevops.domain import Validateable, DnsRecord, C4k, Devops
from src.main.python.ddadevops.domain import (
Validateable,
DnsRecord,
C4k,
Devops,
Docker,
)
from src.main.python.ddadevops.c4k_mixin import add_c4k_mixin_config
@ -62,6 +68,18 @@ def test_devops_buildpath():
assert "../../../target/meissa/cloud" == sut.build_path()
def test_devops_build_commons_path():
devops = Devops(
stage="test", project_root_path="../../..", module="cloud", name="meissa"
)
sut = Docker(
dockerhub_user="user",
dockerhub_password="password",
devops = devops,
)
assert "docker/" == sut.docker_build_commons_path()
def test_c4k_build_should_update_fqdn():
project_config = {
"stage": "test",
@ -108,8 +126,7 @@ def test_c4k_build_should_update_fqdn():
def test_c4k_build_should_calculate_command():
devops = Devops(stage="test", project_root_path='',
module="module", name="name")
devops = Devops(stage="test", project_root_path="", module="module", name="name")
project_config = {
"stage": "test",
"name": "name",