-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistro_handler.py
80 lines (60 loc) · 2.19 KB
/
distro_handler.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
import exception_handler as exception
distros = { # Defines all the distros
"ubuntu": {
"name": "Ubuntu",
"id": "ubuntu",
"default": False,
"install_file": "install_script_ubuntu.sh.j2",
"image": "kokofixcomputers/docker-openssh-server-fork-ubuntu:latest",
},
"alpine": {
"name": "Alpine",
"id": "alpine",
"default": True, # Default because of small file size
"install_file": "install_script.sh.j2",
"image": "kokofixcomputers/docker-openssh-server-fork:latest",
},
}
def get_install_file(distro):
try:
return distros[distro]["install_file"]
except BaseException:
return None
def validate_distros():
# Check if there are any distros
if not distros:
raise exception.NoDistrosError()
# Initialize a variable to track the count of defaults
default_count = 0
# Iterate through each distro in the dictionary
for key, value in distros.items():
# Check for required fields
if "name" not in value or not value["name"]:
raise exception.MissingFieldError("name", key)
if "id" not in value or not value["id"]:
raise exception.MissingFieldError("id", key)
if "image" not in value or not value["image"]:
raise exception.MissingFieldError("image", key)
# Check the default flag
if value.get("default", False):
default_count += 1
# Ensure that only one distro is marked as default
if default_count != 1:
raise exception.DefaultDistroError(default_count)
return True
def get_image(distro):
distro_info = distros.get(distro)
if distro_info is not None:
return distro_info.get("image")
return None # Return None if distro is not found
def validate_distro(distro):
return distro in distros
def get_distro_friendly_name(distro):
distro_info = distros.get(distro)
if distro_info is not None:
return distro_info.get("name")
return None # Return None if distro is not found
def get_distro_friendly_name_list():
return [distro_info.get("name") for distro_info in distros.values()]
def get_distro_list():
return distros