remove reset limits, disable debug, improve get next starrs ip, add start of template code

This commit is contained in:
Jordan Rodgers 2018-01-31 22:23:22 -05:00
parent 8fbe4de70f
commit 7b4e7fb6e8
7 changed files with 70 additions and 22 deletions

View file

@ -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/<string:user>/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/<string: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()

View file

@ -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:

View file

@ -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)

View file

@ -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

View file

@ -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):

View file

@ -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()

View file

@ -92,7 +92,6 @@
</div>
</div>
<button class="btn btn-info proxstar-poolbtn edit-limit" data-user="{{ pool['user'] }}" data-cpu="{{ pool['limits']['cpu'] }}" data-mem="{{ pool['limits']['mem'] }}" data-disk="{{ pool['limits']['disk'] }}">EDIT</button>
<button class="btn btn-warning proxstar-poolbtn reset-limit" data-user="{{ pool['user'] }}">RESET</button>
{% if not pool['vms'] %}
<button class="btn btn-danger proxstar-poolbtn delete-user" data-user="{{ pool['user'] }}">DELETE</button>
{% endif %}