def delete_instance(username, roles, instanceid):
    instances = get_instances_by_username_and_id(username=username,
                                                 instanceid=instanceid)
    logger.info(f"Got instance data {instances}")
    if instanceid in instances:
        # need to trigger delete
        instance = instances[instanceid]
        machine_def = get_machine_def(
            machine_def_id=instance["machine_def_id"])
        # create task to provision
        response = destroy_desktop_instance(
            desktop_id=instanceid,
            ami_id=machine_def["ami_id"],
            machine_username=username,
            screen_geometry=instance["screengeometry"],
            machine_def_id=instance["machine_def_id"],
            instance_type=machine_def["instance_type"],
            user_data=machine_def["user_data"])
        if response:
            return success_json_response({
                "desktop_id":
                instanceid,
                "status":
                "okay",
                "message":
                "created task to remove instance"
            })
        else:
            return success_json_response({"status": "error"})
    else:
        raise ResourceNotFoundException(
            f"An instance with id '{instanceid}' was not found")
示例#2
0
def get_pipelines(username, groups):
    if MODE == "EXP" and len(groups) == 0:
        return success_json_response([])
    else:
        if MODE == "EXP" and len(groups) == 1:
            if groups[0] == "":
                return success_json_response([])
            else:
                pipes = get_pipelines_with_status(groups)
                return success_json_response(pipes)
        else:
            pipes = get_pipelines_with_status(groups)
            return success_json_response(pipes)
def get_instance(username, roles, instanceid):
    instance = get_instances_by_username_and_id(username=username,
                                                instanceid=instanceid)
    logger.info(f"Got instance data {instance}")
    if len(instance) > 0:
        return success_json_response(instance[instanceid])
    else:
        raise ResourceNotFoundException(
            f"An instance with id '{instanceid}' was not found")
def change_instance(username, roles, instanceid):
    instance = get_instances_by_username_and_id(username=username,
                                                instanceid=instanceid)
    logger.info(f"Got instance data {instance}")
    if len(instance) > 0:
        # check request is valid
        if request.json:
            missing = check_for_keys(dict=request.json, keys=["state"])
            if missing:
                raise BadRequestException(
                    f"The following keys are missing from the request: {missing}"
                )
            else:
                if request.json["state"] not in ["stopped", "running"]:
                    raise BadRequestException("Invalid state request")
                else:
                    if request.json["state"] == "stopped":
                        stop_instance(
                            instanceid=instance[instanceid]["instanceid"])
                        return success_json_response({
                            "desktop_id":
                            instanceid,
                            "status":
                            "okay",
                            "message":
                            "stopping instance"
                        })
                    elif request.json["state"] == "running":
                        start_instance(
                            instanceid=instance[instanceid]["instanceid"])
                        return success_json_response({
                            "desktop_id":
                            instanceid,
                            "status":
                            "okay",
                            "message":
                            "starting instance"
                        })
        else:
            raise BadRequestException("Request should be JSON")
    else:
        raise ResourceNotFoundException(
            f"An instance with id '{instanceid}' was not found")
示例#5
0
def get_users():
    realm = request.args.get("realm")
    token = get_keycloak_token(server=SERVER,
                               client_id=CLIENTID,
                               client_secret=CLIENTSECRET)
    users = get_keycloak_users(server=SERVER,
                               realm=realm,
                               token=token,
                               briefRepresentation=True,
                               max=1000)
    return success_json_response({"data": users})
示例#6
0
def get_build_logs(id):
    cb = get_build(id)
    if cb:
        log_group = cb["logs"]["groupName"]
        log_stream = cb["logs"]["streamName"]
        logs = get_logs(log_group=log_group, stream_name=log_stream)
        logs = [l["message"] for l in logs]
        return success_json_response({
            "log_group": log_group,
            "log_stream": log_stream,
            "events": logs
        })
def create_instance(username, roles):
    # check if the request is JSON
    if request.json:
        missing = check_for_keys(
            dict=request.json,
            keys=["action", "machine_def_id", "screen_geometry"])
        if missing:
            raise BadRequestException(
                f"The following keys are missing from the request: {missing}")
        else:
            if request.json["action"] not in ["create"]:
                raise BadRequestException("Invalid action")
            if request.json["screen_geometry"] not in [
                    "1920x1080", "1280x720"
            ]:
                raise BadRequestException("Invalid screen geometry")
            # get entitlements
            entitlements = get_entitlements_for_roles(roles, username)
            selected_entitlement = None
            for entitlement in entitlements:
                if entitlement["machine_def_id"] == request.json[
                        "machine_def_id"]:
                    selected_entitlement = entitlement
            if selected_entitlement:
                if selected_entitlement[
                        "total_allowed_instances"] - selected_entitlement[
                            "current_instances"] > 0:
                    # we can provision it
                    # get machine def
                    machine_def = get_machine_def(
                        machine_def_id=selected_entitlement["machine_def_id"])
                    desktop_id = get_rand_string(8)
                    # if an override id was set then use it
                    if "desktop_id" in request.json:
                        desktop_id = request.json["desktop_id"]
                    # create task to provision
                    response = create_desktop_instance(
                        desktop_id=desktop_id,
                        ami_id=machine_def["ami_id"],
                        machine_username=username,
                        screen_geometry=request.json["screen_geometry"],
                        machine_def_id=selected_entitlement["machine_def_id"],
                        instance_type=machine_def["instance_type"],
                        user_data=machine_def["user_data"])
                    if response:
                        return success_json_response({
                            "desktop_id":
                            desktop_id,
                            "status":
                            "okay",
                            "message":
                            "created task to create instance"
                        })
                    else:
                        return success_json_response({"status": "error"})
                else:
                    raise NoAvailableCapacity(
                        "No available capacity to start this instance")
            else:
                raise ResourceNotFoundException(
                    "No matching machine_def found")
    else:
        raise BadRequestException("Request should be JSON")
def get_instances(username, roles):
    instances = get_instances_by_username(username)
    return success_json_response(instances)
def get_entitlements(username, roles):
    return success_json_response(get_entitlements_for_roles(roles, username))
def refresh_config(username, roles):
    get_groups_and_roles()
    return success_json_response({"status": "okay"})
def root(username, roles):
    return success_json_response({
        "status": "okay",
        "username": username,
        "roles": roles
    })
示例#12
0
def root(username, groups):
    return success_json_response({
        "ping": "pong",
        "username": username,
        "groups": groups
    })
示例#13
0
def get_realms():
    token = get_keycloak_token(server=SERVER,
                               client_id=CLIENTID,
                               client_secret=CLIENTSECRET)
    realms = get_keycloak_realms(server=SERVER, token=token)
    return success_json_response({"data": realms})