async def restore_snapshot(request, machine_id, snapshot_id): with data.Connection.use() as conn: action = request.headers['json_params'].get('action') if action is None: raise InvalidUsage('\'action\' is missing in passed data!') elif action == 'restore': machine_ro = data.Machine.get_one({'_id': machine_id}, conn=conn) if snapshot_id not in machine_ro.snapshots: raise InvalidUsage( f'Machine \'{machine_id}\' does not have snapshot \'{snapshot_id}\'' ) new_request = data.Request(type=data.RequestType.RESTORE_SNAPSHOT) new_request.machine = machine_ro.id new_request.subject_id = snapshot_id new_request.save(conn=conn) # enqueue snapshot restoration data.Action(type='other', request=new_request.id).save(conn=conn) return sanic.response.json( { "responses": [{ "type": "request_id", "request_id": new_request.id, "is_last": True }] }, status=200) else: raise InvalidUsage(f'Invalid \'action\' value: {action}')
async def delete_snapshot(request, machine_id, snapshot_id): with data.Connection.use() as conn: machine_ro = data.Machine.get_one({'_id': machine_id}, conn=conn) if snapshot_id not in machine_ro.snapshots: raise InvalidUsage( f'Machine \'{machine_id}\' does not have snapshot \'{snapshot_id}\'!' ) new_request = data.Request(type=data.RequestType.DELETE_SNAPSHOT) new_request.machine = machine_id new_request.subject_id = snapshot_id new_request.save(conn=conn) # enqueue snapshot deletion data.Action(type='other', request=new_request.id).save(conn=conn) return sanic.response.json( { "responses": [{ "type": "request_id", "request_id": new_request.id, "is_last": True }] }, status=200)
async def machine_deploy(request): await check_payload_deploy(request) logger.debug("POST /machines wanted by: {}".format( request.headers.get("AUTHORISED_LOGIN", "<n/a>"))) labels = request.headers['json_params']['labels'] await check_resources() with data.Connection.use() as conn: new_request = data.Request(type=data.RequestType.DEPLOY) new_request.save(conn=conn) if settings.app['service']['personalised']: new_machine = data.Machine( labels=labels, requests=[new_request.id], owner=request.headers["AUTHORISED_LOGIN"], ) else: new_machine = data.Machine(labels=labels, requests=[new_request.id]) new_machine.save(conn=conn) new_request.machine = str(new_machine.id) new_request.save(conn=conn) # begin machine preparation data.Action(type='deploy', request=new_request.id).save(conn=conn) return {'request_id': '{}'.format(new_request.id), 'is_last': False}
async def machine_do_start_stop(request, machine_id): logger.debug('Current thread name: {}'.format( threading.current_thread().name)) action = request.headers.get('json_params').get('action') if action not in ['start', 'stop']: raise sanic.exceptions.InvalidUsage( 'malformatted input json data, invalid or none \'action\' specified' ) request_type = data.RequestType(action) # do start / stop with data.Connection.use() as conn: asyncio.sleep(0.1) machine = data.Machine.get_one_for_update({'_id': machine_id}, conn=conn) await check_machine_owner(machine, request) new_request = data.Request(type=request_type, machine=str(machine_id)) new_request.save(conn=conn) machine.requests.append(new_request.id) machine.save(conn=conn) data.Action(type='other', request=new_request.id).save(conn=conn) return {'request_id': '{}'.format(new_request.id), 'is_last': False}
def enqueue_get_info_request(request, machine, conn): # create another task to get info about that machine logger.debug('creating another get_info action for {}'.format(request.machine)) new_request = data.Request(type=RequestType.GET_INFO, machine=str(machine.id)) new_request.save(conn=conn) machine.requests.append(new_request.id) machine.save(conn=conn) data.Action( type='other', request=new_request.id, repetitions=20, delay=10, next_try=datetime.datetime.now() + datetime.timedelta(seconds=5) ).save(conn=conn)
async def machine_get_info(request, machine_id): logger.debug('Current thread name: {}'.format( threading.current_thread().name)) with data.Connection.use() as conn: asyncio.sleep(0.1) machine = data.Machine.get_one_for_update({'_id': machine_id}, conn=conn) await check_machine_owner(machine, request) new_request = data.Request(type=data.RequestType.UNDEPLOY, machine=str(machine_id)) new_request.save(conn=conn) machine.requests.append(new_request.id) machine.save(conn=conn) data.Action(type='other', request=new_request.id).save(conn=conn) return {'request_id': '{}'.format(new_request.id), 'is_last': False}
async def take_snapshot(request, machine_id): with data.Connection.use() as conn: snapshot_name = request.headers['json_params']['name'] new_snapshot = data.Snapshot(machine=machine_id, name=snapshot_name) new_snapshot.save(conn=conn) new_request = data.Request(type=data.RequestType.TAKE_SNAPSHOT) new_request.machine = machine_id new_request.subject_id = new_snapshot.id new_request.save(conn=conn) # enqueue snapshot preparation data.Action(type='other', request=new_request.id).save(conn=conn) return sanic.response.json( { "responses": [{ "type": "request_id", "request_id": new_request.id, "snapshot_id": new_snapshot.id, "is_last": True }] }, status=200)
async def take_screenshot(request, machine_id): with data.Connection.use() as conn: machine = data.Machine.get_one_for_update({'_id': machine_id}, conn=conn) new_screenshot = data.Screenshot(machine='{}'.format(machine_id)) new_screenshot.save(conn=conn) machine.screenshots.append(new_screenshot.id) machine.save(conn=conn) new_request = data.Request(type=data.RequestType.TAKE_SCREENSHOT) new_request.machine = str(machine.id) new_request.subject_id = str(new_screenshot.id) new_request.save(conn=conn) # begin screenshot preparation data.Action(type='other', request=new_request.id).save(conn=conn) return { 'result': { 'screenshot_id': '{}'.format(new_screenshot.id), }, 'is_last': True, }