From e50ae8aa032d44a1d03548c67f408ae88e0d83c4 Mon Sep 17 00:00:00 2001 From: Alex Frantz Date: Fri, 1 Aug 2025 00:33:42 -0400 Subject: [PATCH] use a python script --- .gitea/workflows/deploy.yml | 3 +- scripts/deploy_containers.py | 58 ++++++++++++++++++++++++++++++++++++ scripts/deploy_containers.sh | 19 ------------ 3 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 scripts/deploy_containers.py delete mode 100644 scripts/deploy_containers.sh diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 47cbedb..3d64506 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -24,5 +24,4 @@ jobs: ssh-add <(echo "${{ secrets.SSH_KEY }}") echo "HOST *" > ~/.ssh/config echo "${{ secrets.VAULT_PASS }}" > ~/.vault_pass.txt - chmod +x ./scripts/deploy_containers.sh - ./scripts/deploy_containers.sh "${{ github.event.before }}" "${{ github.sha }}" + python3 ./scripts/deploy_containers.py "${{ github.event.before }}" "${{ github.sha }}" diff --git a/scripts/deploy_containers.py b/scripts/deploy_containers.py new file mode 100644 index 0000000..9459553 --- /dev/null +++ b/scripts/deploy_containers.py @@ -0,0 +1,58 @@ +import re +import sys +import subprocess + +bracket_regex = r'\[([^\]]*)\]' +quote_regex = r'"([^"]*)"' + +def git_diff(): + args = sys.argv + res = subprocess.run(f"git diff --name-only {args[1]} {args[2]}", capture_output=True, shell=True, text=True) + return res.stdout.strip().split("\n") + +def construct_ansible_command(server_name = None, tag = None): + command = "ANSIBLE_CONFIG=ansible.cfg /usr/bin/ansible-playbook main.yml --vault-password-file ~/.vault_pass.txt" + + if server_name: + command += f" -l {server_name}" + if tag: + command += f" --tags {tag}" + + return command + +def run_deployment(server_name = None, tag = None): + if tag: + print(f"Deploying task '{tag}'..") + command = construct_ansible_command(tag=tag) + elif server_name: + print(f"Deploying caddy on server '{server_name}'..") + command = construct_ansible_command(server_name, "caddy_server") + + res = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + lines = res.stdout.decode(encoding='utf-8').split("\n") + for ind, line in enumerate(lines): + if "fatal:" in line: + host = re.findall(bracket_regex, line)[0] + task_failed = re.findall(bracket_regex, lines[ind - 1])[0] + reason_failed = re.findall(quote_regex, line) + + print("\n---------------------") + print(" Deployment failed!") + print(f" Task: {task_failed}") + print(f" Host: {host}") + print(f" Reason: {reason_failed[2].split(":")[1].strip()}") + print("---------------------\n") + +def main(): + diff = git_diff() + + for file in diff: + if "host_vars" in file: + server_name = file.split("/")[1].split(".")[0] + run_deployment(server_name=server_name) + if "tasks" in file: + task_name = file.split("/")[1].split(".")[0] + "_deploy" + run_deployment(tag=task_name) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/deploy_containers.sh b/scripts/deploy_containers.sh deleted file mode 100644 index b619db6..0000000 --- a/scripts/deploy_containers.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -# all new/updated tasks in the diff -new_tasks=($(git diff --name-only $1 $2 | grep '\.yml$')) -echo $new_tasks -echo $1 $2 - -if [ ! -z "$new_tasks" ]; then - for task in "${new_tasks[@]}"; do - ansible_tag=$(echo "$task" | awk -F/ '{print $2}') - if [[ "$ansible_tag" != "all.yml" && "$ansible_tag" != "all.template.yml" && "$ansible_tag" != "main.yml" ]] ; then - tag=${ansible_tag%.*}_deploy - if [[ "$tag" != "_deploy" ]] ; then - ansible-playbook main.yml --tags "$tag" --vault-password-file ~/.vault_pass.txt - fi - elif [[ "$task" == "host_vars/jade.yml" || "$task" == "host_vars/jackson.yml" ]] ; then - ansible-playbook main.yml --tags "caddy_deploy" --vault-password-file ~/.vault_pass.txt - fi - done -fi \ No newline at end of file