Merge pull request #31 from MasterChief-John-117/pylint

Add pylint_quotes and fix all issues
This commit is contained in:
Devin Matte 2019-11-01 18:17:25 -04:00 committed by GitHub
commit 1ee7198363
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 117 additions and 117 deletions

View file

@ -5,4 +5,4 @@ python:
install: install:
- "pip install -r requirements.txt" - "pip install -r requirements.txt"
script: script:
- "pylint proxstar" - "pylint --load-plugins pylint_quotes proxstar"

View file

@ -31,13 +31,13 @@ app = Flask(__name__)
app.config.from_object(rq_dashboard.default_settings) app.config.from_object(rq_dashboard.default_settings)
if os.path.exists( if os.path.exists(
os.path.join( os.path.join(
app.config.get('ROOT_DIR', os.getcwd()), "config.local.py")): app.config.get('ROOT_DIR', os.getcwd()), 'config.local.py')):
config = os.path.join( config = os.path.join(
app.config.get('ROOT_DIR', os.getcwd()), "config.local.py") app.config.get('ROOT_DIR', os.getcwd()), 'config.local.py')
else: else:
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py") config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), 'config.py')
app.config.from_pyfile(config) app.config.from_pyfile(config)
app.config["GIT_REVISION"] = subprocess.check_output( app.config['GIT_REVISION'] = subprocess.check_output(
['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip() ['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip()
with open('proxmox_ssh_key', 'w') as ssh_key_file: with open('proxmox_ssh_key', 'w') as ssh_key_file:
@ -98,7 +98,7 @@ def add_rq_dashboard_auth(blueprint):
rq_dashboard_blueprint = rq_dashboard.blueprint rq_dashboard_blueprint = rq_dashboard.blueprint
add_rq_dashboard_auth(rq_dashboard_blueprint) add_rq_dashboard_auth(rq_dashboard_blueprint)
app.register_blueprint(rq_dashboard_blueprint, url_prefix="/rq") app.register_blueprint(rq_dashboard_blueprint, url_prefix='/rq')
@app.errorhandler(404) @app.errorhandler(404)
@ -113,8 +113,8 @@ def forbidden(e):
return render_template('403.html', user=user, e=e), 403 return render_template('403.html', user=user, e=e), 403
@app.route("/") @app.route('/')
@app.route("/user/<string:user_view>") @app.route('/user/<string:user_view>')
@auth.oidc_auth @auth.oidc_auth
def list_vms(user_view=None): def list_vms(user_view=None):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -155,15 +155,15 @@ def list_vms(user_view=None):
'list_vms.html', user=user, rtp_view=rtp_view, vms=vms) 'list_vms.html', user=user, rtp_view=rtp_view, vms=vms)
@app.route("/isos") @app.route('/isos')
@auth.oidc_auth @auth.oidc_auth
def isos(): def isos():
proxmox = connect_proxmox() proxmox = connect_proxmox()
stored_isos = get_isos(proxmox, app.config['PROXMOX_ISO_STORAGE']) stored_isos = get_isos(proxmox, app.config['PROXMOX_ISO_STORAGE'])
return json.dumps({"isos": stored_isos}) return json.dumps({'isos': stored_isos})
@app.route("/hostname/<string:name>") @app.route('/hostname/<string:name>')
@auth.oidc_auth @auth.oidc_auth
def hostname(name): def hostname(name):
valid, available = check_hostname(starrs, name) valid, available = check_hostname(starrs, name)
@ -175,7 +175,7 @@ def hostname(name):
return 'ok' return 'ok'
@app.route("/vm/<string:vmid>") @app.route('/vm/<string:vmid>')
@auth.oidc_auth @auth.oidc_auth
def vm_details(vmid): def vm_details(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -194,7 +194,7 @@ def vm_details(vmid):
return abort(403) return abort(403)
@app.route("/vm/<string:vmid>/power/<string:action>", methods=['POST']) @app.route('/vm/<string:vmid>/power/<string:action>', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_power(vmid, action): def vm_power(vmid, action):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -226,7 +226,7 @@ def vm_power(vmid, action):
return '', 403 return '', 403
@app.route("/console/vm/<string:vmid>/stop", methods=['POST']) @app.route('/console/vm/<string:vmid>/stop', methods=['POST'])
def vm_console_stop(vmid): def vm_console_stop(vmid):
if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']: if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']:
stop_ssh_tunnel(vmid, ssh_tunnels) stop_ssh_tunnel(vmid, ssh_tunnels)
@ -235,7 +235,7 @@ def vm_console_stop(vmid):
return '', 403 return '', 403
@app.route("/console/vm/<string:vmid>", methods=['POST']) @app.route('/console/vm/<string:vmid>', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_console(vmid): def vm_console(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -245,8 +245,8 @@ def vm_console(vmid):
stop_ssh_tunnel(vm.id, ssh_tunnels) stop_ssh_tunnel(vm.id, ssh_tunnels)
port = str(5900 + int(vmid)) port = str(5900 + int(vmid))
token = add_vnc_target(port) token = add_vnc_target(port)
node = "{}.csh.rit.edu".format(vm.node) node = '{}.csh.rit.edu'.format(vm.node)
logging.info("creating SSH tunnel to %s for VM %s", node, vm.id) logging.info('creating SSH tunnel to %s for VM %s', node, vm.id)
tunnel = start_ssh_tunnel(node, port) tunnel = start_ssh_tunnel(node, port)
ssh_tunnels.append(tunnel) ssh_tunnels.append(tunnel)
vm.start_vnc(port) vm.start_vnc(port)
@ -255,7 +255,7 @@ def vm_console(vmid):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/cpu/<int:cores>", methods=['POST']) @app.route('/vm/<string:vmid>/cpu/<int:cores>', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_cpu(vmid, cores): def vm_cpu(vmid, cores):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -276,7 +276,7 @@ def vm_cpu(vmid, cores):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/mem/<int:mem>", methods=['POST']) @app.route('/vm/<string:vmid>/mem/<int:mem>', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_mem(vmid, mem): def vm_mem(vmid, mem):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -297,7 +297,7 @@ def vm_mem(vmid, mem):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/disk/<string:disk>/<int:size>", methods=['POST']) @app.route('/vm/<string:vmid>/disk/<string:disk>/<int:size>', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_disk(vmid, disk, size): def vm_disk(vmid, disk, size):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -313,7 +313,7 @@ def vm_disk(vmid, disk, size):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/renew", methods=['POST']) @app.route('/vm/<string:vmid>/renew', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_renew(vmid): def vm_renew(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -329,7 +329,7 @@ def vm_renew(vmid):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/eject", methods=['POST']) @app.route('/vm/<string:vmid>/eject', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def iso_eject(vmid): def iso_eject(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -342,13 +342,13 @@ def iso_eject(vmid):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/mount/<string:iso>", methods=['POST']) @app.route('/vm/<string:vmid>/mount/<string:iso>', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def iso_mount(vmid, iso): def iso_mount(vmid, iso):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
connect_proxmox() connect_proxmox()
if user.rtp or int(vmid) in user.allowed_vms: if user.rtp or int(vmid) in user.allowed_vms:
iso = "{}:iso/{}".format(app.config['PROXMOX_ISO_STORAGE'], iso) iso = '{}:iso/{}'.format(app.config['PROXMOX_ISO_STORAGE'], iso)
vm = VM(vmid) vm = VM(vmid)
vm.mount_iso(iso) vm.mount_iso(iso)
return '', 200 return '', 200
@ -356,7 +356,7 @@ def iso_mount(vmid, iso):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/delete", methods=['POST']) @app.route('/vm/<string:vmid>/delete', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def delete(vmid): def delete(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -370,7 +370,7 @@ def delete(vmid):
return '', 403 return '', 403
@app.route("/vm/<string:vmid>/boot_order", methods=['POST']) @app.route('/vm/<string:vmid>/boot_order', methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def get_boot_order(vmid): def get_boot_order(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -386,7 +386,7 @@ def get_boot_order(vmid):
return '', 403 return '', 403
@app.route("/vm/create", methods=['GET', 'POST']) @app.route('/vm/create', methods=['GET', 'POST'])
@auth.oidc_auth @auth.oidc_auth
def create(): def create():
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -414,7 +414,7 @@ def create():
iso = request.form['iso'] iso = request.form['iso']
ssh_key = request.form['ssh_key'] ssh_key = request.form['ssh_key']
if iso != 'none': if iso != 'none':
iso = "{}:iso/{}".format(app.config['PROXMOX_ISO_STORAGE'], iso = '{}:iso/{}'.format(app.config['PROXMOX_ISO_STORAGE'],
iso) iso)
if not user.rtp: if not user.rtp:
if template == 'none': if template == 'none':
@ -481,7 +481,7 @@ def delete_user(user):
return '', 403 return '', 403
@app.route("/settings") @app.route('/settings')
@auth.oidc_auth @auth.oidc_auth
def settings(): def settings():
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
@ -499,33 +499,33 @@ def settings():
return abort(403) return abort(403)
@app.route("/pool/<string:pool>/ignore", methods=['POST', 'DELETE']) @app.route('/pool/<string:pool>/ignore', methods=['POST', 'DELETE'])
@auth.oidc_auth @auth.oidc_auth
def ignored_pools(pool): def ignored_pools(pool):
if 'rtp' in session['userinfo']['groups']: if 'rtp' in session['userinfo']['groups']:
if request.method == 'POST': if request.method == 'POST':
add_ignored_pool(db, pool) add_ignored_pool(db, pool)
elif request.method == "DELETE": elif request.method == 'DELETE':
delete_ignored_pool(db, pool) delete_ignored_pool(db, pool)
return '', 200 return '', 200
else: else:
return '', 403 return '', 403
@app.route("/user/<string:user>/allow", methods=['POST', 'DELETE']) @app.route('/user/<string:user>/allow', methods=['POST', 'DELETE'])
@auth.oidc_auth @auth.oidc_auth
def allowed_users(user): def allowed_users(user):
if 'rtp' in session['userinfo']['groups']: if 'rtp' in session['userinfo']['groups']:
if request.method == 'POST': if request.method == 'POST':
add_allowed_user(db, user) add_allowed_user(db, user)
elif request.method == "DELETE": elif request.method == 'DELETE':
delete_allowed_user(db, user) delete_allowed_user(db, user)
return '', 200 return '', 200
else: else:
return '', 403 return '', 403
@app.route("/console/cleanup", methods=['POST']) @app.route('/console/cleanup', methods=['POST'])
def cleanup_vnc(): def cleanup_vnc():
if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']: if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']:
for target in get_vnc_targets(): for target in get_vnc_targets():
@ -567,7 +567,7 @@ def template_edit(template_id):
return '', 403 return '', 403
@app.route("/logout") @app.route('/logout')
@auth.oidc_logout @auth.oidc_logout
def logout(): def logout():
return redirect(url_for('list_vms'), 302) return redirect(url_for('list_vms'), 302)
@ -584,5 +584,5 @@ def exit_handler():
atexit.register(exit_handler) atexit.register(exit_handler)
if __name__ == "__main__": if __name__ == '__main__':
app.run(threaded=False) app.run(threaded=False)

View file

@ -8,7 +8,7 @@ def connect_ldap():
try: try:
ldap = CSHLDAP(app.config['LDAP_BIND_DN'], app.config['LDAP_BIND_PW']) ldap = CSHLDAP(app.config['LDAP_BIND_DN'], app.config['LDAP_BIND_PW'])
except Exception as e: except Exception as e:
logging.error("unable to connect to LDAP: %s", e) logging.error('unable to connect to LDAP: %s', e)
raise raise
return ldap return ldap

View file

@ -21,38 +21,38 @@ def send_email(toaddr, subject, body):
def send_vm_expire_email(user, vms): def send_vm_expire_email(user, vms):
toaddr = "{}@csh.rit.edu".format(user) toaddr = '{}@csh.rit.edu'.format(user)
subject = 'Proxstar VM Expiration Notice' subject = 'Proxstar VM Expiration Notice'
body = "The following VMs in Proxstar are expiring soon or have already expired:\n\n" body = 'The following VMs in Proxstar are expiring soon or have already expired:\n\n'
for vm in vms: for vm in vms:
if vm[2] == -6: if vm[2] == -6:
body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format( body += ' - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n'.format(
vm[1], vm[0]) vm[1], vm[0])
elif vm[2] < 0: elif vm[2] < 0:
body += " - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n".format( body += ' - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n'.format(
vm[1], vm[0], (7 + int(vm[2]))) vm[1], vm[0], (7 + int(vm[2])))
elif vm[2] == 0: elif vm[2] == 0:
body += " - {} ({}) expires today (VM has been stopped and will be deleted in 7 days)\n".format( body += ' - {} ({}) expires today (VM has been stopped and will be deleted in 7 days)\n'.format(
vm[1], vm[0]) vm[1], vm[0])
elif vm[2] == 1: elif vm[2] == 1:
body += " - {} ({}) expires in 1 day\n".format(vm[1], vm[0]) body += ' - {} ({}) expires in 1 day\n'.format(vm[1], vm[0])
else: else:
body += " - {} ({}) expires in {} days\n".format( body += ' - {} ({}) expires in {} days\n'.format(
vm[1], vm[0], vm[2]) vm[1], vm[0], vm[2])
body += "\nPlease login to Proxstar (https://proxstar.csh.rit.edu/) and renew any VMs you would like to keep." body += '\nPlease login to Proxstar (https://proxstar.csh.rit.edu/) and renew any VMs you would like to keep.'
send_email(toaddr, subject, body) send_email(toaddr, subject, body)
def send_rtp_vm_delete_email(vms): def send_rtp_vm_delete_email(vms):
toaddr = 'rtp@csh.rit.edu' toaddr = 'rtp@csh.rit.edu'
subject = 'Proxstar VM Deletion Report' subject = 'Proxstar VM Deletion Report'
body = "The following VMs in Proxstar have expired and will be deleted soon:\n\n" body = 'The following VMs in Proxstar have expired and will be deleted soon:\n\n'
for vm in vms: for vm in vms:
if vm[2] == -6: if vm[2] == -6:
body += " - {} ({}) will be deleted in 1 day\n".format( body += ' - {} ({}) will be deleted in 1 day\n'.format(
vm[1], vm[0]) vm[1], vm[0])
else: else:
body += " - {} ({}) will be deleted in {} days\n".format( body += ' - {} ({}) will be deleted in {} days\n'.format(
vm[1], vm[0], (7 + int(vm[2]))) vm[1], vm[0], (7 + int(vm[2])))
body += "\nPlease verify this list to ensure there aren't any pools included in Proxstar that shouldn't be." body += "\nPlease verify this list to ensure there aren't any pools included in Proxstar that shouldn't be."
send_email(toaddr, subject, body) send_email(toaddr, subject, body)

View file

@ -4,11 +4,11 @@ import psycopg2
def get_next_ip(starrs, range_name): def get_next_ip(starrs, range_name):
c = starrs.cursor() c = starrs.cursor()
try: try:
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.get_address_from_range", (range_name, )) c.callproc('api.get_address_from_range', (range_name, ))
results = c.fetchall() results = c.fetchall()
c.execute("COMMIT") c.execute('COMMIT')
finally: finally:
c.close() c.close()
return results[0][0] return results[0][0]
@ -17,11 +17,11 @@ def get_next_ip(starrs, range_name):
def get_ip_for_mac(starrs, mac): def get_ip_for_mac(starrs, mac):
c = starrs.cursor() c = starrs.cursor()
try: try:
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.get_system_interface_addresses", (mac.lower(), )) c.callproc('api.get_system_interface_addresses', (mac.lower(), ))
results = c.fetchall() results = c.fetchall()
c.execute("COMMIT") c.execute('COMMIT')
finally: finally:
c.close() c.close()
if not results: if not results:
@ -32,11 +32,11 @@ def get_ip_for_mac(starrs, mac):
def renew_ip(starrs, addr): def renew_ip(starrs, addr):
c = starrs.cursor() c = starrs.cursor()
try: try:
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.renew_interface_address", (addr, )) c.callproc('api.renew_interface_address', (addr, ))
results = c.fetchall() results = c.fetchall()
c.execute("COMMIT") c.execute('COMMIT')
finally: finally:
c.close() c.close()
return results return results
@ -47,31 +47,31 @@ def check_hostname(starrs, hostname):
c = starrs.cursor() c = starrs.cursor()
try: try:
# Check for invalid characters in hostname # Check for invalid characters in hostname
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.validate_name", (hostname, )) c.callproc('api.validate_name', (hostname, ))
c.execute("COMMIT") c.execute('COMMIT')
# Validate the entire domain name using Data::Validate::Domain # Validate the entire domain name using Data::Validate::Domain
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.validate_domain", (hostname, 'csh.rit.edu')) c.callproc('api.validate_domain', (hostname, 'csh.rit.edu'))
valid = c.fetchall()[0][0] valid = c.fetchall()[0][0]
c.execute("COMMIT") c.execute('COMMIT')
# Check if the hostname is available (checks A/SRV/CNAME records) # Check if the hostname is available (checks A/SRV/CNAME records)
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.check_dns_hostname", (hostname, 'csh.rit.edu')) c.callproc('api.check_dns_hostname', (hostname, 'csh.rit.edu'))
available = False available = False
if not c.fetchall()[0][0]: if not c.fetchall()[0][0]:
available = True available = True
c.execute("COMMIT") c.execute('COMMIT')
# Check if the system name is taken # Check if the system name is taken
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.get_system", (hostname, )) c.callproc('api.get_system', (hostname, ))
if c.fetchall(): if c.fetchall():
available = False available = False
c.execute("COMMIT") c.execute('COMMIT')
except psycopg2.InternalError: except psycopg2.InternalError:
valid = False valid = False
available = False available = False
@ -83,13 +83,13 @@ def check_hostname(starrs, hostname):
def register_starrs(starrs, name, owner, mac, addr): def register_starrs(starrs, name, owner, mac, addr):
c = starrs.cursor() c = starrs.cursor()
try: try:
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc( c.callproc(
"api.create_system_quick", 'api.create_system_quick',
(name, owner, 'members', mac, addr, 'csh.rit.edu', 'dhcp', True)) (name, owner, 'members', mac, addr, 'csh.rit.edu', 'dhcp', True))
results = c.fetchall() results = c.fetchall()
c.execute("COMMIT") c.execute('COMMIT')
finally: finally:
c.close() c.close()
return results return results
@ -98,11 +98,11 @@ def register_starrs(starrs, name, owner, mac, addr):
def delete_starrs(starrs, name): def delete_starrs(starrs, name):
c = starrs.cursor() c = starrs.cursor()
try: try:
c.execute("BEGIN") c.execute('BEGIN')
c.callproc("api.initialize", ('root', )) c.callproc('api.initialize', ('root', ))
c.callproc("api.remove_system", (name, )) c.callproc('api.remove_system', (name, ))
results = c.fetchall() results = c.fetchall()
c.execute("COMMIT") c.execute('COMMIT')
finally: finally:
c.close() c.close()
return results return results

View file

@ -23,11 +23,11 @@ logging.basicConfig(
app = Flask(__name__) app = Flask(__name__)
if os.path.exists( if os.path.exists(
os.path.join( os.path.join(
app.config.get('ROOT_DIR', os.getcwd()), "config.local.py")): app.config.get('ROOT_DIR', os.getcwd()), 'config.local.py')):
config = os.path.join( config = os.path.join(
app.config.get('ROOT_DIR', os.getcwd()), "config.local.py") app.config.get('ROOT_DIR', os.getcwd()), 'config.local.py')
else: else:
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py") config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), 'config.py')
app.config.from_pyfile(config) app.config.from_pyfile(config)
@ -58,11 +58,11 @@ def create_vm_task(user, name, cores, memory, disk, iso):
proxmox = connect_proxmox() proxmox = connect_proxmox()
db = connect_db() db = connect_db()
starrs = connect_starrs() starrs = connect_starrs()
logging.info("[{}] Creating VM.".format(name)) logging.info('[{}] Creating VM.'.format(name))
set_job_status(job, 'creating VM') set_job_status(job, 'creating VM')
vmid = create_vm(proxmox, user, name, cores, memory, disk, iso) vmid = create_vm(proxmox, user, name, cores, memory, disk, iso)
logging.info( logging.info(
"[{}] Waiting until Proxmox is done provisioning.".format(name)) '[{}] Waiting until Proxmox is done provisioning.'.format(name))
set_job_status(job, 'waiting for Proxmox') set_job_status(job, 'waiting for Proxmox')
timeout = 20 timeout = 20
retry = 0 retry = 0
@ -73,11 +73,11 @@ def create_vm_task(user, name, cores, memory, disk, iso):
continue continue
break break
if retry == timeout: if retry == timeout:
logging.info("[{}] Failed to provision, deleting.".format(name)) logging.info('[{}] Failed to provision, deleting.'.format(name))
set_job_status(job, 'failed to provision') set_job_status(job, 'failed to provision')
delete_vm_task(vmid) delete_vm_task(vmid)
return return
logging.info("[{}] Registering in STARRS.".format(name)) logging.info('[{}] Registering in STARRS.'.format(name))
set_job_status(job, 'registering in STARRS') set_job_status(job, 'registering in STARRS')
vm = VM(vmid) vm = VM(vmid)
ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE']) ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE'])
@ -85,7 +85,7 @@ def create_vm_task(user, name, cores, memory, disk, iso):
ip) ip)
set_job_status(job, 'setting VM expiration') set_job_status(job, 'setting VM expiration')
get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
logging.info("[{}] VM successfully provisioned.".format(name)) logging.info('[{}] VM successfully provisioned.'.format(name))
set_job_status(job, 'complete') set_job_status(job, 'complete')
@ -129,7 +129,7 @@ def process_expiring_vms_task():
vm.stop() vm.stop()
elif days <= -7: elif days <= -7:
logging.info( logging.info(
"Deleting {} ({}) as it has been at least a week since expiration." 'Deleting {} ({}) as it has been at least a week since expiration.'
.format(vm.name, vm.id)) .format(vm.name, vm.id))
send_stop_ssh_tunnel(vm.id) send_stop_ssh_tunnel(vm.id)
delete_vm_task(vm.id) delete_vm_task(vm.id)
@ -153,14 +153,14 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
proxmox = connect_proxmox() proxmox = connect_proxmox()
starrs = connect_starrs() starrs = connect_starrs()
db = connect_db() db = connect_db()
logging.info("[{}] Retrieving template info for template {}.".format( logging.info('[{}] Retrieving template info for template {}.'.format(
name, template_id)) name, template_id))
get_template(db, template_id) get_template(db, template_id)
logging.info("[{}] Cloning template {}.".format(name, template_id)) logging.info('[{}] Cloning template {}.'.format(name, template_id))
set_job_status(job, 'cloning template') set_job_status(job, 'cloning template')
vmid = clone_vm(proxmox, template_id, name, user) vmid = clone_vm(proxmox, template_id, name, user)
logging.info( logging.info(
"[{}] Waiting until Proxmox is done provisioning.".format(name)) '[{}] Waiting until Proxmox is done provisioning.'.format(name))
set_job_status(job, 'waiting for Proxmox') set_job_status(job, 'waiting for Proxmox')
timeout = 20 timeout = 20
retry = 0 retry = 0
@ -171,43 +171,43 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
continue continue
break break
if retry == timeout: if retry == timeout:
logging.info("[{}] Failed to provision, deleting.".format(name)) logging.info('[{}] Failed to provision, deleting.'.format(name))
set_job_status(job, 'failed to provision') set_job_status(job, 'failed to provision')
delete_vm_task(vmid) delete_vm_task(vmid)
return return
logging.info("[{}] Registering in STARRS.".format(name)) logging.info('[{}] Registering in STARRS.'.format(name))
set_job_status(job, 'registering in STARRS') set_job_status(job, 'registering in STARRS')
vm = VM(vmid) vm = VM(vmid)
ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE']) ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE'])
register_starrs(starrs, name, app.config['STARRS_USER'], vm.get_mac(), register_starrs(starrs, name, app.config['STARRS_USER'], vm.get_mac(),
ip) ip)
get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
logging.info("[{}] Setting CPU and memory.".format(name)) logging.info('[{}] Setting CPU and memory.'.format(name))
set_job_status(job, 'setting CPU and memory') set_job_status(job, 'setting CPU and memory')
vm.set_cpu(cores) vm.set_cpu(cores)
vm.set_mem(memory) vm.set_mem(memory)
logging.info("[{}] Applying cloud-init config.".format(name)) logging.info('[{}] Applying cloud-init config.'.format(name))
set_job_status(job, 'applying cloud-init') set_job_status(job, 'applying cloud-init')
vm.set_ci_user(user) vm.set_ci_user(user)
vm.set_ci_ssh_key(ssh_key) vm.set_ci_ssh_key(ssh_key)
vm.set_ci_network() vm.set_ci_network()
logging.info( logging.info(
"[{}] Waiting for STARRS to propogate before starting VM.".format( '[{}] Waiting for STARRS to propogate before starting VM.'.format(
name)) name))
set_job_status(job, 'waiting for STARRS') set_job_status(job, 'waiting for STARRS')
job.save_meta() job.save_meta()
time.sleep(90) time.sleep(90)
logging.info("[{}] Starting VM.".format(name)) logging.info('[{}] Starting VM.'.format(name))
set_job_status(job, 'starting VM') set_job_status(job, 'starting VM')
job.save_meta() job.save_meta()
vm.start() vm.start()
logging.info("[{}] Template successfully provisioned.".format(name)) logging.info('[{}] Template successfully provisioned.'.format(name))
set_job_status(job, 'completed') set_job_status(job, 'completed')
job.save_meta() job.save_meta()
def cleanup_vnc_task(): def cleanup_vnc_task():
requests.post( requests.post(
"https://{}/console/cleanup".format(app.config['SERVER_NAME']), 'https://{}/console/cleanup'.format(app.config['SERVER_NAME']),
data={'token': app.config['VNC_CLEANUP_TOKEN']}, data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False) verify=False)

View file

@ -108,11 +108,11 @@ class User():
proxmox = connect_proxmox() proxmox = connect_proxmox()
proxmox.pools(self.name).delete() proxmox.pools(self.name).delete()
users = proxmox.access.users.get() users = proxmox.access.users.get()
if any(user['userid'] == "{}@csh.rit.edu".format(self.name) if any(user['userid'] == '{}@csh.rit.edu'.format(self.name)
for user in users): for user in users):
if 'rtp' not in proxmox.access.users("{}@csh.rit.edu".format( if 'rtp' not in proxmox.access.users('{}@csh.rit.edu'.format(
self.name)).get()['groups']: self.name)).get()['groups']:
proxmox.access.users("{}@csh.rit.edu".format( proxmox.access.users('{}@csh.rit.edu'.format(
self.name)).delete() self.name)).delete()

View file

@ -3,7 +3,7 @@ import random
def gen_password( def gen_password(
length, length,
charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*" charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*'
): ):
# use secrets module once this works in python 3.6 # use secrets module once this works in python 3.6
return ''.join(random.choice(charset) for x in range(length)) return ''.join(random.choice(charset) for x in range(length))

View file

@ -210,7 +210,7 @@ class VM():
proxmox = connect_proxmox() proxmox = connect_proxmox()
port = str(int(port) - 5900) port = str(int(port) - 5900)
proxmox.nodes(self.node).qemu(self.id).monitor.post( proxmox.nodes(self.node).qemu(self.id).monitor.post(
command="change vnc 127.0.0.1:{}".format(port)) command='change vnc 127.0.0.1:{}'.format(port))
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5)) @retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
def eject_iso(self): def eject_iso(self):
@ -222,12 +222,12 @@ class VM():
def mount_iso(self, iso): def mount_iso(self, iso):
proxmox = connect_proxmox() proxmox = connect_proxmox()
proxmox.nodes(self.node).qemu( proxmox.nodes(self.node).qemu(
self.id).config.post(ide2="{},media=cdrom".format(iso)) self.id).config.post(ide2='{},media=cdrom'.format(iso))
def resize_disk(self, disk, size): def resize_disk(self, disk, size):
proxmox = connect_proxmox() proxmox = connect_proxmox()
proxmox.nodes(self.node).qemu(self.id).resize.put( proxmox.nodes(self.node).qemu(self.id).resize.put(
disk=disk, size="+{}G".format(size)) disk=disk, size='+{}G'.format(size))
@lazy_property @lazy_property
def expire(self): def expire(self):
@ -263,8 +263,8 @@ def create_vm(proxmox, user, name, cores, memory, disk, iso):
cores=cores, cores=cores,
memory=memory, memory=memory,
storage='ceph', storage='ceph',
virtio0="ceph:{}".format(disk), virtio0='ceph:{}'.format(disk),
ide2="{},media=cdrom".format(iso), ide2='{},media=cdrom'.format(iso),
net0='virtio,bridge=vmbr0', net0='virtio,bridge=vmbr0',
pool=user, pool=user,
description='Managed by Proxstar') description='Managed by Proxstar')

View file

@ -21,7 +21,7 @@ def stop_websockify():
time.sleep(10) time.sleep(10)
if subprocess.run(['pgrep', 'websockify'], if subprocess.run(['pgrep', 'websockify'],
stdout=subprocess.PIPE).stdout: stdout=subprocess.PIPE).stdout:
logging.info('websockify didn\'t stop, killing forcefully') logging.info("websockify didn't stop, killing forcefully")
subprocess.run(['kill', '-9', pid], stdout=subprocess.PIPE) subprocess.run(['kill', '-9', pid], stdout=subprocess.PIPE)
@ -48,7 +48,7 @@ def add_vnc_target(port):
else: else:
target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'a') target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'a')
token = gen_password(32, 'abcdefghijklmnopqrstuvwxyz0123456789') token = gen_password(32, 'abcdefghijklmnopqrstuvwxyz0123456789')
target_file.write("{}: 127.0.0.1:{}\n".format(token, str(port))) target_file.write('{}: 127.0.0.1:{}\n'.format(token, str(port)))
target_file.close() target_file.close()
return token return token
@ -61,7 +61,7 @@ def delete_vnc_target(port):
targets.remove(target) targets.remove(target)
target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w') target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w')
for target in targets: for target in targets:
target_file.write("{}: 127.0.0.1:{}\n".format( target_file.write('{}: 127.0.0.1:{}\n'.format(
target['token'], target['port'])) target['token'], target['port']))
target_file.close() target_file.close()
@ -97,7 +97,7 @@ def stop_ssh_tunnel(vmid, ssh_tunnels):
def send_stop_ssh_tunnel(vmid): def send_stop_ssh_tunnel(vmid):
requests.post( requests.post(
"https://{}/console/vm/{}/stop".format(app.config['SERVER_NAME'], 'https://{}/console/vm/{}/stop'.format(app.config['SERVER_NAME'],
vmid), vmid),
data={'token': app.config['VNC_CLEANUP_TOKEN']}, data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False) verify=False)