From b1d2cf7d2fcfdc456c4db13cec5f0dcc736d3722 Mon Sep 17 00:00:00 2001 From: albamig Date: Thu, 10 Feb 2022 14:17:11 +0100 Subject: [PATCH] - Microservice added to docker-compose. issue #25 --- .../init/.build_docker_compose.txt.jinja | 1 + microservice/language/python/init/_utils.py | 27 +++++++++++++++++++ microservice/language/python/init/copier.yml | 3 ++- project/init/copier.yml | 10 +++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 microservice/language/python/init/.build_docker_compose.txt.jinja diff --git a/microservice/language/python/init/.build_docker_compose.txt.jinja b/microservice/language/python/init/.build_docker_compose.txt.jinja new file mode 100644 index 0000000..e357280 --- /dev/null +++ b/microservice/language/python/init/.build_docker_compose.txt.jinja @@ -0,0 +1 @@ +{{ build_docker_compose(destination.parent.parent / "docker-compose.yml", name) }} diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 7b66f39..9ae24b4 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -22,3 +22,30 @@ def build_deploy_playbook(path: Path) -> str: data.append({"name": "Create Database", "import_playbook": "create-database.yaml"}) return yaml.dump(data, sort_keys=False) + + +def build_docker_compose(path: Path, microservice_name: str) -> str: + """Build Docker Compose file content.""" + + if not path.exists(): + raise ValueError("A base Compose file must exist.") + + with path.open() as file: + data = yaml.safe_load(file) + + microservice_container = { + "restart": "always", + "build": { + "context": f"microservices/{microservice_name}", + "target": "production" + }, + "environment": data["x-microservice-environment"], + "depends_on": data["x-microservice-depends-on"] + } + + data["services"][f"microservice-{microservice_name}"] = microservice_container + + with path.open("w") as file: + yaml.dump(data, file, sort_keys=False) + + return "" diff --git a/microservice/language/python/init/copier.yml b/microservice/language/python/init/copier.yml index 99e08d5..f3bc518 100644 --- a/microservice/language/python/init/copier.yml +++ b/microservice/language/python/init/copier.yml @@ -26,7 +26,7 @@ deploy: choices: - docker-compose - kubernetes - default: kubernetes + default: docker-compose _envops: block_start_string: "{%" @@ -45,3 +45,4 @@ _exclude: _functions: - "_utils.build_deploy_playbook" + - "_utils.build_docker_compose" diff --git a/project/init/copier.yml b/project/init/copier.yml index 5a39f62..53b7c6b 100644 --- a/project/init/copier.yml +++ b/project/init/copier.yml @@ -9,6 +9,16 @@ project_version: type: str help: What is the version of the project? default: 0.1.0 +project_deploy: + type: str + link: + docker-compose: "{{ template_registry }}/project-deploy-docker-compose.tar.gz" + kubernetes: "{{ template_registry }}/project-deploy-kubernetes.tar.gz" + help: What deploy system do you want to use? + choices: + - docker-compose + - kubernetes + default: docker-compose _envops: block_start_string: "{%"