diff --git a/.gitea/workflows/update.yml b/.gitea/workflows/update.yml index f7f8e1a..67dc092 100644 --- a/.gitea/workflows/update.yml +++ b/.gitea/workflows/update.yml @@ -13,7 +13,7 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Run update script + - name: Configure credentials run: | mkdir -p ~/.ssh echo "${{ secrets.SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts @@ -21,8 +21,13 @@ jobs: eval $(ssh-agent -s) ssh-add <(echo "${{ secrets.SSH_KEY }}") echo "${{ secrets.VAULT_PASS }}" > ~/.vault_pass.txt + + - name: Install required dependencies + run: | python3 -m pip install requests pyyaml ansible-core --break-system-packages ansible-galaxy collection install community.docker - python3 scripts/run_updates.py + + - name: Run update script + run: python3 scripts/run_updates.py env: UPDATES_DISCORD_WEBHOOK: ${{vars.UPDATES_DISCORD_WEBHOOK}} diff --git a/main.yml b/main.yml index 42beeb4..bf52d90 100644 --- a/main.yml +++ b/main.yml @@ -20,7 +20,7 @@ import_tasks: tasks/utility/adguard.yml tags: adguard_deploy - name: Deploy Glance - import_tasks: tasks/glance.yml + import_tasks: tasks/utility/glance.yml tags: glance_deploy - name: Deploy Dozzle import_tasks: tasks/utility/dozzle.yml @@ -86,7 +86,7 @@ import_tasks: tasks/media/weatherstar.yml tags: weatherstar_deploy - name: Deploy Nextcloud - import_tasks: tasks/nextcloud.yml + import_tasks: tasks/utility/nextcloud.yml tags: nextcloud_deploy - hosts: jackson @@ -121,7 +121,7 @@ import_tasks: tasks/cobalt.yml tags: cobalt_deploy - name: Deploy Nextcloud - import_tasks: tasks/nextcloud.yml + import_tasks: tasks/utility/nextcloud.yml tags: nextcloud_deploy - name: Deploy Postgres import_tasks: tasks/infra/postgres.yml diff --git a/scripts/deploy_containers.py b/scripts/deploy_containers.py index 87441f5..1807f70 100644 --- a/scripts/deploy_containers.py +++ b/scripts/deploy_containers.py @@ -2,10 +2,9 @@ import sys import os import subprocess -vpn_containers = [ # containers that need to be recreated re: vpn - "tasks/qbittorrent.yml", - "tasks/jackett.yml" -] +# c08dc6b088e90512fb8a2dafe9f1ae5de3f486d1 +# 894be8a440ba41aa228e53d7d523bcb1ae34da41 + ignore_deploys_for = [ # don't auto-deploy these "tasks/runner", "templates/runner", @@ -19,7 +18,6 @@ def git_diff(): def construct_command(tags): command = f"ANSIBLE_CONFIG=ansible.cfg /usr/bin/ansible-playbook main.yml --tags={",".join(tags)} --vault-password-file ~/.vault_pass.txt" - print(command) return command def deploy(tags): @@ -30,7 +28,11 @@ def deploy(tags): def get_normalized_task_name(container): if "tasks/" in container: - task_name = container.split("/")[1].split(".")[0] + split_path = container.split("/") + if len(split_path) > 2: + task_name = split_path[2].split(".")[0] + else: + task_name = split_path[1].split(".")[0] elif "roles/" or "templates/" in container: task_name = container.split("/")[1] else: @@ -51,8 +53,8 @@ def main(): else: removable_containers.append(file) - print(f"[MAIN] Deployable: {deployable_containers}") - print(f"[MAIN] Removable: {removable_containers}") + print(f"[MAIN] Deployable: {len(deployable_containers)}") + print(f"[MAIN] Removable: {len(removable_containers)}") if len(deployable_containers) > 0: to_deploy = [] @@ -72,6 +74,7 @@ def main(): shell=True, capture_output=True ) + result.check_returncode() # fail the action if this doesn't succeed for line in result.stdout.splitlines(): container_id = line.strip().decode("utf8") if not container_id: @@ -79,8 +82,8 @@ def main(): print(f"[MAIN] Found Docker container {container_id} related to {task_name}, removing..") subprocess.run(f"/usr/bin/docker container stop {container_id}", shell=True) subprocess.run(f"/usr/bin/docker container rm {container_id}", shell=True) - subprocess.run("/usr/bin/docker image prune -f", shell=True) - subprocess.run("/usr/bin/docker container prune -f", shell=True) - + subprocess.run("/usr/bin/docker image prune -af", shell=True) + subprocess.run("/usr/bin/docker container prune -af", shell=True) + if __name__ == "__main__": main() \ No newline at end of file diff --git a/scripts/run_updates.py b/scripts/run_updates.py index c92d6f1..13b2bda 100644 --- a/scripts/run_updates.py +++ b/scripts/run_updates.py @@ -11,14 +11,16 @@ def search_for_image(image_name): image_name = image_name.replace("library/", "") image_name = image_name.split(":")[0] - for task in os.listdir(tasks_folder): - with open(os.path.join(tasks_folder, task), 'r') as file: - data = yaml.safe_load(file) + for category in os.listdir(tasks_folder): + tasks = os.listdir(os.path.join(tasks_folder, category)) + for task in tasks: + with open(os.path.join(tasks_folder, category, task), 'r') as file: + data = yaml.safe_load(file) - for key in data: - if "vars" in key: - if image_name in key["vars"]["image"]["name"] or image_name == key["vars"]["image"]["name"]: - return f"{task.split(".")[0]}_deploy" + for key in data: + if "vars" in key: + if image_name in key["vars"]["image"]["name"] or image_name == key["vars"]["image"]["name"]: + return f"{task.split(".")[0]}_deploy" def main(): update_list = requests.get("https://cup.fntz.net/api/v3/json") @@ -54,7 +56,7 @@ def main(): subprocess.run(f'ANSIBLE_CONFIG=ansible.cfg ansible-playbook main.yml --tags {tag_string} -l bear --vault-password-file=~/.vault_pass.txt', shell=True) print("Attempting to clean up dangling/unassumed images") - subprocess.run(f"docker image prune -a -f", shell=True) + subprocess.run(f"docker image prune -af", shell=True) print("Redeployed all images, refreshing Cup") requests.get("https://cup.fntz.net/api/v3/refresh") diff --git a/tasks/glance.yml b/tasks/utility/glance.yml similarity index 100% rename from tasks/glance.yml rename to tasks/utility/glance.yml diff --git a/tasks/nextcloud.yml b/tasks/utility/nextcloud.yml similarity index 100% rename from tasks/nextcloud.yml rename to tasks/utility/nextcloud.yml