Skip to content

Commit

Permalink
chore: Add script to clean and back up ACR
Browse files Browse the repository at this point in the history
  • Loading branch information
mhamilton723 committed Sep 10, 2021
1 parent d85aae8 commit e08a8e2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tools/docker/clean_acr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import json
from azure.storage.blob import BlobClient
import sys
import subprocess
from tqdm import tqdm

acr = "mmlsparkmcr"
container = "acrbackup"
rg = "marhamil-mmlspark"
pipeline = "mmlsparkacrexport3"

conn_string = sys.argv[1]

repos = json.loads(os.popen(
'az acr repository list -n {}'.format(acr)).read())
for repo in repos:
tags = json.loads(os.popen(
'az acr repository show-tags -n {} --repository {} --orderby time_desc'.format(acr, repo)).read())

for tag in tqdm(tags):
target_blob = repo + "/" + tag + ".tar"
image = repo + ":" + tag

backup_exists = BlobClient.from_connection_string(
conn_string, container_name=container, blob_name=target_blob).exists()
if not backup_exists:
subprocess.run(["sudo", "az", "acr", "pipeline-run", "create", "--resource-group", rg,
"--registry", acr, "--pipeline", pipeline, "--name", str(abs(hash(target_blob))),
"--pipeline-type", "export", "--storage-blob", target_blob, "-a", image])
print("Transferred {}".format(target_blob))
else:
print("Skipped existing {}".format(image))

backup_exists = BlobClient.from_connection_string(
conn_string, container_name=container, blob_name=target_blob).exists()
if backup_exists:
print("Deleting {}".format(image))
result = os.system("az acr repository delete --name {} --image {} --yes".format(acr, image))
assert result == 0

0 comments on commit e08a8e2

Please sign in to comment.