def create_vm_task(user, name, cores, memory, disk, iso): with app.app_context(): job = get_current_job() proxmox = connect_proxmox() db = connect_db() starrs = connect_starrs() logging.info('[{}] Creating VM.'.format(name)) set_job_status(job, 'creating VM') vmid = create_vm(proxmox, user, name, cores, memory, disk, iso) logging.info( '[{}] Waiting until Proxmox is done provisioning.'.format(name)) set_job_status(job, 'waiting for Proxmox') timeout = 20 retry = 0 while retry < timeout: if not VM(vmid).is_provisioned(): retry += 1 time.sleep(3) continue break if retry == timeout: logging.info('[{}] Failed to provision, deleting.'.format(name)) set_job_status(job, 'failed to provision') delete_vm_task(vmid) return logging.info('[{}] Registering in STARRS.'.format(name)) set_job_status(job, 'registering in STARRS') vm = VM(vmid) ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE']) register_starrs(starrs, name, app.config['STARRS_USER'], vm.get_mac(), ip) set_job_status(job, 'setting VM expiration') get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) logging.info('[{}] VM successfully provisioned.'.format(name)) set_job_status(job, 'complete')
def setup_template_task(template_id, name, user, ssh_key, cores, memory): with app.app_context(): job = get_current_job() proxmox = connect_proxmox() starrs = connect_starrs() db = connect_db() logging.info('[{}] Retrieving template info for template {}.'.format( name, template_id)) get_template(db, template_id) logging.info('[{}] Cloning template {}.'.format(name, template_id)) set_job_status(job, 'cloning template') vmid = clone_vm(proxmox, template_id, name, user) logging.info( '[{}] Waiting until Proxmox is done provisioning.'.format(name)) set_job_status(job, 'waiting for Proxmox') timeout = 25 retry = 0 while retry < timeout: if not VM(vmid).is_provisioned(): retry += 1 time.sleep(12) continue break if retry == timeout: logging.info('[{}] Failed to provision, deleting.'.format(name)) set_job_status(job, 'failed to provision') delete_vm_task(vmid) return logging.info('[{}] Registering in STARRS.'.format(name)) set_job_status(job, 'registering in STARRS') vm = VM(vmid) ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE']) register_starrs(starrs, name, app.config['STARRS_USER'], vm.get_mac(), ip) get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) logging.info('[{}] Setting CPU and memory.'.format(name)) set_job_status(job, 'setting CPU and memory') vm.set_cpu(cores) vm.set_mem(memory) logging.info('[{}] Applying cloud-init config.'.format(name)) set_job_status(job, 'applying cloud-init') vm.set_ci_user(user) vm.set_ci_ssh_key(ssh_key) vm.set_ci_network() logging.info( '[{}] Waiting for STARRS to propogate before starting VM.'.format( name)) set_job_status(job, 'waiting for STARRS') job.save_meta() time.sleep(90) logging.info('[{}] Starting VM.'.format(name)) set_job_status(job, 'starting VM') job.save_meta() vm.start() logging.info('[{}] Template successfully provisioned.'.format(name)) set_job_status(job, 'completed') job.save_meta()
def expire(self): return get_vm_expire(db, self.id, app.config['VM_EXPIRE_MONTHS'])