docker_build now works

This commit is contained in:
Michael Jerger 2023-03-17 16:05:39 +01:00
parent b63993e61e
commit 51304f3b85
7 changed files with 72 additions and 55 deletions

View file

@ -28,7 +28,7 @@ use_plugin("python.distutils")
default_task = "publish" default_task = "publish"
name = "ddadevops" 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" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud"
description = __doc__ description = __doc__
authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")]

View file

@ -4,30 +4,39 @@ from pybuilder.core import task, init
from ddadevops import * from ddadevops import *
import logging import logging
name = 'clojure' name = "clojure"
MODULE = 'docker' MODULE = "docker"
PROJECT_ROOT_PATH = '../..' PROJECT_ROOT_PATH = "../.."
class MyBuild(DevopsDockerBuild):
pass
@init @init
def initialize(project): def initialize(project):
project.build_depends_on('ddadevops>=0.13.0') project.build_depends_on("ddadevops>=4.0.0-dev")
stage = 'notused' stage = "notused"
dockerhub_user = environ.get('DOCKERHUB_USER') dockerhub_user = environ.get("DOCKERHUB_USER")
if not dockerhub_user: if not dockerhub_user:
dockerhub_user = gopass_field_from_path('meissa/web/docker.com', 'login') dockerhub_user = gopass_field_from_path("meissa/web/docker.com", "login")
dockerhub_password = environ.get('DOCKERHUB_PASSWORD') dockerhub_password = environ.get("DOCKERHUB_PASSWORD")
if not dockerhub_password: if not dockerhub_password:
dockerhub_password = gopass_password_from_path('meissa/web/docker.com') dockerhub_password = gopass_password_from_path("meissa/web/docker.com")
tag = environ.get('CI_COMMIT_TAG') tag = environ.get("CI_COMMIT_TAG")
if not tag: if not tag:
tag = get_tag_from_latest_commit() 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) devops = Devops(
build = MyBuild(project, config) 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() build.initialize_build_dir()
@ -36,16 +45,19 @@ def image(project):
build = get_devops_build(project) build = get_devops_build(project)
build.image() build.image()
@task @task
def drun(project): def drun(project):
build = get_devops_build(project) build = get_devops_build(project)
build.drun() build.drun()
@task @task
def test(project): def test(project):
build = get_devops_build(project) build = get_devops_build(project)
build.test() build.test()
@task @task
def publish(project): def publish(project):
build = get_devops_build(project) build = get_devops_build(project)

View file

@ -9,7 +9,7 @@ class DockerBuildService:
self.docker_api = DockerApi() self.docker_api = DockerApi()
def __copy_build_resource_file_from_package__(self, resource_name, docker: Docker): 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( self.file_api.write_data_to_file(
f"{docker.devops.build_path()}/{resource_name}", data f"{docker.devops.build_path()}/{resource_name}", data
) )

View file

@ -1,4 +1,5 @@
import deprecation import deprecation
from subprocess import run, CalledProcessError
from .domain import Devops from .domain import Devops
from .infrastructure import ProjectRepository, FileApi from .infrastructure import ProjectRepository, FileApi
@ -14,9 +15,6 @@ def create_devops_build_config(
"build_dir_name": build_dir_name, "build_dir_name": build_dir_name,
} }
@deprecation.deprecated(deprecated_in="3.2")
# Do not expose build to outside
def get_devops_build(project): def get_devops_build(project):
return project.get_property("devops_build") return project.get_property("devops_build")
@ -50,6 +48,7 @@ class DevopsBuild:
build_dir_name=config["build_dir_name"], build_dir_name=config["build_dir_name"],
) )
self.repo.set_devops(self.project, devops) self.repo.set_devops(self.project, devops)
self.repo.set_build(self.project, self)
def name(self): def name(self):
devops = self.repo.get_devops(self.project) devops = self.repo.get_devops(self.project)

View file

@ -71,4 +71,4 @@ class DevopsDockerBuild(DevopsBuild):
def test(self): def test(self):
docker = self.repo.get_docker(self.project) docker = self.repo.get_docker(self.project)
self.test.dockerhub_publish(docker) self.docker_build_service.test(docker)

View file

@ -2,12 +2,16 @@ from pathlib import Path
from sys import stdout from sys import stdout
from pkg_resources import resource_string from pkg_resources import resource_string
from os import chmod from os import chmod
from subprocess import run
import yaml import yaml
from .domain import Devops, Docker, C4k from .domain import Devops, Docker, C4k
from .python_util import execute from .python_util import execute, execute_live
class ProjectRepository: class ProjectRepository:
def set_build(self, project, build):
project.set_property("devops_build", build)
def get_devops(self, project) -> Devops: def get_devops(self, project) -> Devops:
return project.get_property("build") return project.get_property("build")
@ -55,49 +59,54 @@ class FileApi:
class DockerApi: class DockerApi:
def image(self, name: str, path: Path): def image(self, name: str, path: Path):
execute( run(
"docker build -t " f"docker build -t {name} --file {path}/image/Dockerfile {path}/image",
+ name
+ " --file "
+ path
+ "/image/Dockerfile "
+ path
+ "/image",
shell=True, shell=True,
check=True,
) )
def drun(self, name: str): 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): def dockerhub_login(self, username: str, password: str):
execute( run(
"docker login --username " + username + " --password " + password, f"docker login --username {username} --password {password}",
shell=True, shell=True,
check=True,
) )
def dockerhub_publish(self, name: str, username: str, tag=None): def dockerhub_publish(self, name: str, username: str, tag=None):
if tag is not None: if tag is not None:
execute( run(
"docker tag " + name + " " + username + "/" + name + ":" + tag, f"docker tag {name} {username} /{name}:{tag}",
shell=True, shell=True,
check=True,
) )
execute("docker push " + username + "/" + name + ":" + tag, shell=True) run(
execute( f"docker push {username} / {name}:{tag}",
"docker tag " + name + " " + username + "/" + name + ":latest", shell=True 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): def test(self, name: str, path: Path):
execute( run(
"docker build -t " f"docker build -t {name} -test --file {path}/test/Dockerfile {path}/test",
+ name
+ "-test "
+ "--file "
+ path
+ "/test/Dockerfile "
+ path
+ "/test",
shell=True, shell=True,
check=True,
) )

View file

@ -2,10 +2,7 @@ from subprocess import check_output, Popen, PIPE
import sys import sys
def execute(cmd, shell=False): def execute(cmd, shell=False):
if sys.version_info.major == 3:
output = check_output(cmd, encoding='UTF-8', shell=shell) output = check_output(cmd, encoding='UTF-8', shell=shell)
else:
output = check_output(cmd, shell=shell)
return output.rstrip() return output.rstrip()
def execute_live(cmd): def execute_live(cmd):