From 57e596fea2b93f9a89a54e5c08f5f270e9d23aa5 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 23 Oct 2024 18:36:26 +0200 Subject: [PATCH 1/2] feat: static runtime for site --- app/controllers/general.php | 54 ++++++++++++++----- .../Modules/Functions/Workers/Builds.php | 14 ++++- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 483fa88a12..62cc3b9001 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -100,9 +100,9 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $type = $route->getAttribute('resourceType'); - if ($type === 'function' || $type === 'sites') { + if ($type === 'function' || $type === 'site') { $isFunction = $type === 'function' ; - $isSite = $type === 'sites'; + $isSite = $type === 'site'; $utopia->getRoute()?->label('sdk.namespace', 'functions'); $utopia->getRoute()?->label('sdk.method', 'createExecution'); @@ -132,6 +132,7 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $project = Authorization::skip(fn () => $dbForConsole->getDocument('projects', $projectId)); + /** @var Database $dbForProject */ $dbForProject = $getProjectDB($project); $function = Authorization::skip(fn () => $dbForProject->getDocument($isSite ? 'sites' : 'functions', $resourceId)); @@ -146,11 +147,28 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $runtime = (isset($runtimes[$function->getAttribute('runtime', '')])) ? $runtimes[$function->getAttribute('runtime', '')] : null; - if (\is_null($runtime)) { - throw new AppwriteException(AppwriteException::FUNCTION_RUNTIME_UNSUPPORTED, 'Runtime "' . $function->getAttribute('runtime', '') . '" is not supported'); + // todo: fallback for static sites runtime + if ($isSite) { + $runtime = [ + 'key' => 'static-for-now', + 'name' => 'Static', + 'logo' => 'node.png', + 'startCommand' => null, + 'version' => 'v1', + 'base' => 'static:1.0', + 'image' => 'static:1.0', + 'supports' => [System::X86, System::ARM64, System::ARMV7, System::ARMV8] + ]; } - $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $function->getAttribute('deployment', ''))); + //todo: figure out for sites/functions + if ($isFunction) { + if (\is_null($runtime)) { + throw new AppwriteException(AppwriteException::FUNCTION_RUNTIME_UNSUPPORTED, 'Runtime "' . $function->getAttribute('runtime', '') . '" is not supported'); + } + } + $deploymentId = $isSite ? $function->getAttribute('deploymentId', '') : $function->getAttribute('deployment', ''); + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $deploymentId)); if ($deployment->getAttribute('resourceId') !== $function->getId()) { throw new AppwriteException(AppwriteException::DEPLOYMENT_NOT_FOUND, 'Deployment not found. Create a deployment before trying to execute a function'); @@ -170,10 +188,13 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo throw new AppwriteException(AppwriteException::BUILD_NOT_READY); } - $permissions = $function->getAttribute('execute'); + //todo: figure out for sites/functions + if ($isFunction) { + $permissions = $function->getAttribute('execute'); - if (!(\in_array('any', $permissions)) && !(\in_array('guests', $permissions))) { - throw new AppwriteException(AppwriteException::USER_UNAUTHORIZED, 'To execute function using domain, execute permissions must include "any" or "guests"'); + if (!(\in_array('any', $permissions)) && !(\in_array('guests', $permissions))) { + throw new AppwriteException(AppwriteException::USER_UNAUTHORIZED, 'To execute function using domain, execute permissions must include "any" or "guests"'); + } } $jwtExpiry = $function->getAttribute('timeout', 900); @@ -299,22 +320,28 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $executor = new Executor(System::getEnv('_APP_EXECUTOR_HOST')); try { $version = $function->getAttribute('version', 'v2'); - $command = $runtime['startCommand']; - $command = $version === 'v2' ? '' : 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $command . '"'; + $entrypoint = $deployment->getAttribute('entrypoint', ''); + // todo: figure out site specific settings + if ($isSite) { + $version = 'v4'; + $entrypoint = 'placeholder'; + } + $runtimeEntrypoint = $version === 'v2' ? '' : 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $runtime['startCommand'] . '"'; $executionResponse = $executor->createExecution( projectId: $project->getId(), deploymentId: $deployment->getId(), body: \strlen($body) > 0 ? $body : null, variables: $vars, - timeout: $function->getAttribute('timeout', 0), + // todo: figure out timeouts for sites + timeout: $function->getAttribute('timeout', 30), image: $runtime['image'], source: $build->getAttribute('path', ''), - entrypoint: $deployment->getAttribute('entrypoint', ''), + entrypoint: $entrypoint, version: $version, path: $path, method: $method, headers: $headers, - runtimeEntrypoint: $command, + runtimeEntrypoint: $runtimeEntrypoint, cpus: $spec['cpus'] ?? APP_FUNCTION_CPUS_DEFAULT, memory: $spec['memory'] ?? APP_FUNCTION_MEMORY_DEFAULT, logging: $function->getAttribute('logging', true), @@ -336,7 +363,6 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $execution->setAttribute('logs', $executionResponse['logs']); $execution->setAttribute('errors', $executionResponse['errors']); $execution->setAttribute('duration', $executionResponse['duration']); - } catch (\Throwable $th) { $durationEnd = \microtime(true); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 4f662f566a..312b9edcf3 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -145,6 +145,8 @@ class Builds extends Action } $version = $resource->getAttribute('version', 'v2'); + + // todo: fallback for sites if ($isSite) { $version = 'v4'; } @@ -154,6 +156,7 @@ class Builds extends Action $key = $resource->getAttribute('runtime'); $runtime = $runtimes[$key] ?? null; + // todo: fallback for sites if ($isSite) { $runtime = $runtimes['node-18.0']; } @@ -687,9 +690,16 @@ class Builds extends Action /** Set auto deploy */ if ($deployment->getAttribute('activate') === true) { $resource->setAttribute('deploymentInternalId', $deployment->getInternalId()); - $resource->setAttribute('deployment', $deployment->getId()); $resource->setAttribute('live', true); - $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); + // todo: fix here how clean this is + if ($isSite) { + $resource->setAttribute('deploymentId', $deployment->getId()); + $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); + } + if ($isFunction) { + $resource->setAttribute('deployment', $deployment->getId()); + $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); + } } if ($dbForProject->getDocument('builds', $buildId)->getAttribute('status') === 'canceled') { From 83b5aecbfde4e9225daf76a22f2991b56e6f71b9 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 24 Oct 2024 12:19:10 +0200 Subject: [PATCH 2/2] chore: add todo comment --- app/controllers/general.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/general.php b/app/controllers/general.php index 62cc3b9001..88ece71e8a 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -167,6 +167,7 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo throw new AppwriteException(AppwriteException::FUNCTION_RUNTIME_UNSUPPORTED, 'Runtime "' . $function->getAttribute('runtime', '') . '" is not supported'); } } + //todo: find a better approach $deploymentId = $isSite ? $function->getAttribute('deploymentId', '') : $function->getAttribute('deployment', ''); $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $deploymentId));