async def request(request): """ Method: GET A blocking machine request API, return until timeout (with 301) or there is a valid machine useful for clients that only wants a machine and nothing else. """ if request.method == 'GET': query_params = parse_query(parse_request_params(request.query)) elif request.method == 'POST': query_params = parse_query(await request.json()) query_params = sanitize_query(query_params, Parameters) # Find / provision a ready machine and reserve it query_params.setdefault('status', 'ready') machines = await Pipeline(request).query(query_params) if not machines: try: machines = await Pipeline(request).provision(query_params, timeout=None) except RuntimeError as error: return web.json_response({'message': error}, status=400) if machines: machines = await Pipeline(request).reserve(query_params) if machines and len(machines): return web.json_response([m.to_json() for m in machines]) else: return web.json_response( {'message': 'Failed to find or provision a machine'}, status=404)
async def delete(request): query_params = sanitize_query( parse_query(parse_request_params(request.query)), Parameters) data = [] machines = await Pipeline(request).query(query_params, nocount=True) for machine in machines: await machine.delete() data.append(machine.to_json()) return web.json_response(data, status=200)
async def release(request): """ Method: POST Non blocking API to request to force release a machine """ query_params = sanitize_query(parse_query(await request.json()), Parameters) machines = await Pipeline(request).release(query_params) return web.json_response([m.to_json() for m in machines])
async def provision(request): """ Method: POST Non blocking API to request to provision a machine """ query_params = sanitize_query(parse_query(await request.json()), Parameters) if not await request['magic'].allow_provision(query_params): return web.json_response({'message': 'no avaliable'}, status=406) machines = await Pipeline(request).provision(query_params) return web.json_response([m.to_json() for m in machines])
def __init__(self, machines, query: dict = None, loop=None, uuid=None, context=None): self.uuid = uuid or str(uuid1()) self.resume = False self.loop = loop or asyncio.get_event_loop() # Machines this task related to, if task failed, all related machine will be # moved to failure pool self.machines = machines # If not None, a async task is running and when task.cancel() is called, this # future is cancelled self.future = None # Task status, task should be reentrant, but we still keep a status attribute for now # for easier tracking, status should be pending, running, success, failed self.status = 'pending' self.running = True # the query object that issued this task, could be None for pool scheduled task self.query = sanitize_query(query, self.PARAMETERS) Tasks[self.uuid] = self
async def get(request): query_params = sanitize_query( parse_query(parse_request_params(request.query)), Parameters) machines = await Pipeline(request).query(query_params, nocount=True) return web.json_response([machine.to_json() for machine in machines])