proxstar/app.py

76 lines
2.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
app = Flask(__name__)
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py")
app.config.from_pyfile(config)
2017-11-26 17:55:14 +00:00
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'
2017-11-26 16:48:51 +00:00
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("/")
def get_vms():
vms = get_vms_for_user(proxmox, user)
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'])
2017-11-26 17:01:30 +00:00
print(vm)
2017-11-26 17:55:14 +00:00
return render_template('get_vms.html', username='com6056', vms=vms)
2017-11-26 16:48:51 +00:00
@app.route("/create")
def create():
2017-11-26 17:55:14 +00:00
return render_template('create.html', username='com6056')
2017-11-26 16:48:51 +00:00
@app.route("/get_create", methods=['POST'])
def get_create():
name = request.form['name']
cores = request.form['cores']
memory = request.form['memory']
disk = request.form['disk']
print(name, cores, memory, disk)
2017-11-26 17:01:30 +00:00
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]))
2017-11-26 16:48:51 +00:00
print(vmid)
2017-11-26 17:01:30 +00:00
return vmid
@app.route("/delete", methods=['POST'])
def delete():
vmid = request.form['delete']
vmname = get_vm_config(proxmox, vmid)['name']
2017-11-26 17:55:14 +00:00
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)
print(delete_starrs(starrs, vmname))
return 'SUCCESS'
2017-11-26 16:48:51 +00:00
if __name__ == "__main__":
app.run()