From a78529f2b153db36b8bf8b73904136a0b0cb002a Mon Sep 17 00:00:00 2001 From: Jordan Rodgers Date: Sun, 26 Nov 2017 12:01:30 -0500 Subject: [PATCH] can delete vms with confirmation --- app.py | 28 +++++++++++++++++++++------- proxmox.py | 6 ++---- templates/confirm_delete.html | 20 ++++++++++++++++++++ templates/create.html | 16 ++++++++++++++-- templates/get_vms.html | 12 +++++++++++- 5 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 templates/confirm_delete.html diff --git a/app.py b/app.py index 640ac4d..157ae00 100644 --- a/app.py +++ b/app.py @@ -16,13 +16,9 @@ config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py") app.config.from_pyfile(config) -user = 'ram' +user = 'proxstar' proxmox = connect_proxmox(app.config['PROXMOX_HOST'], app.config['PROXMOX_USER'], app.config['PROXMOX_PASS']) starrs = connect_starrs(app.config['STARRS_DB_NAME'], app.config['STARRS_DB_USER'], app.config['STARRS_DB_HOST'], app.config['STARRS_DB_PASS']) -#print(get_vms_for_user(user)) -#vmid = create_vm(proxmox, starrs, user, name) -#time.sleep(10) -#delete_vm(proxmox, starrs, vmid, name) @app.route("/") @@ -31,6 +27,7 @@ def get_vms(): for vm in vms: vm['config'] = get_vm_config(proxmox, vm['vmid']) vm['disk_size'] = get_vm_disk_size(proxmox, vm['vmid'], config=vm['config']) + print(vm) return render_template('get_vms.html', vms=vms) @@ -46,9 +43,26 @@ def get_create(): memory = request.form['memory'] disk = request.form['disk'] print(name, cores, memory, disk) - vmid = create_vm(proxmox, starrs, user, name, cores, memory, disk) + vmid, mac = create_vm(proxmox, starrs, user, name, cores, memory, disk) + print(register_starrs(starrs, name, user, mac, get_next_ip(starrs, '49net Public Fixed')[0][0])) print(vmid) - return vmid + return vmid + + +@app.route("/delete", methods=['POST']) +def delete(): + vmid = request.form['delete'] + vmname = get_vm_config(proxmox, vmid)['name'] + return render_template('confirm_delete.html', vmid=vmid, vmname=vmname) + + +@app.route("/confirm_delete", methods=['POST']) +def confirm_delete(): + vmid = request.form['delete'] + vmname = get_vm_config(proxmox, vmid)['name'] + delete_vm(proxmox, starrs, vmid) + print(delete_starrs(starrs, vmname)) + return 'SUCCESS' if __name__ == "__main__": diff --git a/proxmox.py b/proxmox.py index 43e21b0..d683459 100644 --- a/proxmox.py +++ b/proxmox.py @@ -64,12 +64,10 @@ def create_vm(proxmox, starrs, user, name, cores, memory, disk): node.qemu.create(vmid=vmid, name=name, cores=cores, memory=memory, storage='ceph', virtio0='ceph:10', net0='virtio,bridge=vmbr0', pool=user) time.sleep(3) mac = get_vm_mac(proxmox, vmid) - register_starrs(starrs, name, user, mac, get_next_ip(starrs, '49net Public Fixed')[0][0]) - return vmid + return vmid, mac -def delete_vm(proxmox, starrs, vmid, name): +def delete_vm(proxmox, starrs, vmid): print(vmid) print(get_vm_node(proxmox, vmid)) node = proxmox.nodes(get_vm_node(proxmox, vmid)) node.qemu(vmid).delete() - delete_starrs(starrs, name) diff --git a/templates/confirm_delete.html b/templates/confirm_delete.html new file mode 100644 index 0000000..bb74cbd --- /dev/null +++ b/templates/confirm_delete.html @@ -0,0 +1,20 @@ + + + + + + + +Create VM + +

+ +Are you sure you want to delete "{{ vmname }}"? + +
+ +
+ + + diff --git a/templates/create.html b/templates/create.html index 6ebfba5..bd8bda3 100644 --- a/templates/create.html +++ b/templates/create.html @@ -11,15 +11,27 @@ table, th, td { VM List +

+ Create VM +

+
- + - + diff --git a/templates/get_vms.html b/templates/get_vms.html index c4396c1..21002e0 100644 --- a/templates/get_vms.html +++ b/templates/get_vms.html @@ -11,6 +11,8 @@ table, th, td { Create VM +

+ @@ -18,18 +20,26 @@ table, th, td { + {% for vm in vms %} - + + {% endfor %}
Name Cores Memory Disk Size Delete
{{ vm['name'] }} {{ vm['status'] }} {{ vm['config']['cores'] * vm['config']['sockets'] }} {{ vm['config']['cores'] * vm['config'].get(sockets, 1) }} {{ vm['config']['memory'] }} MB {{ vm['disk_size'] }} + + + +
+
+ {{ vms }}