Expand options to not use starrs.

This commit is contained in:
Will Nilges 2021-10-05 23:02:16 -04:00
parent c641ba1d1c
commit 33a8cdb523
3 changed files with 39 additions and 21 deletions

View file

@ -5,6 +5,7 @@ VM_EXPIRE_MONTHS = int(environ.get('PROXSTAR_VM_EXPIRE_MONTHS', '3'))
VNC_CLEANUP_TOKEN = environ.get('PROXSTAR_VNC_CLEANUP_TOKEN', '') VNC_CLEANUP_TOKEN = environ.get('PROXSTAR_VNC_CLEANUP_TOKEN', '')
# Development options # Development options
# Determines weather or not to run STARRS queries (for doing stuff like checking for available IPs)
USE_STARRS = environ.get('PROXSTAR_USE_STARRS', 'True') USE_STARRS = environ.get('PROXSTAR_USE_STARRS', 'True')
FORCE_STANDARD_USER = environ.get('PROXSTAR_FORCE_STANDARD_USER', 'False') FORCE_STANDARD_USER = environ.get('PROXSTAR_FORCE_STANDARD_USER', 'False')

View file

@ -83,6 +83,7 @@ Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine) DBSession = sessionmaker(bind=engine)
db = DBSession() db = DBSession()
if app.config['USE_STARRS']:
starrs = psycopg2.connect( starrs = psycopg2.connect(
"dbname='{}' user='{}' host='{}' password='{}'".format( "dbname='{}' user='{}' host='{}' password='{}'".format(
app.config['STARRS_DB_NAME'], app.config['STARRS_DB_NAME'],
@ -202,7 +203,10 @@ def isos():
@app.route('/hostname/<string:name>') @app.route('/hostname/<string:name>')
@auth.oidc_auth @auth.oidc_auth
def hostname(name): def hostname(name):
if app.config['USE_STARRS']:
valid, available = check_hostname(starrs, name) valid, available = check_hostname(starrs, name)
else:
valid, available = (True, True)
if not valid: if not valid:
return 'invalid' return 'invalid'
if not available: if not available:
@ -358,7 +362,7 @@ def vm_renew(vmid):
vm = VM(vmid) vm = VM(vmid)
renew_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) renew_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
for interface in vm.interfaces: for interface in vm.interfaces:
if interface[2] != 'No IP': if interface[2] != 'No IP' and app.config['USE_STARRS']:
renew_ip(starrs, interface[2]) renew_ip(starrs, interface[2])
return '', 200 return '', 200
else: else:
@ -464,7 +468,11 @@ def create():
if usage_check: if usage_check:
return usage_check return usage_check
else: else:
if app.config['USE_STARRS']:
valid, available = check_hostname(starrs, name) valid, available = check_hostname(starrs, name)
else:
valid, available = (True, True)
if valid and available: if valid and available:
if template == 'none': if template == 'none':
q.enqueue( q.enqueue(

View file

@ -64,6 +64,7 @@ def create_vm_task(user, name, cores, memory, disk, iso):
job = get_current_job() job = get_current_job()
proxmox = connect_proxmox() proxmox = connect_proxmox()
db = connect_db() db = connect_db()
if app.config['USE_STARRS']:
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')
@ -99,12 +100,14 @@ def create_vm_task(user, name, cores, memory, disk, iso):
def delete_vm_task(vmid): def delete_vm_task(vmid):
with app.app_context(): with app.app_context():
db = connect_db() db = connect_db()
if app.config['USE_STARRS']:
starrs = connect_starrs() starrs = connect_starrs()
vm = VM(vmid) vm = VM(vmid)
# do this before deleting the VM since it is hard to reconcile later # do this before deleting the VM since it is hard to reconcile later
retry = 0 retry = 0
while retry < 3: while retry < 3:
try: try:
if app.config['USE_STARRS']:
delete_starrs(starrs, vm.name) delete_starrs(starrs, vm.name)
break break
except: except:
@ -127,6 +130,7 @@ def process_expiring_vms_task():
with app.app_context(): with app.app_context():
proxmox = connect_proxmox() proxmox = connect_proxmox()
db = connect_db() db = connect_db()
if app.config['USE_STARRS']:
connect_starrs() connect_starrs()
pools = get_pools(proxmox, db) pools = get_pools(proxmox, db)
expired_vms = [] expired_vms = []
@ -168,6 +172,7 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
with app.app_context(): with app.app_context():
job = get_current_job() job = get_current_job()
proxmox = connect_proxmox() proxmox = connect_proxmox()
if app.config['USE_STARRS']:
starrs = connect_starrs() starrs = connect_starrs()
db = connect_db() db = connect_db()
logging.info('[{}] Retrieving template info for template {}.'.format(name, template_id)) logging.info('[{}] Retrieving template info for template {}.'.format(name, template_id))
@ -190,11 +195,13 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
set_job_status(job, 'failed to provision') set_job_status(job, 'failed to provision')
delete_vm_task(vmid) delete_vm_task(vmid)
return return
if app.config['USE_STARRS']:
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)
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(), ip) register_starrs(starrs, name, app.config['STARRS_USER'], vm.get_mac(), ip)
vm = VM(vmid)
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')
@ -205,6 +212,8 @@ def setup_template_task(template_id, name, user, ssh_key, cores, memory):
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()
if app.config['USE_STARRS']:
logging.info('[{}] Waiting for STARRS to propogate before starting VM.'.format(name)) logging.info('[{}] Waiting for STARRS to propogate before starting VM.'.format(name))
set_job_status(job, 'waiting for STARRS') set_job_status(job, 'waiting for STARRS')
job.save_meta() job.save_meta()