Skip to content

Commit

Permalink
feat: implemented spring-clean for remote incus
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ-Jackson committed Dec 25, 2024
1 parent a0975cf commit f3e865e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
1 change: 1 addition & 0 deletions remote-incus/_opt/script/spring-clean-metal.py
76 changes: 76 additions & 0 deletions remote-incus/_opt/script/spring-clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
import argparse
import os.path
import pathlib
import subprocess
import time

parser = argparse.ArgumentParser(description="Clean out older deploy on incus container and keep the last defined amount")

parser.add_argument("--keep", default=20, help="The amount of last deploy to keep")
parser.add_argument("--real-run", action='store_true')
parser.add_argument("--incus", required=True)

args = parser.parse_args()

arg_keep = int(args.keep)
arg_real_run = args.real_run
arg_incus = args.incus

unsorted_images = []
try:
unsorted_images = subprocess.run([
"incus", "exec", arg_incus, "--", "sh", "-c", "for image in /opt/run-deploy/image/*/*.blame; do (echo ${image}); done"
], check=True, capture_output=True).stdout.decode('utf-8').strip().splitlines()
except subprocess.CalledProcessError:
exit(0)

images = {}
for unsorted_image in unsorted_images:
image_key = os.path.dirname(unsorted_image)
if image_key not in images:
images[image_key] = []
images[image_key].append(unsorted_image)

images_to_delete = []
for _, image_list in images.items():
image_list.sort()
image_list = list(reversed(image_list))
if len(image_list) > arg_keep:
images_to_delete += image_list[arg_keep:]

if len(images_to_delete) == 0:
exit(0)

files_to_delete = []
for image in images_to_delete:
image = str(image)
files_to_delete.append(f"rm '{image}'")
files_to_delete.append(f"rm '{image.removesuffix('.blame')}'")
files_to_delete.append(f"rm '{image.removesuffix('.blame')}.squashfs'")

if len(files_to_delete) == 0:
exit(0)

files_to_delete = "\n".join(files_to_delete)

script = f"""#!/bin/sh
{files_to_delete}"""

if not arg_real_run:
print(script)
exit(0)

script_name = f"/tmp/run-deploy-spring-clean-incus-{time.time()}"
script_path = pathlib.Path(script_name)
script_path.write_text(script, 'utf-8')
script_path.chmod(0o700)

subprocess.run([
"incus", "file", "push", script_name, f"{arg_incus}/tmp/"
], check=True)
subprocess.run([
"incus", "exec", arg_incus, "--", script_name
], check=True)
os.remove(script_name)
5 changes: 4 additions & 1 deletion remote-metal/_opt/script/spring-clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess
import time

parser = argparse.ArgumentParser(description="Clean out older deply and keep the last defined amount")
parser = argparse.ArgumentParser(description="Clean out older deploy on host and keep the last defined amount")

parser.add_argument("--keep", default=20, help="The amount of last deploy to keep")
parser.add_argument("--real-run", action='store_true')
Expand Down Expand Up @@ -33,6 +33,9 @@
if len(image_list) > arg_keep:
images_to_delete += image_list[arg_keep:]

if len(images_to_delete) == 0:
exit(0)

files_to_delete = []
for image in images_to_delete:
image = str(image)
Expand Down
2 changes: 1 addition & 1 deletion test-util/create_test_image_for_spring_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import pathlib

os.chdir("/opt/run-deploy/images")
os.chdir("/opt/run-deploy/image")

for dir_name in range(5):
dir_name = f"run-deploy-test-{dir_name:02}"
Expand Down

0 comments on commit f3e865e

Please sign in to comment.