From 7b4e7fb6e8d04caf1771d1162d654027d057494e Mon Sep 17 00:00:00 2001 From: Jordan Rodgers Date: Wed, 31 Jan 2018 22:23:22 -0500 Subject: [PATCH] remove reset limits, disable debug, improve get next starrs ip, add start of template code --- proxstar/__init__.py | 29 ++++++++++++++--------------- proxstar/mail.py | 3 ++- proxstar/models.py | 7 +++++++ proxstar/proxmox.py | 22 ++++++++++++++++++++++ proxstar/starrs.py | 2 +- proxstar/tasks.py | 28 ++++++++++++++++++++++++---- proxstar/templates/list_vms.html | 1 - 7 files changed, 70 insertions(+), 22 deletions(-) diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 26491fe..9d2a13b 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -26,10 +26,19 @@ else: app.config.from_pyfile(config) app.config["GIT_REVISION"] = subprocess.check_output( ['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip() -auth = OIDCAuthentication( - app, - issuer=app.config['OIDC_ISSUER'], - client_registration_info=app.config['OIDC_CLIENT_CONFIG']) + +retry = 0 +while retry < 5: + try: + auth = OIDCAuthentication( + app, + issuer=app.config['OIDC_ISSUER'], + client_registration_info=app.config['OIDC_CLIENT_CONFIG']) + break + except: + retry += 1 + time.sleep(2) + cache = SimpleCache() redis_conn = Redis(app.config['REDIS_HOST'], app.config['REDIS_PORT']) @@ -334,16 +343,6 @@ def set_limits(user): return '', 403 -@app.route('/limits//reset', methods=['POST']) -@auth.oidc_auth -def reset_limits(user): - if 'rtp' in session['userinfo']['groups']: - delete_user_usage_limits(user) - return '', 200 - else: - return '', 403 - - @app.route('/user//delete', methods=['POST']) @auth.oidc_auth def delete_user(user): @@ -375,4 +374,4 @@ def logout(): if __name__ == "__main__": - app.run(debug=True) + app.run() diff --git a/proxstar/mail.py b/proxstar/mail.py index 3dd957b..344c604 100644 --- a/proxstar/mail.py +++ b/proxstar/mail.py @@ -24,7 +24,8 @@ def send_vm_expire_email(user, vms): body = "The following VMs in Proxstar are expiring soon:\n\n" for vm in vms: if vm[1] == 0: - body += " - {} today (VM has been stopped)\n".format(vm[0], vm[1]) + body += " - {} today (VM has been stopped)\n".format( + vm[0], vm[1]) if vm[1] == 1: body += " - {} in 1 day\n".format(vm[0]) else: diff --git a/proxstar/models.py b/proxstar/models.py index 711c89c..42238da 100644 --- a/proxstar/models.py +++ b/proxstar/models.py @@ -28,3 +28,10 @@ class Pool_Cache(Base): usage = Column(JSON, nullable=False) limits = Column(JSON, nullable=False) percents = Column(JSON, nullable=False) + + +class Template(Base): + __tablename__ = 'template' + id = Column(Integer, primary_key=True) + name = Column(String(32), nullable=False) + desc = Column(Text) diff --git a/proxstar/proxmox.py b/proxstar/proxmox.py index 72c6ae0..beadbc2 100644 --- a/proxstar/proxmox.py +++ b/proxstar/proxmox.py @@ -296,3 +296,25 @@ def delete_user_pool(proxmox, pool): if 'rtp' not in proxmox.access.users( "{}@csh.rit.edu".format(pool)).get()['groups']: proxmox.access.users("{}@csh.rit.edu".format(pool)).delete() + + +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 = get_vm_mac(proxmox, newid) + break + except: + retry += 1 + time.sleep(3) + return newid, mac diff --git a/proxstar/starrs.py b/proxstar/starrs.py index 5cbf7db..8b50691 100644 --- a/proxstar/starrs.py +++ b/proxstar/starrs.py @@ -12,7 +12,7 @@ def get_next_ip(starrs, range_name): c.execute("COMMIT") finally: c.close() - return results + return results[0][0] def get_ip_for_mac(starrs, mac): diff --git a/proxstar/tasks.py b/proxstar/tasks.py index 831fb8f..b72444d 100644 --- a/proxstar/tasks.py +++ b/proxstar/tasks.py @@ -42,8 +42,7 @@ def create_vm_task(user, name, cores, memory, disk, iso): starrs = connect_starrs() vmid, mac = create_vm(proxmox, user, name, cores, memory, disk, iso) register_starrs(starrs, name, app.config['STARRS_USER'], mac, - get_next_ip(starrs, - app.config['STARRS_IP_RANGE'])[0][0]) + get_next_ip(starrs, app.config['STARRS_IP_RANGE'])) get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) @@ -104,9 +103,30 @@ def generate_pool_cache_task(): store_pool_cache(db, pools) -def setup_template(hostname): +def setup_template(template_id, name, user, cores, memory): with app.app_context(): + proxmox = connect_proxmox() + starrs = connect_starrs() db = connect_db() + vmid, mac = clone_vm(proxmox, template_id, name, user) + ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE']) + register_starrs(starrs, name, app.config['STARRS_USER'], mac, ip) + get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) + change_vm_cpu(proxmox, vmid, cores) + change_vm_mem(proxmox, vmid, memory) + time.sleep(60) + change_vm_power(proxmox, vmid, 'start') client = paramiko.SSHClient() - client.connect('ssh.example.com', username='root', password='todo') + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + retry = 0 + while retry < 30: + try: + client.connect(ip, username='root', password='') + break + except: + retry += 1 + time.sleep(3) stdin, stdout, stderr = client.exec_command('ls') + for line in stdout: + print('... ' + line.strip('\n')) + client.close() diff --git a/proxstar/templates/list_vms.html b/proxstar/templates/list_vms.html index 550867b..de3a9dd 100644 --- a/proxstar/templates/list_vms.html +++ b/proxstar/templates/list_vms.html @@ -92,7 +92,6 @@ - {% if not pool['vms'] %} {% endif %}