Exemple #1
0
def application_api_GET(request, public_host):
    try:
        application_instance = get_api_visible_application_instance_by_public_host(
            public_host)
    except ApplicationInstance.DoesNotExist:
        return JsonResponse({}, status=404)

    return JsonResponse(api_application_dict(application_instance), status=200)
Exemple #2
0
def applications_api_GET(request):
    return JsonResponse(
        {
            "applications": [
                api_application_dict(application)
                for application in ApplicationInstance.objects.filter(
                    state__in=["RUNNING", "SPAWNING"])
            ]
        },
        status=200,
    )
Exemple #3
0
def application_api_PUT(request, public_host):
    # A transaction is unnecessary: the single_running_or_spawning_integrity
    # key prevents duplicate spawning/running applications at the same
    # public host
    try:
        application_instance = get_api_visible_application_instance_by_public_host(
            public_host)
    except ApplicationInstance.DoesNotExist:
        pass
    else:
        return JsonResponse({"message": "Application instance already exists"},
                            status=409)

    try:
        (
            application_template,
            tag,
            _,
            commit_id,
        ) = application_template_tag_user_commit_from_host(public_host)
    except ApplicationTemplate.DoesNotExist:
        return JsonResponse({"message": "Application template does not exist"},
                            status=400)

    app_type = application_template.application_type

    if app_type == "TOOL":
        tool_configuration = (
            application_template.user_tool_configuration.filter(
                user=request.user).first()
            or UserToolConfiguration.default_config())
        cpu = tool_configuration.size_config.cpu
        memory = tool_configuration.size_config.memory
    else:
        cpu = application_template.default_cpu
        memory = application_template.default_memory

    spawner_options = json.dumps(application_options(application_template))

    try:
        application_instance = ApplicationInstance.objects.create(
            owner=request.user,
            application_template=application_template,
            spawner=application_template.spawner,
            spawner_application_template_options=spawner_options,
            spawner_application_instance_id=json.dumps({}),
            public_host=public_host,
            state="SPAWNING",
            single_running_or_spawning_integrity=public_host,
            cpu=cpu,
            memory=memory,
            commit_id=commit_id,
        )
    except IntegrityError:
        application_instance = get_api_visible_application_instance_by_public_host(
            public_host)
    else:
        spawn.delay(
            application_template.spawner,
            request.user.pk,
            tag,
            application_instance.id,
            spawner_options,
        )

    return JsonResponse(api_application_dict(application_instance), status=200)
Exemple #4
0
def application_api_PUT(request, public_host):
    # A transaction is unnecessary: the single_running_or_spawning_integrity
    # key prevents duplicate spawning/running applications at the same
    # public host
    try:
        application_instance = get_api_visible_application_instance_by_public_host(
            public_host)
    except ApplicationInstance.DoesNotExist:
        pass
    else:
        return JsonResponse({'message': 'Application instance already exists'},
                            status=409)

    try:
        (
            application_template,
            tag,
            _,
            commit_id,
        ) = application_template_tag_user_commit_from_host(public_host)
    except ApplicationTemplate.DoesNotExist:
        return JsonResponse({'message': 'Application template does not exist'},
                            status=400)

    app_type = application_template.application_type

    (source_tables, db_role_schema_suffix, db_user) = ((
        source_tables_for_user(request.user),
        db_role_schema_suffix_for_user(request.user),
        postgres_user(request.user.email),
    ) if app_type == 'TOOL' else (
        source_tables_for_app(application_template),
        db_role_schema_suffix_for_app(application_template),
        postgres_user(application_template.host_basename),
    ))

    credentials = new_private_database_credentials(db_role_schema_suffix,
                                                   source_tables, db_user)

    if app_type == 'TOOL':
        # For AppStream to access credentials
        write_credentials_to_bucket(request.user, credentials)

    try:
        memory, cpu = request.GET['__memory_cpu'].split('_')
    except KeyError:
        memory = None
        cpu = None

    spawner_options = json.dumps(application_options(application_template))

    try:
        application_instance = ApplicationInstance.objects.create(
            owner=request.user,
            application_template=application_template,
            spawner=application_template.spawner,
            spawner_application_template_options=spawner_options,
            spawner_application_instance_id=json.dumps({}),
            public_host=public_host,
            state='SPAWNING',
            single_running_or_spawning_integrity=public_host,
            cpu=cpu,
            memory=memory,
            commit_id=commit_id,
        )
    except IntegrityError:
        application_instance = get_api_visible_application_instance_by_public_host(
            public_host)
    else:
        # The database users are stored so when the database users are cleaned up,
        # we know _not_ to delete any users used by running or spawning apps
        for creds in credentials:
            ApplicationInstanceDbUsers.objects.create(
                application_instance=application_instance,
                db_id=creds['db_id'],
                db_username=creds['db_user'],
            )

        app_schema = f'{USER_SCHEMA_STEM}{db_role_schema_suffix}'

        spawn.delay(
            application_template.spawner,
            request.user.email,
            str(request.user.profile.sso_id),
            tag,
            application_instance.id,
            spawner_options,
            credentials,
            app_schema,
        )

    return JsonResponse(api_application_dict(application_instance), status=200)
def application_api_PUT(request, public_host):
    # A transaction is unnecessary: the single_running_or_spawning_integrity
    # key prevents duplicate spawning/running applications at the same
    # public host
    try:
        application_instance = get_api_visible_application_instance_by_public_host(
            public_host
        )
    except ApplicationInstance.DoesNotExist:
        pass
    else:
        return JsonResponse(
            {'message': 'Application instance already exists'}, status=409
        )

    try:
        (
            application_template,
            public_host_data,
        ) = application_template_and_data_from_host(public_host)
    except ApplicationTemplate.DoesNotExist:
        return JsonResponse(
            {'message': 'Application template does not exist'}, status=400
        )

    app_type = application_template.application_type
    credentials = (
        new_private_database_credentials(request.user) if app_type == 'TOOL' else []
    )

    try:
        memory, cpu = request.GET['__memory_cpu'].split('_')
    except KeyError:
        memory = None
        cpu = None

    application_instance = ApplicationInstance.objects.create(
        owner=request.user,
        application_template=application_template,
        spawner=application_template.spawner,
        spawner_application_template_options=application_template.spawner_options,
        spawner_application_instance_id=json.dumps({}),
        public_host=public_host,
        state='SPAWNING',
        single_running_or_spawning_integrity=public_host,
        cpu=cpu,
        memory=memory,
    )

    # The database users are stored so when the database users are cleaned up,
    # we know _not_ to delete any users used by running or spawning apps
    for creds in credentials:
        ApplicationInstanceDbUsers.objects.create(
            application_instance=application_instance,
            db_id=creds['db_id'],
            db_username=creds['db_user'],
        )

    spawn.delay(
        application_template.spawner,
        request.user.email,
        str(request.user.profile.sso_id),
        public_host_data,
        application_instance.id,
        application_template.spawner_options,
        credentials,
    )

    return JsonResponse(api_application_dict(application_instance), status=200)