Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix deployment handling in python api [WIP]; Fix url handling

Tomas Valenta2ab70fd0a9ec
diff --git a/api-service/.openapi-generator/FILES b/api-service/.openapi-generator/FILES
index 1c33f61d9..579a5596c 100644
--- a/api-service/.openapi-generator/FILES
+++ b/api-service/.openapi-generator/FILES
@@ -22,7 +22,6 @@ playground_client/docs/ProcessResponse.md
playground_client/docs/ReadFilesystemFileResponse.md
playground_client/docs/SessionResponse.md
playground_client/docs/StartProcessParams.md
-playground_client/docs/UpdateDeploymentRequest.md
playground_client/docs/WriteFilesystemFileRequest.md
playground_client/docs/WriteProcessStdinRequest.md
playground_client/exceptions.py
@@ -45,7 +44,6 @@ playground_client/models/process_response.py
playground_client/models/read_filesystem_file_response.py
playground_client/models/session_response.py
playground_client/models/start_process_params.py
-playground_client/models/update_deployment_request.py
playground_client/models/write_filesystem_file_request.py
playground_client/models/write_process_stdin_request.py
playground_client/rest.py
@@ -69,7 +67,6 @@ playground_client/test/test_process_response.py
playground_client/test/test_read_filesystem_file_response.py
playground_client/test/test_session_response.py
playground_client/test/test_start_process_params.py
-playground_client/test/test_update_deployment_request.py
playground_client/test/test_write_filesystem_file_request.py
playground_client/test/test_write_process_stdin_request.py
playground_client_README.md
diff --git a/api-service/app.py b/api-service/app.py
index e8ebe48e1..cd1b28efd 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -4,7 +4,7 @@ from session.deploy import Deployment
from session.env import EnvVar
-from typing import Dict, List
+from typing import List
from flask import Flask, abort, request
from flask_cors import CORS
@@ -21,19 +21,14 @@ app = Flask(__name__)
CORS(app)
-def dump(obj):
- for attr in dir(obj):
- print("obj.%s = %r" % (attr, getattr(obj, attr)))
-
-
-def replace_file_content(fp_in: str, fp_out: str, pairs: Dict[str, str]) -> None:
- with open(fp_in, "rt") as fin:
- with open(fp_out, "wt") as fout:
- for line in fin:
- for old, new in pairs.items():
- if old in line:
- line = line.replace(old, new)
- fout.write(line)
+def get_request_body_template(blocks: List[dict[str, str]]):
+ request_body_blocks = [
+ block for block in blocks if block.get("type") == "RequestBody"
+ ]
+ request_body_template = (
+ request_body_blocks[0]["prompt"] if len(request_body_blocks) > 0 else None
+ )
+ return request_body_template
@app.route("/health", methods=["GET"])
@@ -57,14 +52,7 @@ def generate():
envs: List[EnvVar] = body["envs"]
url: str | None = body["url"]
- code = ""
-
- request_body_blocks = [
- block for block in blocks if block.get("type") == "RequestBody"
- ]
- request_body_template = (
- request_body_blocks[0]["prompt"] if len(request_body_blocks) > 0 else None
- )
+ request_body_template = get_request_body_template(blocks)
db.create_deployment(run_id=run_id, project_id=project_id, route_id=route_id)
try:
@@ -87,18 +75,19 @@ def generate():
deployment = Deployment(playground.session, project_id)
if url is None:
- url = deployment.new(code=code, envs=envs)
+ url = deployment.new(envs=envs)
else:
- deployment.update(code=code, envs=envs)
+ deployment.update(envs=envs)
db.finish_deployment(run_id=run_id, url=url)
return {
- "code": code,
+ "code": "",
"prompt": "",
"url": url,
}
except:
+ db.update_state(run_id=run_id, state=DeploymentState.Error)
raise
diff --git a/api-service/playground/src/deployments/deployment.ts b/api-service/playground/src/deployments/deployment.ts
index 4d2f080fb..bb7876447 100644
--- a/api-service/playground/src/deployments/deployment.ts
+++ b/api-service/playground/src/deployments/deployment.ts
@@ -6,8 +6,10 @@ import {
CreateFunctionUrlConfigCommand,
waitUntilFunctionUpdatedV2,
AddPermissionCommand,
+ GetFunctionCommand,
+ GetFunctionUrlConfigCommand,
} from '@aws-sdk/client-lambda'
-import { APIGatewayClient } from '@aws-sdk/client-api-gateway'
+// import { APIGatewayClient } from '@aws-sdk/client-api-gateway'
import { EnvVars, Session } from '@devbookhq/sdk'
import { packageFunction } from './packaging'
@@ -31,10 +33,10 @@ const lambda = new LambdaClient({
})
// TODO: gateway.destroy() on quit?
-const gateway = new APIGatewayClient({
- region,
- credentials,
-})
+// const gateway = new APIGatewayClient({
+// region,
+// credentials,
+// })
const deploymentParams = {
Runtime: 'nodejs16.x',
@@ -56,15 +58,51 @@ async function waitForUpdate(projectID: string) {
)
}
+async function getIsFunctionDeployed(projectID: string) {
+ try {
+ await lambda.send(
+ new GetFunctionCommand(
+ { FunctionName: projectID }
+ )
+ )
+ return true
+ } catch (err: any) {
+ if (err.name === 'ResourceNotFoundException') {
+ return false
+ }
+ throw err
+ }
+}
+
+async function getFunctionURL(projectID: string) {
+ try {
+ const func = await lambda.send(
+ new GetFunctionUrlConfigCommand(
+ { FunctionName: projectID }
+ )
+ )
+ return func.FunctionUrl
+ } catch (err: any) {
+ if (err.name === 'ResourceNotFoundException') {
+ return undefined
+ }
+ throw err
+ }
+}
+
export async function createDeploymentInSession(
session: Session,
projectID: string,
- code: string,
- envVars: EnvVars
+ envVars?: EnvVars
) {
- const zip = await packageFunction(session, code)
-
- try {
+ // Start packaging function in session
+ const zipping = packageFunction(session)
+ const gettingURL = getFunctionURL(projectID)
+ const isFunctionDeployed = await getIsFunctionDeployed(projectID)
+
+ // Deploy function
+ if (!isFunctionDeployed) {
+ const zip = await zipping
await lambda
.send(
new CreateFunctionCommand({
@@ -104,44 +142,9 @@ export async function createDeploymentInSession(
// TODO: Configure the Gateway to handle custom domain wildcards
return urlResult.FunctionUrl
- } catch (err: any) {
- if (err.name === 'ResourceConflictException') {
- await lambda.send(
- new UpdateFunctionConfigurationCommand({
- ...deploymentParams,
- FunctionName: projectID,
- Environment: {
- Variables: envVars,
- },
- })
- )
- await waitForUpdate(projectID)
-
- await lambda.send(
- new UpdateFunctionCodeCommand({
- FunctionName: projectID,
- ZipFile: zip,
- })
- )
- await waitForUpdate(projectID)
-
- return
- } else {
- throw err
- }
}
-}
-
-export async function updateDeploymentInSession(
- session: Session,
- projectID: string,
- code?: string,
- envVars?: EnvVars
-) {
- const zipPromise = code
- ? packageFunction(session, code)
- : Promise.resolve(undefined)
+ // Update function
if (envVars) {
await lambda.send(
new UpdateFunctionConfigurationCommand({
@@ -155,15 +158,15 @@ export async function updateDeploymentInSession(
await waitForUpdate(projectID)
}
- const zip = await zipPromise
+ const zip = await zipping
- if (zip) {
- await lambda.send(
- new UpdateFunctionCodeCommand({
- FunctionName: projectID,
- ZipFile: zip,
- })
- )
- await waitForUpdate(projectID)
- }
+ await lambda.send(
+ new UpdateFunctionCodeCommand({
+ FunctionName: projectID,
+ ZipFile: zip,
+ })
+ )
+ await waitForUpdate(projectID)
+
+ return await gettingURL
}
diff --git a/api-service/playground/src/deployments/deploymentsController.ts b/api-service/playground/src/deployments/deploymentsController.ts
index d09081f09..f6d6ec4ce 100644
--- a/api-service/playground/src/deployments/deploymentsController.ts
+++ b/api-service/playground/src/deployments/deploymentsController.ts
@@ -9,39 +9,34 @@ import {
} from 'tsoa'
import { EnvVars } from '@devbookhq/sdk'
-import {
- createDeploymentInSession,
- updateDeploymentInSession,
-} from './deployment'
+import { createDeploymentInSession } from './deployment'
import { CachedSession } from '../sessions/session'
-
interface DeploymentResponse {
+ /**
+ * If the function is deployed for the first time the url will be defined.
+ */
url?: string
}
-
@Route('deployments')
export class DeploymentsController extends Controller {
/**
*
* @param projectID
* @param sessionID active session to use for deployment
- * @param code
* @param envVars
*/
@Put('{projectID}')
public async createDeployment(
@Path() projectID: string,
@Query() sessionID: string,
- @BodyProp() code: string,
- @BodyProp() envVars: EnvVars = {},
+ @BodyProp() envVars?: EnvVars,
): Promise<DeploymentResponse> {
const cachedSession = CachedSession.findSession(sessionID)
const url = await createDeploymentInSession(
cachedSession.session,
projectID,
- code,
envVars,
)
@@ -49,27 +44,4 @@ export class DeploymentsController extends Controller {
url,
}
}
-
- /**
- *
- * @param projectID
- * @param sessionID active session to use for deployment
- * @param code
- * @param envVars
- */
- @Patch('{projectID}')
- public async updateDeployment(
- @Path() projectID: string,
- @Query() sessionID: string,
- @BodyProp() envVars?: EnvVars,
- @BodyProp() code?: string,
- ): Promise<void> {
- const cachedSession = CachedSession.findSession(sessionID)
- await updateDeploymentInSession(
- cachedSession.session,
- projectID,
- code,
- envVars,
- )
- }
}
diff --git a/api-service/playground/src/deployments/packaging.ts b/api-service/playground/src/deployments/packaging.ts
index b7987a11f..89dd9c871 100644
--- a/api-service/playground/src/deployments/packaging.ts
+++ b/api-service/playground/src/deployments/packaging.ts
@@ -1,4 +1,5 @@
import {
+ FilesystemManager,
OutStderrResponse,
Session,
createSessionProcess
@@ -7,13 +8,45 @@ import path from 'path'
const rootdir = '/code'
+const bundleDir = '/bundles'
-const functionsDir = 'funcs'
-const bundleDir = 'bundles'
+const mjsRootPath = path.join(rootdir, 'index.mjs')
+const jsRootPath = path.join(rootdir, 'index.js')
+const bundlePath = path.join(bundleDir, 'index.zip')
+const dumpPath = path.join(bundleDir, 'base')
+
+const possibleRootfiles = [
+ jsRootPath,
+ mjsRootPath,
+]
+
+async function getRootFile(filesystem: FilesystemManager) {
+ const rootResults = await Promise.allSettled(possibleRootfiles.map(async r => ({
+ path: r,
+ content: await filesystem.read(r),
+ })))
+
+ for (const root of rootResults) {
+ if (root.status === 'fulfilled') {
+ return root.value
+ }
+ }
+}
+
+function addAWSLambdaHandlers(code: string) {
+ const isECMA = !code.includes('require')
+
+ if (isECMA) {
+ code = 'import serverless from "serverless-http;"\n' + code
+ code = code + "\nexport const handler = serverless(app);"
+ } else {
+ code = "const serverless = require('serverless-http');\n" + code
+ code = code + "\nexports.handler = serverless(app);"
+ }
+
+ return code.replace("app.listen(", "; ({})?.listen?.(")
+}
-const rootfilePath = path.join(rootdir, functionsDir, 'index.mjs')
-const bundlePath = path.join(rootdir, bundleDir, 'index.zip')
-const dumpPath = path.join(rootdir, bundleDir, 'base')
/**
*
@@ -22,17 +55,24 @@ const dumpPath = path.join(rootdir, bundleDir, 'base')
* TODO: Implement binary file reading in devbookd so we don't have to send data via base64 (cca 25% overhead) and process stdout.
*
*/
-export async function packageFunction(session: Session, code: string) {
+export async function packageFunction(session: Session) {
if (!session.filesystem || !session.process) {
throw new Error('Session is not active')
}
const stderr: OutStderrResponse[] = []
- await session.filesystem?.write(rootfilePath, code)
+ const rootFile = await getRootFile(session.filesystem)
+ if (!rootFile) {
+ throw new Error(`Cannot find rootfile matching ${possibleRootfiles.join(', ')}`)
+ }
+
+ const rootFileWithLambdaHandlers = addAWSLambdaHandlers(rootFile.content)
+
+ await session.filesystem.write(rootFile.path, rootFileWithLambdaHandlers)
// Bundle the code and dependencies via https://github.com/netlify/zip-it-and-ship-it and dump the base64 string representationto a separate file.
const bundling = await createSessionProcess({
manager: session.process,
- cmd: `zip-it-and-ship-it ${functionsDir} ${bundleDir} && base64 -w 0 ${bundlePath} > ${dumpPath}`,
+ cmd: `zip-it-and-ship-it ${rootdir} ${bundleDir} && base64 -w 0 ${bundlePath} > ${dumpPath}`,
rootdir,
onStderr: o => stderr.push(o),
})
diff --git a/api-service/playground/src/generated/routes.ts b/api-service/playground/src/generated/routes.ts
index e91e26726..67766fd15 100644
--- a/api-service/playground/src/generated/routes.ts
+++ b/api-service/playground/src/generated/routes.ts
@@ -188,39 +188,10 @@ export function RegisterRoutes(app: Router) {
...(fetchMiddlewares<RequestHandler>(DeploymentsController.prototype.createDeployment)),
function DeploymentsController_createDeployment(request: any, response: any, next: any) {
- const args = {
- projectID: {"in":"path","name":"projectID","required":true,"dataType":"string"},
- sessionID: {"in":"query","name":"sessionID","required":true,"dataType":"string"},
- code: {"in":"body-prop","name":"code","required":true,"dataType":"string"},
- envVars: {"default":{},"in":"body-prop","name":"envVars","ref":"EnvVars"},
- };
-
- // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
-
- let validatedArgs: any[] = [];
- try {
- validatedArgs = getValidatedArgs(args, request, response);
-
- const controller = new DeploymentsController();
-
-
- const promise = controller.createDeployment.apply(controller, validatedArgs as any);
- promiseHandler(controller, promise, response, undefined, next);
- } catch (err) {
- return next(err);
- }
- });
- // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
- app.patch('/deployments/:projectID',
- ...(fetchMiddlewares<RequestHandler>(DeploymentsController)),
- ...(fetchMiddlewares<RequestHandler>(DeploymentsController.prototype.updateDeployment)),
-
- function DeploymentsController_updateDeployment(request: any, response: any, next: any) {
const args = {
projectID: {"in":"path","name":"projectID","required":true,"dataType":"string"},
sessionID: {"in":"query","name":"sessionID","required":true,"dataType":"string"},
envVars: {"in":"body-prop","name":"envVars","ref":"EnvVars"},
- code: {"in":"body-prop","name":"code","dataType":"string"},
};
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
@@ -232,7 +203,7 @@ export function RegisterRoutes(app: Router) {
const controller = new DeploymentsController();
- const promise = controller.updateDeployment.apply(controller, validatedArgs as any);
+ const promise = controller.createDeployment.apply(controller, validatedArgs as any);
promiseHandler(controller, promise, response, undefined, next);
} catch (err) {
return next(err);
diff --git a/api-service/playground/swagger.json b/api-service/playground/swagger.json
index 010b14121..6676f395d 100644
--- a/api-service/playground/swagger.json
+++ b/api-service/playground/swagger.json
@@ -37,7 +37,8 @@
"DeploymentResponse": {
"properties": {
"url": {
- "type": "string"
+ "type": "string",
+ "description": "If the function is deployed for the first time the url will be defined."
}
},
"type": "object",
@@ -334,55 +335,6 @@
}
}
],
- "requestBody": {
- "required": true,
- "content": {
- "application/json": {
- "schema": {
- "properties": {
- "code": {
- "type": "string"
- },
- "envVars": {
- "$ref": "#/components/schemas/EnvVars"
- }
- },
- "required": [
- "code"
- ],
- "type": "object"
- }
- }
- }
- }
- },
- "patch": {
- "operationId": "UpdateDeployment",
- "responses": {
- "204": {
- "description": "No content"
- }
- },
- "security": [],
- "parameters": [
- {
- "in": "path",
- "name": "projectID",
- "required": true,
- "schema": {
- "type": "string"
- }
- },
- {
- "description": "active session to use for deployment",
- "in": "query",
- "name": "sessionID",
- "required": true,
- "schema": {
- "type": "string"
- }
- }
- ],
"requestBody": {
"required": true,
"content": {
@@ -391,9 +343,6 @@
"properties": {
"envVars": {
"$ref": "#/components/schemas/EnvVars"
- },
- "code": {
- "type": "string"
}
},
"type": "object"
diff --git a/api-service/playground_client/__init__.py b/api-service/playground_client/__init__.py
index eec70b268..69fc080c1 100644
--- a/api-service/playground_client/__init__.py
+++ b/api-service/playground_client/__init__.py
@@ -47,6 +47,5 @@ from playground_client.models.process_response import ProcessResponse
from playground_client.models.read_filesystem_file_response import ReadFilesystemFileResponse
from playground_client.models.session_response import SessionResponse
from playground_client.models.start_process_params import StartProcessParams
-from playground_client.models.update_deployment_request import UpdateDeploymentRequest
from playground_client.models.write_filesystem_file_request import WriteFilesystemFileRequest
from playground_client.models.write_process_stdin_request import WriteProcessStdinRequest
diff --git a/api-service/playground_client/api/default_api.py b/api-service/playground_client/api/default_api.py
index b841d3674..b4fd0a4f7 100644
--- a/api-service/playground_client/api/default_api.py
+++ b/api-service/playground_client/api/default_api.py
@@ -31,7 +31,6 @@ from playground_client.models.process_response import ProcessResponse
from playground_client.models.read_filesystem_file_response import ReadFilesystemFileResponse
from playground_client.models.session_response import SessionResponse
from playground_client.models.start_process_params import StartProcessParams
-from playground_client.models.update_deployment_request import UpdateDeploymentRequest
from playground_client.models.write_filesystem_file_request import WriteFilesystemFileRequest
from playground_client.models.write_process_stdin_request import WriteProcessStdinRequest
@@ -1840,162 +1839,6 @@ class DefaultApi(object):
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))
- @validate_arguments
- def update_deployment(self, project_id : StrictStr, session_id : Annotated[StrictStr, Field(..., description="active session to use for deployment")], update_deployment_request : UpdateDeploymentRequest, **kwargs) -> None: # noqa: E501
- """update_deployment # noqa: E501
-
- This method makes a synchronous HTTP request by default. To make an
- asynchronous HTTP request, please pass async_req=True
-
- >>> thread = api.update_deployment(project_id, session_id, update_deployment_request, async_req=True)
- >>> result = thread.get()
-
- :param project_id: (required)
- :type project_id: str
- :param session_id: active session to use for deployment (required)
- :type session_id: str
- :param update_deployment_request: (required)
- :type update_deployment_request: UpdateDeploymentRequest
- :param async_req: Whether to execute the request asynchronously.
- :type async_req: bool, optional
- :param _preload_content: if False, the urllib3.HTTPResponse object will
- be returned without reading/decoding response
- data. Default is True.
- :type _preload_content: bool, optional
- :param _request_timeout: timeout setting for this request. If one
- number provided, it will be total request
- timeout. It can also be a pair (tuple) of
- (connection, read) timeouts.
- :return: Returns the result object.
- If the method is called asynchronously,
- returns the request thread.
- :rtype: None
- """
- kwargs['_return_http_data_only'] = True
- return self.update_deployment_with_http_info(project_id, session_id, update_deployment_request, **kwargs) # noqa: E501
-
- @validate_arguments
- def update_deployment_with_http_info(self, project_id : StrictStr, session_id : Annotated[StrictStr, Field(..., description="active session to use for deployment")], update_deployment_request : UpdateDeploymentRequest, **kwargs): # noqa: E501
- """update_deployment # noqa: E501
-
- This method makes a synchronous HTTP request by default. To make an
- asynchronous HTTP request, please pass async_req=True
-
- >>> thread = api.update_deployment_with_http_info(project_id, session_id, update_deployment_request, async_req=True)
- >>> result = thread.get()
-
- :param project_id: (required)
- :type project_id: str
- :param session_id: active session to use for deployment (required)
- :type session_id: str
- :param update_deployment_request: (required)
- :type update_deployment_request: UpdateDeploymentRequest
- :param async_req: Whether to execute the request asynchronously.
- :type async_req: bool, optional
- :param _return_http_data_only: response data without head status code
- and headers
- :type _return_http_data_only: bool, optional
- :param _preload_content: if False, the urllib3.HTTPResponse object will
- be returned without reading/decoding response
- data. Default is True.
- :type _preload_content: bool, optional
- :param _request_timeout: timeout setting for this request. If one
- number provided, it will be total request
- timeout. It can also be a pair (tuple) of
- (connection, read) timeouts.
- :param _request_auth: set to override the auth_settings for an a single
- request; this effectively ignores the authentication
- in the spec for a single request.
- :type _request_auth: dict, optional
- :type _content_type: string, optional: force content-type for the request
- :return: Returns the result object.
- If the method is called asynchronously,
- returns the request thread.
- :rtype: None
- """
-
- _params = locals()
-
- _all_params = [
- 'project_id',
- 'session_id',
- 'update_deployment_request'
- ]
- _all_params.extend(
- [
- 'async_req',
- '_return_http_data_only',
- '_preload_content',
- '_request_timeout',
- '_request_auth',
- '_content_type',
- '_headers'
- ]
- )
-
- # validate the arguments
- for _key, _val in _params['kwargs'].items():
- if _key not in _all_params:
- raise ApiTypeError(
- "Got an unexpected keyword argument '%s'"
- " to method update_deployment" % _key
- )
- _params[_key] = _val
- del _params['kwargs']
-
- _collection_formats = {}
-
- # process the path parameters
- _path_params = {}
- if _params['project_id']:
- _path_params['projectID'] = _params['project_id']
-
- # process the query parameters
- _query_params = []
- if _params.get('session_id') is not None: # noqa: E501
- _query_params.append(('sessionID', _params['session_id']))
-
- # process the header parameters
- _header_params = dict(_params.get('_headers', {}))
-
- # process the form parameters
- _form_params = []
- _files = {}
-
- # process the body parameter
- _body_params = None
- if _params['update_deployment_request']:
- _body_params = _params['update_deployment_request']
-
- # set the HTTP header `Content-Type`
- _content_types_list = _params.get('_content_type',
- self.api_client.select_header_content_type(
- ['application/json']))
- if _content_types_list:
- _header_params['Content-Type'] = _content_types_list
-
- # authentication setting
- _auth_settings = [] # noqa: E501
-
- _response_types_map = {}
-
- return self.api_client.call_api(
- '/deployments/{projectID}', 'PATCH',
- _path_params,
- _query_params,
- _header_params,
- body=_body_params,
- post_params=_form_params,
- files=_files,
- response_types_map=_response_types_map,
- auth_settings=_auth_settings,
- async_req=_params.get('async_req'),
- _return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
- _preload_content=_params.get('_preload_content', True),
- _request_timeout=_params.get('_request_timeout'),
- collection_formats=_collection_formats,
- _request_auth=_params.get('_request_auth'))
-
@validate_arguments
def write_filesystem_file(self, session_id : StrictStr, path : StrictStr, write_filesystem_file_request : WriteFilesystemFileRequest, **kwargs) -> None: # noqa: E501
"""write_filesystem_file # noqa: E501
diff --git a/api-service/playground_client/docs/CreateDeploymentRequest.md b/api-service/playground_client/docs/CreateDeploymentRequest.md
index e041a92b6..6f25973dd 100644
--- a/api-service/playground_client/docs/CreateDeploymentRequest.md
+++ b/api-service/playground_client/docs/CreateDeploymentRequest.md
@@ -4,7 +4,6 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
-**code** | **str** | |
**env_vars** | **Dict[str, str]** | | [optional]
## Example
diff --git a/api-service/playground_client/docs/DefaultApi.md b/api-service/playground_client/docs/DefaultApi.md
index c32fe8e94..033606ae7 100644
--- a/api-service/playground_client/docs/DefaultApi.md
+++ b/api-service/playground_client/docs/DefaultApi.md
@@ -16,7 +16,6 @@ Method | HTTP request | Description
[**read_filesystem_file**](DefaultApi.md#read_filesystem_file) | **GET** /sessions/{sessionID}/filesystem/file |
[**start_process**](DefaultApi.md#start_process) | **POST** /sessions/{sessionID}/processes |
[**stop_process**](DefaultApi.md#stop_process) | **DELETE** /sessions/{sessionID}/processes/{processID} |
-[**update_deployment**](DefaultApi.md#update_deployment) | **PATCH** /deployments/{projectID} |
[**write_filesystem_file**](DefaultApi.md#write_filesystem_file) | **PUT** /sessions/{sessionID}/filesystem/file |
[**write_process_stdin**](DefaultApi.md#write_process_stdin) | **POST** /sessions/{sessionID}/processes/{processID}/stdin |
@@ -771,69 +770,6 @@ No authorization required
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
-# **update_deployment**
-> update_deployment(project_id, session_id, update_deployment_request)
-
-
-
-### Example
-
-```python
-from __future__ import print_function
-import time
-import os
-import playground_client
-from playground_client.rest import ApiException
-from pprint import pprint
-# Defining the host is optional and defaults to https://localhost:9001
-# See configuration.py for a list of all supported configuration parameters.
-configuration = playground_client.Configuration(
- host = "https://localhost:9001"
-)
-
-
-# Enter a context with an instance of the API client
-with playground_client.ApiClient(configuration) as api_client:
- # Create an instance of the API class
- api_instance = playground_client.DefaultApi(api_client)
- project_id = 'project_id_example' # str |
- session_id = 'session_id_example' # str | active session to use for deployment
- update_deployment_request = playground_client.UpdateDeploymentRequest() # UpdateDeploymentRequest |
-
- try:
- api_instance.update_deployment(project_id, session_id, update_deployment_request)
- except Exception as e:
- print("Exception when calling DefaultApi->update_deployment: %s\n" % e)
-```
-
-### Parameters
-
-Name | Type | Description | Notes
-------------- | ------------- | ------------- | -------------
- **project_id** | **str**| |
- **session_id** | **str**| active session to use for deployment |
- **update_deployment_request** | [**UpdateDeploymentRequest**](UpdateDeploymentRequest.md)| |
-
-### Return type
-
-void (empty response body)
-
-### Authorization
-
-No authorization required
-
-### HTTP request headers
-
- - **Content-Type**: application/json
- - **Accept**: Not defined
-
-### HTTP response details
-| Status code | Description | Response headers |
-|-------------|-------------|------------------|
-**204** | No content | - |
-
-[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
-
# **write_filesystem_file**
> write_filesystem_file(session_id, path, write_filesystem_file_request)
diff --git a/api-service/playground_client/docs/DeploymentResponse.md b/api-service/playground_client/docs/DeploymentResponse.md
index 316e476e9..e0d1c706e 100644
--- a/api-service/playground_client/docs/DeploymentResponse.md
+++ b/api-service/playground_client/docs/DeploymentResponse.md
@@ -4,7 +4,7 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
-**url** | **str** | | [optional]
+**url** | **str** | If the function is deployed for the first time the url will be defined. | [optional]
## Example
diff --git a/api-service/playground_client/docs/UpdateDeploymentRequest.md b/api-service/playground_client/docs/UpdateDeploymentRequest.md
deleted file mode 100644
index dd5213daf..000000000
--- a/api-service/playground_client/docs/UpdateDeploymentRequest.md
+++ /dev/null
@@ -1,29 +0,0 @@
-# UpdateDeploymentRequest
-
-
-## Properties
-Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
-**env_vars** | **Dict[str, str]** | | [optional]
-**code** | **str** | | [optional]
-
-## Example
-
-```python
-from playground_client.models.update_deployment_request import UpdateDeploymentRequest
-
-# TODO update the JSON string below
-json = "{}"
-# create an instance of UpdateDeploymentRequest from a JSON string
-update_deployment_request_instance = UpdateDeploymentRequest.from_json(json)
-# print the JSON string representation of the object
-print UpdateDeploymentRequest.to_json()
-
-# convert the object into a dict
-update_deployment_request_dict = update_deployment_request_instance.to_dict()
-# create an instance of UpdateDeploymentRequest from a dict
-update_deployment_request_form_dict = update_deployment_request.from_dict(update_deployment_request_dict)
-```
-[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
-
-
diff --git a/api-service/playground_client/models/__init__.py b/api-service/playground_client/models/__init__.py
index 24196c795..748e4ca28 100644
--- a/api-service/playground_client/models/__init__.py
+++ b/api-service/playground_client/models/__init__.py
@@ -32,6 +32,5 @@ from playground_client.models.process_response import ProcessResponse
from playground_client.models.read_filesystem_file_response import ReadFilesystemFileResponse
from playground_client.models.session_response import SessionResponse
from playground_client.models.start_process_params import StartProcessParams
-from playground_client.models.update_deployment_request import UpdateDeploymentRequest
from playground_client.models.write_filesystem_file_request import WriteFilesystemFileRequest
from playground_client.models.write_process_stdin_request import WriteProcessStdinRequest
diff --git a/api-service/playground_client/models/create_deployment_request.py b/api-service/playground_client/models/create_deployment_request.py
index 6d09af60b..85fa561de 100644
--- a/api-service/playground_client/models/create_deployment_request.py
+++ b/api-service/playground_client/models/create_deployment_request.py
@@ -26,9 +26,8 @@ class CreateDeploymentRequest(BaseModel):
Do not edit the class manually.
"""
- code: StrictStr = ...
env_vars: Optional[Dict[str, StrictStr]] = Field(None, alias="envVars")
- __properties = ["code", "envVars"]
+ __properties = ["envVars"]
class Config:
allow_population_by_field_name = True
@@ -70,7 +69,6 @@ class CreateDeploymentRequest(BaseModel):
raise ValueError("Error due to additional fields (not defined in CreateDeploymentRequest) in the input: " + obj)
_obj = CreateDeploymentRequest.parse_obj({
- "code": obj.get("code"),
"env_vars": obj.get("envVars")
})
return _obj
diff --git a/api-service/playground_client/models/deployment_response.py b/api-service/playground_client/models/deployment_response.py
index b662130ec..a50c0c656 100644
--- a/api-service/playground_client/models/deployment_response.py
+++ b/api-service/playground_client/models/deployment_response.py
@@ -18,7 +18,7 @@ import json
from typing import Optional
-from pydantic import BaseModel, StrictStr
+from pydantic import BaseModel, Field, StrictStr
class DeploymentResponse(BaseModel):
"""NOTE: This class is auto generated by OpenAPI Generator.
@@ -26,7 +26,7 @@ class DeploymentResponse(BaseModel):
Do not edit the class manually.
"""
- url: Optional[StrictStr] = None
+ url: Optional[StrictStr] = Field(None, description="If the function is deployed for the first time the url will be defined.")
__properties = ["url"]
class Config:
diff --git a/api-service/playground_client/models/update_deployment_request.py b/api-service/playground_client/models/update_deployment_request.py
deleted file mode 100644
index cf9f028cf..000000000
--- a/api-service/playground_client/models/update_deployment_request.py
+++ /dev/null
@@ -1,77 +0,0 @@
-# coding: utf-8
-
-"""
- playground
-
- No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501
-
- The version of the OpenAPI document: 1.0.0
- Generated by: https://openapi-generator.tech
-"""
-
-
-from __future__ import annotations
-from inspect import getfullargspec
-import pprint
-import re # noqa: F401
-import json
-
-
-from typing import Dict, Optional
-from pydantic import BaseModel, Field, StrictStr
-
-class UpdateDeploymentRequest(BaseModel):
- """NOTE: This class is auto generated by OpenAPI Generator.
- Ref: https://openapi-generator.tech
-
- Do not edit the class manually.
- """
- env_vars: Optional[Dict[str, StrictStr]] = Field(None, alias="envVars")
- code: Optional[StrictStr] = None
- __properties = ["envVars", "code"]
-
- class Config:
- allow_population_by_field_name = True
- validate_assignment = True
-
- def to_str(self) -> str:
- """Returns the string representation of the model using alias"""
- return pprint.pformat(self.dict(by_alias=True))
-
- def to_json(self) -> str:
- """Returns the JSON representation of the model using alias"""
- return json.dumps(self.to_dict())
-
- @classmethod
- def from_json(cls, json_str: str) -> UpdateDeploymentRequest:
- """Create an instance of UpdateDeploymentRequest from a JSON string"""
- return cls.from_dict(json.loads(json_str))
-
- def to_dict(self):
- """Returns the dictionary representation of the model using alias"""
- _dict = self.dict(by_alias=True,
- exclude={
- },
- exclude_none=True)
- return _dict
-
- @classmethod
- def from_dict(cls, obj: dict) -> UpdateDeploymentRequest:
- """Create an instance of UpdateDeploymentRequest from a dict"""
- if obj is None:
- return None
-
- if type(obj) is not dict:
- return UpdateDeploymentRequest.parse_obj(obj)
-
- # raise errors for additional fields in the input
- for _key in obj.keys():
- if _key not in cls.__properties:
- raise ValueError("Error due to additional fields (not defined in UpdateDeploymentRequest) in the input: " + obj)
-
- _obj = UpdateDeploymentRequest.parse_obj({
- "env_vars": obj.get("envVars"),
- "code": obj.get("code")
- })
- return _obj
-
diff --git a/api-service/playground_client/test/test_create_deployment_request.py b/api-service/playground_client/test/test_create_deployment_request.py
index 0cbec0abe..a769a2a7a 100644
--- a/api-service/playground_client/test/test_create_deployment_request.py
+++ b/api-service/playground_client/test/test_create_deployment_request.py
@@ -38,14 +38,12 @@ class TestCreateDeploymentRequest(unittest.TestCase):
model = playground_client.models.create_deployment_request.CreateDeploymentRequest() # noqa: E501
if include_optional :
return CreateDeploymentRequest(
- code = '',
env_vars = {
'key' : ''
}
)
else :
return CreateDeploymentRequest(
- code = '',
)
"""
diff --git a/api-service/playground_client/test/test_default_api.py b/api-service/playground_client/test/test_default_api.py
index 44b442386..1cf5e5a1f 100644
--- a/api-service/playground_client/test/test_default_api.py
+++ b/api-service/playground_client/test/test_default_api.py
@@ -100,12 +100,6 @@ class TestDefaultApi(unittest.TestCase):
"""
pass
- def test_update_deployment(self):
- """Test case for update_deployment
-
- """
- pass
-
def test_write_filesystem_file(self):
"""Test case for write_filesystem_file
diff --git a/api-service/playground_client/test/test_update_deployment_request.py b/api-service/playground_client/test/test_update_deployment_request.py
deleted file mode 100644
index 602ab5079..000000000
--- a/api-service/playground_client/test/test_update_deployment_request.py
+++ /dev/null
@@ -1,57 +0,0 @@
-# coding: utf-8
-
-"""
- playground
-
- No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501
-
- The version of the OpenAPI document: 1.0.0
- Generated by: https://openapi-generator.tech
-"""
-
-
-from __future__ import absolute_import
-
-import unittest
-import datetime
-
-import playground_client
-from playground_client.models.update_deployment_request import UpdateDeploymentRequest # noqa: E501
-from playground_client.rest import ApiException
-
-class TestUpdateDeploymentRequest(unittest.TestCase):
- """UpdateDeploymentRequest unit test stubs"""
-
- def setUp(self):
- pass
-
- def tearDown(self):
- pass
-
- def make_instance(self, include_optional):
- """Test UpdateDeploymentRequest
- include_option is a boolean, when False only required
- params are included, when True both required and
- optional params are included """
- # uncomment below to create an instance of `UpdateDeploymentRequest`
- """
- model = playground_client.models.update_deployment_request.UpdateDeploymentRequest() # noqa: E501
- if include_optional :
- return UpdateDeploymentRequest(
- env_vars = {
- 'key' : ''
- },
- code = ''
- )
- else :
- return UpdateDeploymentRequest(
- )
- """
-
- def testUpdateDeploymentRequest(self):
- """Test UpdateDeploymentRequest"""
- # inst_req_only = self.make_instance(include_optional=False)
- # inst_req_and_optional = self.make_instance(include_optional=True)
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/api-service/playground_client_README.md b/api-service/playground_client_README.md
index 5ff95fe8a..4f2e745d2 100644
--- a/api-service/playground_client_README.md
+++ b/api-service/playground_client_README.md
@@ -78,7 +78,6 @@ Class | Method | HTTP request | Description
*DefaultApi* | [**read_filesystem_file**](playground_client/docs/DefaultApi.md#read_filesystem_file) | **GET** /sessions/{sessionID}/filesystem/file |
*DefaultApi* | [**start_process**](playground_client/docs/DefaultApi.md#start_process) | **POST** /sessions/{sessionID}/processes |
*DefaultApi* | [**stop_process**](playground_client/docs/DefaultApi.md#stop_process) | **DELETE** /sessions/{sessionID}/processes/{processID} |
-*DefaultApi* | [**update_deployment**](playground_client/docs/DefaultApi.md#update_deployment) | **PATCH** /deployments/{projectID} |
*DefaultApi* | [**write_filesystem_file**](playground_client/docs/DefaultApi.md#write_filesystem_file) | **PUT** /sessions/{sessionID}/filesystem/file |
*DefaultApi* | [**write_process_stdin**](playground_client/docs/DefaultApi.md#write_process_stdin) | **POST** /sessions/{sessionID}/processes/{processID}/stdin |
@@ -103,7 +102,6 @@ Class | Method | HTTP request | Description
- [ReadFilesystemFileResponse](playground_client/docs/ReadFilesystemFileResponse.md)
- [SessionResponse](playground_client/docs/SessionResponse.md)
- [StartProcessParams](playground_client/docs/StartProcessParams.md)
- - [UpdateDeploymentRequest](playground_client/docs/UpdateDeploymentRequest.md)
- [WriteFilesystemFileRequest](playground_client/docs/WriteFilesystemFileRequest.md)
- [WriteProcessStdinRequest](playground_client/docs/WriteProcessStdinRequest.md)
diff --git a/api-service/session/api.py b/api-service/session/api.py
deleted file mode 100644
index b4cde39ec..000000000
--- a/api-service/session/api.py
+++ /dev/null
@@ -1,20 +0,0 @@
-import playground_client
-
-
-configuration = playground_client.Configuration(
- host="http://localhost:9001",
-)
-
-# Configuration for local server
-configuration.verify_ssl = False
-configuration.ssl_ca_cert = None
-configuration.cert_file = None
-
-
-class API:
- def __init__(self):
- self.client = playground_client.ApiClient(configuration)
- self.api = playground_client.DefaultApi(self.client)
-
- def __del__(self):
- self.client.close()
diff --git a/api-service/session/deploy.py b/api-service/session/deploy.py
deleted file mode 100644
index d1bc6f2b4..000000000
--- a/api-service/session/deploy.py
+++ /dev/null
@@ -1,47 +0,0 @@
-from typing import List
-
-import playground_client
-
-from session.api import API
-from session.env import EnvVar, format_env_vars
-from session.session import Session
-
-
-class Deployment(API):
- def __init__(self, session: Session, project_id: str):
- super().__init__()
- self.session = session
- self.project_id = project_id
-
- @staticmethod
- def modify_code(code: str, ecma_script=False):
- # TODO: Make these edits via ChatGPT
-
- if ecma_script:
- code = 'import serverless from "serverless-http;"\n' + code
- code = code + "\nexport const handler = serverless(app);"
- else:
- code = "const serverless = require('serverless-http');\n" + code
- code = code + "\nexports.handler = serverless(app);"
-
- return code.replace("app.listen(", "; ({})?.listen?.(")
-
- def update(self, code: str | None = None, envs: List[EnvVar] | None = None):
- self.api.update_deployment(
- self.project_id,
- session_id=self.session.id,
- update_deployment_request=playground_client.UpdateDeploymentRequest(
- envVars=format_env_vars(envs) if envs is not None else None,
- code=Deployment.modify_code(code) if code is not None else None,
- ),
- )
-
- def new(self, code: str, envs: List[EnvVar]):
- self.api.create_deployment(
- self.project_id,
- session_id=self.session.id,
- create_deployment_request=playground_client.CreateDeploymentRequest(
- envVars=format_env_vars(envs),
- code=Deployment.modify_code(code),
- ),
- )
diff --git a/api-service/session/playground/base.py b/api-service/session/playground/base.py
index 0150ea897..ab8c66304 100644
--- a/api-service/session/playground/base.py
+++ b/api-service/session/playground/base.py
@@ -2,16 +2,14 @@ import time
import math
from session.session import Session
-from session.api import API
-
-import playground_client
from playground_client.models.create_mock_body_data_request import (
CreateMockBodyDataRequest,
)
+import playground_client
from playground_client.models.file import File
-class Playground(API):
+class Playground(Session):
port_check_interval = 1 # 1s
max_port_checks = 10
@@ -20,18 +18,11 @@ class Playground(API):
mock_data_filename = "index.ts"
def __init__(self, env_id: str):
- super().__init__()
- self.session = Session(env_id)
+ super().__init__(env_id)
self.is_closed = False
- def __del__(self):
- super().__del__()
-
- def close(self):
- self.session.close()
-
def get_open_ports(self):
- response = self.api.get_session(self.session.id)
+ response = self.api.get_session(self.id)
return response.ports
def is_port_open(self, port: float):
@@ -49,7 +40,7 @@ class Playground(API):
timeout: float | None = None,
):
response = self.api.start_process(
- self.session.id,
+ self.id,
playground_client.StartProcessParams(
cmd=cmd,
envVars=env_vars,
@@ -64,7 +55,7 @@ class Playground(API):
# Multiply the timeout by the frequency so when we sleep for the 1/frequency the total time sleeping will be the timeout.
for _ in range(math.ceil(timeout * self.run_command_timeout_frequency)):
response = self.api.get_process(
- self.session.id,
+ self.id,
process_id=response.process_id,
)
if response.finished:
@@ -73,7 +64,7 @@ class Playground(API):
if not response.finished:
self.api.stop_process(
- self.session.id,
+ self.id,
process_id=response.process_id,
results=False,
)
@@ -88,7 +79,7 @@ class Playground(API):
):
"""Start process and return the process ID."""
return self.api.start_process(
- self.session.id,
+ self.id,
playground_client.StartProcessParams(
cmd=cmd,
envVars=env_vars,
@@ -98,19 +89,19 @@ class Playground(API):
def stop_process(self, process_id: str):
return self.api.stop_process(
- self.session.id,
+ self.id,
process_id=process_id,
results=True,
)
def read_file(self, path: str):
- return self.api.read_filesystem_file(self.session.id, path).content
+ return self.api.read_filesystem_file(self.id, path).content
def list_dir(self, path: str):
- return self.api.list_filesystem_dir(self.session.id, path).entries
+ return self.api.list_filesystem_dir(self.id, path).entries
def delete_file(self, path: str):
- self.api.delete_filesystem_entry(self.session.id, path)
+ self.api.delete_filesystem_entry(self.id, path)
def mock_body_data(self, code: str, interface: str):
return self.api.create_mock_body_data(
@@ -121,17 +112,17 @@ class Playground(API):
).body_data
def delete_dir(self, path: str):
- self.api.delete_filesystem_entry(self.session.id, path)
+ self.api.delete_filesystem_entry(self.id, path)
def write_file(self, path: str, content: str):
self.api.write_filesystem_file(
- self.session.id,
+ self.id,
path,
playground_client.WriteFilesystemFileRequest(content=content),
)
def make_dir(self, path: str):
- self.api.make_filesystem_dir(self.session.id, path)
+ self.api.make_filesystem_dir(self.id, path)
def run_server_with_request(
self,
diff --git a/api-service/session/session.py b/api-service/session/session.py
index b535fdf41..732082a2d 100644
--- a/api-service/session/session.py
+++ b/api-service/session/session.py
@@ -1,11 +1,25 @@
+from typing import List
+
import playground_client
-from session.api import API
+from session.env import EnvVar, format_env_vars
+
+
+configuration = playground_client.Configuration(
+ host="http://localhost:9001",
+)
+# Configuration for local server
+configuration.verify_ssl = False
+configuration.ssl_ca_cert = None
+configuration.cert_file = None
-class Session(API):
+
+class Session:
def __init__(self, env_id: str):
- super().__init__()
+ self.client = playground_client.ApiClient(configuration)
+ self.api = playground_client.DefaultApi(self.client)
+
result = self.api.create_sessions(
playground_client.CreateSessionsRequest(envID=env_id)
)
@@ -14,9 +28,18 @@ class Session(API):
def __del__(self):
self.close()
- super().__del__()
def close(self):
if not self.is_closed:
self.api.delete_session(self.id)
self.is_closed = True
+ self.client.close()
+
+ def deploy(self, project_id: str, envs: List[EnvVar]):
+ return self.api.create_deployment(
+ project_id=project_id,
+ session_id=self.id,
+ create_deployment_request=playground_client.CreateDeploymentRequest(
+ envVars=format_env_vars(envs),
+ ),
+ ).url