-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelete_machine_templates.py
More file actions
92 lines (64 loc) · 3.27 KB
/
Copy pathdelete_machine_templates.py
File metadata and controls
92 lines (64 loc) · 3.27 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
88
89
90
91
92
import subprocess
from backend.cleanup import teardown_remaining_challenges
from backend.proxmox_api_calls import delete_vm_api_call
from backend.DatabaseClasses import ChallengeTemplate, MachineTemplate, Challenge
def delete_machine_templates(challenge_template_id, db_conn):
"""
Delete the machine template VMs for a challenge.
"""
try:
disable_pooling_for_challenge_template(challenge_template_id, db_conn)
challenge_template = fetch_challenge_and_machine_templates(challenge_template_id, db_conn)
challenges = fetch_running_challenges(challenge_template, db_conn)
except Exception as e:
raise ValueError(f"Error fetching challenge and machine templates: {str(e)}")
teardown_remaining_challenges([challenge.id for challenge in challenges])
delete_machine_template_vms(challenge_template)
def disable_pooling_for_challenge_template(challenge_template_id, db_conn):
"""
Disable pooling for a challenge template.
"""
with db_conn.cursor() as cursor:
cursor.execute("UPDATE challenge_templates SET ready_to_launch = FALSE WHERE id = %s", (challenge_template_id,))
db_conn.commit()
def fetch_challenge_and_machine_templates(challenge_template_id, db_conn):
"""
Fetch the machine template IDs for a challenge.
"""
with db_conn.cursor() as cursor:
cursor.execute("SELECT id FROM challenge_templates WHERE id = %s", (challenge_template_id,))
result = cursor.fetchone()
if result is None:
raise ValueError(f"Challenge template with ID {challenge_template_id} not found.")
challenge_template = ChallengeTemplate(challenge_template_id=challenge_template_id)
with db_conn.cursor() as cursor:
cursor.execute("SELECT id FROM machine_templates WHERE challenge_template_id = %s", (challenge_template.id,))
for machine_template_id in cursor.fetchall():
machine_template = MachineTemplate(
machine_template_id=machine_template_id[0],
challenge_template=challenge_template
)
challenge_template.add_machine_template(machine_template)
return challenge_template
def fetch_running_challenges(challenge_template, db_conn):
"""
Fetch the running machine template instances for a challenge.
"""
challenges = []
with db_conn.cursor() as cursor:
cursor.execute("SELECT id, subnet FROM challenges WHERE challenge_template_id = %s", (challenge_template.id,))
for challenge_id, subnet in cursor.fetchall():
challenge = Challenge(challenge_id=challenge_id, template=challenge_template, subnet=subnet)
challenges.append(challenge)
return challenges
def delete_machine_template_vms(challenge_template):
"""
Delete the machine template VMs for a challenge.
"""
for machine_template in challenge_template.machine_templates.values():
try:
delete_vm_api_call(machine_template)
except Exception:
subprocess.run(["qm", "stop", str(machine_template.id)], check=False, capture_output=True)
subprocess.run(["qm", "unlock", str(machine_template.id)], check=True, capture_output=True)
subprocess.run(["qm", "destroy", str(machine_template.id)], check=True, capture_output=True)