From 44c1154a428f75354ffdb5987b4c388931bb54b7 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 17 Feb 2022 20:49:18 +0400 Subject: [PATCH 01/28] feat: retry execution logic --- src/Executor/Executor.php | 55 ++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 26d9773f66..b033e3f0c9 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -35,15 +35,14 @@ class Executor string $deploymentId, string $projectId, string $source, - array $vars, string $runtime, string $baseImage, - array $commands + array $vars = [], + array $commands = [] ) { $route = "/runtimes"; $headers = [ 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, 'x-appwrite-executor-key' => App::getEnv('_APP_EXECUTOR_SECRET', '') ]; $params = [ @@ -72,7 +71,6 @@ class Executor $route = "/runtimes/$runtimeId"; $headers = [ 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, 'x-appwrite-executor-key' => App::getEnv('_APP_EXECUTOR_SECRET', '') ]; @@ -99,12 +97,23 @@ class Executor string $runtime, string $baseImage, $timeout - ) - { + ) { + + // - 1st request + // - POST /execution + // - 404 - runtime not found + // - POST /runtimes - 200 + // - POST /execution + + // - 2nd request + // - POST /execution + // - 425 - runtime is not ready + // - Retries x 3 + 100ms wait + // - POST /execution + // - if failes anyway - throw and 4xx/5xx error $route = "/execution"; $headers = [ 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, 'x-appwrite-executor-key' => App::getEnv('_APP_EXECUTOR_SECRET', '') ]; $params = [ @@ -119,11 +128,37 @@ class Executor ]; $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); - $status = $response['headers']['status-code']; + if ($status >= 400) { - throw new \Exception($response['body']['message'], $status); - } + switch ($status) { + case 404: + $response = $this->createRuntime($functionId, $deploymentId, $projectId, $path, $runtime, $baseImage, $vars); + /** Try to create the execution once more */ + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + $status = $response['headers']['status-code']; + if ($status >= 400) { + throw new \Exception($response['body']['message'], $status); + } + break; + case 425: + for ($i = 0; $i < 3; $i++) { + sleep(1); + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + $status = $response['headers']['status-code']; + + if ($status < 400) { + break; + } + if ($status !== 425) { + throw new Exception($response['body']['message'], $status); + } + } + throw new Exception($response['body']['message'], 503); + default: + throw new \Exception($response['body']['message'], $status); + } + } return $response['body']; } From 37cb58bbde22b7f8da68a0710809c78cda15be51 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 17 Feb 2022 20:50:21 +0400 Subject: [PATCH 02/28] feat: retry execution logic --- src/Executor/Executor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index b033e3f0c9..fc39ea46d6 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -142,7 +142,7 @@ class Executor } break; case 425: - for ($i = 0; $i < 3; $i++) { + for ($attempts = 0; $attempts < 3; $attempts++) { sleep(1); $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; From 281e306d14c5c600849e2f3723140a0dd9aca527 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 18 Feb 2022 01:40:28 +0400 Subject: [PATCH 03/28] feat: improve create runtime strucutre --- app/executor.php | 77 +++++++++++++++++++++------------------ app/workers/builds.php | 1 + src/Executor/Executor.php | 6 ++- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/app/executor.php b/app/executor.php index 2f5242e507..6bb9f13ce6 100644 --- a/app/executor.php +++ b/app/executor.php @@ -123,10 +123,11 @@ App::post('/v1/runtimes') ->param('commands', [], new ArrayList(new Text(0)), 'Commands required to build the container') ->param('runtime', '', new Text(128), 'Runtime for the cloud function') ->param('baseImage', '', new Text(128), 'Base image name of the runtime') + ->param('workdir', '', new Text(256), 'Working directory') ->inject('orchestrationPool') ->inject('activeRuntimes') ->inject('response') - ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $baseImage, $orchestrationPool, $activeRuntimes, Response $response) { + ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $baseImage, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { $container = 'r-' . $runtimeId; @@ -147,24 +148,23 @@ App::post('/v1/runtimes') * Temporary file paths in the executor */ $tmpSource = "/tmp/$runtimeId/code.tar.gz"; - $tmpBuildDir = "/tmp/$runtimeId/builds"; $tmpBuild = "/tmp/$runtimeId/builds/code.tar.gz"; /** * Copy code files from source to a temporary location on the executor */ - $device = new Local($destination); + $device = new Local(); $buffer = $device->read($source); if(!$device->write($tmpSource, $buffer)) { throw new Exception('Failed to copy source code to temporary directory', 500); }; /** - * Create a temporary folder to store builds + * Create the mount folder */ - if (!\file_exists($tmpBuildDir)) { - if (!@\mkdir($tmpBuildDir, 0755, true)) { - throw new Exception("Can't create directory : $tmpBuildDir", 500); + if (!\file_exists(\dirname($tmpBuild))) { + if (!@\mkdir(\dirname($tmpBuild), 0755, true)) { + throw new Exception("Failed to create temporary directory", 500); } } @@ -182,8 +182,8 @@ App::post('/v1/runtimes') $buildId = $orchestration->run( image: $baseImage, name: $container, + hostname: $container, vars: $vars, - workdir: '/usr/code', labels: [ 'openruntimes-id' => $runtimeId, 'openruntimes-type' => 'build', @@ -195,10 +195,10 @@ App::post('/v1/runtimes') '-f', '/dev/null' ], - hostname: $container, mountFolder: \dirname($tmpSource), + workdir: $workdir, volumes: [ - "$tmpBuildDir:/usr/builds:rw" + \dirname($tmpBuild). ":/usr/builds:rw" ] ); @@ -207,38 +207,44 @@ App::post('/v1/runtimes') } /** - * Extract user code into build container + * Execute any commands if they were provided */ - $status = $orchestration->execute( - name: $container, - command: $commands, - stdout: $buildStdout, - stderr: $buildStderr, - timeout: App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) - ); + if (!empty($commands)) { + $status = $orchestration->execute( + name: $container, + command: $commands, + stdout: $buildStdout, + stderr: $buildStderr, + timeout: App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) + ); - if (!$status) { - throw new Exception('Failed to build dependenices ' . $buildStderr, 500); - } - - // Check if the build was successful by checking if file exists - if (!\file_exists($tmpBuild)) { - throw new Exception('Something went wrong during the build process', 500); + if (!$status) { + throw new Exception('Failed to build dependenices ' . $buildStderr, 500); + } } /** * Move built code to expected build directory */ - $outputPath = $device->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); + if (!empty($destination)) { + // Check if the build was successful by checking if file exists + if (!\file_exists($tmpBuild)) { + throw new Exception('Something went wrong during the build process', 500); + } - if (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL) === Storage::DEVICE_LOCAL) { - if (!$device->move($tmpBuild, $outputPath)) { - throw new Exception('Failed to move built code to storage', 500); - } - } else { - if (!$device->upload($tmpBuild, $outputPath)) { - throw new Exception('Failed to upload built code upload to storage', 500); + $device = new Local($destination); + $outputPath = $device->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); + + if (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL) === Storage::DEVICE_LOCAL) { + if (!$device->move($tmpBuild, $outputPath)) { + throw new Exception('Failed to move built code to storage', 500); + } + } else { + if (!$device->upload($tmpBuild, $outputPath)) { + throw new Exception('Failed to upload built code upload to storage', 500); + } } + $build['outputPath'] = $outputPath; } if ($buildStdout === '') { @@ -246,15 +252,14 @@ App::post('/v1/runtimes') } $buildEnd = \time(); - $build = [ - 'outputPath' => $outputPath, + $build = array_merge($build, [ 'status' => 'ready', 'stdout' => \utf8_encode($buildStdout), 'stderr' => \utf8_encode($buildStderr), 'startTime' => $buildStart, 'endTime' => $buildEnd, 'duration' => $buildEnd - $buildStart, - ]; + ]); Console::success('Build Stage completed in ' . ($buildEnd - $buildStart) . ' seconds'); diff --git a/app/workers/builds.php b/app/workers/builds.php index 0bdea41126..420dba3ad7 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -120,6 +120,7 @@ class BuildsV1 extends Worker vars: $vars, runtime: $key, baseImage: $baseImage, + workdir: '/usr/code', commands: [ 'sh', '-c', 'mkdir -p /usr/code && \ diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index fc39ea46d6..7833b99e02 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -37,6 +37,7 @@ class Executor string $source, string $runtime, string $baseImage, + string $workdir, array $vars = [], array $commands = [] ) { @@ -49,9 +50,10 @@ class Executor 'runtimeId' => "$projectId-$deploymentId", 'source' => $source, 'destination' => APP_STORAGE_BUILDS . "/app-$projectId", - 'vars' => $vars, 'runtime' => $runtime, 'baseImage' => $baseImage, + 'workdir' => $workdir, + 'vars' => $vars, 'commands' => $commands ]; @@ -133,7 +135,7 @@ class Executor if ($status >= 400) { switch ($status) { case 404: - $response = $this->createRuntime($functionId, $deploymentId, $projectId, $path, $runtime, $baseImage, $vars); + $response = $this->createRuntime($functionId, $deploymentId, $projectId, $path, $runtime, $baseImage, '', $vars); /** Try to create the execution once more */ $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; From f75280779016dcd4115d0d61480b9da587dd2f04 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 18 Feb 2022 04:43:04 +0400 Subject: [PATCH 04/28] feat: add 406 status code --- app/executor.php | 161 +++++++++++++++++++++----------------- app/workers/builds.php | 2 + composer.lock | 12 +-- src/Executor/Executor.php | 77 +++++++++++------- 4 files changed, 145 insertions(+), 107 deletions(-) diff --git a/app/executor.php b/app/executor.php index 6bb9f13ce6..af68fb30ad 100644 --- a/app/executor.php +++ b/app/executor.php @@ -20,6 +20,7 @@ use Utopia\Swoole\Request; use Utopia\Swoole\Response; use Utopia\Validator\ArrayList; use Utopia\Validator\Assoc; +use Utopia\Validator\Boolean; use Utopia\Validator\Range as ValidatorRange; use Utopia\Validator\Text; @@ -118,16 +119,18 @@ App::post('/v1/runtimes') ->desc("Create a new runtime server") ->param('runtimeId', '', new Text(62), 'Unique runtime ID.') ->param('source', '', new Text(0), 'Path to source files.') - ->param('destination', '', new Text(0), 'Destination folder to store build files into.') + ->param('destination', '', new Text(0), 'Destination folder to store build files into.', true) ->param('vars', [], new Assoc(), 'Environment Variables required for the build') ->param('commands', [], new ArrayList(new Text(0)), 'Commands required to build the container') ->param('runtime', '', new Text(128), 'Runtime for the cloud function') + ->param('network', '', new Text(128), 'Network to attach the container to') ->param('baseImage', '', new Text(128), 'Base image name of the runtime') - ->param('workdir', '', new Text(256), 'Working directory') + ->param('remove', false, new Boolean(), 'Remove a runtime after execution') + ->param('workdir', '', new Text(256), 'Working directory', true) ->inject('orchestrationPool') ->inject('activeRuntimes') ->inject('response') - ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $baseImage, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { + ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $network, string $baseImage, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { $container = 'r-' . $runtimeId; @@ -172,7 +175,11 @@ App::post('/v1/runtimes') * Create container */ $orchestration = $orchestrationPool->get(); - $container = 'b-' . $runtimeId; + $container = 'r-' . $runtimeId; + $secret = \bin2hex(\random_bytes(16)); + $vars = \array_merge($vars, [ + 'INTERNAL_RUNTIME_KEY' => $secret + ]); $vars = array_map(fn ($v) => strval($v), $vars); $orchestration ->setCpus(App::getEnv('_APP_FUNCTIONS_CPUS', 1)) @@ -190,11 +197,11 @@ App::post('/v1/runtimes') 'openruntimes-created' => strval($buildStart), 'openruntimes-runtime' => $runtime, ], - command: [ - 'tail', - '-f', - '/dev/null' - ], + // command: [ + // 'tail', + // '-f', + // '/dev/null' + // ], mountFolder: \dirname($tmpSource), workdir: $workdir, volumes: [ @@ -206,6 +213,10 @@ App::post('/v1/runtimes') throw new Exception('Failed to create build container', 500); } + if (!empty($network)) { + $orchestration->networkConnect($container, $network); + } + /** * Execute any commands if they were provided */ @@ -247,7 +258,7 @@ App::post('/v1/runtimes') $build['outputPath'] = $outputPath; } - if ($buildStdout === '') { + if (empty($buildStdout)) { $buildStdout = 'Build Successful!'; } @@ -261,86 +272,91 @@ App::post('/v1/runtimes') 'duration' => $buildEnd - $buildStart, ]); + if (!$remove) { + $activeRuntimes->set($container, [ + 'id' => $buildId, + 'name' => $container, + 'created' => $buildStart, + 'updated' => $buildEnd, + 'status' => 'Up ' . \round($buildEnd - $buildStart, 2) . 's', + 'key' => $secret, + ]); + } + + Console::success('Build Stage completed in ' . ($buildEnd - $buildStart) . ' seconds'); } catch (Throwable $th) { Console::error('Build failed: ' . $th->getMessage()); throw new Exception($th->getMessage(), 500); } finally { - if (!empty($buildId)) { + if (!empty($buildId) && $remove) { $orchestration->remove($buildId, true); } $orchestrationPool->put($orchestration); } - /** Create runtime server */ - try { - $orchestration = $orchestrationPool->get(); - /** - * Copy code files from source to a temporary location on the executor - */ - $buffer = $device->read($outputPath); - if(!$device->write($tmpBuild, $buffer)) { - throw new Exception('Failed to copy built code to temporary location.', 500); - }; + // /** Create runtime server */ + // try { + // $orchestration = $orchestrationPool->get(); + // /** + // * Copy code files from source to a temporary location on the executor + // */ + // $buffer = $device->read($source); + // if(!$device->write($tmpSource, $buffer)) { + // throw new Exception('Failed to copy built code to temporary location.', 500); + // }; - /** - * Launch Runtime - */ - $container = 'r-' . $runtimeId; - $secret = \bin2hex(\random_bytes(16)); - $vars = \array_merge($vars, [ - 'INTERNAL_RUNTIME_KEY' => $secret - ]); + // /** + // * Launch Runtime + // */ + // $container = 'r-' . $runtimeId; + // $secret = \bin2hex(\random_bytes(16)); + // $vars = \array_merge($vars, [ + // 'INTERNAL_RUNTIME_KEY' => $secret + // ]); - $executionStart = \microtime(true); - $executionTime = \time(); + // $executionStart = \microtime(true); + // $executionTime = \time(); - $vars = array_map(fn ($v) => strval($v), $vars); + // $vars = array_map(fn ($v) => strval($v), $vars); - $orchestration - ->setCpus(App::getEnv('_APP_FUNCTIONS_CPUS', 1)) - ->setMemory(App::getEnv('_APP_FUNCTIONS_MEMORY', 256)) - ->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256)); + // $orchestration + // ->setCpus(App::getEnv('_APP_FUNCTIONS_CPUS', 1)) + // ->setMemory(App::getEnv('_APP_FUNCTIONS_MEMORY', 256)) + // ->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256)); - $id = $orchestration->run( - image: $baseImage, - name: $container, - vars: $vars, - labels: [ - 'openruntimes-id' => $runtimeId, - 'openruntimes-type' => 'function', - 'openruntimes-created' => strval($executionTime), - 'openruntimes-runtime' => $runtime - ], - hostname: $container, - mountFolder: \dirname($tmpBuild), - ); + // $id = $orchestration->run( + // image: $baseImage, + // name: $container, + // vars: $vars, + // labels: [ + // 'openruntimes-id' => $runtimeId, + // 'openruntimes-type' => 'function', + // 'openruntimes-created' => strval($executionTime), + // 'openruntimes-runtime' => $runtime + // ], + // hostname: $container, + // mountFolder: \dirname($tmpSource), + // ); - if (empty($id)) { - throw new Exception('Failed to create runtime', 500); - } + // if (empty($id)) { + // throw new Exception('Failed to create runtime', 500); + // } - $orchestration->networkConnect($container, App::getEnv('_APP_EXECUTOR_RUNTIME_NETWORK', 'openruntimes')); + // $orchestration->networkConnect($container, App::getEnv('_APP_EXECUTOR_RUNTIME_NETWORK', 'openruntimes')); - $executionEnd = \microtime(true); + // $executionEnd = \microtime(true); - $activeRuntimes->set($container, [ - 'id' => $id, - 'name' => $container, - 'created' => $executionTime, - 'updated' => $executionTime, - 'status' => 'Up ' . \round($executionEnd - $executionStart, 2) . 's', - 'key' => $secret, - ]); + - Console::success('Runtime Server created in ' . ($executionEnd - $executionStart) . ' seconds'); - } catch (\Throwable $th) { - Console::error('Runtime Server Creation Failed: '. $th->getMessage()); - throw new Exception($th->getMessage(), 500); - } finally { - $orchestrationPool->put($orchestration); - } + // Console::success('Runtime Server created in ' . ($executionEnd - $executionStart) . ' seconds'); + // } catch (\Throwable $th) { + // Console::error('Runtime Server Creation Failed: '. $th->getMessage()); + // throw new Exception($th->getMessage(), 500); + // } finally { + // $orchestrationPool->put($orchestration); + // } $response ->setStatusCode(Response::STATUS_CODE_CREATED) @@ -502,7 +518,7 @@ App::post('/v1/execution') // 110 is the Swoole error code for timeout, see: https://www.swoole.co.uk/docs/swoole-error-code if ($errNo !== 0 && $errNo !== CURLE_COULDNT_CONNECT && $errNo !== CURLE_OPERATION_TIMEDOUT && $errNo !== 110) { - throw new Exception('An internal curl error has occurred within the executor! Error Msg: ' . $error, 500); + throw new Exception('An internal curl error has occurred within the executor! Error Msg: ' . $error, 406); } $executionData = []; @@ -567,15 +583,16 @@ App::setResource('activeRuntimes', fn() => $activeRuntimes); App::error(function ($error, $response) { // $route = $utopia->match($request); // logError($error, "httpError", $route); - switch ($error->getCode()) { case 400: // Error allowed publicly case 401: // Error allowed publicly case 402: // Error allowed publicly case 403: // Error allowed publicly case 404: // Error allowed publicly + case 406: // Error allowed publicly case 409: // Error allowed publicly case 412: // Error allowed publicly + case 425: // Error allowed publicly case 429: // Error allowed publicly case 501: // Error allowed publicly case 503: // Error allowed publicly @@ -594,6 +611,8 @@ App::error(function ($error, $response) { 'version' => App::getEnv('OPENRUNTIMES_VERSION', 'UNKNOWN'), ]; + var_dump($output); + $response ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') ->addHeader('Expires', '0') diff --git a/app/workers/builds.php b/app/workers/builds.php index 420dba3ad7..bf825d49e5 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -117,10 +117,12 @@ class BuildsV1 extends Worker functionId: $functionId, deploymentId: $deploymentId, source: $source, + destination: APP_STORAGE_BUILDS . "/app-$projectId", vars: $vars, runtime: $key, baseImage: $baseImage, workdir: '/usr/code', + remove: true, commands: [ 'sh', '-c', 'mkdir -p /usr/code && \ diff --git a/composer.lock b/composer.lock index 71ff25ebef..2a68fa1d19 100644 --- a/composer.lock +++ b/composer.lock @@ -2252,16 +2252,16 @@ }, { "name": "utopia-php/framework", - "version": "0.19.6", + "version": "0.19.7", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "7d9b28365fb794001cb34dd028659452d4e71b7d" + "reference": "f17afe77a21873b9be18ebc05283813468b4283a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/7d9b28365fb794001cb34dd028659452d4e71b7d", - "reference": "7d9b28365fb794001cb34dd028659452d4e71b7d", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/f17afe77a21873b9be18ebc05283813468b4283a", + "reference": "f17afe77a21873b9be18ebc05283813468b4283a", "shasum": "" }, "require": { @@ -2295,9 +2295,9 @@ ], "support": { "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.19.6" + "source": "https://github.com/utopia-php/framework/tree/0.19.7" }, - "time": "2022-02-10T17:05:22+00:00" + "time": "2022-02-18T00:04:49+00:00" }, { "name": "utopia-php/image", diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 7833b99e02..5ca0f4a706 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -3,7 +3,9 @@ namespace Executor; use Exception; +use phpDocumentor\Reflection\DocBlock\Tags\Var_; use Utopia\App; +use Utopia\CLI\Console; class Executor { @@ -34,10 +36,13 @@ class Executor string $functionId, string $deploymentId, string $projectId, - string $source, + string $source, string $runtime, string $baseImage, - string $workdir, + bool $remove = false, + string $workdir = '', + string $destination = '', + string $network = '', array $vars = [], array $commands = [] ) { @@ -49,14 +54,18 @@ class Executor $params = [ 'runtimeId' => "$projectId-$deploymentId", 'source' => $source, - 'destination' => APP_STORAGE_BUILDS . "/app-$projectId", + 'destination' => $destination, 'runtime' => $runtime, 'baseImage' => $baseImage, 'workdir' => $workdir, + 'network' => empty($network) ? App::getEnv('_APP_EXECUTOR_RUNTIME_NETWORK', 'openruntimes') : $network, 'vars' => $vars, + 'remove' => $remove, 'commands' => $commands ]; + var_dump($params); + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; @@ -131,35 +140,47 @@ class Executor $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; - + if ($status >= 400) { - switch ($status) { - case 404: - $response = $this->createRuntime($functionId, $deploymentId, $projectId, $path, $runtime, $baseImage, '', $vars); - /** Try to create the execution once more */ - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); - $status = $response['headers']['status-code']; - if ($status >= 400) { - throw new \Exception($response['body']['message'], $status); - } - break; - case 425: - for ($attempts = 0; $attempts < 3; $attempts++) { - sleep(1); + for ($attempts = 0; $attempts < 3; $attempts++) { + switch ($status) { + case 404: + Console::error("Runtime not found. Creating runtine"); + $response = $this->createRuntime( + functionId: $functionId, + deploymentId: $deploymentId, + projectId: $projectId, + source: $path, + runtime: $runtime, + baseImage: $baseImage, + vars: $vars, + commands: [] + ); + /** Try to create the execution once more */ + Console::error("Creating execution"); $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; + var_dump($status); + break; + case 406: + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + $status = $response['headers']['status-code']; + break; + default: + throw new \Exception($response['body']['message'], $status); + } - if ($status < 400) { - break; - } - if ($status !== 425) { - throw new Exception($response['body']['message'], $status); - } - } - throw new Exception($response['body']['message'], 503); - default: + if ($status < 400) { + return $response['body']; + } + + if ($status != 406) { throw new \Exception($response['body']['message'], $status); + } + + sleep(1); } + throw new Exception($response['body']['message'], 503); } return $response['body']; @@ -262,10 +283,6 @@ class Executor $responseHeaders['status-code'] = $responseStatus; - if ($responseStatus === 500) { - echo 'Server error('.$method.': '.$path.'. Params: '.json_encode($params).'): '.json_encode($responseBody)."\n"; - } - return [ 'headers' => $responseHeaders, 'body' => $responseBody From 7fb3df3b38e3acb995ea08f3a1d2614547680b2a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 18 Feb 2022 13:21:59 +0400 Subject: [PATCH 05/28] feat: cleanup --- app/executor.php | 5 ----- src/Executor/Executor.php | 17 ----------------- 2 files changed, 22 deletions(-) diff --git a/app/executor.php b/app/executor.php index af68fb30ad..b64b4b0761 100644 --- a/app/executor.php +++ b/app/executor.php @@ -197,11 +197,6 @@ App::post('/v1/runtimes') 'openruntimes-created' => strval($buildStart), 'openruntimes-runtime' => $runtime, ], - // command: [ - // 'tail', - // '-f', - // '/dev/null' - // ], mountFolder: \dirname($tmpSource), workdir: $workdir, volumes: [ diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 5ca0f4a706..c1f7694312 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -3,7 +3,6 @@ namespace Executor; use Exception; -use phpDocumentor\Reflection\DocBlock\Tags\Var_; use Utopia\App; use Utopia\CLI\Console; @@ -110,18 +109,6 @@ class Executor $timeout ) { - // - 1st request - // - POST /execution - // - 404 - runtime not found - // - POST /runtimes - 200 - // - POST /execution - - // - 2nd request - // - POST /execution - // - 425 - runtime is not ready - // - Retries x 3 + 100ms wait - // - POST /execution - // - if failes anyway - throw and 4xx/5xx error $route = "/execution"; $headers = [ 'content-type' => 'application/json', @@ -145,7 +132,6 @@ class Executor for ($attempts = 0; $attempts < 3; $attempts++) { switch ($status) { case 404: - Console::error("Runtime not found. Creating runtine"); $response = $this->createRuntime( functionId: $functionId, deploymentId: $deploymentId, @@ -156,11 +142,8 @@ class Executor vars: $vars, commands: [] ); - /** Try to create the execution once more */ - Console::error("Creating execution"); $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; - var_dump($status); break; case 406: $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); From ac14d5fd8c987588e59fca5c191b912234fa8b2d Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 18 Feb 2022 19:25:54 +0400 Subject: [PATCH 06/28] feat: update composer --- app/executor.php | 89 +++++++----------------------------------------- 1 file changed, 12 insertions(+), 77 deletions(-) diff --git a/app/executor.php b/app/executor.php index b64b4b0761..211c254b3b 100644 --- a/app/executor.php +++ b/app/executor.php @@ -132,9 +132,9 @@ App::post('/v1/runtimes') ->inject('response') ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $network, string $baseImage, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; + $containerName = 'r-' . $runtimeId; - if ($activeRuntimes->exists($container)) { + if ($activeRuntimes->exists($containerName)) { throw new Exception('Runtime already exists.', 409); } @@ -146,7 +146,7 @@ App::post('/v1/runtimes') $buildEnd = 0; try { - Console::info('Building runtime with ID : ' . $runtimeId); + Console::info('Building container : ' . $containerName); /** * Temporary file paths in the executor */ @@ -175,7 +175,6 @@ App::post('/v1/runtimes') * Create container */ $orchestration = $orchestrationPool->get(); - $container = 'r-' . $runtimeId; $secret = \bin2hex(\random_bytes(16)); $vars = \array_merge($vars, [ 'INTERNAL_RUNTIME_KEY' => $secret @@ -188,12 +187,12 @@ App::post('/v1/runtimes') $buildId = $orchestration->run( image: $baseImage, - name: $container, - hostname: $container, + name: $containerName, + hostname: $containerName, vars: $vars, labels: [ 'openruntimes-id' => $runtimeId, - 'openruntimes-type' => 'build', + 'openruntimes-type' => 'runtime', 'openruntimes-created' => strval($buildStart), 'openruntimes-runtime' => $runtime, ], @@ -209,7 +208,7 @@ App::post('/v1/runtimes') } if (!empty($network)) { - $orchestration->networkConnect($container, $network); + $orchestration->networkConnect($containerName, $network); } /** @@ -217,7 +216,7 @@ App::post('/v1/runtimes') */ if (!empty($commands)) { $status = $orchestration->execute( - name: $container, + name: $containerName, command: $commands, stdout: $buildStdout, stderr: $buildStderr, @@ -268,9 +267,9 @@ App::post('/v1/runtimes') ]); if (!$remove) { - $activeRuntimes->set($container, [ + $activeRuntimes->set($containerName, [ 'id' => $buildId, - 'name' => $container, + 'name' => $containerName, 'created' => $buildStart, 'updated' => $buildEnd, 'status' => 'Up ' . \round($buildEnd - $buildStart, 2) . 's', @@ -291,68 +290,6 @@ App::post('/v1/runtimes') $orchestrationPool->put($orchestration); } - // /** Create runtime server */ - // try { - // $orchestration = $orchestrationPool->get(); - // /** - // * Copy code files from source to a temporary location on the executor - // */ - // $buffer = $device->read($source); - // if(!$device->write($tmpSource, $buffer)) { - // throw new Exception('Failed to copy built code to temporary location.', 500); - // }; - - // /** - // * Launch Runtime - // */ - // $container = 'r-' . $runtimeId; - // $secret = \bin2hex(\random_bytes(16)); - // $vars = \array_merge($vars, [ - // 'INTERNAL_RUNTIME_KEY' => $secret - // ]); - - // $executionStart = \microtime(true); - // $executionTime = \time(); - - // $vars = array_map(fn ($v) => strval($v), $vars); - - // $orchestration - // ->setCpus(App::getEnv('_APP_FUNCTIONS_CPUS', 1)) - // ->setMemory(App::getEnv('_APP_FUNCTIONS_MEMORY', 256)) - // ->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256)); - - // $id = $orchestration->run( - // image: $baseImage, - // name: $container, - // vars: $vars, - // labels: [ - // 'openruntimes-id' => $runtimeId, - // 'openruntimes-type' => 'function', - // 'openruntimes-created' => strval($executionTime), - // 'openruntimes-runtime' => $runtime - // ], - // hostname: $container, - // mountFolder: \dirname($tmpSource), - // ); - - // if (empty($id)) { - // throw new Exception('Failed to create runtime', 500); - // } - - // $orchestration->networkConnect($container, App::getEnv('_APP_EXECUTOR_RUNTIME_NETWORK', 'openruntimes')); - - // $executionEnd = \microtime(true); - - - - // Console::success('Runtime Server created in ' . ($executionEnd - $executionStart) . ' seconds'); - // } catch (\Throwable $th) { - // Console::error('Runtime Server Creation Failed: '. $th->getMessage()); - // throw new Exception($th->getMessage(), 500); - // } finally { - // $orchestrationPool->put($orchestration); - // } - $response ->setStatusCode(Response::STATUS_CODE_CREATED) ->json($build); @@ -606,8 +543,6 @@ App::error(function ($error, $response) { 'version' => App::getEnv('OPENRUNTIMES_VERSION', 'UNKNOWN'), ]; - var_dump($output); - $response ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') ->addHeader('Expires', '0') @@ -663,7 +598,7 @@ $http->on('start', function ($http) { Console::info('Removing orphan runtimes...'); try { $orchestration = $orchestrationPool->get(); - $orphans = $orchestration->list(['label' => 'openruntimes-type=function']); + $orphans = $orchestration->list(['label' => 'openruntimes-type=runtime']); } catch (\Throwable $th) { } finally { $orchestrationPool->put($orchestration); @@ -734,7 +669,7 @@ $http->on('beforeShutdown', function() { Console::info('Cleaning up containers before shutdown...'); $orchestration = $orchestrationPool->get(); - $functionsToRemove = $orchestration->list(['label' => 'openruntimes-type=function']); + $functionsToRemove = $orchestration->list(['label' => 'openruntimes-type=runtime']); $orchestrationPool->put($orchestration); foreach ($functionsToRemove as $container) { From f7a08718483c96bd37e490bf55656686c9a43e48 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 18 Feb 2022 19:58:04 +0400 Subject: [PATCH 07/28] feat: enable logging --- app/executor.php | 97 +++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/app/executor.php b/app/executor.php index 211c254b3b..19c16a90e9 100644 --- a/app/executor.php +++ b/app/executor.php @@ -12,6 +12,7 @@ use Swoole\Timer; use Utopia\App; use Utopia\CLI\Console; use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Orchestration\Adapter\DockerCLI; use Utopia\Orchestration\Orchestration; use Utopia\Storage\Device\Local; @@ -39,9 +40,8 @@ use Utopia\Validator\Text; // Fix error handling - done // Decide on logic for build and runtime containers names ( runtime-ID and build-ID) - done // Add size validators for the runtime IDs - done +// Fix logging - done - -// Fix logging // Fix delete endpoint // Incorporate Matej's changes in the build stage ( moving of the tar file will be performed by the runtime and not the build stage ) @@ -72,11 +72,23 @@ $orchestrationPool = new ConnectionPool(function () { return $orchestration; }, 10); + +/** + * Create logger instance + */ +$providerName = App::getEnv('_APP_LOGGING_PROVIDER', ''); +$providerConfig = App::getEnv('_APP_LOGGING_CONFIG', ''); +$logger = null; + +if(!empty($providerName) && !empty($providerConfig) && Logger::hasProvider($providerName)) { + $classname = '\\Utopia\\Logger\\Adapter\\'.\ucfirst($providerName); + $adapter = new $classname($providerConfig); + $logger = new Logger($adapter); +} + function logError(Throwable $error, string $action, Utopia\Route $route = null) { - global $register; - - $logger = $register->get('logger'); + global $logger; if ($logger) { $version = App::getEnv('_APP_VERSION', 'UNKNOWN'); @@ -138,12 +150,12 @@ App::post('/v1/runtimes') throw new Exception('Runtime already exists.', 409); } - $build = []; - $buildId = ''; - $buildStdout = ''; - $buildStderr = ''; - $buildStart = \time(); - $buildEnd = 0; + $container = []; + $containerId = ''; + $stdout = ''; + $stderr = ''; + $startTime = \time(); + $endTime = 0; try { Console::info('Building container : ' . $containerName); @@ -185,7 +197,7 @@ App::post('/v1/runtimes') ->setMemory(App::getEnv('_APP_FUNCTIONS_MEMORY', 256)) ->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256)); - $buildId = $orchestration->run( + $containerId = $orchestration->run( image: $baseImage, name: $containerName, hostname: $containerName, @@ -193,7 +205,7 @@ App::post('/v1/runtimes') labels: [ 'openruntimes-id' => $runtimeId, 'openruntimes-type' => 'runtime', - 'openruntimes-created' => strval($buildStart), + 'openruntimes-created' => strval($startTime), 'openruntimes-runtime' => $runtime, ], mountFolder: \dirname($tmpSource), @@ -203,7 +215,7 @@ App::post('/v1/runtimes') ] ); - if (empty($buildId)) { + if (empty($containerId)) { throw new Exception('Failed to create build container', 500); } @@ -218,13 +230,13 @@ App::post('/v1/runtimes') $status = $orchestration->execute( name: $containerName, command: $commands, - stdout: $buildStdout, - stderr: $buildStderr, + stdout: $stdout, + stderr: $stderr, timeout: App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) ); if (!$status) { - throw new Exception('Failed to build dependenices ' . $buildStderr, 500); + throw new Exception('Failed to build dependenices ' . $stderr, 500); } } @@ -249,50 +261,50 @@ App::post('/v1/runtimes') throw new Exception('Failed to upload built code upload to storage', 500); } } - $build['outputPath'] = $outputPath; + $container['outputPath'] = $outputPath; } - if (empty($buildStdout)) { - $buildStdout = 'Build Successful!'; + if (empty($stdout)) { + $stdout = 'Build Successful!'; } - $buildEnd = \time(); - $build = array_merge($build, [ + $endTime = \time(); + $container = array_merge($container, [ 'status' => 'ready', - 'stdout' => \utf8_encode($buildStdout), - 'stderr' => \utf8_encode($buildStderr), - 'startTime' => $buildStart, - 'endTime' => $buildEnd, - 'duration' => $buildEnd - $buildStart, + 'stdout' => \utf8_encode($stdout), + 'stderr' => \utf8_encode($stderr), + 'startTime' => $startTime, + 'endTime' => $endTime, + 'duration' => $endTime - $startTime, ]); if (!$remove) { $activeRuntimes->set($containerName, [ - 'id' => $buildId, + 'id' => $containerId, 'name' => $containerName, - 'created' => $buildStart, - 'updated' => $buildEnd, - 'status' => 'Up ' . \round($buildEnd - $buildStart, 2) . 's', + 'created' => $startTime, + 'updated' => $endTime, + 'status' => 'Up ' . \round($endTime - $startTime, 2) . 's', 'key' => $secret, ]); } - Console::success('Build Stage completed in ' . ($buildEnd - $buildStart) . ' seconds'); + Console::success('Build Stage completed in ' . ($endTime - $startTime) . ' seconds'); } catch (Throwable $th) { Console::error('Build failed: ' . $th->getMessage()); throw new Exception($th->getMessage(), 500); } finally { - if (!empty($buildId) && $remove) { - $orchestration->remove($buildId, true); + if (!empty($containerId) && $remove) { + $orchestration->remove($containerId, true); } $orchestrationPool->put($orchestration); } $response ->setStatusCode(Response::STATUS_CODE_CREATED) - ->json($build); + ->json($container); }); @@ -308,7 +320,7 @@ App::get('/v1/runtimes') } $response - ->setStatusCode(200) + ->setStatusCode(Response::STATUS_CODE_OK) ->json($runtimes); }); @@ -328,7 +340,7 @@ App::get('/v1/runtimes/:runtimeId') $runtime = $activeRuntimes->get($container); $response - ->setStatusCode(200) + ->setStatusCode(Response::STATUS_CODE_OK) ->json($runtime); }); @@ -512,9 +524,10 @@ App::setResource('orchestrationPool', fn() => $orchestrationPool); App::setResource('activeRuntimes', fn() => $activeRuntimes); /** Set callbacks */ -App::error(function ($error, $response) { - // $route = $utopia->match($request); - // logError($error, "httpError", $route); +App::error(function ($utopia, $error, $request, $response) { + $route = $utopia->match($request); + logError($error, "httpError", $route); + switch ($error->getCode()) { case 400: // Error allowed publicly case 401: // Error allowed publicly @@ -550,7 +563,7 @@ App::error(function ($error, $response) { ->setStatusCode($code); $response->json($output); -}, ['error', 'response']); +}, ['utopia', 'error', 'request', 'response']); App::init(function ($request, $response) { $secretKey = $request->getHeader('x-appwrite-executor-key', ''); @@ -696,7 +709,7 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo try { $app->run($request, $response); } catch (\Throwable $th) { - // logError($e, "serverError"); + logError($th, "serverError"); $swooleResponse->setStatusCode(500); $output = [ 'message' => 'Error: '. $th->getMessage(), From 8fffe5766d0015c74881cbec6bede3e3dfa4f728 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 03:32:09 +0400 Subject: [PATCH 08/28] feat: migrated to new runtimes --- app/executor.php | 27 +++++++++++++++------------ app/workers/builds.php | 3 +-- src/Executor/Executor.php | 5 ++++- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/executor.php b/app/executor.php index 19c16a90e9..ad5728a7fd 100644 --- a/app/executor.php +++ b/app/executor.php @@ -137,12 +137,13 @@ App::post('/v1/runtimes') ->param('runtime', '', new Text(128), 'Runtime for the cloud function') ->param('network', '', new Text(128), 'Network to attach the container to') ->param('baseImage', '', new Text(128), 'Base image name of the runtime') + ->param('entrypoint', '', new Text(256), 'Entrypoint of the code file', true) ->param('remove', false, new Boolean(), 'Remove a runtime after execution') ->param('workdir', '', new Text(256), 'Working directory', true) ->inject('orchestrationPool') ->inject('activeRuntimes') ->inject('response') - ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $network, string $baseImage, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { + ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $network, string $baseImage, string $entrypoint, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { $containerName = 'r-' . $runtimeId; @@ -189,7 +190,8 @@ App::post('/v1/runtimes') $orchestration = $orchestrationPool->get(); $secret = \bin2hex(\random_bytes(16)); $vars = \array_merge($vars, [ - 'INTERNAL_RUNTIME_KEY' => $secret + 'INTERNAL_RUNTIME_KEY' => $secret, + 'INTERNAL_RUNTIME_ENTRYPOINT' => $entrypoint, ]); $vars = array_map(fn ($v) => strval($v), $vars); $orchestration @@ -211,7 +213,7 @@ App::post('/v1/runtimes') mountFolder: \dirname($tmpSource), workdir: $workdir, volumes: [ - \dirname($tmpBuild). ":/usr/builds:rw" + \dirname($tmpBuild). ":/usr/code:rw" ] ); @@ -240,6 +242,10 @@ App::post('/v1/runtimes') } } + + var_dump($destination); + var_dump($tmpBuild); + /** * Move built code to expected build directory */ @@ -251,16 +257,13 @@ App::post('/v1/runtimes') $device = new Local($destination); $outputPath = $device->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); + var_dump($outputPath); + + $buffer = $device->read($tmpBuild); + if(!$device->write($outputPath, $buffer)) { + throw new Exception('Failed to move built code to storage', 500); + }; - if (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL) === Storage::DEVICE_LOCAL) { - if (!$device->move($tmpBuild, $outputPath)) { - throw new Exception('Failed to move built code to storage', 500); - } - } else { - if (!$device->upload($tmpBuild, $outputPath)) { - throw new Exception('Failed to upload built code upload to storage', 500); - } - } $container['outputPath'] = $outputPath; } diff --git a/app/workers/builds.php b/app/workers/builds.php index bf825d49e5..2356b28dec 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -131,8 +131,7 @@ class BuildsV1 extends Worker tar -zxf /usr/workspace/code.tar.gz -C /usr/code && \ rm /usr/workspace/code.tar.gz && \ cd /usr/local/src && \ - ./build.sh && \ - tar -C /usr/code -czf /usr/builds/code.tar.gz ./' + ./build.sh' ] ); diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index c1f7694312..23c8505f57 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -39,6 +39,7 @@ class Executor string $runtime, string $baseImage, bool $remove = false, + string $entrypoint = '', string $workdir = '', string $destination = '', string $network = '', @@ -56,6 +57,7 @@ class Executor 'destination' => $destination, 'runtime' => $runtime, 'baseImage' => $baseImage, + 'entrypoint' => $entrypoint, 'workdir' => $workdir, 'network' => empty($network) ? App::getEnv('_APP_EXECUTOR_RUNTIME_NETWORK', 'openruntimes') : $network, 'vars' => $vars, @@ -129,7 +131,7 @@ class Executor $status = $response['headers']['status-code']; if ($status >= 400) { - for ($attempts = 0; $attempts < 3; $attempts++) { + for ($attempts = 0; $attempts < 10; $attempts++) { switch ($status) { case 404: $response = $this->createRuntime( @@ -140,6 +142,7 @@ class Executor runtime: $runtime, baseImage: $baseImage, vars: $vars, + entrypoint: $entrypoint, commands: [] ); $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); From 360629326871faf5e37eb209782cec75081eb696 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 03:46:39 +0400 Subject: [PATCH 09/28] feat: cleanup the build command --- app/workers/builds.php | 9 ++------- src/Executor/Executor.php | 2 -- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/app/workers/builds.php b/app/workers/builds.php index 2356b28dec..e8a283fdbb 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -125,13 +125,8 @@ class BuildsV1 extends Worker remove: true, commands: [ 'sh', '-c', - 'mkdir -p /usr/code && \ - cp /tmp/code.tar.gz /usr/workspace/code.tar.gz && \ - cd /usr/workspace/ && \ - tar -zxf /usr/workspace/code.tar.gz -C /usr/code && \ - rm /usr/workspace/code.tar.gz && \ - cd /usr/local/src && \ - ./build.sh' + 'tar -zxf /tmp/code.tar.gz -C /usr/code && \ + cd /usr/local/src && ./build.sh' ] ); diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 23c8505f57..5ee1499bde 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -65,8 +65,6 @@ class Executor 'commands' => $commands ]; - var_dump($params); - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $status = $response['headers']['status-code']; From 38ca8dc6fca249fcfad9687d7f85c538b31b5875 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 04:15:03 +0400 Subject: [PATCH 10/28] feat: update php test code --- app/executor.php | 60 ++++++++------------- src/Executor/Executor.php | 2 +- tests/resources/functions/php-fn.tar.gz | Bin 351 -> 415 bytes tests/resources/functions/php-fn/index.php | 1 + tests/resources/functions/php.tar.gz | Bin 386 -> 446 bytes tests/resources/functions/php/index.php | 1 + 6 files changed, 26 insertions(+), 38 deletions(-) diff --git a/app/executor.php b/app/executor.php index ad5728a7fd..873d50e7b6 100644 --- a/app/executor.php +++ b/app/executor.php @@ -129,7 +129,7 @@ function logError(Throwable $error, string $action, Utopia\Route $route = null) App::post('/v1/runtimes') ->desc("Create a new runtime server") - ->param('runtimeId', '', new Text(62), 'Unique runtime ID.') + ->param('runtimeId', '', new Text(64), 'Unique runtime ID.') ->param('source', '', new Text(0), 'Path to source files.') ->param('destination', '', new Text(0), 'Destination folder to store build files into.', true) ->param('vars', [], new Assoc(), 'Environment Variables required for the build') @@ -145,9 +145,7 @@ App::post('/v1/runtimes') ->inject('response') ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $network, string $baseImage, string $entrypoint, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { - $containerName = 'r-' . $runtimeId; - - if ($activeRuntimes->exists($containerName)) { + if ($activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime already exists.', 409); } @@ -159,7 +157,7 @@ App::post('/v1/runtimes') $endTime = 0; try { - Console::info('Building container : ' . $containerName); + Console::info('Building container : ' . $runtimeId); /** * Temporary file paths in the executor */ @@ -201,8 +199,8 @@ App::post('/v1/runtimes') $containerId = $orchestration->run( image: $baseImage, - name: $containerName, - hostname: $containerName, + name: $runtimeId, + hostname: $runtimeId, vars: $vars, labels: [ 'openruntimes-id' => $runtimeId, @@ -222,7 +220,7 @@ App::post('/v1/runtimes') } if (!empty($network)) { - $orchestration->networkConnect($containerName, $network); + $orchestration->networkConnect($runtimeId, $network); } /** @@ -230,7 +228,7 @@ App::post('/v1/runtimes') */ if (!empty($commands)) { $status = $orchestration->execute( - name: $containerName, + name: $runtimeId, command: $commands, stdout: $stdout, stderr: $stderr, @@ -242,10 +240,6 @@ App::post('/v1/runtimes') } } - - var_dump($destination); - var_dump($tmpBuild); - /** * Move built code to expected build directory */ @@ -257,7 +251,6 @@ App::post('/v1/runtimes') $device = new Local($destination); $outputPath = $device->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); - var_dump($outputPath); $buffer = $device->read($tmpBuild); if(!$device->write($outputPath, $buffer)) { @@ -282,9 +275,9 @@ App::post('/v1/runtimes') ]); if (!$remove) { - $activeRuntimes->set($containerName, [ + $activeRuntimes->set($runtimeId, [ 'id' => $containerId, - 'name' => $containerName, + 'name' => $runtimeId, 'created' => $startTime, 'updated' => $endTime, 'status' => 'Up ' . \round($endTime - $startTime, 2) . 's', @@ -329,18 +322,16 @@ App::get('/v1/runtimes') App::get('/v1/runtimes/:runtimeId') ->desc("Get a runtime by its ID") - ->param('runtimeId', '', new Text(62), 'Runtime unique ID.') + ->param('runtimeId', '', new Text(64), 'Runtime unique ID.') ->inject('activeRuntimes') ->inject('response') ->action(function ($runtimeId, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; - - if(!$activeRuntimes->exists($container)) { + if(!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found', 404); } - $runtime = $activeRuntimes->get($container); + $runtime = $activeRuntimes->get($runtimeId); $response ->setStatusCode(Response::STATUS_CODE_OK) @@ -349,25 +340,23 @@ App::get('/v1/runtimes/:runtimeId') App::delete('/v1/runtimes/:runtimeId') ->desc('Delete a runtime') - ->param('runtimeId', '', new Text(62), 'Runtime unique ID.', false) + ->param('runtimeId', '', new Text(64), 'Runtime unique ID.', false) ->inject('orchestrationPool') ->inject('activeRuntimes') ->inject('response') ->action(function (string $runtimeId, $orchestrationPool, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; - - if(!$activeRuntimes->exists($container)) { + if(!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found', 404); } - Console::info('Deleting runtime: ' . $container); + Console::info('Deleting runtime: ' . $runtimeId); try { $orchestration = $orchestrationPool->get(); - $orchestration->remove($container, true); - $activeRuntimes->del($container); - Console::success('Removed runtime container: ' . $container); + $orchestration->remove($runtimeId, true); + $activeRuntimes->del($runtimeId); + Console::success('Removed runtime container: ' . $runtimeId); } finally { $orchestrationPool->put($orchestration); } @@ -391,7 +380,7 @@ App::delete('/v1/runtimes/:runtimeId') App::post('/v1/execution') ->desc('Create an execution') - ->param('runtimeId', '', new Text(62), 'The runtimeID to execute') + ->param('runtimeId', '', new Text(64), 'The runtimeID to execute') ->param('path', '', new Text(0), 'Path containing the built files.', false) ->param('vars', [], new Assoc(), 'Environment variables required for the build', false) ->param('data', '', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) @@ -404,13 +393,11 @@ App::post('/v1/execution') ->action( function (string $runtimeId, string $path, array $vars, string $data, string $runtime, string $entrypoint, $timeout, string $baseImage, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; - - if (!$activeRuntimes->exists($container)) { + if (!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found. Please create the runtime.', 404); } - $runtime = $activeRuntimes->get($container); + $runtime = $activeRuntimes->get($runtimeId); $secret = $runtime['key']; if (empty($secret)) { throw new Exception('Runtime secret not found. Please re-create the runtime.', 500); @@ -434,7 +421,7 @@ App::post('/v1/execution') 'payload' => $data, 'timeout' => $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) ]); - \curl_setopt($ch, CURLOPT_URL, "http://" . $container . ":3000/"); + \curl_setopt($ch, CURLOPT_URL, "http://" . $runtimeId . ":3000/"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -510,7 +497,7 @@ App::post('/v1/execution') /** Update swoole table */ $runtime['updated'] = \time(); - $activeRuntimes->set($container, $runtime); + $activeRuntimes->set($runtimeId, $runtime); $response ->setStatusCode(Response::STATUS_CODE_OK) @@ -615,7 +602,6 @@ $http->on('start', function ($http) { try { $orchestration = $orchestrationPool->get(); $orphans = $orchestration->list(['label' => 'openruntimes-type=runtime']); - } catch (\Throwable $th) { } finally { $orchestrationPool->put($orchestration); } diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 5ee1499bde..e68e67c83b 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -129,7 +129,7 @@ class Executor $status = $response['headers']['status-code']; if ($status >= 400) { - for ($attempts = 0; $attempts < 10; $attempts++) { + for ($attempts = 0; $attempts < 5; $attempts++) { switch ($status) { case 404: $response = $this->createRuntime( diff --git a/tests/resources/functions/php-fn.tar.gz b/tests/resources/functions/php-fn.tar.gz index 7d5a0c5f649384044e9025c17e210945e9a6d235..e4bdd8bae6e1b708d8075fed3228985b6d227dc6 100644 GIT binary patch literal 415 zcmV;Q0bu?giwFSOH4tI|1MQT{Zh}A*hFS9zvuJ`%ybMAQCjAvUpeT#%+I|K>38pKlK5Os3vo3KxX1Da+sxl_WwzO5#y~mjoz5QD}-h zfV>Drg#mK$r7=ttgiD{4;%xkF5Wek}j`wuVM{MhL4(IqES^OiaH@5Byzt;DgePMf$ zekhXk?feB^5wiJCr+u%4{u{} zs$tAb%|iCwMDJSKNVl~f2il!%pIV3T=I4EjY92^ehlmb!aOT5$$haRp43TcpAy54I zV9V6{ePq&6Q*%DjoamM|L>DyRO9mZ$ip()tU&c?9^0z=!>NY1<3Y^_jE%gFs;{V}H zP$MZ|lQA->&wgsoM-FC`U5t(SD8n2LP}ia^@Wg)vFUVc+(PDr5#w%5-RH;(2J^_4; J{#gJH005na$X);d literal 351 zcmV-l0igaLiwFQz$M0bP1MQSgYr-%XhduXG$XWMDpjidHlL52$tnO0007)*sG diff --git a/tests/resources/functions/php-fn/index.php b/tests/resources/functions/php-fn/index.php index 17cf700d36..8b729ed40a 100644 --- a/tests/resources/functions/php-fn/index.php +++ b/tests/resources/functions/php-fn/index.php @@ -1,6 +1,7 @@ json([ 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], diff --git a/tests/resources/functions/php.tar.gz b/tests/resources/functions/php.tar.gz index 71404d2e7f7092f55e285859a7f30b5e748c4e7d..a7d6026c7e3852ecabe474822258e3793f30d3a4 100644 GIT binary patch literal 446 zcmV;v0YUyBiwFR}H4tI|1MQUCYQjJehI5^#7^I+rhgD;QS}kH$3DiX5YAIHNH7hDK zu{lUf>D%&PhT3xd5$}_St*m5J$Kx<#du;p9r7{TbQ;0ge8(>S6Xo)A)1a@p z27C1mBt7uPW-v66h)_I<{sq5J$Og(er&MgyC zS2lg|sN-gUJvmFubZ8phd0^TuK@A=kj%Dfwomd9lr3{nQTqQ>taBYvKt3l3&zhhOd_A!Un7&u*O zkj4?r(7zAvD{@nnuugCNRLIJ2njP%(*`o;QF)TTF}}Im(yf- oj`iH5_KMnsE&7*lfFH4G`Ka=LeJ5OW(M9K>Z*W6N2>=WL0Q(Qsr2qf` literal 386 zcmV-|0e$`-iwFQV$M0bP1MQUGYQiuS#=Y)S9Auz^&Qz}__n*YL%bwki%>sTVQ^{ccW^eEH?%lpLE`baTCWUga()giuqGAdIbMBZ{aN zk3)$K34thyt$ItUHBlXqDAkad19ZWh4hlSf?($S@huI6;qb@z|=p?VFX8K$<+j=}?g~8K&UjgZ?zY8cByd@+X6d zq4xXONJrh9laa<)BkBO3QGg#i(!nR#81eCW{QainBk&}3nPY1ToTkebOqqVuH(DMkZ~zJb03;{72mk;8 diff --git a/tests/resources/functions/php/index.php b/tests/resources/functions/php/index.php index f70ef96748..352e3c109c 100644 --- a/tests/resources/functions/php/index.php +++ b/tests/resources/functions/php/index.php @@ -1,6 +1,7 @@ json([ 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], From c7a645037a774028087a3ca19f0d0ba836bace87 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 04:34:47 +0400 Subject: [PATCH 11/28] feat: update php test code --- tests/resources/functions/php-fn.tar.gz | Bin 415 -> 388 bytes tests/resources/functions/php-fn/index.php | 1 - tests/resources/functions/php.tar.gz | Bin 446 -> 420 bytes tests/resources/functions/php/index.php | 1 - 4 files changed, 2 deletions(-) diff --git a/tests/resources/functions/php-fn.tar.gz b/tests/resources/functions/php-fn.tar.gz index e4bdd8bae6e1b708d8075fed3228985b6d227dc6..c7467b95b51f7ec4e038635953d1cf9eb8613ff2 100644 GIT binary patch literal 388 zcmV-~0ek)*iwFSCI1pk01MQSuYr-%ThI`##k&A&2{D^JZUbs1mW>T58#Hb7rajv3X zw5}gxW9+{#S}P7*76)aF^}RXi;mwoNb3!UrDRV&x4NU{Ps3#FBD2aQ48d4yFDpG42 zfHf7Wx&*ZBOOvEHjAj8VWxfnnVf4D5&HaZ{K4x1iIGp`|9O55QrMmJS-IvN{xykGd z)(;(_FXykY>|y?jVX*ll-B2U|Gpp!G|9t*eH*E5<9Jo;&cwiBG^JwLJpm-6uPq7TP>5wQl)}ike$GhxS1=Ddx#G-h5JFuJ7>e*APRRGrwkmAY>*LxnH z_DxDWkZpFc7;5L_!&=z6>)m&;O~sH$emAQCjAvUpeT#%+I|K>38pKlK5Os3vo3KxX1Da+sxl_WwzO5#y~mjoz5QD}-h zfV>Drg#mK$r7=ttgiD{4;%xkF5Wek}j`wuVM{MhL4(IqES^OiaH@5Byzt;DgePMf$ zekhXk?feB^5wiJCr+u%4{u{} zs$tAb%|iCwMDJSKNVl~f2il!%pIV3T=I4EjY92^ehlmb!aOT5$$haRp43TcpAy54I zV9V6{ePq&6Q*%DjoamM|L>DyRO9mZ$ip()tU&c?9^0z=!>NY1<3Y^_jE%gFs;{V}H zP$MZ|lQA->&wgsoM-FC`U5t(SD8n2LP}ia^@Wg)vFUVc+(PDr5#w%5-RH;(2J^_4; J{#gJH005na$X);d diff --git a/tests/resources/functions/php-fn/index.php b/tests/resources/functions/php-fn/index.php index 8b729ed40a..17cf700d36 100644 --- a/tests/resources/functions/php-fn/index.php +++ b/tests/resources/functions/php-fn/index.php @@ -1,7 +1,6 @@ json([ 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], diff --git a/tests/resources/functions/php.tar.gz b/tests/resources/functions/php.tar.gz index a7d6026c7e3852ecabe474822258e3793f30d3a4..412915e7a405ea0c930884e4ee526b1a8a0faa00 100644 GIT binary patch literal 420 zcmV;V0bBkbiwFQVIS^t11MQUEYQiuS$Gz@T9Auz^A5l|m1E>=-M|GfVD*%XC-<-UZ;=c@OV%^@U@Z>EU*Aqg12Yeb8h|^j2SKQdw1WSxUie9rn~* zotdlQao;E#22vUw5)HL=hQlP%y;mPQM4{1;0e?7{YI3_xv}n}5IUK3=6)Jbg85{6p zMKt(;XgxkYkH23Oo`MV_Z?kWWfYYT0ZJfdM{XuYFk(;VSbV_=Damn@9`ZEcI0#H>_ zuR&wE@pKBFX-ups1DEFj*MipWpg2u_e^}2wX0Mn{`Q+vz-Qq`?n($E*e*1jKiWMu? ODSZRZ9pD!L3;+Niiq75u literal 446 zcmV;v0YUyBiwFR}H4tI|1MQUCYQjJehI5^#7^I+rhgD;QS}kH$3DiX5YAIHNH7hDK zu{lUf>D%&PhT3xd5$}_St*m5J$Kx<#du;p9r7{TbQ;0ge8(>S6Xo)A)1a@p z27C1mBt7uPW-v66h)_I<{sq5J$Og(er&MgyC zS2lg|sN-gUJvmFubZ8phd0^TuK@A=kj%Dfwomd9lr3{nQTqQ>taBYvKt3l3&zhhOd_A!Un7&u*O zkj4?r(7zAvD{@nnuugCNRLIJ2njP%(*`o;QF)TTF}}Im(yf- oj`iH5_KMnsE&7*lfFH4G`Ka=LeJ5OW(M9K>Z*W6N2>=WL0Q(Qsr2qf` diff --git a/tests/resources/functions/php/index.php b/tests/resources/functions/php/index.php index 352e3c109c..f70ef96748 100644 --- a/tests/resources/functions/php/index.php +++ b/tests/resources/functions/php/index.php @@ -1,7 +1,6 @@ json([ 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], From 4d16072972bbda1ee6d520607133e1c04820bb2f Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 15:08:55 +0400 Subject: [PATCH 12/28] feat: updated test functions and bumped appwrite runtimes version --- app/executor.php | 2 -- composer.json | 2 +- tests/resources/functions/php-fn.tar.gz | Bin 388 -> 390 bytes tests/resources/functions/php-fn/index.php | 24 ++++++++++----------- tests/resources/functions/php.tar.gz | Bin 420 -> 423 bytes tests/resources/functions/php/index.php | 16 +++++++------- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/executor.php b/app/executor.php index 873d50e7b6..9e3a117848 100644 --- a/app/executor.php +++ b/app/executor.php @@ -415,8 +415,6 @@ App::post('/v1/execution') $ch = \curl_init(); $body = \json_encode([ - 'path' => '/usr/code', - 'file' => $entrypoint, 'env' => $vars, 'payload' => $data, 'timeout' => $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) diff --git a/composer.json b/composer.json index 55834b825b..0839feee08 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "ext-zlib": "*", "ext-sockets": "*", "appwrite/php-clamav": "1.1.*", - "appwrite/php-runtimes": "dev-refactor", + "appwrite/runtimes": "0.7.0", "utopia-php/framework": "0.19.*", "utopia-php/logger": "0.1.*", "utopia-php/abuse": "0.7.*", diff --git a/tests/resources/functions/php-fn.tar.gz b/tests/resources/functions/php-fn.tar.gz index c7467b95b51f7ec4e038635953d1cf9eb8613ff2..fddecf1f8ac299875890264e8c7ae3b6607630a8 100644 GIT binary patch literal 390 zcmV;10eSu(iwFSg%MfA!1MQSeZ-PJ&hB@;q_Rs`PtS-3V!PbW8)?g|UL``Fiu@<8q z7R!fe)AYZ06|pw4D|;YKQ{KzK=ACDT8K7JdG8crfrmA2cwKzfr#ql5@BLyNEsWr6< zpj?HhCIB`2)&wC6!kJH7nJ@iS5WcNvbMNV#kJ=Us2Iue`$M{E7uB^zj`&!;CH<{I- z^Ux6basP^}XkYu6Wx9W)X^H?~W*wdApYQ+Xj&5EQeK(AJ0u~XOhbxbO!jc1EeExwEoimfh>% kro++XNyY&^Db~f0y7=3FwOqM!<;rzQpTX`nrT`8A0R4%-G5`Po literal 388 zcmV-~0ek)*iwFSCI1pk01MQSuYr-%ThI`##k&A&2{D^JZUbs1mW>T58#Hb7rajv3X zw5}gxW9+{#S}P7*76)aF^}RXi;mwoNb3!UrDRV&x4NU{Ps3#FBD2aQ48d4yFDpG42 zfHf7Wx&*ZBOOvEHjAj8VWxfnnVf4D5&HaZ{K4x1iIGp`|9O55QrMmJS-IvN{xykGd z)(;(_FXykY>|y?jVX*ll-B2U|Gpp!G|9t*eH*E5<9Jo;&cwiBG^JwLJpm-6uPq7TP>5wQl)}ike$GhxS1=Ddx#G-h5JFuJ7>e*APRRGrwkmAY>*LxnH z_DxDWkZpFc7;5L_!&=z6>)m&;O~sH$emjson([ - 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], - 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], - 'APPWRITE_FUNCTION_DEPLOYMENT' => $request->env['APPWRITE_FUNCTION_DEPLOYMENT'], - 'APPWRITE_FUNCTION_TRIGGER' => $request->env['APPWRITE_FUNCTION_TRIGGER'], - 'APPWRITE_FUNCTION_RUNTIME_NAME' => $request->env['APPWRITE_FUNCTION_RUNTIME_NAME'], - 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $request->env['APPWRITE_FUNCTION_RUNTIME_VERSION'], - 'APPWRITE_FUNCTION_EVENT' => $request->env['APPWRITE_FUNCTION_EVENT'], - 'APPWRITE_FUNCTION_EVENT_DATA' => $request->env['APPWRITE_FUNCTION_EVENT_DATA'], - 'APPWRITE_FUNCTION_DATA' => $request->env['APPWRITE_FUNCTION_DATA'], - 'APPWRITE_FUNCTION_USER_ID' => $request->env['APPWRITE_FUNCTION_USER_ID'], - 'APPWRITE_FUNCTION_JWT' => $request->env['APPWRITE_FUNCTION_JWT'], - 'APPWRITE_FUNCTION_PROJECT_ID' => $request->env['APPWRITE_FUNCTION_PROJECT_ID'], + 'APPWRITE_FUNCTION_ID' => $request['env']['APPWRITE_FUNCTION_ID'], + 'APPWRITE_FUNCTION_NAME' => $request['env']['APPWRITE_FUNCTION_NAME'], + 'APPWRITE_FUNCTION_DEPLOYMENT' => $request['env']['APPWRITE_FUNCTION_DEPLOYMENT'], + 'APPWRITE_FUNCTION_TRIGGER' => $request['env']['APPWRITE_FUNCTION_TRIGGER'], + 'APPWRITE_FUNCTION_RUNTIME_NAME' => $request['env']['APPWRITE_FUNCTION_RUNTIME_NAME'], + 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $request['env']['APPWRITE_FUNCTION_RUNTIME_VERSION'], + 'APPWRITE_FUNCTION_EVENT' => $request['env']['APPWRITE_FUNCTION_EVENT'], + 'APPWRITE_FUNCTION_EVENT_DATA' => $request['env']['APPWRITE_FUNCTION_EVENT_DATA'], + 'APPWRITE_FUNCTION_DATA' => $request['env']['APPWRITE_FUNCTION_DATA'], + 'APPWRITE_FUNCTION_USER_ID' => $request['env']['APPWRITE_FUNCTION_USER_ID'], + 'APPWRITE_FUNCTION_JWT' => $request['env']['APPWRITE_FUNCTION_JWT'], + 'APPWRITE_FUNCTION_PROJECT_ID' => $request['env']['APPWRITE_FUNCTION_PROJECT_ID'], ]); }; diff --git a/tests/resources/functions/php.tar.gz b/tests/resources/functions/php.tar.gz index 412915e7a405ea0c930884e4ee526b1a8a0faa00..4feb138f427631f373f56ec2e80a15314c933d98 100644 GIT binary patch literal 423 zcmV;Y0a*SYiwFSD%MfA!1MQUEYQjJeg>#*!7^GkYHHqCssI5iJDuJ3vTrEXJY_g(4 z6Pq7tDSew>^)-C4uEtmkHpB}lrJkEfvS+@T?66xDxT8xH#j-5JIx0~VODKvPfrLsT zl8{`M6cD8g5h)zV$8U|}LO&RJtQBkGP5t0yHnQA@eLiLzj}thX|JcSqqM|T$Z2DZB zPv%Ff!S+Kz=*Rg>vhp>5MOn=sDP_cgc(jgo^v~yib;Bml^BxUC&w+91Si#hF;QY*^ zPa*Y#Jg_H!?m9lr!7C3;8}Igep*r(j=e)mOTq#>?x9?1i;9;Yy*NJB6Lru-XbroW2 zKTDlOcF^DN9ORQwdaZ@i!q(nooQm7G#(fLxBrS9&BNXcF|p3s~ajF65NRz%q%~a?->vT zAcksOf=LS_+lJ7YTCPnYU{xS+p|LqQex2PX?qi2~3+fTJxVl)^_(871zg76(KJO=; Rbka$&J^^|7@{a%v006z*$_xMi literal 420 zcmV;V0bBkbiwFQVIS^t11MQUEYQiuS$Gz@T9Auz^A5l|m1E>=-M|GfVD*%XC-<-UZ;=c@OV%^@U@Z>EU*Aqg12Yeb8h|^j2SKQdw1WSxUie9rn~* zotdlQao;E#22vUw5)HL=hQlP%y;mPQM4{1;0e?7{YI3_xv}n}5IUK3=6)Jbg85{6p zMKt(;XgxkYkH23Oo`MV_Z?kWWfYYT0ZJfdM{XuYFk(;VSbV_=Damn@9`ZEcI0#H>_ zuR&wE@pKBFX-ups1DEFj*MipWpg2u_e^}2wX0Mn{`Q+vz-Qq`?n($E*e*1jKiWMu? ODSZRZ9pD!L3;+Niiq75u diff --git a/tests/resources/functions/php/index.php b/tests/resources/functions/php/index.php index f70ef96748..dd111af766 100644 --- a/tests/resources/functions/php/index.php +++ b/tests/resources/functions/php/index.php @@ -2,14 +2,14 @@ return function ($request, $response) { return $response->json([ - 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], - 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], - 'APPWRITE_FUNCTION_DEPLOYMENT' => $request->env['APPWRITE_FUNCTION_DEPLOYMENT'], - 'APPWRITE_FUNCTION_TRIGGER' => $request->env['APPWRITE_FUNCTION_TRIGGER'], - 'APPWRITE_FUNCTION_RUNTIME_NAME' => $request->env['APPWRITE_FUNCTION_RUNTIME_NAME'], - 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $request->env['APPWRITE_FUNCTION_RUNTIME_VERSION'], - 'APPWRITE_FUNCTION_EVENT' => $request->env['APPWRITE_FUNCTION_EVENT'], - 'APPWRITE_FUNCTION_EVENT_DATA' => $request->env['APPWRITE_FUNCTION_EVENT_DATA'], + 'APPWRITE_FUNCTION_ID' => $request['env']['APPWRITE_FUNCTION_ID'], + 'APPWRITE_FUNCTION_NAME' => $request['env']['APPWRITE_FUNCTION_NAME'], + 'APPWRITE_FUNCTION_DEPLOYMENT' => $request['env']['APPWRITE_FUNCTION_DEPLOYMENT'], + 'APPWRITE_FUNCTION_TRIGGER' => $request['env']['APPWRITE_FUNCTION_TRIGGER'], + 'APPWRITE_FUNCTION_RUNTIME_NAME' => $request['env']['APPWRITE_FUNCTION_RUNTIME_NAME'], + 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $request['env']['APPWRITE_FUNCTION_RUNTIME_VERSION'], + 'APPWRITE_FUNCTION_EVENT' => $request['env']['APPWRITE_FUNCTION_EVENT'], + 'APPWRITE_FUNCTION_EVENT_DATA' => $request['env']['APPWRITE_FUNCTION_EVENT_DATA'], 'UNICODE_TEST' => "êä" // TODO: Re-add unicode test to FunctionsCustomServerTest.php ]); }; From c94913d3f2a03589af45a20860bfb198b9905693 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 15:11:34 +0400 Subject: [PATCH 13/28] feat: updated test functions and bumped appwrite runtimes version --- app/executor.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/executor.php b/app/executor.php index 9e3a117848..e6e09216da 100644 --- a/app/executor.php +++ b/app/executor.php @@ -41,9 +41,10 @@ use Utopia\Validator\Text; // Decide on logic for build and runtime containers names ( runtime-ID and build-ID) - done // Add size validators for the runtime IDs - done // Fix logging - done +// Incorporate Matej's changes in the build stage ( moving of the tar file will be performed by the runtime and not the build stage ) - done + // Fix delete endpoint -// Incorporate Matej's changes in the build stage ( moving of the tar file will be performed by the runtime and not the build stage ) Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); From cc8fac0297dde483cb2e288a33d5f006fb90ea5e Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 17:57:48 +0400 Subject: [PATCH 14/28] feat: improve functions tests --- .gitignore | 1 + composer.json | 2 +- composer.lock | 30 +- .../e2e/Services/Functions/FunctionsBase.php | 8 +- .../Functions/FunctionsCustomClientTest.php | 19 +- .../Functions/FunctionsCustomServerTest.php | 130 +++++- tests/resources/functions/node/index.js | 28 ++ .../functions/node/package-lock.json | 376 ------------------ tests/resources/functions/package-php-fn.sh | 12 - tests/resources/functions/package-php.sh | 12 - tests/resources/functions/package-timeout.sh | 10 - tests/resources/functions/php-fn.tar.gz | Bin 390 -> 0 bytes tests/resources/functions/php.tar.gz | Bin 423 -> 0 bytes .../functions/packages/php-fn/code.tar.gz | Bin 104 -> 0 bytes tests/resources/functions/timeout.tar.gz | Bin 204 -> 0 bytes 15 files changed, 189 insertions(+), 439 deletions(-) create mode 100644 tests/resources/functions/node/index.js delete mode 100644 tests/resources/functions/node/package-lock.json delete mode 100755 tests/resources/functions/package-php-fn.sh delete mode 100755 tests/resources/functions/package-php.sh delete mode 100644 tests/resources/functions/package-timeout.sh delete mode 100644 tests/resources/functions/php-fn.tar.gz delete mode 100644 tests/resources/functions/php.tar.gz delete mode 100644 tests/resources/functions/tests/resources/functions/packages/php-fn/code.tar.gz delete mode 100644 tests/resources/functions/timeout.tar.gz diff --git a/.gitignore b/.gitignore index 5433e2ba3a..3d6001ca06 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /vendor/ /node_modules/ /tests/resources/storage/ +/tests/resources/functions/**/code.tar.gz /app/sdks/* /.idea/ .DS_Store diff --git a/composer.json b/composer.json index 0839feee08..e3e134dfa0 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "ext-zlib": "*", "ext-sockets": "*", "appwrite/php-clamav": "1.1.*", - "appwrite/runtimes": "0.7.0", + "appwrite/php-runtimes": "0.7.0", "utopia-php/framework": "0.19.*", "utopia-php/logger": "0.1.*", "utopia-php/abuse": "0.7.*", diff --git a/composer.lock b/composer.lock index 2a68fa1d19..2b439e5505 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e28c5946b102f637cc247b026f42ae0e", + "content-hash": "366fe0389feaf13e3b3132b684ea2e20", "packages": [ { "name": "adhocore/jwt", @@ -115,20 +115,18 @@ }, { "name": "appwrite/php-runtimes", - "version": "dev-refactor", + "version": "0.7.0", "source": { "type": "git", "url": "https://github.com/appwrite/runtimes.git", - "reference": "ea44eb19af851a3fe4ecf73e789f374f70d778c5" + "reference": "e41ecbfec8c4adb1f5da01dc70c2578a7e32e9c6" }, "require": { "php": ">=8.0", - "utopia-php/orchestration": "0.4.*", "utopia-php/system": "0.4.*" }, "require-dev": { "phpunit/phpunit": "^9.3", - "utopia-php/cli": "0.11.*", "vimeo/psalm": "4.0.1" }, "type": "library", @@ -148,10 +146,6 @@ { "name": "Torsten Dittmann", "email": "torsten@appwrite.io" - }, - { - "name": "Bradley Schofield", - "email": "bradley@appwrite.io" } ], "description": "Appwrite repository for Cloud Function runtimes that contains the configurations and tests for all of the Appwrite runtime environments.", @@ -160,7 +154,7 @@ "php", "runtimes" ], - "time": "2022-02-17T12:06:12+00:00" + "time": "2022-02-19T00:08:41+00:00" }, { "name": "chillerlan/php-qrcode", @@ -4235,16 +4229,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.10", + "version": "9.2.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687" + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/d5850aaf931743067f4bfc1ae4cbd06468400687", - "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/665a1ac0a763c51afc30d6d130dac0813092b17f", + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f", "shasum": "" }, "require": { @@ -4300,7 +4294,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.10" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.11" }, "funding": [ { @@ -4308,7 +4302,7 @@ "type": "github" } ], - "time": "2021-12-05T09:12:13+00:00" + "time": "2022-02-18T12:46:09+00:00" }, { "name": "phpunit/php-file-iterator", @@ -6638,9 +6632,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "appwrite/php-runtimes": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/tests/e2e/Services/Functions/FunctionsBase.php b/tests/e2e/Services/Functions/FunctionsBase.php index a167b3763a..ff1b37c7a9 100644 --- a/tests/e2e/Services/Functions/FunctionsBase.php +++ b/tests/e2e/Services/Functions/FunctionsBase.php @@ -3,10 +3,16 @@ namespace Tests\E2E\Services\Functions; use Tests\E2E\Client; +use Utopia\CLI\Console; trait FunctionsBase { - + protected string $stdout = ''; + protected string $stderr = ''; + + protected function packageCode($folder) { + Console::execute('cd '.realpath(__DIR__ . "/../../../resources/functions") . "/$folder && tar --exclude code.tar.gz -czf code.tar.gz .", '', $this->stdout, $this->stderr); + } // /** // * @depends testCreateTeam diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 26e41542ff..bedeee6ed6 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -7,6 +7,7 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideClient; +use Utopia\CLI\Console; use Utopia\Database\Database; class FunctionsCustomClientTest extends Scope @@ -73,13 +74,17 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals(201, $function['headers']['status-code']); + $folder = 'php'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); + $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$function['body']['$id'].'/deployments', [ 'content-type' => 'multipart/form-data', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'], ], [ 'entrypoint' => 'index.php', - 'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/php.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'), + 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), ]); $deploymentId = $deployment['body']['$id'] ?? ''; @@ -156,13 +161,17 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals(201, $function['headers']['status-code']); + $folder = 'php-fn'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); + $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/deployments', [ 'content-type' => 'multipart/form-data', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $apikey, ], [ 'entrypoint' => 'index.php', - 'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/php-fn.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'), //different tarball names intentional + 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional ]); $deploymentId = $deployment['body']['$id'] ?? ''; @@ -338,13 +347,17 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals(201, $function['headers']['status-code']); + $folder = 'php-fn'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); + $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/deployments', [ 'content-type' => 'multipart/form-data', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $apikey, ], [ 'entrypoint' => 'index.php', - 'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/php-fn.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'), //different tarball names intentional + 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional ]); $deploymentId = $deployment['body']['$id'] ?? ''; diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index deb1b5b205..425a976ecc 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -7,6 +7,7 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; +use Utopia\CLI\Console; use Utopia\Database\Database; class FunctionsCustomServerTest extends Scope @@ -277,12 +278,16 @@ class FunctionsCustomServerTest extends Scope /** * Test for SUCCESS */ + $folder = 'php'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); + $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$data['functionId'].'/deployments', array_merge([ 'content-type' => 'multipart/form-data', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'entrypoint' => 'index.php', - 'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/php.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'), + 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), ]); $deploymentId = $deployment['body']['$id'] ?? ''; @@ -650,10 +655,12 @@ class FunctionsCustomServerTest extends Scope public function testTimeout() { $name = 'php-8.0'; - $code = realpath(__DIR__ . '/../../../resources/functions').'/timeout.tar.gz'; $entrypoint = 'index.php'; $timeout = 2; - + $folder = 'timeout'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); + $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -736,12 +743,14 @@ class FunctionsCustomServerTest extends Scope /** * @depends testTimeout */ - public function testCreateCustomExecution() + public function testCreateCustomPHPExecution() { $name = 'php-8.0'; - $code = realpath(__DIR__ . '/../../../resources/functions').'/php-fn.tar.gz'; $entrypoint = 'index.php'; $timeout = 2; + $folder = 'php-fn'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ 'content-type' => 'application/json', @@ -841,6 +850,117 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $response['headers']['status-code']); } + // /** + // * @depends testTimeout + // */ + public function testCreateCustomNodeExecution() + { + $name = 'node-17.0'; + $folder = 'node'; + $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + $this->packageCode($folder); + + $entrypoint = 'index.js'; + $timeout = 2; + + $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'functionId' => 'unique()', + 'name' => 'Test '.$name, + 'runtime' => $name, + 'vars' => [], + 'events' => [], + 'schedule' => '', + 'timeout' => $timeout, + ]); + + $functionId = $function['body']['$id'] ?? ''; + + $this->assertEquals(201, $function['headers']['status-code']); + + $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/deployments', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'entrypoint' => $entrypoint, + 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), + ]); + + $deploymentId = $deployment['body']['$id'] ?? ''; + $this->assertEquals(201, $deployment['headers']['status-code']); + + // Allow build step to run + sleep(10); + + $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/'.$functionId.'/deployments/'.$deploymentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $deployment['headers']['status-code']); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'data' => 'foobar', + ]); + + $executionId = $execution['body']['$id'] ?? ''; + + $this->assertEquals(201, $execution['headers']['status-code']); + + $executionId = $execution['body']['$id'] ?? ''; + + sleep(10); + + $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $output = json_decode($executions['body']['stdout'], true); + + $this->assertEquals(200, $executions['headers']['status-code']); + $this->assertEquals('completed', $executions['body']['status']); + $this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']); + $this->assertEquals('Test '.$name, $output['APPWRITE_FUNCTION_NAME']); + $this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']); + $this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']); + $this->assertEquals('Node.js', $output['APPWRITE_FUNCTION_RUNTIME_NAME']); + $this->assertEquals('17.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']); + $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']); + $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']); + $this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']); + $this->assertEquals('', $output['APPWRITE_FUNCTION_USER_ID']); + $this->assertEmpty($output['APPWRITE_FUNCTION_JWT']); + $this->assertEquals($this->getProject()['$id'], $output['APPWRITE_FUNCTION_PROJECT_ID']); + + $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($executions['headers']['status-code'], 200); + $this->assertEquals($executions['body']['sum'], 1); + $this->assertIsArray($executions['body']['executions']); + $this->assertCount(1, $executions['body']['executions']); + $this->assertEquals($executions['body']['executions'][0]['$id'], $executionId); + $this->assertEquals($executions['body']['executions'][0]['trigger'], 'http'); + $this->assertStringContainsString('foobar', $executions['body']['executions'][0]['stdout']); + + // Cleanup : Delete function + $response = $this->client->call(Client::METHOD_DELETE, '/functions/'. $functionId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], []); + + $this->assertEquals(204, $response['headers']['status-code']); + } + public function testGetRuntimes() { diff --git a/tests/resources/functions/node/index.js b/tests/resources/functions/node/index.js new file mode 100644 index 0000000000..ea62c50ac7 --- /dev/null +++ b/tests/resources/functions/node/index.js @@ -0,0 +1,28 @@ +/* + 'req' variable has: + 'headers' - object with request headers + 'payload' - object with request body data + 'env' - object with environment variables + 'res' variable has: + 'send(text, status)' - function to return text response. Status code defaults to 200 + 'json(obj, status)' - function to return JSON response. Status code defaults to 200 + + If an error is thrown, a response with code 500 will be returned. +*/ + +module.exports = async (req, res) => { + res.json({ + 'APPWRITE_FUNCTION_ID' : req.env.APPWRITE_FUNCTION_ID, + 'APPWRITE_FUNCTION_NAME' : req.env.APPWRITE_FUNCTION_NAME, + 'APPWRITE_FUNCTION_DEPLOYMENT' : req.env.APPWRITE_FUNCTION_DEPLOYMENT, + 'APPWRITE_FUNCTION_TRIGGER' : req.env.APPWRITE_FUNCTION_TRIGGER, + 'APPWRITE_FUNCTION_RUNTIME_NAME' : req.env.APPWRITE_FUNCTION_RUNTIME_NAME, + 'APPWRITE_FUNCTION_RUNTIME_VERSION' : req.env.APPWRITE_FUNCTION_RUNTIME_VERSION, + 'APPWRITE_FUNCTION_EVENT' : req.env.APPWRITE_FUNCTION_EVENT, + 'APPWRITE_FUNCTION_EVENT_DATA' : req.env.APPWRITE_FUNCTION_EVENT_DATA, + 'APPWRITE_FUNCTION_DATA' : req.env.APPWRITE_FUNCTION_DATA, + 'APPWRITE_FUNCTION_USER_ID' : req.env.APPWRITE_FUNCTION_USER_ID, + 'APPWRITE_FUNCTION_JWT' : req.env.APPWRITE_FUNCTION_JWT, + 'APPWRITE_FUNCTION_PROJECT_ID' : req.env.APPWRITE_FUNCTION_PROJECT_ID, + }); +} \ No newline at end of file diff --git a/tests/resources/functions/node/package-lock.json b/tests/resources/functions/node/package-lock.json deleted file mode 100644 index 0602482860..0000000000 --- a/tests/resources/functions/node/package-lock.json +++ /dev/null @@ -1,376 +0,0 @@ -{ - "name": "appwrite-cloud-function-demo", - "version": "0.1.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "ajv": { - "version": "6.12.6", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", - "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", - "requires": { - "fast-deep-equal": "^3.1.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.2" - } - }, - "asn1": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", - "integrity": "sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==", - "requires": { - "safer-buffer": "~2.1.0" - } - }, - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" - }, - "asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" - }, - "aws-sign2": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz", - "integrity": "sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg=" - }, - "aws4": { - "version": "1.11.0", - "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz", - "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==" - }, - "bcrypt-pbkdf": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", - "integrity": "sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4=", - "requires": { - "tweetnacl": "^0.14.3" - } - }, - "caseless": { - "version": "0.12.0", - "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", - "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" - }, - "combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "requires": { - "delayed-stream": "~1.0.0" - } - }, - "core-util-is": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", - "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" - }, - "dashdash": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", - "integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=", - "requires": { - "assert-plus": "^1.0.0" - } - }, - "delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" - }, - "ecc-jsbn": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", - "integrity": "sha1-OoOpBOVDUyh4dMVkt1SThoSamMk=", - "requires": { - "jsbn": "~0.1.0", - "safer-buffer": "^2.1.0" - } - }, - "extend": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", - "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==" - }, - "extsprintf": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz", - "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=" - }, - "fast-deep-equal": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", - "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" - }, - "fast-json-stable-stringify": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", - "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" - }, - "forever-agent": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", - "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=" - }, - "form-data": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz", - "integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==", - "requires": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.6", - "mime-types": "^2.1.12" - } - }, - "getpass": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", - "integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=", - "requires": { - "assert-plus": "^1.0.0" - } - }, - "har-schema": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", - "integrity": "sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI=" - }, - "har-validator": { - "version": "5.1.5", - "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.5.tgz", - "integrity": "sha512-nmT2T0lljbxdQZfspsno9hgrG3Uir6Ks5afism62poxqBM6sDnMEuPmzTq8XN0OEwqKLLdh1jQI3qyE66Nzb3w==", - "requires": { - "ajv": "^6.12.3", - "har-schema": "^2.0.0" - } - }, - "http-signature": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", - "integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=", - "requires": { - "assert-plus": "^1.0.0", - "jsprim": "^1.2.2", - "sshpk": "^1.7.0" - } - }, - "is-typedarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" - }, - "isstream": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", - "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" - }, - "jsbn": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", - "integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=" - }, - "json-schema": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", - "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=" - }, - "json-schema-traverse": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", - "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" - }, - "json-stringify-safe": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", - "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=" - }, - "jsprim": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", - "integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=", - "requires": { - "assert-plus": "1.0.0", - "extsprintf": "1.3.0", - "json-schema": "0.2.3", - "verror": "1.10.0" - } - }, - "lodash": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" - }, - "mime-db": { - "version": "1.44.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", - "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" - }, - "mime-types": { - "version": "2.1.27", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", - "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", - "requires": { - "mime-db": "1.44.0" - } - }, - "node-appwrite": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-1.1.0.tgz", - "integrity": "sha512-ZLOsxmsGry4ZRT63QRbNgUxhYiUzJlQntQL2nKAy6PBL1S/hMCChKrdLIdJv3ytGxPrrIWiDWxXivMeUeCW5KA==", - "requires": { - "request": "^2.88.0", - "request-promise-native": "^1.0.7" - } - }, - "oauth-sign": { - "version": "0.9.0", - "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", - "integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==" - }, - "performance-now": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", - "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" - }, - "psl": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", - "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==" - }, - "punycode": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" - }, - "qs": { - "version": "6.5.2", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", - "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" - }, - "request": { - "version": "2.88.2", - "resolved": "https://registry.npmjs.org/request/-/request-2.88.2.tgz", - "integrity": "sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw==", - "requires": { - "aws-sign2": "~0.7.0", - "aws4": "^1.8.0", - "caseless": "~0.12.0", - "combined-stream": "~1.0.6", - "extend": "~3.0.2", - "forever-agent": "~0.6.1", - "form-data": "~2.3.2", - "har-validator": "~5.1.3", - "http-signature": "~1.2.0", - "is-typedarray": "~1.0.0", - "isstream": "~0.1.2", - "json-stringify-safe": "~5.0.1", - "mime-types": "~2.1.19", - "oauth-sign": "~0.9.0", - "performance-now": "^2.1.0", - "qs": "~6.5.2", - "safe-buffer": "^5.1.2", - "tough-cookie": "~2.5.0", - "tunnel-agent": "^0.6.0", - "uuid": "^3.3.2" - } - }, - "request-promise-core": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.4.tgz", - "integrity": "sha512-TTbAfBBRdWD7aNNOoVOBH4pN/KigV6LyapYNNlAPA8JwbovRti1E88m3sYAwsLi5ryhPKsE9APwnjFTgdUjTpw==", - "requires": { - "lodash": "^4.17.19" - } - }, - "request-promise-native": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/request-promise-native/-/request-promise-native-1.0.9.tgz", - "integrity": "sha512-wcW+sIUiWnKgNY0dqCpOZkUbF/I+YPi+f09JZIDa39Ec+q82CpSYniDp+ISgTTbKmnpJWASeJBPZmoxH84wt3g==", - "requires": { - "request-promise-core": "1.1.4", - "stealthy-require": "^1.1.1", - "tough-cookie": "^2.3.3" - } - }, - "safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" - }, - "safer-buffer": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", - "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" - }, - "sshpk": { - "version": "1.16.1", - "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", - "integrity": "sha512-HXXqVUq7+pcKeLqqZj6mHFUMvXtOJt1uoUx09pFW6011inTMxqI8BA8PM95myrIyyKwdnzjdFjLiE6KBPVtJIg==", - "requires": { - "asn1": "~0.2.3", - "assert-plus": "^1.0.0", - "bcrypt-pbkdf": "^1.0.0", - "dashdash": "^1.12.0", - "ecc-jsbn": "~0.1.1", - "getpass": "^0.1.1", - "jsbn": "~0.1.0", - "safer-buffer": "^2.0.2", - "tweetnacl": "~0.14.0" - } - }, - "stealthy-require": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", - "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" - }, - "tough-cookie": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz", - "integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==", - "requires": { - "psl": "^1.1.28", - "punycode": "^2.1.1" - } - }, - "tunnel-agent": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", - "integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=", - "requires": { - "safe-buffer": "^5.0.1" - } - }, - "tweetnacl": { - "version": "0.14.5", - "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", - "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=" - }, - "uri-js": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.0.tgz", - "integrity": "sha512-B0yRTzYdUCCn9n+F4+Gh4yIDtMQcaJsmYBDsTSG8g/OejKBodLQ2IHfN3bM7jUsRXndopT7OIXWdYqc1fjmV6g==", - "requires": { - "punycode": "^2.1.0" - } - }, - "uuid": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", - "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==" - }, - "verror": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz", - "integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=", - "requires": { - "assert-plus": "^1.0.0", - "core-util-is": "1.0.2", - "extsprintf": "^1.2.0" - } - } - } -} diff --git a/tests/resources/functions/package-php-fn.sh b/tests/resources/functions/package-php-fn.sh deleted file mode 100755 index bbb4920cbc..0000000000 --- a/tests/resources/functions/package-php-fn.sh +++ /dev/null @@ -1,12 +0,0 @@ - -echo 'PHP Packaging...' - -cp -r $(pwd)/tests/resources/functions/php-fn $(pwd)/tests/resources/functions/packages/php-fn - -docker run --rm -v $(pwd)/tests/resources/functions/packages/php-fn:/app -w /app composer:2.0 composer install --ignore-platform-reqs - -docker run --rm -v $(pwd)/tests/resources/functions/packages/php-fn:/app -w /app appwrite/env-php-8.0:1.0.0 tar -zcvf code.tar.gz . - -mv $(pwd)/tests/resources/functions/packages/php-fn/code.tar.gz $(pwd)/tests/resources/functions/php-fn.tar.gz - -rm -r $(pwd)/tests/resources/functions/packages/php-fn diff --git a/tests/resources/functions/package-php.sh b/tests/resources/functions/package-php.sh deleted file mode 100755 index 86f0844d9f..0000000000 --- a/tests/resources/functions/package-php.sh +++ /dev/null @@ -1,12 +0,0 @@ - -echo 'PHP Packaging...' - -cp -r $(pwd)/tests/resources/functions/php $(pwd)/tests/resources/functions/packages/php - -docker run --rm -v $(pwd)/tests/resources/functions/packages/php:/app -w /app composer:2.0 composer install --ignore-platform-reqs - -docker run --rm -v $(pwd)/tests/resources/functions/packages/php:/app -w /app appwrite/env-php-8.0:1.0.0 tar -zcvf code.tar.gz . - -mv $(pwd)/tests/resources/functions/packages/php/code.tar.gz $(pwd)/tests/resources/functions/php.tar.gz - -rm -r $(pwd)/tests/resources/functions/packages/php \ No newline at end of file diff --git a/tests/resources/functions/package-timeout.sh b/tests/resources/functions/package-timeout.sh deleted file mode 100644 index 6ea5eeab64..0000000000 --- a/tests/resources/functions/package-timeout.sh +++ /dev/null @@ -1,10 +0,0 @@ - -echo 'Timeout Packaging...' - -cp -r $(pwd)/tests/resources/functions/timeout $(pwd)/tests/resources/functions/packages/timeout - -docker run --rm -v $(pwd)/tests/resources/functions/packages/timeout:/app -w /app appwrite/env-php-8.0:1.0.0 tar -zcvf code.tar.gz . - -mv $(pwd)/tests/resources/functions/packages/timeout/code.tar.gz $(pwd)/tests/resources/functions/timeout.tar.gz - -rm -r $(pwd)/tests/resources/functions/packages/timeout \ No newline at end of file diff --git a/tests/resources/functions/php-fn.tar.gz b/tests/resources/functions/php-fn.tar.gz deleted file mode 100644 index fddecf1f8ac299875890264e8c7ae3b6607630a8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 390 zcmV;10eSu(iwFSg%MfA!1MQSeZ-PJ&hB@;q_Rs`PtS-3V!PbW8)?g|UL``Fiu@<8q z7R!fe)AYZ06|pw4D|;YKQ{KzK=ACDT8K7JdG8crfrmA2cwKzfr#ql5@BLyNEsWr6< zpj?HhCIB`2)&wC6!kJH7nJ@iS5WcNvbMNV#kJ=Us2Iue`$M{E7uB^zj`&!;CH<{I- z^Ux6basP^}XkYu6Wx9W)X^H?~W*wdApYQ+Xj&5EQeK(AJ0u~XOhbxbO!jc1EeExwEoimfh>% kro++XNyY&^Db~f0y7=3FwOqM!<;rzQpTX`nrT`8A0R4%-G5`Po diff --git a/tests/resources/functions/php.tar.gz b/tests/resources/functions/php.tar.gz deleted file mode 100644 index 4feb138f427631f373f56ec2e80a15314c933d98..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 423 zcmV;Y0a*SYiwFSD%MfA!1MQUEYQjJeg>#*!7^GkYHHqCssI5iJDuJ3vTrEXJY_g(4 z6Pq7tDSew>^)-C4uEtmkHpB}lrJkEfvS+@T?66xDxT8xH#j-5JIx0~VODKvPfrLsT zl8{`M6cD8g5h)zV$8U|}LO&RJtQBkGP5t0yHnQA@eLiLzj}thX|JcSqqM|T$Z2DZB zPv%Ff!S+Kz=*Rg>vhp>5MOn=sDP_cgc(jgo^v~yib;Bml^BxUC&w+91Si#hF;QY*^ zPa*Y#Jg_H!?m9lr!7C3;8}Igep*r(j=e)mOTq#>?x9?1i;9;Yy*NJB6Lru-XbroW2 zKTDlOcF^DN9ORQwdaZ@i!q(nooQm7G#(fLxBrS9&BNXcF|p3s~ajF65NRz%q%~a?->vT zAcksOf=LS_+lJ7YTCPnYU{xS+p|LqQex2PX?qi2~3+fTJxVl)^_(871zg76(KJO=; Rbka$&J^^|7@{a%v006z*$_xMi diff --git a/tests/resources/functions/tests/resources/functions/packages/php-fn/code.tar.gz b/tests/resources/functions/tests/resources/functions/packages/php-fn/code.tar.gz deleted file mode 100644 index a58d1389354d511a7c08c752398c3f2bf1751ba5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 104 zcmb2|=3oE;Cg!*2Hu5$*2(TQ`eyY8}^5y*X$3+zqU&#A-G$?nZO|s22-*MeK&-_hQ zw)?wtm3JbIkIyPSbAR3UHG#MOu6JE?|EckGq0c6|;k)Z?!3Kf|n+)dIUn{E_G#D5F Dr@krW diff --git a/tests/resources/functions/timeout.tar.gz b/tests/resources/functions/timeout.tar.gz deleted file mode 100644 index b1675928a0f3783a2baae7548c0347abde8a94da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 204 zcmV;-05ks|iwFQP!Z2X~1MQJb3c@fDg|p5nX5pd*X_Gc-78N{;)QAO1Ykoiw@9qe- z%PN*4g?t2FLWaqkyeub+!x&Ri0xDOM4a#3S0$gxhMNx?YupCbWRZ;I`^DbC|EPqR$X}^3f5jCrUY1M{n4QTtyz_56(>4=zpYB(h|89@T zQ);yj*1##4#^a=rY^=Tp?fe#S=lae#ox+_0;x1bEiA-zysQ+z05{X12Kk@{hGL=*S G3IG6|3|=Y# From 7a6d8502de48ffa9b23e3a9d824f75356c7199e7 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 19:14:35 +0400 Subject: [PATCH 15/28] feat: improve functions tests --- tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 5 ++++- tests/resources/functions/node/index.js | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 425a976ecc..9d434fa425 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -870,7 +870,9 @@ class FunctionsCustomServerTest extends Scope 'functionId' => 'unique()', 'name' => 'Test '.$name, 'runtime' => $name, - 'vars' => [], + 'vars' => [ + 'CUSTOM_VARIABLE' => 'variable', + ], 'events' => [], 'schedule' => '', 'timeout' => $timeout, @@ -934,6 +936,7 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']); $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']); $this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']); + $this->assertEquals('variable', $output['CUSTOM_VARIABLE']); $this->assertEquals('', $output['APPWRITE_FUNCTION_USER_ID']); $this->assertEmpty($output['APPWRITE_FUNCTION_JWT']); $this->assertEquals($this->getProject()['$id'], $output['APPWRITE_FUNCTION_PROJECT_ID']); diff --git a/tests/resources/functions/node/index.js b/tests/resources/functions/node/index.js index ea62c50ac7..8dc0fbef39 100644 --- a/tests/resources/functions/node/index.js +++ b/tests/resources/functions/node/index.js @@ -24,5 +24,6 @@ module.exports = async (req, res) => { 'APPWRITE_FUNCTION_USER_ID' : req.env.APPWRITE_FUNCTION_USER_ID, 'APPWRITE_FUNCTION_JWT' : req.env.APPWRITE_FUNCTION_JWT, 'APPWRITE_FUNCTION_PROJECT_ID' : req.env.APPWRITE_FUNCTION_PROJECT_ID, + 'CUSTOM_VARIABLE' : req.env.CUSTOM_VARIABLE }); } \ No newline at end of file From be4c1d756ebfc765a326a29dee61ae497d2f063b Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 20:14:31 +0400 Subject: [PATCH 16/28] feat: improve functions tests --- .../Functions/FunctionsCustomServerTest.php | 129 +++++++++++++++--- tests/resources/functions/deno/mod.ts | 30 ++++ 2 files changed, 139 insertions(+), 20 deletions(-) create mode 100644 tests/resources/functions/deno/mod.ts diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9d434fa425..06765086cc 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -684,20 +684,13 @@ class FunctionsCustomServerTest extends Scope ], $this->getHeaders()), [ 'entrypoint' => $entrypoint, 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), + 'activate' => true, ]); - $deploymentId = $deployment['body']['$id'] ?? ''; $this->assertEquals(201, $deployment['headers']['status-code']); // Allow build step to run - sleep(10); - - $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/'.$functionId.'/deployments/'.$deploymentId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(200, $deployment['headers']['status-code']); + sleep(5); $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ 'content-type' => 'application/json', @@ -710,7 +703,7 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(201, $execution['headers']['status-code']); - sleep(10); + sleep(5); $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([ 'content-type' => 'application/json', @@ -850,9 +843,7 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $response['headers']['status-code']); } - // /** - // * @depends testTimeout - // */ + public function testCreateCustomNodeExecution() { $name = 'node-17.0'; @@ -888,6 +879,7 @@ class FunctionsCustomServerTest extends Scope ], $this->getHeaders()), [ 'entrypoint' => $entrypoint, 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), + 'activate' => true, ]); $deploymentId = $deployment['body']['$id'] ?? ''; @@ -896,13 +888,6 @@ class FunctionsCustomServerTest extends Scope // Allow build step to run sleep(10); - $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/'.$functionId.'/deployments/'.$deploymentId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(200, $deployment['headers']['status-code']); - $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -964,6 +949,110 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $response['headers']['status-code']); } + // public function testCreateCustomDenoExecution() + // { + // $name = 'deno-1.14'; + // $folder = 'deno'; + // $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; + // $this->packageCode($folder); + // $entrypoint = 'mod.ts'; + // $timeout = 2; + + // $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ + // 'content-type' => 'application/json', + // 'x-appwrite-project' => $this->getProject()['$id'], + // ], $this->getHeaders()), [ + // 'functionId' => 'unique()', + // 'name' => 'Test '.$name, + // 'runtime' => $name, + // 'vars' => [ + // 'CUSTOM_VARIABLE' => 'variable', + // ], + // 'events' => [], + // 'schedule' => '', + // 'timeout' => $timeout, + // ]); + + // $functionId = $function['body']['$id'] ?? ''; + + // $this->assertEquals(201, $function['headers']['status-code']); + + // $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/deployments', array_merge([ + // 'content-type' => 'multipart/form-data', + // 'x-appwrite-project' => $this->getProject()['$id'], + // ], $this->getHeaders()), [ + // 'entrypoint' => $entrypoint, + // 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), + // 'activate' => true, + // ]); + + // $deploymentId = $deployment['body']['$id'] ?? ''; + // $this->assertEquals(201, $deployment['headers']['status-code']); + + // // Allow build step to run + // sleep(10); + + // $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ + // 'content-type' => 'application/json', + // 'x-appwrite-project' => $this->getProject()['$id'], + // ], $this->getHeaders()), [ + // 'data' => 'foobar', + // ]); + + // $executionId = $execution['body']['$id'] ?? ''; + + // $this->assertEquals(201, $execution['headers']['status-code']); + + // $executionId = $execution['body']['$id'] ?? ''; + + // sleep(10); + + // $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, array_merge([ + // 'content-type' => 'application/json', + // 'x-appwrite-project' => $this->getProject()['$id'], + // ], $this->getHeaders())); + + // $output = json_decode($executions['body']['stdout'], true); + + // $this->assertEquals(200, $executions['headers']['status-code']); + // $this->assertEquals('completed', $executions['body']['status']); + // $this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']); + // $this->assertEquals('Test '.$name, $output['APPWRITE_FUNCTION_NAME']); + // $this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']); + // $this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']); + // $this->assertEquals('Node.js', $output['APPWRITE_FUNCTION_RUNTIME_NAME']); + // $this->assertEquals('17.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']); + // $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']); + // $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']); + // $this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']); + // $this->assertEquals('variable', $output['CUSTOM_VARIABLE']); + // $this->assertEquals('', $output['APPWRITE_FUNCTION_USER_ID']); + // $this->assertEmpty($output['APPWRITE_FUNCTION_JWT']); + // $this->assertEquals($this->getProject()['$id'], $output['APPWRITE_FUNCTION_PROJECT_ID']); + + // $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([ + // 'content-type' => 'application/json', + // 'x-appwrite-project' => $this->getProject()['$id'], + // ], $this->getHeaders())); + + // $this->assertEquals($executions['headers']['status-code'], 200); + // $this->assertEquals($executions['body']['sum'], 1); + // $this->assertIsArray($executions['body']['executions']); + // $this->assertCount(1, $executions['body']['executions']); + // $this->assertEquals($executions['body']['executions'][0]['$id'], $executionId); + // $this->assertEquals($executions['body']['executions'][0]['trigger'], 'http'); + // $this->assertStringContainsString('foobar', $executions['body']['executions'][0]['stdout']); + + // // Cleanup : Delete function + // $response = $this->client->call(Client::METHOD_DELETE, '/functions/'. $functionId, [ + // 'content-type' => 'application/json', + // 'x-appwrite-project' => $this->getProject()['$id'], + // 'x-appwrite-key' => $this->getProject()['apiKey'], + // ], []); + + // $this->assertEquals(204, $response['headers']['status-code']); + // } + public function testGetRuntimes() { diff --git a/tests/resources/functions/deno/mod.ts b/tests/resources/functions/deno/mod.ts new file mode 100644 index 0000000000..041f83ac97 --- /dev/null +++ b/tests/resources/functions/deno/mod.ts @@ -0,0 +1,30 @@ + +/* + 'req' variable has: + 'headers' - object with request headers + 'payload' - object with request body data + 'env' - object with environment variables + 'res' variable has: + 'send(text, status)' - function to return text response. Status code defaults to 200 + 'json(obj, status)' - function to return JSON response. Status code defaults to 200 + + If an error is thrown, a response with code 500 will be returned. +*/ + +export default async function(req: any, res: any) { + res.json({ + 'APPWRITE_FUNCTION_ID' : req.env.APPWRITE_FUNCTION_ID, + 'APPWRITE_FUNCTION_NAME' : req.env.APPWRITE_FUNCTION_NAME, + 'APPWRITE_FUNCTION_DEPLOYMENT' : req.env.APPWRITE_FUNCTION_DEPLOYMENT, + 'APPWRITE_FUNCTION_TRIGGER' : req.env.APPWRITE_FUNCTION_TRIGGER, + 'APPWRITE_FUNCTION_RUNTIME_NAME' : req.env.APPWRITE_FUNCTION_RUNTIME_NAME, + 'APPWRITE_FUNCTION_RUNTIME_VERSION' : req.env.APPWRITE_FUNCTION_RUNTIME_VERSION, + 'APPWRITE_FUNCTION_EVENT' : req.env.APPWRITE_FUNCTION_EVENT, + 'APPWRITE_FUNCTION_EVENT_DATA' : req.env.APPWRITE_FUNCTION_EVENT_DATA, + 'APPWRITE_FUNCTION_DATA' : req.env.APPWRITE_FUNCTION_DATA, + 'APPWRITE_FUNCTION_USER_ID' : req.env.APPWRITE_FUNCTION_USER_ID, + 'APPWRITE_FUNCTION_JWT' : req.env.APPWRITE_FUNCTION_JWT, + 'APPWRITE_FUNCTION_PROJECT_ID' : req.env.APPWRITE_FUNCTION_PROJECT_ID, + 'CUSTOM_VARIABLE' : req.env.CUSTOM_VARIABLE + }); +} \ No newline at end of file From b4b3b91c0b14944550dba65b4568ecee016f7ad9 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 21:33:27 +0400 Subject: [PATCH 17/28] feat: improve functions tests --- app/workers/builds.php | 2 +- src/Executor/Executor.php | 2 +- .../Functions/FunctionsCustomServerTest.php | 105 ------------------ tests/resources/functions/deno/mod.ts | 30 ----- 4 files changed, 2 insertions(+), 137 deletions(-) delete mode 100644 tests/resources/functions/deno/mod.ts diff --git a/app/workers/builds.php b/app/workers/builds.php index e8a283fdbb..516825dd37 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -126,7 +126,7 @@ class BuildsV1 extends Worker commands: [ 'sh', '-c', 'tar -zxf /tmp/code.tar.gz -C /usr/code && \ - cd /usr/local/src && ./build.sh' + cd /usr/local/src/ && ./build.sh' ] ); diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index e68e67c83b..d8da13e902 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -65,7 +65,7 @@ class Executor 'commands' => $commands ]; - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)); $status = $response['headers']['status-code']; if ($status >= 400) { diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 06765086cc..f9c7f2a3e8 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -949,113 +949,8 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $response['headers']['status-code']); } - // public function testCreateCustomDenoExecution() - // { - // $name = 'deno-1.14'; - // $folder = 'deno'; - // $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz"; - // $this->packageCode($folder); - // $entrypoint = 'mod.ts'; - // $timeout = 2; - - // $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ - // 'content-type' => 'application/json', - // 'x-appwrite-project' => $this->getProject()['$id'], - // ], $this->getHeaders()), [ - // 'functionId' => 'unique()', - // 'name' => 'Test '.$name, - // 'runtime' => $name, - // 'vars' => [ - // 'CUSTOM_VARIABLE' => 'variable', - // ], - // 'events' => [], - // 'schedule' => '', - // 'timeout' => $timeout, - // ]); - - // $functionId = $function['body']['$id'] ?? ''; - - // $this->assertEquals(201, $function['headers']['status-code']); - - // $deployment = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/deployments', array_merge([ - // 'content-type' => 'multipart/form-data', - // 'x-appwrite-project' => $this->getProject()['$id'], - // ], $this->getHeaders()), [ - // 'entrypoint' => $entrypoint, - // 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), - // 'activate' => true, - // ]); - - // $deploymentId = $deployment['body']['$id'] ?? ''; - // $this->assertEquals(201, $deployment['headers']['status-code']); - - // // Allow build step to run - // sleep(10); - - // $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ - // 'content-type' => 'application/json', - // 'x-appwrite-project' => $this->getProject()['$id'], - // ], $this->getHeaders()), [ - // 'data' => 'foobar', - // ]); - - // $executionId = $execution['body']['$id'] ?? ''; - - // $this->assertEquals(201, $execution['headers']['status-code']); - - // $executionId = $execution['body']['$id'] ?? ''; - - // sleep(10); - - // $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, array_merge([ - // 'content-type' => 'application/json', - // 'x-appwrite-project' => $this->getProject()['$id'], - // ], $this->getHeaders())); - - // $output = json_decode($executions['body']['stdout'], true); - - // $this->assertEquals(200, $executions['headers']['status-code']); - // $this->assertEquals('completed', $executions['body']['status']); - // $this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']); - // $this->assertEquals('Test '.$name, $output['APPWRITE_FUNCTION_NAME']); - // $this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']); - // $this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']); - // $this->assertEquals('Node.js', $output['APPWRITE_FUNCTION_RUNTIME_NAME']); - // $this->assertEquals('17.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']); - // $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']); - // $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']); - // $this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']); - // $this->assertEquals('variable', $output['CUSTOM_VARIABLE']); - // $this->assertEquals('', $output['APPWRITE_FUNCTION_USER_ID']); - // $this->assertEmpty($output['APPWRITE_FUNCTION_JWT']); - // $this->assertEquals($this->getProject()['$id'], $output['APPWRITE_FUNCTION_PROJECT_ID']); - - // $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([ - // 'content-type' => 'application/json', - // 'x-appwrite-project' => $this->getProject()['$id'], - // ], $this->getHeaders())); - - // $this->assertEquals($executions['headers']['status-code'], 200); - // $this->assertEquals($executions['body']['sum'], 1); - // $this->assertIsArray($executions['body']['executions']); - // $this->assertCount(1, $executions['body']['executions']); - // $this->assertEquals($executions['body']['executions'][0]['$id'], $executionId); - // $this->assertEquals($executions['body']['executions'][0]['trigger'], 'http'); - // $this->assertStringContainsString('foobar', $executions['body']['executions'][0]['stdout']); - - // // Cleanup : Delete function - // $response = $this->client->call(Client::METHOD_DELETE, '/functions/'. $functionId, [ - // 'content-type' => 'application/json', - // 'x-appwrite-project' => $this->getProject()['$id'], - // 'x-appwrite-key' => $this->getProject()['apiKey'], - // ], []); - - // $this->assertEquals(204, $response['headers']['status-code']); - // } - public function testGetRuntimes() { - $runtimes = $this->client->call(Client::METHOD_GET, '/functions/runtimes', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], diff --git a/tests/resources/functions/deno/mod.ts b/tests/resources/functions/deno/mod.ts deleted file mode 100644 index 041f83ac97..0000000000 --- a/tests/resources/functions/deno/mod.ts +++ /dev/null @@ -1,30 +0,0 @@ - -/* - 'req' variable has: - 'headers' - object with request headers - 'payload' - object with request body data - 'env' - object with environment variables - 'res' variable has: - 'send(text, status)' - function to return text response. Status code defaults to 200 - 'json(obj, status)' - function to return JSON response. Status code defaults to 200 - - If an error is thrown, a response with code 500 will be returned. -*/ - -export default async function(req: any, res: any) { - res.json({ - 'APPWRITE_FUNCTION_ID' : req.env.APPWRITE_FUNCTION_ID, - 'APPWRITE_FUNCTION_NAME' : req.env.APPWRITE_FUNCTION_NAME, - 'APPWRITE_FUNCTION_DEPLOYMENT' : req.env.APPWRITE_FUNCTION_DEPLOYMENT, - 'APPWRITE_FUNCTION_TRIGGER' : req.env.APPWRITE_FUNCTION_TRIGGER, - 'APPWRITE_FUNCTION_RUNTIME_NAME' : req.env.APPWRITE_FUNCTION_RUNTIME_NAME, - 'APPWRITE_FUNCTION_RUNTIME_VERSION' : req.env.APPWRITE_FUNCTION_RUNTIME_VERSION, - 'APPWRITE_FUNCTION_EVENT' : req.env.APPWRITE_FUNCTION_EVENT, - 'APPWRITE_FUNCTION_EVENT_DATA' : req.env.APPWRITE_FUNCTION_EVENT_DATA, - 'APPWRITE_FUNCTION_DATA' : req.env.APPWRITE_FUNCTION_DATA, - 'APPWRITE_FUNCTION_USER_ID' : req.env.APPWRITE_FUNCTION_USER_ID, - 'APPWRITE_FUNCTION_JWT' : req.env.APPWRITE_FUNCTION_JWT, - 'APPWRITE_FUNCTION_PROJECT_ID' : req.env.APPWRITE_FUNCTION_PROJECT_ID, - 'CUSTOM_VARIABLE' : req.env.CUSTOM_VARIABLE - }); -} \ No newline at end of file From 734c38ebb4e17e28670307d2fc2b52d647a1ec9c Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 21 Feb 2022 00:25:35 +0400 Subject: [PATCH 18/28] feat: fix runtime bugs --- app/executor.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/executor.php b/app/executor.php index e6e09216da..72fe2827ed 100644 --- a/app/executor.php +++ b/app/executor.php @@ -198,20 +198,26 @@ App::post('/v1/runtimes') ->setMemory(App::getEnv('_APP_FUNCTIONS_MEMORY', 256)) ->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256)); + $entrypoint = empty($commands) ? [] : [ + 'tail', + '-f', + '/dev/null' + ]; $containerId = $orchestration->run( image: $baseImage, name: $runtimeId, hostname: $runtimeId, vars: $vars, + command: $entrypoint, labels: [ 'openruntimes-id' => $runtimeId, 'openruntimes-type' => 'runtime', 'openruntimes-created' => strval($startTime), 'openruntimes-runtime' => $runtime, ], - mountFolder: \dirname($tmpSource), workdir: $workdir, volumes: [ + \dirname($tmpSource). ':/tmp:rw', \dirname($tmpBuild). ":/usr/code:rw" ] ); @@ -285,7 +291,6 @@ App::post('/v1/runtimes') 'key' => $secret, ]); } - Console::success('Build Stage completed in ' . ($endTime - $startTime) . ' seconds'); From 6752300cc15b9e755fc17b1b45da8f3bbfdc6096 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 21 Feb 2022 02:21:36 +0400 Subject: [PATCH 19/28] feat: adjust html text --- app/views/console/functions/function.phtml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index c4ef2a9374..f810741507 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -687,7 +687,7 @@ $usageStatsEnabled = $this->getParam('usageStatsEnabled', true);
(Max file size allowed: )
- +