-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.py
executable file
·50 lines (39 loc) · 1.22 KB
/
destroy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
import sys
import json
import os
import subprocess
outputs_path = sys.argv[1]
outputs = json.load(open(outputs_path))
if os.path.exists("imported_image"):
imported_image = open("imported_image").read()
if imported_image != "":
print "Skipping destroy on imported image", imported_image
sys.exit(0)
if 'image' not in outputs:
print "Missing 'image' output variable."
sys.exit(1)
def run_docker(cmd):
print "Running '%s'" % " ".join(cmd)
sys.stdout.flush()
try:
output = subprocess.check_output(cmd)
except subprocess.CalledProcessError, e:
print "Failed to run", cmd
print e
sys.exit(1)
return output
image = outputs['image']
print "Removing Docker image '%s'" % image
env = os.environ
docker_cmd = json.loads(env['INPUT_docker_cmd'])
output = run_docker(docker_cmd + ["images", "-q", image]).strip()
if output == "":
print "Image '%s' already removed" % image
parts = image.split(":")
latest = ":".join(parts[:-1]) + ":latest"
latest_out = run_docker(docker_cmd + ["images", "-q", latest]).strip()
is_latest = latest_out == output
run_docker(docker_cmd + ["rmi", image])
if is_latest:
run_docker(docker_cmd + ["rmi", latest])