mirror of
https://github.com/ComputerScienceHouse/proxstar.git
synced 2025-03-09 15:40:09 +00:00
fix import hell... for now...
This commit is contained in:
parent
06920e1216
commit
0d5b76c28e
6 changed files with 80 additions and 78 deletions
|
@ -14,8 +14,7 @@ from flask import Flask, render_template, request, redirect, session
|
|||
from proxstar.db import *
|
||||
from proxstar.vm import VM
|
||||
from proxstar.vnc import *
|
||||
from proxstar.util import *
|
||||
from proxstar.tasks import *
|
||||
from proxstar.util import gen_password
|
||||
from proxstar.starrs import *
|
||||
from proxstar.ldapdb import *
|
||||
from proxstar.proxmox import *
|
||||
|
@ -63,6 +62,9 @@ starrs = psycopg2.connect(
|
|||
app.config['STARRS_DB_NAME'], app.config['STARRS_DB_USER'],
|
||||
app.config['STARRS_DB_HOST'], app.config['STARRS_DB_PASS']))
|
||||
|
||||
from proxstar.user import User
|
||||
from proxstar.tasks import generate_pool_cache_task, process_expiring_vms_task, cleanup_vnc_task, delete_vm_task, create_vm_task, setup_template_task
|
||||
|
||||
if 'generate_pool_cache' not in scheduler:
|
||||
scheduler.schedule(
|
||||
id='generate_pool_cache',
|
||||
|
@ -81,8 +83,6 @@ if 'cleanup_vnc' not in scheduler:
|
|||
func=cleanup_vnc_task,
|
||||
interval=3600)
|
||||
|
||||
from proxstar.user import User
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@app.route("/user/<string:user_view>")
|
||||
|
@ -393,7 +393,7 @@ def create():
|
|||
else:
|
||||
password = gen_password(16)
|
||||
q.enqueue(
|
||||
setup_template,
|
||||
setup_template_task,
|
||||
template,
|
||||
name,
|
||||
username,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import time
|
||||
from proxmoxer import ProxmoxAPI
|
||||
from proxstar.db import *
|
||||
from proxstar.ldapdb import *
|
||||
from proxstar.ldapdb import is_user
|
||||
from proxstar.db import get_user_usage_limits, get_ignored_pools
|
||||
from flask import current_app as app
|
||||
|
||||
|
||||
def connect_proxmox():
|
||||
|
@ -21,21 +21,6 @@ def connect_proxmox():
|
|||
raise
|
||||
|
||||
|
||||
def get_vms_for_rtp(proxmox, db):
|
||||
pools = []
|
||||
for pool in get_pools(proxmox, db):
|
||||
pool_dict = dict()
|
||||
pool_dict['user'] = pool
|
||||
pool_dict['vms'] = get_vms_for_user(proxmox, db, pool)
|
||||
pool_dict['num_vms'] = len(pool_dict['vms'])
|
||||
pool_dict['usage'] = get_user_usage(proxmox, db, pool)
|
||||
pool_dict['limits'] = get_user_usage_limits(db, pool)
|
||||
pool_dict['percents'] = get_user_usage_percent(
|
||||
proxmox, pool, pool_dict['usage'], pool_dict['limits'])
|
||||
pools.append(pool_dict)
|
||||
return pools
|
||||
|
||||
|
||||
def get_node_least_mem(proxmox):
|
||||
nodes = proxmox.nodes.get()
|
||||
sorted_nodes = sorted(nodes, key=lambda x: x['mem'])
|
||||
|
@ -52,31 +37,6 @@ def get_vm_node(proxmox, vmid):
|
|||
return vm['node']
|
||||
|
||||
|
||||
def create_vm(proxmox, user, name, cores, memory, disk, iso):
|
||||
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:{}".format(disk),
|
||||
ide2="{},media=cdrom".format(iso),
|
||||
net0='virtio,bridge=vmbr0',
|
||||
pool=user,
|
||||
description='Managed by Proxstar')
|
||||
retry = 0
|
||||
while retry < 5:
|
||||
try:
|
||||
mac = VM(vmid).get_mac()
|
||||
break
|
||||
except:
|
||||
retry += 1
|
||||
time.sleep(3)
|
||||
return vmid, mac
|
||||
|
||||
|
||||
def get_isos(proxmox, storage):
|
||||
isos = []
|
||||
for iso in proxmox.nodes('proxmox01').storage(storage).content.get():
|
||||
|
@ -93,25 +53,3 @@ def get_pools(proxmox, db):
|
|||
pools.append(poolid)
|
||||
pools = sorted(pools)
|
||||
return pools
|
||||
|
||||
|
||||
def clone_vm(proxmox, template_id, name, pool):
|
||||
node = proxmox.nodes(get_vm_node(proxmox, template_id))
|
||||
newid = get_free_vmid(proxmox)
|
||||
target = get_node_least_mem(proxmox)
|
||||
node.qemu(template_id).clone.post(
|
||||
newid=newid,
|
||||
name=name,
|
||||
pool=pool,
|
||||
full=1,
|
||||
description='Managed by Proxstar',
|
||||
target=target)
|
||||
retry = 0
|
||||
while retry < 60:
|
||||
try:
|
||||
mac = VM(newid).get_mac()
|
||||
break
|
||||
except:
|
||||
retry += 1
|
||||
time.sleep(3)
|
||||
return newid, mac
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import time
|
||||
import requests
|
||||
import paramiko
|
||||
import psycopg2
|
||||
|
@ -6,11 +7,12 @@ from flask import Flask
|
|||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from proxstar.db import *
|
||||
from proxstar.vm import VM
|
||||
from proxstar.util import *
|
||||
from proxstar.mail import *
|
||||
from proxstar.starrs import *
|
||||
from proxstar.proxmox import *
|
||||
from proxstar.vm import VM, create_vm, clone_vm
|
||||
from proxstar.user import User, get_vms_for_rtp
|
||||
from proxstar.proxmox import connect_proxmox, get_pools
|
||||
|
||||
app = Flask(__name__)
|
||||
if os.path.exists(
|
||||
|
@ -82,8 +84,9 @@ def process_expiring_vms_task():
|
|||
starrs = connect_starrs()
|
||||
pools = get_pools(proxmox, db)
|
||||
for pool in pools:
|
||||
user = User(pool)
|
||||
expiring_vms = []
|
||||
vms = get_vms_for_user(proxmox, db, pool)
|
||||
vms = user.vms
|
||||
for vm in vms:
|
||||
vmid = vm['vmid']
|
||||
expire = get_vm_expire(db, vmid,
|
||||
|
@ -106,7 +109,7 @@ def generate_pool_cache_task():
|
|||
store_pool_cache(db, pools)
|
||||
|
||||
|
||||
def setup_template(template_id, name, user, password, cores, memory):
|
||||
def setup_template_task(template_id, name, user, password, cores, memory):
|
||||
with app.app_context():
|
||||
proxmox = connect_proxmox()
|
||||
starrs = connect_starrs()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from proxstar import db
|
||||
from proxstar.db import *
|
||||
from proxstar.vm import VM
|
||||
from proxstar.util import *
|
||||
from proxstar.proxmox import *
|
||||
|
||||
|
@ -85,3 +86,18 @@ class User(object):
|
|||
self.name)).get()['groups']:
|
||||
proxmox.access.users("{}@csh.rit.edu".format(
|
||||
self.name)).delete()
|
||||
|
||||
|
||||
def get_vms_for_rtp(proxmox, db):
|
||||
pools = []
|
||||
for pool in get_pools(proxmox, db):
|
||||
user = User(pool)
|
||||
pool_dict = dict()
|
||||
pool_dict['user'] = user.name
|
||||
pool_dict['vms'] = user.vms
|
||||
pool_dict['num_vms'] = len(pool_dict['vms'])
|
||||
pool_dict['usage'] = user.usage
|
||||
pool_dict['limits'] = user.limits
|
||||
pool_dict['percents'] = user.usage_percent
|
||||
pools.append(pool_dict)
|
||||
return pools
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import string
|
||||
import random
|
||||
from proxstar.db import *
|
||||
|
||||
|
||||
def gen_password(
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import time
|
||||
from proxstar.util import *
|
||||
from proxstar.proxmox import *
|
||||
from proxstar.proxmox import connect_proxmox, get_node_least_mem, get_free_vmid, get_vm_node
|
||||
|
||||
|
||||
class VM(object):
|
||||
|
@ -40,12 +41,10 @@ class VM(object):
|
|||
def set_cpu(self, cores):
|
||||
proxmox = connect_proxmox()
|
||||
proxmox.nodes(self.node).qemu(self.id).config.put(cores=cores)
|
||||
self.cpu = cores
|
||||
|
||||
def set_mem(self, mem):
|
||||
proxmox = connect_proxmox()
|
||||
proxmox.nodes(self.node).qemu(self.id).config.put(memory=mem)
|
||||
self.mem = mem
|
||||
|
||||
def start(self):
|
||||
proxmox = connect_proxmox()
|
||||
|
@ -154,3 +153,50 @@ class VM(object):
|
|||
proxmox = connect_proxmox()
|
||||
proxmox.nodes(self.node).qemu(self.id).resize.put(
|
||||
disk=disk, size="+{}G".format(size))
|
||||
|
||||
|
||||
def create_vm(proxmox, user, name, cores, memory, disk, iso):
|
||||
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:{}".format(disk),
|
||||
ide2="{},media=cdrom".format(iso),
|
||||
net0='virtio,bridge=vmbr0',
|
||||
pool=user,
|
||||
description='Managed by Proxstar')
|
||||
retry = 0
|
||||
while retry < 5:
|
||||
try:
|
||||
mac = VM(vmid).get_mac()
|
||||
break
|
||||
except:
|
||||
retry += 1
|
||||
time.sleep(3)
|
||||
return vmid, mac
|
||||
|
||||
|
||||
def clone_vm(proxmox, template_id, name, pool):
|
||||
node = proxmox.nodes(get_vm_node(proxmox, template_id))
|
||||
newid = get_free_vmid(proxmox)
|
||||
target = get_node_least_mem(proxmox)
|
||||
node.qemu(template_id).clone.post(
|
||||
newid=newid,
|
||||
name=name,
|
||||
pool=pool,
|
||||
full=1,
|
||||
description='Managed by Proxstar',
|
||||
target=target)
|
||||
retry = 0
|
||||
while retry < 60:
|
||||
try:
|
||||
mac = VM(newid).get_mac()
|
||||
break
|
||||
except:
|
||||
retry += 1
|
||||
time.sleep(3)
|
||||
return newid, mac
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue