add basic db setup, add vm expirations and cron to handle them, and add fields for sweetalert button classes

This commit is contained in:
Jordan Rodgers 2017-12-08 09:51:45 -05:00
parent 6debe3d9fd
commit 9beecf12fa
7 changed files with 186 additions and 8 deletions

26
app.py
View file

@ -2,9 +2,9 @@ import os
import time
import psycopg2
import subprocess
from db import *
from starrs import *
from proxmox import *
from proxmoxer import ProxmoxAPI
from flask import Flask, render_template, request, redirect, send_from_directory
app = Flask(__name__)
@ -17,9 +17,6 @@ app.config["GIT_REVISION"] = subprocess.check_output(
['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip()
user = 'proxstar'
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("/")
@ -57,6 +54,7 @@ def vm_details(vmid):
vm['iso'] = get_vm_iso(proxmox, vmid, config=vm['config'])
vm['interfaces'] = get_vm_interfaces(
proxmox, vm['vmid'], config=vm['config'])
vm['expire'] = get_vm_expire(vmid, app.config['VM_EXPIRE_MONTHS']).strftime('%m/%d/%Y')
return render_template('vm_details.html', username='com6056', vm=vm)
else:
return '', 403
@ -74,6 +72,18 @@ def vm_power(vmid, action):
return '', 403
@app.route("/vm/<string:vmid>/renew", methods=['POST'])
def vm_renew(vmid):
proxmox = connect_proxmox(app.config['PROXMOX_HOST'],
app.config['PROXMOX_USER'],
app.config['PROXMOX_PASS'])
if int(vmid) in get_user_allowed_vms(proxmox, user):
renew_vm_expire(vmid, app.config['VM_EXPIRE_MONTHS'])
return '', 200
else:
return '', 403
@app.route("/vm/<string:vmid>/eject", methods=['POST'])
def iso_eject(vmid):
proxmox = connect_proxmox(app.config['PROXMOX_HOST'],
@ -104,10 +114,14 @@ def delete(vmid):
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'])
if int(vmid) in get_user_allowed_vms(proxmox, user):
vmname = get_vm_config(proxmox, vmid)['name']
delete_vm(proxmox, starrs, vmid)
delete_starrs(starrs, vmname)
delete_vm_expire(vmid)
return '', 200
else:
return '', 403
@ -118,6 +132,9 @@ def create():
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'])
if request.method == 'GET':
usage = get_user_usage(proxmox, 'proxstar')
limits = get_user_usage_limits(user)
@ -149,6 +166,7 @@ def create():
register_starrs(starrs, name, user, mac,
get_next_ip(starrs,
app.config['STARRS_IP_RANGE'])[0][0])
get_vm_expire(vmid, app.config['VM_EXPIRE_MONTHS'])
return redirect("/proxstar/vm/{}".format(vmid))