proxstar/app.py

102 lines
3.2 KiB
Python
Raw Normal View History

2017-11-26 16:48:51 +00:00
import os
import time
import psycopg2
2017-11-26 17:55:14 +00:00
import subprocess
2017-11-26 16:48:51 +00:00
from starrs import *
from proxmox import *
from proxmoxer import ProxmoxAPI
from flask import Flask, render_template, request, redirect, send_from_directory
2017-11-26 16:48:51 +00:00
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()
2017-11-26 16:48:51 +00:00
2017-11-26 17:01:30 +00:00
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'])
2017-11-26 16:48:51 +00:00
@app.route("/")
def list_vms():
2017-11-26 16:48:51 +00:00
vms = get_vms_for_user(proxmox, user)
for vm in vms:
if 'name' not in vm:
vms.remove(vm)
vms = sorted(vms, key=lambda k: k['name'])
return render_template('list_vms.html', username='com6056', vms=vms)
@app.route("/vm/<string:vmid>")
def vm_details(vmid):
vm = get_vm(proxmox, vmid)
vm['vmid'] = vmid
vm['config'] = get_vm_config(proxmox, vmid)
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)
2017-11-26 16:48:51 +00:00
@app.route("/vm_status/<string:vmid>", methods=['POST'])
def vm_status(vmid):
action = request.form['action']
change_vm_status(proxmox, vmid, action)
return redirect("/proxstar/vm/{}".format(vmid))
2017-11-26 17:01:30 +00:00
@app.route("/create", methods=['GET', 'POST'])
def create():
if request.method == 'GET':
usage = get_user_usage(proxmox, 'proxstar')
limits = get_user_usage_limits(user)
return render_template('create.html', username='com6056', usage=usage, limits=limits)
elif request.method == 'POST':
name = request.form['name']
cores = request.form['cores']
memory = request.form['memory']
disk = request.form['disk']
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)
register_starrs(starrs, name, user, mac,
get_next_ip(starrs, '49net Public Fixed')[0][0])
return redirect("/proxstar/vm/{}".format(vmid))
2017-11-26 17:01:30 +00:00
@app.route("/delete", methods=['POST'])
def delete():
vmid = request.form['delete']
vmname = get_vm_config(proxmox, vmid)['name']
return render_template(
'confirm_delete.html', username='com6056', vmid=vmid, vmname=vmname)
2017-11-26 17:01:30 +00:00
@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)
delete_starrs(starrs, vmname)
time.sleep(3)
return redirect("/proxstar")
2017-11-26 16:48:51 +00:00
@app.route('/novnc/<path:path>')
def send_novnc(path):
return send_from_directory('static/novnc-pve/novnc', path)
2017-11-26 16:48:51 +00:00
if __name__ == "__main__":
app.run()