-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockcrush.py
More file actions
executable file
·87 lines (76 loc) · 3.26 KB
/
rockcrush.py
File metadata and controls
executable file
·87 lines (76 loc) · 3.26 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3
import subprocess
import sys
def get_microk8s_image_list(wanted_image_names: list[str], unwanted_image_names: list[str]):
result = subprocess.run(['microk8s', 'ctr', 'images', 'ls'], stdout=subprocess.PIPE)
output = result.stdout.decode('utf-8')
images = output.splitlines()
rock_images = []
for image in images:
if 'localhost:32000' in image and any(image_name in image for image_name in wanted_image_names) and not any(image_name in image for image_name in unwanted_image_names) :
rock_images.append(image)
return rock_images
def get_docker_image_list(wanted_image_names: list[str], unwanted_image_names: list[str]):
result = subprocess.run(['docker', 'images'], stdout=subprocess.PIPE)
output = result.stdout.decode('utf-8')
images = output.splitlines()
rock_images = []
for image in images:
if image.startswith('localhost:32000')and any(image_name in image for image_name in wanted_image_names) and not any(image_name in image for image_name in unwanted_image_names) :
rock_images.append(image)
return rock_images
def get_microk8s_image_id(rock: str):
return rock.split()[0]
def get_docker_image_id(rock: str):
return rock.split()[2]
def remove_microk8s_rock(image_id: str):
print(f'removing image {image_id}')
result = subprocess.run(['microk8s', 'ctr', 'image', 'remove', image_id], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = result.stdout.decode('utf-8')
def remove_docker_rock(rock: str):
image_id = get_docker_image_id(rock)
print(f'removing image {image_id}')
result = subprocess.run(['docker', 'rmi', '-f', image_id], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = result.stdout.decode('utf-8')
if 'image is being used by running container' in output:
container_name = output.split()[-1]
print(f'removing container {container_name}')
result = subprocess.run(['docker', 'rm', '-f', container_name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(output.splitlines())
result = subprocess.run(['docker', 'rmi', '-f', image_id], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = result.stdout.decode('utf-8')
print(output)
if __name__ == '__main__':
args = sys.argv[1:]
undesired = []
use_microk8s = True
dry_run = False
if '-d' in args:
args.remove('-d')
use_microk8s = False
if '-n' in args:
non_index = args.index('-n')
undesired = args[non_index+1:]
args = args[:non_index]
if '--dry-run' in args:
dry_run = True
print('Current rocks')
if use_microk8s:
rocks = get_microk8s_image_list(args, undesired)
else:
rocks = get_docker_image_list(args, undesired)
for rock in rocks:
print(rock)
if not dry_run:
for rock in rocks:
if use_microk8s:
remove_microk8s_rock(get_microk8s_image_id(rock))
else:
remove_docker_rock(get_docker_image_id(rock))
print('Final rocks')
if use_microk8s:
rocks = get_microk8s_image_list(args, undesired)
else:
rocks = get_docker_image_list(args, undesired)
for rock in rocks:
print(rock)