def list_instances(): """ Lists instances. --- tags: - instance parameters: - name: show in: query type: string description: Show different types of instances default: running enum: [all, starting, deallocated, allocated, running, finished, error] responses: 200: description: Packet Tracer instances schema: properties: instances: type: array items: $ref: '#/definitions/assign_instance_post_Instance' """ show_param = request.args.get("show") if show_param is None or show_param == "running": # default option return get_json_instances(Instance.get_running()) else: states = ("all", "starting", "deallocated", "allocated", "running", "finished", "error") if show_param not in states: state_enum = "[" for s in states: state_enum += "%s, " % s state_enum = state_enum[:-2] + "]" return BadRequest( "The 'show' parameter must contain one of the following values: %s." % state_enum) if show_param == "all": return get_json_instances(Instance.get_all()) # .limit(10) elif show_param == "starting": return get_json_instances(Instance.get_starting()) elif show_param == "deallocated": return get_json_instances(Instance.get_deallocated()) elif show_param == "allocated": return get_json_instances(Instance.get_allocated()) elif show_param == "error": return get_json_instances(Instance.get_erroneous()) else: # show_param is "finished": return get_json_instances(Instance.get_finished())
def list_instances(): """ Lists instances. --- tags: - instance parameters: - name: show in: query type: string description: Show different types of instances default: running enum: [all, starting, deallocated, allocated, running, finished, error] responses: 200: description: Packet Tracer instances schema: properties: instances: type: array items: $ref: '#/definitions/assign_instance_post_Instance' """ show_param = request.args.get("show") if show_param is None or show_param == "running": # default option return get_json_instances(Instance.get_running()) else: states = ("all", "starting", "deallocated", "allocated", "running", "finished", "error") if show_param not in states: state_enum = "[" for s in states: state_enum += "%s, " % s state_enum = state_enum[:-2] + "]" return BadRequest("The 'show' parameter must contain one of the following values: %s." % state_enum) if show_param == "all": return get_json_instances(Instance.get_all()) # .limit(10) elif show_param == "starting": return get_json_instances(Instance.get_starting()) elif show_param == "deallocated": return get_json_instances(Instance.get_deallocated()) elif show_param == "allocated": return get_json_instances(Instance.get_allocated()) elif show_param == "error": return get_json_instances(Instance.get_erroneous()) else: # show_param is "finished": return get_json_instances(Instance.get_finished())
def allocate_instance(): """Unpauses available container and marks associated instance as allocated.""" logger.info('Allocating instance.') docker = get_docker_client() error_discovered = False allocation_id = None for instance in Instance.get_deallocated(): try: docker.unpause(instance.docker_id) allocation_id = instance.allocate().id break except APIError as ae: logger.error('Error allocating instance %s.' % instance.id) logger.error('Docker API exception. %s.' % ae) # e.g., if it was already unpaused or it has been stopped instance.mark_error() if not allocation_id: # If there were no instances available, consider the creation of a new one instance_id = create_instance.s()() # Execute task inline allocation_id = Instance.get(instance_id).allocate().id return allocation_id
def allocate_instance(): """Unpauses available container and marks associated instance as allocated.""" logger.info('Allocating instance.') docker = get_docker_client() error_discovered = False allocation_id = None for instance in Instance.get_deallocated(): try: docker.unpause(instance.docker_id) allocation_id = instance.allocate().id break except APIError as ae: logger.error('Error allocating instance %s.' % instance.id) logger.error('Docker API exception. %s.' % ae) # e.g., if it was already unpaused or it has been stopped instance.mark_error() if not allocation_id: # If there were no instances available, consider the creation of a new one instance_id = create_instance.s()() # Execute task inline allocation_id = Instance.get(instance_id).allocate().id return allocation_id