From 064c2d2766aac107e9dab884b8e1b68d4f1944cd Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 17 Apr 2020 21:57:21 +0200 Subject: [PATCH] inital devops build --- build.py | 4 +- src/main/python/ddadevops/__init__.py | 1 + .../python/ddadevops/devops_docker_build.py | 87 +++++++++++++++++++ .../ddadevops/devops_terraform_build.py | 23 ----- src/main/python/ddadevops/hetzner_mixin.py | 1 - .../image/resources/install_functions.sh | 27 ++++++ 6 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 src/main/python/ddadevops/devops_docker_build.py create mode 100755 src/main/resources/docker/image/resources/install_functions.sh diff --git a/build.py b/build.py index 41456ed..be30624 100644 --- a/build.py +++ b/build.py @@ -28,7 +28,7 @@ use_plugin("python.distutils") default_task = "publish" name = "ddadevops" -version = "0.6.0.dev3" +version = "0.6.0.dev6" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" description = __doc__ authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] @@ -48,8 +48,10 @@ def initialize(project): project.set_property("copy_resources_target", "$dir_dist/ddadevops") project.get_property("copy_resources_glob").append("LICENSE") project.get_property("copy_resources_glob").append("src/main/resources/terraform/*") + project.get_property("copy_resources_glob").append("src/main/resources/docker/image/resources/*") project.include_file("ddadevops", "LICENSE") project.include_file("ddadevops", "src/main/resources/terraform/*") + project.include_file("ddadevops", "src/main/resources/docker/image/resources/*") #project.set_property('distutils_upload_sign', True) #project.set_property('distutils_upload_sign_identity', '') diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index c04ea36..1c7b4ba 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -7,6 +7,7 @@ terraform, dda-pallet, aws & hetzner-cloud. from .credential import gopass_credential_from_env_path, gopass_credential_from_path, gopass_password_from_path, gopass_field_from_path from .devops_build import DevopsBuild, create_devops_build_config, get_devops_build from .devops_terraform_build import WorkaroundTerraform, DevopsTerraformBuild, create_devops_terraform_build_config +from .devops_docker_build import DevopsDockerBuild, create_devops_docker_build_config from .hetzner_mixin import HetznerMixin, add_hetzner_mixin_config from .aws_backend_properties_mixin import AwsBackendPropertiesMixin, add_aws_backend_properties_mixin_config from .aws_mfa_mixin import AwsMfaMixin, add_aws_mfa_mixin_config diff --git a/src/main/python/ddadevops/devops_docker_build.py b/src/main/python/ddadevops/devops_docker_build.py new file mode 100644 index 0000000..c85523e --- /dev/null +++ b/src/main/python/ddadevops/devops_docker_build.py @@ -0,0 +1,87 @@ +from subprocess import run +from .python_util import filter_none +from pkg_resources import * +from .devops_terraform_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',): + 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, }) + return ret + + +class DevopsDockerBuild(DevopsBuild): + + def __init__(self, project, config): + super().__init__(project, config) + project.build_depends_on('python-terraform') + self.dockerhub_user = config['dockerhub_user'] + self.dockerhub_password = config['dockerhub_password'] + self.use_package_common_files = config['use_package_common_files'] + self.build_commons_path = config['build_commons_path'] + self.docker_build_commons_dir_name = config['docker_build_commons_dir_name'] + + def docker_build_commons_path(self): + mylist = [self.build_commons_path, + self.docker_build_commons_dir_name] + return '/'.join(filter_none(mylist)) + '/' + + def copy_build_resource_file_from_package(self, name): + my_data = resource_string( + __name__, "src/main/resources/docker/" + name) + with open(self.build_path() + '/' + name, "w") as output_file: + output_file.write(my_data.decode(sys.stdout.encoding)) + + def copy_build_resources_from_package(self): + self.copy_build_resource_file_from_package( + 'image/resources/install_functions.sh') + + def copy_build_resources_from_dir(self): + run('cp -f ' + self.docker_build_commons_path() + + '* ' + self.build_path(), shell=True) + + def initialize_build_dir(self): + super().initialize_build_dir() + if self.use_package_common_files: + self.copy_build_resources_from_package() + else: + self.copy_build_resources_from_dir() + run('cp -r image ' + self.build_path(), shell=True) + run('cp -r test ' + self.build_path(), shell=True) + + def image(self): + run('docker build -t ' + self.name() + + ' --file ' + self.build_path() + '/image/Dockerfile ' + + self.build_path() + '/image', shell=True) + + def drun(self): + run('docker run --expose 8080 -it ' + + self.name() + ' /bin/bash', shell=True) + + def dockerhub_login(self): + run('docker login --username ' + self.dockerhub_user + + ' --password ' + self.dockerhub_password, shell=True) + + def dockerhub_publish(self): + run('docker tag ' + self.name() + ' ' + self.dockerhub_user + + '/' + self.name(), shell=True) + run('docker push ' + self.dockerhub_user + + '/' + self.name(), shell=True) + + def test(self): + run('docker build -t ' + self.name() + '-test ' + + '--file ' + self.build_path() + '/test/Dockerfile ' + + self.build_path() + '/test', shell=True) diff --git a/src/main/python/ddadevops/devops_terraform_build.py b/src/main/python/ddadevops/devops_terraform_build.py index fc670bb..d001e75 100644 --- a/src/main/python/ddadevops/devops_terraform_build.py +++ b/src/main/python/ddadevops/devops_terraform_build.py @@ -46,29 +46,6 @@ class WorkaroundTerraform(Terraform): var_file, terraform_bin_path, is_env_vars_included) self.latest_cmd = '' - # def apply(self, dir_or_plan=None, input=False, skip_plan=False, no_color=IsFlagged, - # **kwargs): - # """ - # refer to https://terraform.io/docs/commands/apply.html - # no-color is flagged by default - # :param no_color: disable color of stdout - # :param input: disable prompt for a missing variable - # :param dir_or_plan: folder relative to working folder - # :param skip_plan: force apply without plan (default: false) - # :param kwargs: same as kwags in method 'cmd' - # :returns return_code, stdout, stderr - # """ - # default = kwargs - # default['input'] = input - # default['no_color'] = no_color - # if skip_plan: - # default['auto-approve'] = IsFlagged - # else: - # default['auto-approve'] = None - # option_dict = self._generate_default_options(default) - # args = self._generate_default_args(dir_or_plan) - # return self.cmd('apply', *args, **option_dict) - def generate_cmd_string(self, cmd, *args, **kwargs): result = super().generate_cmd_string(cmd, *args, **kwargs) self.latest_cmd = ' '.join(result) diff --git a/src/main/python/ddadevops/hetzner_mixin.py b/src/main/python/ddadevops/hetzner_mixin.py index dfea6ec..49a3b9d 100644 --- a/src/main/python/ddadevops/hetzner_mixin.py +++ b/src/main/python/ddadevops/hetzner_mixin.py @@ -1,4 +1,3 @@ -from .credential import gopass_credential_from_env_path from .devops_terraform_build import DevopsTerraformBuild diff --git a/src/main/resources/docker/image/resources/install_functions.sh b/src/main/resources/docker/image/resources/install_functions.sh new file mode 100755 index 0000000..8b708d1 --- /dev/null +++ b/src/main/resources/docker/image/resources/install_functions.sh @@ -0,0 +1,27 @@ +function upgradeSystem() { + export DEBIAN_FRONTEND=noninteractive + apt-get update > /dev/null + apt-get -y install apt-utils > /dev/null + apt-get -qqy dist-upgrade > /dev/null +} + +function cleanupDocker() { + rm -f /root/.ssh/authorized_keys + rm -f /root/.ssh/authorized_keys2 + + apt-get clean + apt-get -qqy autoremove --purge + apt-get -qqy autoclean + rm -rf /var/lib/apt/lists/ + + rm -rf /tmp/* + + find /var/cache -type f -exec rm -rf {} \; + find /var/log/ -name '*.log' -exec rm -f {} \; +} + +function cleanupAmi() { + rm -f /home/ubuntu/.ssh/authorized_keys + rm -f /home/ubuntu/.ssh/authorized_keys2 + cleanupDocker +}