Skip to content

Commit b13fd30

Browse files
Merge pull request #295 from matyasselmeci/pr/keep-days
docker-image-puller: Allow overriding the number of days to keep images (OPS-464)
2 parents d29016b + e759ea6 commit b13fd30

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

opensciencegrid/ospool-image-puller/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ COPY --chmod=0755 startup.sh /bin/startup.sh
1818
ENV CRON_EXPR="30 5 * * *"
1919
ENV TIMEOUT="23h"
2020
ENV IMAGES_UID="1000"
21+
ENV KEEP_DAYS=10
2122

2223
VOLUME ["/ospool/images"]
2324

2425
CMD ["/bin/startup.sh"]
25-

opensciencegrid/ospool-image-puller/startup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ install -o images -d "${TARGET_DIR}"
1616
# the container's stdout/err, but then drop privileges to run the
1717
# actual script.
1818

19-
echo "${CRON_EXPR?} root cd /ospool/images-scripts && /usr/sbin/runuser -u images -- timeout -k 60s ${TIMEOUT?} ./update.py ${TARGET_DIR}" '> /proc/1/fd/1 2>&1' > /etc/cron.d/update.cron
19+
echo "${CRON_EXPR?} root cd /ospool/images-scripts && /usr/sbin/runuser -u images -- timeout -k 60s ${TIMEOUT?} ./update.py --keep-days=${KEEP_DAYS:-10} ${TARGET_DIR}" '> /proc/1/fd/1 2>&1' > /etc/cron.d/update.cron
2020

2121
# Start cron in non-daemon (foreground) mode
2222
echo >&2 "Starting cron"

opensciencegrid/ospool-image-puller/update.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
#!/usr/bin/env python3
22

33
import os
4-
import sys
54
import time
65
import yaml
76
import datetime
87
import subprocess
8+
import argparse
99

1010
start_dir = os.getcwd()
1111

1212
def main():
13-
if len(sys.argv) != 2:
14-
print("Usage: update.py <target_dir>")
15-
sys.exit(1)
16-
17-
target_dir = sys.argv[1]
18-
19-
with open("images.yaml") as file:
13+
parser = argparse.ArgumentParser(
14+
description='Pull and update container images from Docker to Apptainer/Singularity format'
15+
)
16+
parser.add_argument(
17+
'target_dir',
18+
help='Target directory where images will be stored'
19+
)
20+
parser.add_argument(
21+
'--config',
22+
default='images.yaml',
23+
help='Path to the configuration YAML file (default: images.yaml)'
24+
)
25+
parser.add_argument(
26+
'--keep-days',
27+
type=int,
28+
default=10,
29+
help='Number of days to keep old images (default: 10)'
30+
)
31+
32+
args = parser.parse_args()
33+
34+
with open(args.config) as file:
2035
conf = yaml.safe_load(file)
2136

2237
now = datetime.datetime.now(datetime.timezone.utc)
@@ -36,13 +51,13 @@ def main():
3651
sing_arch = "arm64" if arch == "aarch64" else "amd64"
3752

3853
os.chdir(start_dir)
39-
os.makedirs(os.path.join(target_dir, arch, img["name"]), exist_ok=True)
40-
os.chdir(os.path.join(target_dir, arch, img["name"]))
54+
os.makedirs(os.path.join(args.target_dir, arch, img["name"]), exist_ok=True)
55+
os.chdir(os.path.join(args.target_dir, arch, img["name"]))
4156

4257
print(f"Working in {os.getcwd()}")
4358

4459
# log the build to a file in the same structure under logs/
45-
log_dir = os.path.join("../../..", target_dir, "logs", arch)
60+
log_dir = os.path.join("../../..", args.target_dir, "logs", arch)
4661
os.makedirs(log_dir, exist_ok=True)
4762
log_file = os.path.join(log_dir, f"{img['name']}.txt")
4863

@@ -65,7 +80,7 @@ def main():
6580
subprocess.run("ls *.sif | sort | tail -n 1 > latest.txt", shell=True)
6681

6782
# cleanup old images, keep only latest N
68-
subprocess.run("find . -maxdepth 1 -name \\*.sif -mtime +10 -exec rm -f {} \\;", shell=True)
83+
subprocess.run(f"find . -maxdepth 1 -name \\*.sif -mtime +{args.keep_days} -exec rm -f {{}} \\;", shell=True)
6984

7085
now2 = datetime.datetime.now(datetime.timezone.utc)
7186
now2_human = now2.strftime("%Y-%m-%d %H:%M:%S %Z")
@@ -76,4 +91,3 @@ def main():
7691

7792
if __name__ == "__main__":
7893
main()
79-

0 commit comments

Comments
 (0)