Merge branch '1.4.x' of github.com:appwrite/appwrite into cl-1.4.x

This commit is contained in:
Christy Jacob 2023-09-26 00:02:19 +04:00
commit 2273cb50b4
7 changed files with 205 additions and 60 deletions

View file

@ -84,6 +84,7 @@ $redeployVcs = function (Request $request, Document $function, Document $project
Permission::delete(Role::any()),
],
'resourceId' => $function->getId(),
'resourceInternalId' => $function->getInternalId(),
'resourceType' => 'functions',
'entrypoint' => $entrypoint,
'commands' => $function->getAttribute('commands', ''),
@ -1495,7 +1496,7 @@ App::post('/v1/functions/:functionId/executions')
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_EXECUTION)
->param('functionId', '', new UID(), 'Function ID.')
->param('body', '', new Text(8192, 0), 'HTTP body of execution. Default value is empty string.', true)
->param('body', '', new Text(0, 0), 'HTTP body of execution. Default value is empty string.', true)
->param('async', false, new Boolean(), 'Execute code in the background. Default value is false.', true)
->param('path', '/', new Text(2048), 'HTTP path of execution. Path can include query params. Default value is /', true)
->param('method', 'POST', new Whitelist(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], true), 'HTTP method of execution. Default value is GET.', true)

View file

@ -50,6 +50,7 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId
$functionId = $resource->getAttribute('resourceId');
$function = Authorization::skip(fn () => $dbForProject->getDocument('functions', $functionId));
$functionInternalId = $function->getInternalId();
$deploymentId = ID::unique();
$repositoryId = $resource->getId();
@ -173,6 +174,7 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId
Permission::delete(Role::any()),
],
'resourceId' => $functionId,
'resourceInternalId' => $functionInternalId,
'resourceType' => 'functions',
'entrypoint' => $function->getAttribute('entrypoint'),
'commands' => $function->getAttribute('commands'),

View file

@ -1,6 +1,5 @@
<?php
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Utopia\App;
@ -28,17 +27,9 @@ App::get('/console/*')
->groups(['web'])
->label('permission', 'public')
->label('scope', 'home')
->inject('utopia')
->inject('request')
->inject('response')
->action(function (App $utopia, Request $request, Response $response) {
$host = $request->getHostname() ?? '';
$mainDomain = App::getEnv('_APP_DOMAIN', '');
if (App::getEnv('_APP_OPTIONS_ROUTER_PROTECTION', 'disabled') === 'enabled' && $host !== $mainDomain) {
$utopia->getRoute()?->label('error', __DIR__ . '/../../views/general/error.phtml');
throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Router protection does not allow accessing Appwrite Console over custom domain. Please disable _APP_OPTIONS_ROUTER_PROTECTION environment variable.');
}
->action(function (Request $request, Response $response) {
$fallback = file_get_contents(__DIR__ . '/../../../console/index.html');
// Card SSR

View file

@ -14,6 +14,7 @@ use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\CLI\Console;
use Utopia\Audit\Audit;
use Utopia\Database\DateTime;
use Utopia\Storage\Device;
require_once __DIR__ . '/../init.php';
@ -71,7 +72,7 @@ class DeletesV1 extends Worker
$this->deleteInstallation($document, $project);
break;
case DELETE_TYPE_RULES:
$this->deleteRule($document, $project);
$this->deleteRule($document);
break;
default:
if (\str_starts_with($document->getCollection(), 'database_')) {
@ -366,17 +367,8 @@ class DeletesV1 extends Worker
$projectId = $document->getId();
$projectInternalId = $document->getInternalId();
// Delete project certificates
$dbForConsole = $this->getConsoleDB();
$domains = $dbForConsole->find('domains', [
Query::equal('projectInternalId', [$projectInternalId])
]);
foreach ($domains as $domain) {
$this->deleteCertificates($domain);
}
// Delete project tables
$dbForProject = $this->getProjectDB($document);
@ -397,10 +389,12 @@ class DeletesV1 extends Worker
Query::equal('projectInternalId', [$projectInternalId])
], $dbForConsole);
// Delete Domains
$this->deleteByGroup('domains', [
// Delete project and function rules
$this->deleteByGroup('rules', [
Query::equal('projectInternalId', [$projectInternalId])
], $dbForConsole);
], $dbForConsole, function (Document $document) {
$this->deleteRule($document);
});
// Delete Keys
$this->deleteByGroup('keys', [
@ -620,33 +614,25 @@ class DeletesV1 extends Worker
* Delete Deployments
*/
Console::info("Deleting deployments for function " . $functionId);
$storageFunctions = $this->getFunctionsDevice($projectId);
$deviceFunctions = $this->getFunctionsDevice($projectId);
$deploymentInternalIds = [];
$this->deleteByGroup('deployments', [
Query::equal('resourceInternalId', [$functionInternalId])
], $dbForProject, function (Document $document) use ($storageFunctions, &$deploymentInternalIds) {
], $dbForProject, function (Document $document) use ($deviceFunctions, &$deploymentInternalIds) {
$deploymentInternalIds[] = $document->getInternalId();
if ($storageFunctions->delete($document->getAttribute('path', ''), true)) {
Console::success('Deleted deployment files: ' . $document->getAttribute('path', ''));
} else {
Console::error('Failed to delete deployment files: ' . $document->getAttribute('path', ''));
}
$this->deleteDeploymentFiles($deviceFunctions, $document);
});
/**
* Delete builds
*/
Console::info("Deleting builds for function " . $functionId);
$storageBuilds = $this->getBuildsDevice($projectId);
$deviceBuilds = $this->getBuildsDevice($projectId);
foreach ($deploymentInternalIds as $deploymentInternalId) {
$this->deleteByGroup('builds', [
Query::equal('deploymentInternalId', [$deploymentInternalId])
], $dbForProject, function (Document $document) use ($storageBuilds) {
if ($storageBuilds->delete($document->getAttribute('path', ''), true)) {
Console::success('Deleted build files: ' . $document->getAttribute('path', ''));
} else {
Console::error('Failed to delete build files: ' . $document->getAttribute('path', ''));
}
], $dbForProject, function (Document $document) use ($deviceBuilds) {
$this->deleteBuildFiles($deviceBuilds, $document);
});
}
@ -665,6 +651,58 @@ class DeletesV1 extends Worker
$this->deleteRuntimes($document, $project);
}
protected function deleteDeploymentFiles(Device $device, Document $deployment)
{
$deploymentId = $deployment->getId();
$deploymentPath = $deployment->getAttribute('path', '');
if (empty($deploymentPath)) {
Console::info("No deployment files for deployment " . $deploymentId);
return;
}
Console::info("Deleting deployment files for deployment " . $deploymentId);
try {
if ($device->delete($deploymentPath, true)) {
Console::success('Deleted deployment files: ' . $deploymentPath);
} else {
Console::error('Failed to delete deployment files: ' . $deploymentPath);
}
} catch (\Throwable $th) {
Console::error('Failed to delete deployment files: ' . $deploymentPath);
Console::error('[Error] Type: ' . get_class($th));
Console::error('[Error] Message: ' . $th->getMessage());
Console::error('[Error] File: ' . $th->getFile());
Console::error('[Error] Line: ' . $th->getLine());
}
}
protected function deleteBuildFiles(Device $device, Document $build)
{
$buildId = $build->getId();
$buildPath = $build->getAttribute('path', '');
if (empty($buildPath)) {
Console::info("No build files for build " . $buildId);
return;
}
try {
if ($device->delete($buildPath, true)) {
Console::success('Deleted build files: ' . $buildPath);
} else {
Console::error('Failed to delete build files: ' . $buildPath);
}
} catch (\Throwable $th) {
Console::error('Failed to delete deployment files: ' . $buildPath);
Console::error('[Error] Type: ' . get_class($th));
Console::error('[Error] Message: ' . $th->getMessage());
Console::error('[Error] File: ' . $th->getFile());
Console::error('[Error] Line: ' . $th->getLine());
}
}
/**
* @param Document $document deployment document
* @param Document $project
@ -679,27 +717,18 @@ class DeletesV1 extends Worker
/**
* Delete deployment files
*/
Console::info("Deleting deployment files for deployment " . $deploymentId);
$storageFunctions = $this->getFunctionsDevice($projectId);
if ($storageFunctions->delete($document->getAttribute('path', ''), true)) {
Console::success('Deleted deployment files: ' . $document->getAttribute('path', ''));
} else {
Console::error('Failed to delete deployment files: ' . $document->getAttribute('path', ''));
}
$deviceFunctions = $this->getFunctionsDevice($projectId);
$this->deleteDeploymentFiles($deviceFunctions, $document);
/**
* Delete builds
*/
Console::info("Deleting builds for deployment " . $deploymentId);
$storageBuilds = $this->getBuildsDevice($projectId);
$deviceBuilds = $this->getBuildsDevice($projectId);
$this->deleteByGroup('builds', [
Query::equal('deploymentInternalId', [$deploymentInternalId])
], $dbForProject, function (Document $document) use ($storageBuilds) {
if ($storageBuilds->delete($document->getAttribute('path', ''), true)) {
Console::success('Deleted build files: ' . $document->getAttribute('path', ''));
} else {
Console::error('Failed to delete build files: ' . $document->getAttribute('path', ''));
}
], $dbForProject, function (Document $document) use ($deviceBuilds) {
$this->deleteBuildFiles($deviceBuilds, $document);
});
@ -861,7 +890,7 @@ class DeletesV1 extends Worker
* @param Document $document rule document
* @param Document $project project document
*/
protected function deleteRule(Document $document, Document $project): void
protected function deleteRule(Document $document): void
{
$consoleDB = $this->getConsoleDB();

View file

@ -363,7 +363,8 @@ $server->job()
path: '/',
method: 'POST',
headers: [
'user-agent' => 'Appwrite/' . APP_VERSION_STABLE
'user-agent' => 'Appwrite/' . APP_VERSION_STABLE,
'content-type' => 'application/json'
],
);
Console::success('Triggered function: ' . $events[0]);

View file

@ -32,8 +32,8 @@ class FunctionsCustomServerTest extends Scope
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'users.*.create',
'users.*.delete',
'buckets.*.create',
'buckets.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
@ -50,8 +50,8 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(true, $dateValidator->isValid($response1['body']['$updatedAt']));
$this->assertEquals('', $response1['body']['deployment']);
$this->assertEquals([
'users.*.create',
'users.*.delete',
'buckets.*.create',
'buckets.*.delete',
], $response1['body']['events']);
$this->assertEquals('0 0 1 1 *', $response1['body']['schedule']);
$this->assertEquals(10, $response1['body']['timeout']);
@ -191,8 +191,8 @@ class FunctionsCustomServerTest extends Scope
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'users.*.create',
'users.*.delete',
'buckets.*.create',
'buckets.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
@ -1231,4 +1231,117 @@ class FunctionsCustomServerTest extends Scope
$this->assertArrayHasKey('base', $runtime);
$this->assertArrayHasKey('supports', $runtime);
}
public function testEventTrigger()
{
$timeout = 5;
$code = realpath(__DIR__ . '/../../../resources/functions') . "/php-event/code.tar.gz";
$this->packageCode('php-event');
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'functionId' => ID::unique(),
'name' => 'Test PHP Event executions',
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'users.*.create',
],
'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' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', basename($code)),
'activate' => true
]);
$deploymentId = $deployment['body']['$id'] ?? '';
$this->assertEquals(202, $deployment['headers']['status-code']);
// Poll until deployment is built
while (true) {
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/deployments/' . $deploymentId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
if (
$deployment['headers']['status-code'] >= 400
|| \in_array($deployment['body']['status'], ['ready', 'failed'])
) {
break;
}
\sleep(1);
}
$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']);
// Wait a little for activation to finish
sleep(5);
// Create user to trigger event
$user = $this->client->call(Client::METHOD_POST, '/users', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'userId' => 'unique()',
'name' => 'Event User'
]);
$userId = $user['body']['$id'];
$this->assertEquals(201, $user['headers']['status-code']);
// Wait for execution to occur
sleep(15);
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$execution = $executions['body']['executions'][0];
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertEquals('completed', $execution['status']);
$this->assertEquals(204, $execution['responseStatusCode']);
$this->assertStringContainsString($userId, $execution['logs']);
$this->assertStringContainsString('Event User', $execution['logs']);
// 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']);
// Cleanup : Delete user
$response = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
$this->assertEquals(204, $response['headers']['status-code']);
}
}

View file

@ -0,0 +1,8 @@
<?php
return function ($context) {
$context->log($context->req->body['$id']);
$context->log($context->req->body['name']);
return $context->res->empty();
};