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"
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")]

View file

@ -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)

View file

@ -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
)

View file

@ -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)

View file

@ -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)

View file

@ -2,12 +2,16 @@ 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")
@ -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,
)

View file

@ -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)
return output.rstrip()
def execute_live(cmd):