Skip to content

Commit 8bf49a6

Browse files
committed
add retries everywhere cuz proxmox sucks, use json for iso list (for marc <3)
1 parent b658033 commit 8bf49a6

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

proxstar/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import json
23
import time
34
import psutil
45
import atexit
@@ -157,7 +158,7 @@ def list_vms(user_view=None):
157158
def isos():
158159
proxmox = connect_proxmox()
159160
isos = get_isos(proxmox, app.config['PROXMOX_ISO_STORAGE'])
160-
return ','.join(isos)
161+
return json.dumps({"isos": isos})
161162

162163

163164
@app.route("/hostname/<string:name>")

proxstar/static/js/script.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,11 @@ $("#change-iso").click(function(){
171171
fetch(`/isos`, {
172172
credentials: 'same-origin',
173173
}).then((response) => {
174-
return response.text()
175-
}).then((text) => {
176-
var isos = text.split(',');
174+
return response.json()
175+
}).then((json) => {
177176
var iso_list = document.createElement('select');
178-
for (i = 0; i < isos.length; i++) {
179-
iso_list.appendChild(new Option(isos[i], isos[i]));
177+
for (i = 0; i < json.isos.length; i++) {
178+
iso_list.appendChild(new Option(json.isos[i], json.isos[i]));
180179
}
181180
swal({
182181
title: 'Choose an ISO to mount:',

proxstar/tasks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
133133
ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE'])
134134
register_starrs(starrs, name, app.config['STARRS_USER'], mac, ip)
135135
get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
136+
print("[{}] Giving Proxmox some time to finish cloning.".format(name))
137+
job.meta['status'] = 'waiting for Proxmox'
138+
time.sleep(15)
136139
print("[{}] Setting CPU and memory.".format(name))
137140
job.meta['status'] = 'setting CPU and memory'
138141
job.save_meta()

proxstar/vm.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import json
33
import urllib
4+
from tenacity import retry, wait_fixed, stop_after_attempt
45
from proxstar import db, starrs
56
from proxstar.db import get_vm_expire
67
from proxstar.util import lazy_property
@@ -43,39 +44,48 @@ def node(self):
4344
if vm['vmid'] == int(self.id):
4445
return vm['node']
4546

47+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
4648
def delete(self):
4749
proxmox = connect_proxmox()
4850
proxmox.nodes(self.node).qemu(self.id).delete()
4951

52+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
5053
def set_cpu(self, cores):
5154
proxmox = connect_proxmox()
5255
proxmox.nodes(self.node).qemu(self.id).config.put(
5356
cores=cores, sockets=1)
5457

58+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
5559
def set_mem(self, mem):
5660
proxmox = connect_proxmox()
5761
proxmox.nodes(self.node).qemu(self.id).config.put(memory=mem)
5862

63+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
5964
def start(self):
6065
proxmox = connect_proxmox()
6166
proxmox.nodes(self.node).qemu(self.id).status.start.post()
6267

68+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
6369
def stop(self):
6470
proxmox = connect_proxmox()
6571
proxmox.nodes(self.node).qemu(self.id).status.stop.post()
6672

73+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
6774
def shutdown(self):
6875
proxmox = connect_proxmox()
6976
proxmox.nodes(self.node).qemu(self.id).status.shutdown.post()
7077

78+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
7179
def reset(self):
7280
proxmox = connect_proxmox()
7381
proxmox.nodes(self.node).qemu(self.id).status.reset.post()
7482

83+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
7584
def suspend(self):
7685
proxmox = connect_proxmox()
7786
proxmox.nodes(self.node).qemu(self.id).status.suspend.post()
7887

88+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
7989
def resume(self):
8090
proxmox = connect_proxmox()
8191
proxmox.nodes(self.node).qemu(self.id).status.resume.post()
@@ -108,6 +118,7 @@ def boot_order(self):
108118
def boot_order_json(self):
109119
return json.dumps(self.boot_order)
110120

121+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
111122
def set_boot_order(self, boot_order):
112123
proxmox = connect_proxmox()
113124
boot_order_lookup = {
@@ -184,11 +195,13 @@ def start_vnc(self, port):
184195
proxmox.nodes(self.node).qemu(self.id).monitor.post(
185196
command="change vnc 127.0.0.1:{}".format(port))
186197

198+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
187199
def eject_iso(self):
188200
proxmox = connect_proxmox()
189201
proxmox.nodes(self.node).qemu(
190202
self.id).config.post(ide2='none,media=cdrom')
191203

204+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
192205
def mount_iso(self, iso):
193206
proxmox = connect_proxmox()
194207
proxmox.nodes(self.node).qemu(
@@ -203,15 +216,18 @@ def resize_disk(self, disk, size):
203216
def expire(self):
204217
return get_vm_expire(db, self.id, app.config['VM_EXPIRE_MONTHS'])
205218

219+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
206220
def set_ci_user(self, user):
207221
proxmox = connect_proxmox_ssh()
208222
proxmox.nodes(self.node).qemu(self.id).config.put(ciuser=user)
209223

224+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
210225
def set_ci_ssh_key(self, ssh_key):
211226
proxmox = connect_proxmox_ssh()
212227
escaped_key = urllib.parse.quote(ssh_key, safe='')
213228
proxmox.nodes(self.node).qemu(self.id).config.put(sshkey=escaped_key)
214229

230+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
215231
def set_ci_network(self):
216232
proxmox = connect_proxmox_ssh()
217233
proxmox.nodes(self.node).qemu(self.id).config.put(ipconfig0='ip=dhcp')
@@ -232,7 +248,7 @@ def create_vm(proxmox, user, name, cores, memory, disk, iso):
232248
pool=user,
233249
description='Managed by Proxstar')
234250
retry = 0
235-
while retry < 5:
251+
while retry < 20:
236252
try:
237253
mac = VM(vmid).get_mac()
238254
break
@@ -254,7 +270,7 @@ def clone_vm(proxmox, template_id, name, pool):
254270
description='Managed by Proxstar',
255271
target=target)
256272
retry = 0
257-
while retry < 60:
273+
while retry < 100:
258274
try:
259275
mac = VM(newid).get_mac()
260276
break

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ sshtunnel
1515
psutil
1616
requests
1717
rq_dashboard
18+
tenacity

0 commit comments

Comments
 (0)