Skip to content

Commit d65fea5

Browse files
authored
Merge pull request #14 from com6056/jrodgers-fix-templates
fix template provisioning by verifying VM is provisioned before doing anything else
2 parents 84f9a00 + 1acb840 commit d65fea5

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

gunicorn_conf.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
def start_websockify(websockify_path, target_file):
1717
result = subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE)
1818
if not result.stdout:
19-
subprocess.call(
20-
[
21-
websockify_path, '8081', '--token-plugin', 'TokenFile',
22-
'--token-source', target_file, '-D'
23-
],
24-
stdout=subprocess.PIPE)
19+
subprocess.call([
20+
websockify_path, '8081', '--token-plugin', 'TokenFile',
21+
'--token-source', target_file, '-D'
22+
],
23+
stdout=subprocess.PIPE)
2524

2625

2726
def on_starting(server):

proxstar/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ def list_vms(user_view=None):
140140
if user.active:
141141
vms = user.vms
142142
for pending_vm in user.pending_vms:
143-
vm = next((vm for vm in vms
144-
if vm['name'] == pending_vm['name']), None)
143+
vm = next(
144+
(vm for vm in vms if vm['name'] == pending_vm['name']),
145+
None)
145146
if vm:
146147
vms[vms.index(vm)]['status'] = pending_vm['status']
147148
vms[vms.index(vm)]['pending'] = True

proxstar/tasks.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def process_expiring_vms_task():
104104
vm.stop()
105105
elif days <= -7:
106106
print(
107-
"Deleting {} ({}) as it has been at least a week since expiration.".
108-
format(vm.name, vm.id))
107+
"Deleting {} ({}) as it has been at least a week since expiration."
108+
.format(vm.name, vm.id))
109109
send_stop_ssh_tunnel(vm.id)
110110
delete_vm_task(vm.id)
111111
if expiring_vms:
@@ -135,15 +135,29 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
135135
job.meta['status'] = 'cloning template'
136136
job.save_meta()
137137
vmid, mac = clone_vm(proxmox, template_id, name, user)
138+
print("[{}] Waiting until Proxmox is done provisioning.".format(name))
139+
job.meta['status'] = 'waiting for Proxmox'
140+
job.save_meta()
141+
timeout = 200
142+
retry = 0
143+
while retry < timeout:
144+
if not VM(vmid).is_provisioned():
145+
retry += 1
146+
time.sleep(3)
147+
continue
148+
break
149+
if retry == timeout:
150+
print("[{}] Failed to provision, deleting.".format(name))
151+
job.meta['status'] = 'failed to provision'
152+
job.save_meta()
153+
delete_vm_task(vmid)
154+
return
138155
print("[{}] Registering in STARRS.".format(name))
139156
job.meta['status'] = 'registering in STARRS'
140157
job.save_meta()
141158
ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE'])
142159
register_starrs(starrs, name, app.config['STARRS_USER'], mac, ip)
143160
get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
144-
print("[{}] Giving Proxmox some time to finish cloning.".format(name))
145-
job.meta['status'] = 'waiting for Proxmox'
146-
time.sleep(15)
147161
print("[{}] Setting CPU and memory.".format(name))
148162
job.meta['status'] = 'setting CPU and memory'
149163
job.save_meta()
@@ -152,12 +166,12 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
152166
vm.set_mem(memory)
153167
print("[{}] Applying cloud-init config.".format(name))
154168
job.meta['status'] = 'applying cloud-init'
169+
job.save_meta()
155170
vm.set_ci_user(user)
156171
vm.set_ci_ssh_key(ssh_key)
157172
vm.set_ci_network()
158-
print(
159-
"[{}] Waiting for STARRS to propogate before starting VM.".format(
160-
name))
173+
print("[{}] Waiting for STARRS to propogate before starting VM.".
174+
format(name))
161175
job.meta['status'] = 'waiting for STARRS'
162176
job.save_meta()
163177
time.sleep(90)

proxstar/vm.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ def status(self):
3737
def qmpstatus(self):
3838
return self.info['qmpstatus']
3939

40+
def is_provisioned(self):
41+
try:
42+
self.set_cpu(self.cpu)
43+
return True
44+
except:
45+
return False
46+
4047
@lazy_property
4148
def node(self):
4249
proxmox = connect_proxmox()

proxstar/vnc.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
def start_websockify(websockify_path, target_file):
1111
result = subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE)
1212
if not result.stdout:
13-
subprocess.call(
14-
[
15-
websockify_path, '8081', '--token-plugin', 'TokenFile',
16-
'--token-source', target_file, '-D'
17-
],
18-
stdout=subprocess.PIPE)
13+
subprocess.call([
14+
websockify_path, '8081', '--token-plugin', 'TokenFile',
15+
'--token-source', target_file, '-D'
16+
],
17+
stdout=subprocess.PIPE)
1918

2019

2120
def stop_websockify():
@@ -24,11 +23,11 @@ def stop_websockify():
2423
pid = result.stdout.strip()
2524
subprocess.run(['kill', pid], stdout=subprocess.PIPE)
2625
time.sleep(3)
27-
if subprocess.run(
28-
['pgrep', 'websockify'], stdout=subprocess.PIPE).stdout:
26+
if subprocess.run(['pgrep', 'websockify'],
27+
stdout=subprocess.PIPE).stdout:
2928
time.sleep(10)
30-
if subprocess.run(
31-
['pgrep', 'websockify'], stdout=subprocess.PIPE).stdout:
29+
if subprocess.run(['pgrep', 'websockify'],
30+
stdout=subprocess.PIPE).stdout:
3231
print('Websockify didn\'t stop, killing forcefully.')
3332
subprocess.run(['kill', '-9', pid], stdout=subprocess.PIPE)
3433

@@ -90,8 +89,9 @@ def start_ssh_tunnel(node, port):
9089
def stop_ssh_tunnel(vmid, ssh_tunnels):
9190
# Tear down the SSH tunnel and VNC target entry for a given VM
9291
port = 5900 + int(vmid)
93-
tunnel = next((tunnel for tunnel in ssh_tunnels
94-
if tunnel.local_bind_port == port), None)
92+
tunnel = next(
93+
(tunnel for tunnel in ssh_tunnels if tunnel.local_bind_port == port),
94+
None)
9595
if tunnel:
9696
print("Tearing down SSH tunnel for VM {}.".format(vmid))
9797
try:

0 commit comments

Comments
 (0)