From ba36af7dd500d59bfa8a913a007199e83868eb0f Mon Sep 17 00:00:00 2001 From: Jordan Rodgers Date: Wed, 6 Dec 2017 00:34:22 -0500 Subject: [PATCH] allow selection of ISO during creation and display ISO on vm details page --- app.py | 10 ++++++++-- proxmox.py | 27 ++++++++++++++++++++++++++- static/css/styles.css | 1 - templates/create.html | 9 +++++++++ templates/vm_details.html | 4 ++++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 15509ac..3c63f91 100644 --- a/app.py +++ b/app.py @@ -42,6 +42,7 @@ def vm_details(vmid): vm['vmid'] = vmid vm['config'] = get_vm_config(proxmox, vmid) vm['disks'] = get_vm_disks(proxmox, vmid, config=vm['config']) + vm['isos'] = get_vm_isos(proxmox, vmid, config=vm['config']) vm['interfaces'] = get_vm_interfaces( proxmox, vm['vmid'], config=vm['config']) return render_template('vm_details.html', username='com6056', vm=vm) @@ -76,24 +77,29 @@ def create(): limits = get_user_usage_limits(user) full_limits = check_user_limit(proxmox, user, usage, limits) percents = get_user_usage_percent(proxmox, usage, limits) + isos = get_isos(proxmox, app.config['PROXMOX_ISO_STORAGE']) return render_template( 'create.html', username='com6056', usage=usage, limits=limits, full_limits=full_limits, - percents=percents) + percents=percents, + isos=isos) elif request.method == 'POST': name = request.form['name'] cores = request.form['cores'] memory = request.form['memory'] disk = request.form['disk'] + iso = request.form['iso'] + if iso != 'none': + iso = "{}:iso/{}".format(app.config['PROXMOX_ISO_STORAGE'], iso) usage_check = check_user_usage(proxmox, user, cores, memory, disk) if usage_check: return usage_check else: vmid, mac = create_vm(proxmox, starrs, user, name, cores, memory, - disk) + disk, iso) register_starrs(starrs, name, user, mac, get_next_ip(starrs, app.config['STARRS_IP_RANGE'])[0][0]) diff --git a/proxmox.py b/proxmox.py index 7f72a23..dbfeb49 100644 --- a/proxmox.py +++ b/proxmox.py @@ -106,6 +106,23 @@ def get_vm_disks(proxmox, vmid, config=None): return disks +def get_vm_isos(proxmox, vmid, config=None): + if not config: + config = get_vm_config(proxmox, vmid) + drives = [] + for key, val in config.items(): + valid_drive_types = ['ide', 'sata', 'scsi'] + if any(drive_type in key for drive_type in valid_drive_types): + if 'cdrom' in val: + if val.split(',')[0] == 'none': + iso = 'None' + else: + iso = val.split(',')[0].split('/')[1] + drives.append([key, iso]) + drives = sorted(drives, key=lambda x: x[0]) + return drives + + def get_user_usage_limits(user): limits = dict() limits['cpu'] = 4 @@ -169,7 +186,7 @@ def get_user_usage_percent(proxmox, usage=None, limits=None): return percents -def create_vm(proxmox, starrs, user, name, cores, memory, disk): +def create_vm(proxmox, starrs, user, name, cores, memory, disk, iso): node = proxmox.nodes(get_node_least_mem(proxmox)) vmid = get_free_vmid(proxmox) node.qemu.create( @@ -179,6 +196,7 @@ def create_vm(proxmox, starrs, user, name, cores, memory, disk): memory=memory, storage='ceph', virtio0="ceph:{}".format(disk), + ide2="{},media=cdrom".format(iso), net0='virtio,bridge=vmbr0', pool=user) time.sleep(3) @@ -205,3 +223,10 @@ def change_vm_power(proxmox, vmid, action): node.qemu(vmid).status.suspend.post() elif action == 'resume': node.qemu(vmid).status.resume.post() + + +def get_isos(proxmox, storage): + isos = [] + for iso in proxmox.nodes('proxmox01').storage(storage).content.get(): + isos.append(iso['volid'].split('/')[1]) + return isos diff --git a/static/css/styles.css b/static/css/styles.css index e297beb..0e21765 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -55,7 +55,6 @@ table, th, td { font-weight: bold; line-height: 18px; color: #999999; - text-transform: uppercase; margin-left: -15px; margin-right: -15px; } diff --git a/templates/create.html b/templates/create.html index 720483d..ee59dd0 100644 --- a/templates/create.html +++ b/templates/create.html @@ -41,6 +41,15 @@ +
+ + +
{% endif %} diff --git a/templates/vm_details.html b/templates/vm_details.html index e05030c..5bad0e0 100644 --- a/templates/vm_details.html +++ b/templates/vm_details.html @@ -42,6 +42,10 @@ {% for disk in vm['disks'] %}
  • {{ disk[0] }}: {{ disk[1] }}
  • {% endfor %} + + {% for iso in vm['isos'] %} +
  • {{ iso[1] }}
  • + {% endfor %}