-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecklist.py
executable file
·308 lines (262 loc) · 9.71 KB
/
checklist.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
import json
import re
import shutil
import subprocess
import sys
def check_if_installed(command):
return shutil.which(command) is not None
def check_debian_or_ubuntu():
"""
Check if the operating system is Debian or Ubuntu.
"""
try:
with open("/etc/os-release", "r") as file:
content = file.read()
if "ID=debian" in content or "ID=ubuntu" in content:
return True
except FileNotFoundError:
pass
except Exception as e:
print(e)
return False
def check_kubectl_installed(silent=False):
if not check_if_installed("kubectl"):
if not silent:
print("❌ kubectl is not installed.")
print("Check https://kubernetes.io/docs/tasks/tools/")
if sys.platform == "darwin":
print("\nYou can also install kubectl using brew:")
print("brew install kubernetes-cli")
return False
return True
def check_cluster_status(silent=False):
result = subprocess.run(
["kubectl", "cluster-info"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if result.returncode != 0:
if not silent:
print("❌ kubectl cannot contact the Kubernetes cluster.")
print("Please start a kubernetes cluster or check its status.")
if sys.platform == "darwin":
print("colima is a good option for Mac users:")
print("brew install colima")
print("colima start --kubernetes")
elif sys.platform.startswith("linux"):
print("k3s is a good option for Linux users")
print("Check https://k3s.io for installation instructions")
print("If you want to use K3S on top of Docker, check K3D:")
print("https://k3d.io")
print("")
elif sys.platform.startswith("win"):
print("Rancher Desktop is a good option for Windows users")
print("Check https://rancherdesktop.io for installation instructions")
else:
print(
"Check https://kubernetes.io/docs/tasks/tools/ for installation instructions"
)
return False
result = subprocess.run(
["kubectl", "get", "nodes", "--no-headers", "-o", "json"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
if result.returncode != 0:
if not silent:
print("❌ kubectl cannot get nodes from the Kubernetes cluster.")
print("Please check your cluster status.")
return False
nodes = json.loads(result.stdout)["items"]
has_unready_node = False
for node in nodes:
name = node["metadata"]["name"]
status = node["status"]["conditions"][-1]["status"]
if status != "True":
if not silent:
print(f"❌ Node {name} is not ready. Status: {status}")
has_unready_node = True
if has_unready_node:
if not silent:
print("Please check your cluster status.")
return False
return True
def check_helm_installed(silent=False):
if not check_if_installed("helm"):
if not silent:
print("❌ helm is not installed")
print("Check https://helm.sh/docs/intro/install/")
if sys.platform == "darwin":
print("\nYou can also install helm using brew:")
print("brew install helm")
return False
# Check helm version > 3
result = subprocess.run(
["helm", "version", "--short", "--client"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
version = result.stdout.decode("utf-8").strip()
version_re = re.compile(r"^v(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)")
match = version_re.match(version)
if not match or int(match.group("major")) < 3 or int(match.group("minor")) < 7:
if not silent:
print("❌ helm version is not 3.7 or higher")
print("Check https://helm.sh/docs/intro/install/ to upgrade helm")
return False
return True
def check_helm_diff_installed(silent=False):
if not check_if_installed("helm"):
return False
try:
result = subprocess.run(
["helm", "plugin", "list"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=True,
)
except subprocess.CalledProcessError as e:
if not silent:
print(f"❌ Error while checking helm-diff plugin: {e}")
return False
output = result.stdout.decode("utf-8")
helm_diff_pattern = re.compile(
r"^diff\s+(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)", re.MULTILINE
)
match = helm_diff_pattern.search(output)
if match:
if int(match.group("major")) < 3:
if not silent:
print("❌ helm-diff plugin version is not 3.1 or higher")
print("Please upgrade it.")
print("https://github.com/databus23/helm-diff")
return False
else:
if not silent:
print("❌ helm-diff plugin is not installed")
print("Follow the helm plugin manager installation instructions:")
print("https://github.com/databus23/helm-diff")
return False
return True
def check_argo_installed(silent=False):
if not check_if_installed("argo"):
if not silent:
print("❌ argo is not installed")
print(
"Download the argo CLI from https://github.com/argoproj/argo-workflows/releases"
)
print("and add it to your PATH.")
if sys.platform == "darwin":
print("\nYou can also install argo using brew:")
print("brew install argo")
return False
return True
def check_docker_installed(silent=False):
if not check_if_installed("docker"):
if not silent:
print("❌ docker is not installed")
print(
"Check https://docs.docker.com/get-docker/ for installation instructions"
)
if sys.platform == "darwin":
print("\nYou can also install docker using brew:")
print("brew install docker")
return False
if not check_if_installed("docker-buildx"):
# Check if the command "docker buildx version" returns a non-zero exit code
result = subprocess.run(
["docker", "buildx", "version"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if result.returncode != 0:
if not silent:
print("❌ docker-buildx is not installed")
print(
"Check https://docs.docker.com/buildx/working-with-buildx/ for installation instructions"
)
if sys.platform == "darwin":
print("\nYou can also install docker-buildx using brew:")
print("brew install docker-buildx")
return False
return True
def check_ansible_installed():
return all(
[
check_if_installed("ansible"),
check_if_installed("ansible-playbook"),
check_if_installed("ansible-galaxy"),
]
)
def check_tools_installed(silent=False):
return all(
[
check_kubectl_installed(silent),
check_docker_installed(silent),
check_helm_installed(silent),
check_helm_diff_installed(silent),
check_argo_installed(silent),
]
)
def check_simpipe_deployment_presence():
try:
output = subprocess.check_output(
["helm", "list", "--deployed", "--output", "json"]
)
deployments = json.loads(output)
for deployment in deployments:
if deployment["name"] == "simpipe":
return True
return False
except subprocess.CalledProcessError as e:
print(f"❌ Error while checking simpipe's deployment: {e}")
return False
def check_simpipe_pods_health(silent=False):
result = subprocess.run(
[
"kubectl",
"get",
"pods",
"-l",
"app.kubernetes.io/instance=simpipe",
"--no-headers",
"-o",
"json",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if result.returncode != 0:
if not silent:
print("Error while fetching simpipe pods:", result.stderr.decode("utf-8"))
return False
pods = json.loads(result.stdout)["items"]
unhealthy_pods = []
for pod in pods:
name = pod["metadata"]["name"]
status = pod["status"]["phase"]
if status != "Running":
containers = pod["status"].get("containerStatuses", [])
for container in containers:
if "waiting" in container["state"]:
reason = container["state"]["waiting"]["reason"]
if reason not in ["ContainerCreating", "PodInitializing"]:
unhealthy_pods.append((name, reason))
elif "terminated" in container["state"]:
reason = container["state"]["terminated"]["reason"]
unhealthy_pods.append((name, reason))
if unhealthy_pods:
if not silent:
print("❌ Some simpipe pods are not healthy:")
for name, reason in unhealthy_pods:
print(f" - Pod {name} is not healthy. Reason: {reason}")
print("Please check your deployment.")
return False
return True
def main():
if not check_tools_installed():
sys.exit(1)
check_cluster_status()
if __name__ == "__main__":
main()