fix update script
This commit is contained in:
+11
-11
@@ -13,7 +13,16 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Configure credentials
|
- name: Install required dependencies
|
||||||
|
run: |
|
||||||
|
python3 -m pip install requests pyyaml ansible-core --break-system-packages
|
||||||
|
ansible-galaxy collection install community.docker
|
||||||
|
|
||||||
|
# ssh-agent only lives for the shell it was started in, so credentials
|
||||||
|
# must be set up in the same step that runs ansible
|
||||||
|
- name: Run update script
|
||||||
|
env:
|
||||||
|
UPDATES_DISCORD_WEBHOOK: ${{vars.UPDATES_DISCORD_WEBHOOK}}
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts
|
echo "${{ secrets.SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts
|
||||||
@@ -21,13 +30,4 @@ jobs:
|
|||||||
eval $(ssh-agent -s)
|
eval $(ssh-agent -s)
|
||||||
ssh-add <(echo "${{ secrets.SSH_KEY }}")
|
ssh-add <(echo "${{ secrets.SSH_KEY }}")
|
||||||
echo "${{ secrets.VAULT_PASS }}" > ~/.vault_pass.txt
|
echo "${{ secrets.VAULT_PASS }}" > ~/.vault_pass.txt
|
||||||
|
python3 scripts/run_updates.py
|
||||||
- name: Install required dependencies
|
|
||||||
run: |
|
|
||||||
python3 -m pip install requests pyyaml ansible-core --break-system-packages
|
|
||||||
ansible-galaxy collection install community.docker
|
|
||||||
|
|
||||||
- name: Run update script
|
|
||||||
run: python3 scripts/run_updates.py
|
|
||||||
env:
|
|
||||||
UPDATES_DISCORD_WEBHOOK: ${{vars.UPDATES_DISCORD_WEBHOOK}}
|
|
||||||
|
|||||||
+35
-18
@@ -5,22 +5,36 @@ import yaml
|
|||||||
|
|
||||||
DISCORD_WEBHOOK_URL = os.environ.get("UPDATES_DISCORD_WEBHOOK")
|
DISCORD_WEBHOOK_URL = os.environ.get("UPDATES_DISCORD_WEBHOOK")
|
||||||
|
|
||||||
def search_for_image(image_name):
|
def normalize_image_name(image_name):
|
||||||
tasks_folder = os.path.realpath(os.path.join('./', 'tasks'))
|
# strip the tag (but not a registry port) and docker hub prefixes
|
||||||
if "library/" in image_name:
|
name, _, tag = image_name.rpartition(":")
|
||||||
image_name = image_name.replace("library/", "")
|
if name and "/" not in tag:
|
||||||
image_name = image_name.split(":")[0]
|
image_name = name
|
||||||
|
for prefix in ("docker.io/", "library/"):
|
||||||
|
if image_name.startswith(prefix):
|
||||||
|
image_name = image_name[len(prefix):]
|
||||||
|
return image_name
|
||||||
|
|
||||||
for category in os.listdir(tasks_folder):
|
def search_for_images(image_name):
|
||||||
tasks = os.listdir(os.path.join(tasks_folder, category))
|
tasks_folder = os.path.realpath(os.path.join('./', 'tasks'))
|
||||||
|
image_name = normalize_image_name(image_name)
|
||||||
|
matches = []
|
||||||
|
|
||||||
|
for category in sorted(os.listdir(tasks_folder)):
|
||||||
|
tasks = sorted(os.listdir(os.path.join(tasks_folder, category)))
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
with open(os.path.join(tasks_folder, category, task), 'r') as file:
|
with open(os.path.join(tasks_folder, category, task), 'r') as file:
|
||||||
data = yaml.safe_load(file)
|
data = yaml.safe_load(file) or []
|
||||||
|
|
||||||
for key in data:
|
for key in data:
|
||||||
if "vars" in key:
|
image = key.get("vars", {}).get("image")
|
||||||
if image_name in key["vars"]["image"]["name"] or image_name == key["vars"]["image"]["name"]:
|
if not image:
|
||||||
return f"{task.split(".")[0]}_deploy"
|
continue
|
||||||
|
if normalize_image_name(image["name"]) == image_name:
|
||||||
|
matches.append(f"{task.split(".")[0]}_deploy")
|
||||||
|
break
|
||||||
|
|
||||||
|
return matches
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
update_list = requests.get("https://cup.fntz.net/api/v3/json")
|
update_list = requests.get("https://cup.fntz.net/api/v3/json")
|
||||||
@@ -31,32 +45,35 @@ def main():
|
|||||||
|
|
||||||
for image in update_list["images"]:
|
for image in update_list["images"]:
|
||||||
reference = image["reference"]
|
reference = image["reference"]
|
||||||
|
result = image["result"]
|
||||||
|
|
||||||
# don't attempt to update if unable
|
# don't attempt to update if unable
|
||||||
if not image["in_use"]:
|
if not image["in_use"]:
|
||||||
continue
|
continue
|
||||||
if ":latest" not in reference:
|
if not result or not result["has_update"]:
|
||||||
continue
|
continue
|
||||||
if image["result"] and not image["result"]["has_update"]:
|
# version updates need a tag bump in the repo, redeploying won't pick them up
|
||||||
|
if (result.get("info") or {}).get("type") != "digest":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
refs.append(reference)
|
refs.append(reference)
|
||||||
|
|
||||||
print("attempting to match images to references: " + ", ".join(refs))
|
print("attempting to match images to references: " + ", ".join(refs))
|
||||||
for reference in refs:
|
for reference in refs:
|
||||||
tag = search_for_image(reference)
|
tags = search_for_images(reference)
|
||||||
if tag:
|
for tag in tags:
|
||||||
|
if tag not in deployable_tags:
|
||||||
deployable_tags.append(tag)
|
deployable_tags.append(tag)
|
||||||
else:
|
if not tags:
|
||||||
print("Could not find suitable container for " + reference)
|
print("Could not find suitable container for " + reference)
|
||||||
|
|
||||||
if len(deployable_tags) > 0:
|
if len(deployable_tags) > 0:
|
||||||
print(f"Found {len(deployable_tags)}, deploying..")
|
print(f"Found {len(deployable_tags)}, deploying..")
|
||||||
tag_string = ",".join(deployable_tags)
|
tag_string = ",".join(deployable_tags)
|
||||||
subprocess.run(f'ANSIBLE_CONFIG=ansible.cfg ansible-playbook main.yml --tags {tag_string} -l bear --vault-password-file=~/.vault_pass.txt', shell=True)
|
subprocess.run(f'ANSIBLE_CONFIG=ansible.cfg ansible-playbook main.yml --tags {tag_string} -l bear --vault-password-file=~/.vault_pass.txt', shell=True, check=True)
|
||||||
|
|
||||||
print("Attempting to clean up dangling/unassumed images")
|
print("Attempting to clean up dangling/unassumed images")
|
||||||
subprocess.run(f"docker image prune -af", shell=True)
|
subprocess.run('ANSIBLE_CONFIG=ansible.cfg ansible bear -m command -a "docker image prune -af"', shell=True)
|
||||||
|
|
||||||
print("Redeployed all images, refreshing Cup")
|
print("Redeployed all images, refreshing Cup")
|
||||||
requests.get("https://cup.fntz.net/api/v3/refresh")
|
requests.get("https://cup.fntz.net/api/v3/refresh")
|
||||||
|
|||||||
Reference in New Issue
Block a user