-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdelete_all_lxd_containers.py
executable file
·88 lines (64 loc) · 2.7 KB
/
delete_all_lxd_containers.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
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
88
#!/usr/bin/env python3
""" This script deletes all LXC containers and/or images.
All containers and images are deleted. This incldes running containers
and OS images.
The script assumes that the user running the script has non-sudo access
to lxc
No dependencies.
Author: Ilhaan Rasheed
"""
import json
import subprocess
import argparse
import sys
def get_args():
"""Get args from command line"""
parser = argparse.ArgumentParser(description='Delete LXC Containers and \
Images')
parser.add_argument('-c', '--containers', dest='container_delete',
default=False, action='store_true',
help='Enable this to delete all LXC containers')
parser.add_argument('-i', '--images', dest='image_delete', default=False,
action='store_true',
help='Enable this to delete all LXC images')
args = parser.parse_args()
if args.container_delete is False and args.image_delete is False:
print("You need to enable either container or images or both. \
Use --help for more info.")
sys.exit()
else:
return (args.container_delete, args.image_delete)
def container_delete():
"""Function that deletes all containers"""
# Run lxc list command to get JSON output as a string
lxc_output = subprocess.run(["/snap/bin/lxc", "list", "-c", "n", "--format=json"],
stdout=subprocess.PIPE).stdout.decode('utf-8')
# Load json string output
data = json.loads(lxc_output)
# Create empty list to store LXC Container names
container_names = []
for x in range(len(data)):
container_names.append(data[x]["name"])
# Run LXD Delete command for each container
for container in container_names:
subprocess.run(["/snap/bin/lxc", "delete", "--force", container])
def image_delete():
"""Function that deletes all images"""
# Run lxc image list command to get JSON output as a string
lxc_output = subprocess.run(["/snap/bin/lxc", "image", "list", "--format=json"],
stdout=subprocess.PIPE).stdout.decode('utf-8')
# Load json string output
data = json.loads(lxc_output)
# Create empty list to store LXC Image fingerprints
image_fingerprints = []
for x in range(len(data)):
image_fingerprints.append(data[x]["fingerprint"])
# Run LXD Image Delete command for each container
for image in image_fingerprints:
subprocess.run(["/snap/bin/lxc", "image", "delete", image])
if __name__ == '__main__':
(container, image) = get_args()
if container:
container_delete()
if image:
image_delete()