can delete vms with confirmation

This commit is contained in:
Jordan Rodgers 2017-11-26 12:01:30 -05:00
parent 568f242010
commit a78529f2b1
5 changed files with 68 additions and 14 deletions

28
app.py
View file

@ -16,13 +16,9 @@ config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py")
app.config.from_pyfile(config) app.config.from_pyfile(config)
user = 'ram' user = 'proxstar'
proxmox = connect_proxmox(app.config['PROXMOX_HOST'], app.config['PROXMOX_USER'], app.config['PROXMOX_PASS']) 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']) 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("/") @app.route("/")
@ -31,6 +27,7 @@ def get_vms():
for vm in vms: for vm in vms:
vm['config'] = get_vm_config(proxmox, vm['vmid']) vm['config'] = get_vm_config(proxmox, vm['vmid'])
vm['disk_size'] = get_vm_disk_size(proxmox, vm['vmid'], config=vm['config']) vm['disk_size'] = get_vm_disk_size(proxmox, vm['vmid'], config=vm['config'])
print(vm)
return render_template('get_vms.html', vms=vms) return render_template('get_vms.html', vms=vms)
@ -46,9 +43,26 @@ def get_create():
memory = request.form['memory'] memory = request.form['memory']
disk = request.form['disk'] disk = request.form['disk']
print(name, cores, memory, 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) 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__": if __name__ == "__main__":

View file

@ -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) 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) time.sleep(3)
mac = get_vm_mac(proxmox, vmid) mac = get_vm_mac(proxmox, vmid)
register_starrs(starrs, name, user, mac, get_next_ip(starrs, '49net Public Fixed')[0][0]) return vmid, mac
return vmid
def delete_vm(proxmox, starrs, vmid, name): def delete_vm(proxmox, starrs, vmid):
print(vmid) print(vmid)
print(get_vm_node(proxmox, vmid)) print(get_vm_node(proxmox, vmid))
node = proxmox.nodes(get_vm_node(proxmox, vmid)) node = proxmox.nodes(get_vm_node(proxmox, vmid))
node.qemu(vmid).delete() node.qemu(vmid).delete()
delete_starrs(starrs, name)

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<a href="/proxstar/create">Create VM</a>
<br><br>
Are you sure you want to delete "{{ vmname }}"?
<form action="/proxstar/confirm_delete" method="post">
<button type="submit" name="delete" value="{{ vmid }}">CONFIRM</button>
</form>
</body>
</html>

View file

@ -11,15 +11,27 @@ table, th, td {
<a href="/proxstar/">VM List</a> <a href="/proxstar/">VM List</a>
<br><br>
Create VM Create VM
<br><br>
<form action="get_create" method="post"> <form action="get_create" method="post">
<label for="name">VM Name</label> <label for="name">VM Name</label>
<input type="text" name="name"> <input type="text" name="name">
<label for="cores">Cores</label> <label for="cores">Cores</label>
<input type="text" name="cores"> <select name="cores">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
</select>
<label for="memory">Memory</label> <label for="memory">Memory</label>
<input type="text" name="memory"> <select name="memory">
<option value="1024">1GB</option>
<option value="2048">2GB</option>
<option value="4096">4GB</option>
</select>
<label for="disk">Disk</label> <label for="disk">Disk</label>
<input type="text" name="disk"> <input type="text" name="disk">
<input type="submit"> <input type="submit">

View file

@ -11,6 +11,8 @@ table, th, td {
<a href="/proxstar/create">Create VM</a> <a href="/proxstar/create">Create VM</a>
<br><br>
<table> <table>
<tr> <tr>
<th> Name </th> <th> Name </th>
@ -18,18 +20,26 @@ table, th, td {
<th> Cores </th> <th> Cores </th>
<th> Memory </th> <th> Memory </th>
<th> Disk Size </th> <th> Disk Size </th>
<th> Delete </th>
</tr> </tr>
{% for vm in vms %} {% for vm in vms %}
<tr> <tr>
<td> {{ vm['name'] }} </td> <td> {{ vm['name'] }} </td>
<td> {{ vm['status'] }} </td> <td> {{ vm['status'] }} </td>
<td> {{ vm['config']['cores'] * vm['config']['sockets'] }} </td> <td> {{ vm['config']['cores'] * vm['config'].get(sockets, 1) }} </td>
<td> {{ vm['config']['memory'] }} MB </td> <td> {{ vm['config']['memory'] }} MB </td>
<td> {{ vm['disk_size'] }} </td> <td> {{ vm['disk_size'] }} </td>
<td>
<form action="/proxstar/delete" method="post">
<button type="submit" name="delete" value="{{ vm['vmid'] }}">DELETE</button>
</form>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
<br>
{{ vms }} {{ vms }}
</body> </body>