yapf everything, retrieve all disks and interfaces and display them, and make delete button pretty

This commit is contained in:
Jordan Rodgers 2017-11-30 19:41:55 -05:00
parent ee8e439260
commit 598d4688db
4 changed files with 86 additions and 26 deletions

39
app.py
View file

@ -5,26 +5,24 @@ import subprocess
from starrs import *
from proxmox import *
from proxmoxer import ProxmoxAPI
from flask import Flask, render_template, request, redirect
from flask import Flask, render_template, request, redirect, send_from_directory
app = Flask(__name__)
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py")
app.config.from_pyfile(config)
app.config["GIT_REVISION"] = subprocess.check_output(['git',
'rev-parse',
'--short',
'HEAD']).decode('utf-8').rstrip()
app.config["GIT_REVISION"] = subprocess.check_output(
['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip()
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'])
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'])
@app.route("/")
@ -35,7 +33,8 @@ def list_vms():
vms.remove(vm)
else:
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'])
vms = sorted(vms, key=lambda k: k['name'])
return render_template('list_vms.html', username='com6056', vms=vms)
@ -45,7 +44,9 @@ def vm_details(vmid):
vm = get_vm(proxmox, vmid)
vm['vmid'] = vmid
vm['config'] = get_vm_config(proxmox, vmid)
vm['disk_size'] = get_vm_disk_size(proxmox, vmid, config=vm['config'])
vm['disks'] = get_vm_disks(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)
@ -61,7 +62,9 @@ def get_create():
memory = request.form['memory']
disk = request.form['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(
register_starrs(starrs, name, user, mac,
get_next_ip(starrs, '49net Public Fixed')[0][0]))
return redirect("/proxstar/vm/{}".format(vmid))
@ -70,7 +73,8 @@ def delete():
vmid = request.form['delete']
print(vmid)
vmname = get_vm_config(proxmox, vmid)['name']
return render_template('confirm_delete.html', username='com6056', vmid=vmid, vmname=vmname)
return render_template(
'confirm_delete.html', username='com6056', vmid=vmid, vmname=vmname)
@app.route("/confirm_delete", methods=['POST'])
@ -83,5 +87,10 @@ def confirm_delete():
return redirect("/proxstar")
@app.route('/novnc/<path:path>')
def send_novnc(path):
return send_from_directory('static/novnc-pve/novnc', path)
if __name__ == "__main__":
app.run()

View file

@ -4,7 +4,8 @@ from proxmoxer import ProxmoxAPI
def connect_proxmox(host, user, password):
try:
proxmox = ProxmoxAPI(host, user=user, password=password, verify_ssl=False)
proxmox = ProxmoxAPI(
host, user=user, password=password, verify_ssl=False)
except:
print("Unable to connect to Proxmox!")
raise
@ -52,6 +53,22 @@ def get_vm_mac(proxmox, vmid, config=None, interface='net0'):
return mac
def get_vm_interfaces(proxmox, vmid, config=None):
if not config:
config = get_vm_config(proxmox, vmid)
interfaces = []
for key, val in config.items():
if 'net' in key:
mac = config[key].split(',')
valid_int_types = ['virtio', 'e1000', 'rtl8139', 'vmxnet3']
if any(int_type in mac[0] for int_type in valid_int_types):
mac = mac[0].split('=')[1]
else:
mac = mac[1].split('=')[1]
interfaces.append([key, mac])
return interfaces
def get_vm_disk_size(proxmox, vmid, config=None, name='virtio0'):
if not config:
config = get_vm_config(proxmox, vmid)
@ -63,14 +80,39 @@ def get_vm_disk_size(proxmox, vmid, config=None, name='virtio0'):
return disk_size
def get_vm_disks(proxmox, vmid, config=None):
if not config:
config = get_vm_config(proxmox, vmid)
disks = []
for key, val in config.items():
valid_disk_types = ['virtio', 'ide', 'sata', 'scsi']
if any(disk_type in key for disk_type in valid_disk_types):
disk_size = val.split(',')
if 'size' in disk_size[0]:
disk_size = disk_size[0].split('=')[1]
else:
disk_size = disk_size[1].split('=')[1]
disks.append([key, disk_size])
return disks
def create_vm(proxmox, starrs, user, name, cores, memory, disk):
node = proxmox.nodes(get_node_least_mem(proxmox))
vmid = get_free_vmid(proxmox)
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)
mac = get_vm_mac(proxmox, vmid)
return vmid, mac
def delete_vm(proxmox, starrs, vmid):
print(vmid)
print(get_vm_node(proxmox, vmid))

View file

@ -3,7 +3,9 @@ import psycopg2
def connect_starrs(db, user, host, password):
try:
starrs = psycopg2.connect("dbname='{}' user='{}' host='{}' password='{}'".format(db, user, host, password))
starrs = psycopg2.connect(
"dbname='{}' user='{}' host='{}' password='{}'".format(
db, user, host, password))
except:
print("Unable to connect to STARRS database.")
raise
@ -14,8 +16,8 @@ def get_next_ip(starrs, range_name):
c = starrs.cursor()
try:
c.execute("BEGIN")
c.callproc("api.initialize", ('root',))
c.callproc("api.get_address_from_range", (range_name,))
c.callproc("api.initialize", ('root', ))
c.callproc("api.get_address_from_range", (range_name, ))
results = c.fetchall()
c.execute("COMMIT")
finally:
@ -27,8 +29,10 @@ def register_starrs(starrs, name, owner, mac, addr):
c = starrs.cursor()
try:
c.execute("BEGIN")
c.callproc("api.initialize", ('root',))
c.callproc("api.create_system_quick", (name, owner, 'members', mac, addr, 'csh.rit.edu', 'dhcp', True))
c.callproc("api.initialize", ('root', ))
c.callproc(
"api.create_system_quick",
(name, owner, 'members', mac, addr, 'csh.rit.edu', 'dhcp', True))
results = c.fetchall()
c.execute("COMMIT")
finally:
@ -40,8 +44,8 @@ def delete_starrs(starrs, name):
c = starrs.cursor()
try:
c.execute("BEGIN")
c.callproc("api.initialize", ('root',))
c.callproc("api.remove_system", (name,))
c.callproc("api.initialize", ('root', ))
c.callproc("api.remove_system", (name, ))
results = c.fetchall()
c.execute("COMMIT")
finally:

View file

@ -10,7 +10,7 @@
</div>
<div class="panel-body">
<form action="/proxstar/delete" method="post">
<button type="submit" name="delete" value="{{ vm['vmid'] }}">DELETE</button>
<button class="btn btn-danger" type="submit" name="delete" value="{{ vm['vmid'] }}">DELETE</button>
</form>
</div>
</div>
@ -23,8 +23,13 @@
<div class="panel-body">
<ul class="nav nav-list">
<li class="nav-header">Interfaces</li>
{% for interface in vm['interfaces'] %}
<li>{{ interface[0] }}: {{ interface[1] }}</li>
{% endfor %}
<li class="nav-header">Disks</li>
<li>Disk Size: {{ vm['disk_size'] }}</li>
{% for disk in vm['disks'] %}
<li>{{ disk[0] }}: {{ disk[1] }}</li>
{% endfor %}
</ul>
</div>
</div>