feat: rename tag to deployments across worker
This commit is contained in:
parent
c1d71904e8
commit
c595f4eea9
|
@ -584,7 +584,7 @@ App::post('/v1/functions/:functionId/deployments')
|
||||||
'projectId' => $project->getId(),
|
'projectId' => $project->getId(),
|
||||||
'functionId' => $function->getId(),
|
'functionId' => $function->getId(),
|
||||||
'deploymentId' => $deploymentId,
|
'deploymentId' => $deploymentId,
|
||||||
'type' => BUILD_TYPE_TAG
|
'type' => BUILD_TYPE_DEPLOYMENT
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$usage
|
$usage
|
||||||
|
|
|
@ -688,7 +688,7 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
return $build;
|
return $build;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update Tag Status
|
// Update deployment Status
|
||||||
$build->setAttribute('status', 'building');
|
$build->setAttribute('status', 'building');
|
||||||
|
|
||||||
$database->updateDocument('builds', $build->getId(), $build);
|
$database->updateDocument('builds', $build->getId(), $build);
|
||||||
|
@ -700,7 +700,7 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
throw new Exception('Runtime "' . $build->getAttribute('runtime', '') . '" is not supported');
|
throw new Exception('Runtime "' . $build->getAttribute('runtime', '') . '" is not supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab Tag Files
|
// Grab Deployment Files
|
||||||
$deploymentPath = $build->getAttribute('source', '');
|
$deploymentPath = $build->getAttribute('source', '');
|
||||||
$sourceType = $build->getAttribute('sourceType', '');
|
$sourceType = $build->getAttribute('sourceType', '');
|
||||||
|
|
||||||
|
@ -999,24 +999,24 @@ App::delete('/v1/functions/:functionId')
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
App::post('/v1/functions/:functionId/tags/:tagId/runtime')
|
App::post('/v1/functions/:functionId/deployments/:deploymentId/runtime')
|
||||||
->desc('Create a new runtime server for a tag')
|
->desc('Create a new runtime server for a deployment')
|
||||||
->param('functionId', '', new UID(), 'Function unique ID.')
|
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||||
->param('tagId', '', new UID(), 'Tag unique ID.')
|
->param('deploymentId', '', new UID(), 'Deployment unique ID.')
|
||||||
->inject('response')
|
->inject('response')
|
||||||
->inject('dbForProject')
|
->inject('dbForProject')
|
||||||
->inject('projectID')
|
->inject('projectID')
|
||||||
->action(function (string $functionId, string $tagId, Response $response, Database $dbForProject, string $projectID) use ($runtimes) {
|
->action(function (string $functionId, string $deploymentId, Response $response, Database $dbForProject, string $projectID) use ($runtimes) {
|
||||||
// Get function document
|
// Get function document
|
||||||
$function = $dbForProject->getDocument('functions', $functionId);
|
$function = $dbForProject->getDocument('functions', $functionId);
|
||||||
if ($function->isEmpty()) {
|
if ($function->isEmpty()) {
|
||||||
throw new Exception('Function not found', 404);
|
throw new Exception('Function not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tag document
|
// Get deployment document
|
||||||
$tag = $dbForProject->getDocument('tags', $tagId);
|
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
|
||||||
if ($tag->isEmpty()) {
|
if ($deployment->isEmpty()) {
|
||||||
throw new Exception('Tag not found', 404);
|
throw new Exception('Deployment not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$runtime = $runtimes[$function->getAttribute('runtime')] ?? null;
|
$runtime = $runtimes[$function->getAttribute('runtime')] ?? null;
|
||||||
|
@ -1024,7 +1024,7 @@ App::post('/v1/functions/:functionId/tags/:tagId/runtime')
|
||||||
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" not found.', 404);
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" not found.', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
createRuntimeServer($functionId, $projectID, $tagId, $dbForProject);
|
createRuntimeServer($functionId, $projectID, $deploymentId, $dbForProject);
|
||||||
|
|
||||||
$response
|
$response
|
||||||
->setStatusCode(201)
|
->setStatusCode(201)
|
||||||
|
|
|
@ -93,7 +93,7 @@ const DATABASE_TYPE_CREATE_INDEX = 'createIndex';
|
||||||
const DATABASE_TYPE_DELETE_ATTRIBUTE = 'deleteAttribute';
|
const DATABASE_TYPE_DELETE_ATTRIBUTE = 'deleteAttribute';
|
||||||
const DATABASE_TYPE_DELETE_INDEX = 'deleteIndex';
|
const DATABASE_TYPE_DELETE_INDEX = 'deleteIndex';
|
||||||
// Build Worker Types
|
// Build Worker Types
|
||||||
const BUILD_TYPE_TAG = 'tag';
|
const BUILD_TYPE_DEPLOYMENT = 'deployment';
|
||||||
const BUILD_TYPE_RETRY = 'retry';
|
const BUILD_TYPE_RETRY = 'retry';
|
||||||
// Deletion Types
|
// Deletion Types
|
||||||
const DELETE_TYPE_DOCUMENT = 'document';
|
const DELETE_TYPE_DOCUMENT = 'document';
|
||||||
|
|
|
@ -34,11 +34,11 @@ class BuildsV1 extends Worker
|
||||||
$projectId = $this->args['projectId'] ?? '';
|
$projectId = $this->args['projectId'] ?? '';
|
||||||
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case BUILD_TYPE_TAG:
|
case BUILD_TYPE_DEPLOYMENT:
|
||||||
$functionId = $this->args['functionId'] ?? '';
|
$functionId = $this->args['functionId'] ?? '';
|
||||||
$tagId = $this->args['tagId'] ?? '';
|
$deploymentId = $this->args['deploymentId'] ?? '';
|
||||||
Console::info("[ INFO ] Creating build for tag: $tagId");
|
Console::info("[ INFO ] Creating build for deployment: $deploymentId");
|
||||||
$this->buildTag($projectId, $functionId, $tagId);
|
$this->buildDeployment($projectId, $functionId, $deploymentId);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BUILD_TYPE_RETRY:
|
case BUILD_TYPE_RETRY:
|
||||||
|
@ -83,10 +83,10 @@ class BuildsV1 extends Worker
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function triggerCreateRuntimeServer(string $projectId, string $functionId, string $tagId)
|
protected function triggerCreateRuntimeServer(string $projectId, string $functionId, string $deploymentId)
|
||||||
{
|
{
|
||||||
$ch = \curl_init();
|
$ch = \curl_init();
|
||||||
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/functions/$functionId/tags/$tagId/runtime");
|
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/functions/$functionId/deployments/$deploymentId/runtime");
|
||||||
\curl_setopt($ch, CURLOPT_POST, true);
|
\curl_setopt($ch, CURLOPT_POST, true);
|
||||||
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
\curl_setopt($ch, CURLOPT_TIMEOUT, 900);
|
\curl_setopt($ch, CURLOPT_TIMEOUT, 900);
|
||||||
|
@ -112,7 +112,7 @@ class BuildsV1 extends Worker
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildTag(string $projectId, string $functionId, string $tagId)
|
protected function buildDeployment(string $projectId, string $functionId, string $deploymentId)
|
||||||
{
|
{
|
||||||
$dbForProject = $this->getProjectDB($projectId);
|
$dbForProject = $this->getProjectDB($projectId);
|
||||||
|
|
||||||
|
@ -122,10 +122,10 @@ class BuildsV1 extends Worker
|
||||||
throw new Exception('Function not found', 404);
|
throw new Exception('Function not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tag document
|
// Get deployment document
|
||||||
$tag = $dbForProject->getDocument('tags', $tagId);
|
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
|
||||||
if ($tag->isEmpty()) {
|
if ($deployment->isEmpty()) {
|
||||||
throw new Exception('Tag not found', 404);
|
throw new Exception('Deployment not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$runtimes = Config::getParam('runtimes', []);
|
$runtimes = Config::getParam('runtimes', []);
|
||||||
|
@ -135,13 +135,13 @@ class BuildsV1 extends Worker
|
||||||
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported');
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
$buildId = $tag->getAttribute('buildId', '');
|
$buildId = $deployment->getAttribute('buildId', '');
|
||||||
|
|
||||||
// If build ID is empty, create a new build
|
// If build ID is empty, create a new build
|
||||||
if (empty($buildId)) {
|
if (empty($buildId)) {
|
||||||
try {
|
try {
|
||||||
$buildId = $dbForProject->getId();
|
$buildId = $dbForProject->getId();
|
||||||
// TODO : There is no way to associate a build with a tag. So we need to add a tagId attribute to the build document
|
// TODO : There is no way to associate a build with a deployment. So we need to add a deploymentId attribute to the build document
|
||||||
// TODO : What should be the read and write permissions for a build ?
|
// TODO : What should be the read and write permissions for a build ?
|
||||||
$dbForProject->createDocument('builds', new Document([
|
$dbForProject->createDocument('builds', new Document([
|
||||||
'$id' => $buildId,
|
'$id' => $buildId,
|
||||||
|
@ -151,13 +151,13 @@ class BuildsV1 extends Worker
|
||||||
'status' => 'processing',
|
'status' => 'processing',
|
||||||
'runtime' => $function->getAttribute('runtime'),
|
'runtime' => $function->getAttribute('runtime'),
|
||||||
'outputPath' => '',
|
'outputPath' => '',
|
||||||
'source' => $tag->getAttribute('path'),
|
'source' => $deployment->getAttribute('path'),
|
||||||
'sourceType' => Storage::DEVICE_LOCAL,
|
'sourceType' => Storage::DEVICE_LOCAL,
|
||||||
'stdout' => '',
|
'stdout' => '',
|
||||||
'stderr' => '',
|
'stderr' => '',
|
||||||
'time' => 0,
|
'time' => 0,
|
||||||
'vars' => [
|
'vars' => [
|
||||||
'ENTRYPOINT_NAME' => $tag->getAttribute('entrypoint'),
|
'ENTRYPOINT_NAME' => $deployment->getAttribute('entrypoint'),
|
||||||
'APPWRITE_FUNCTION_ID' => $function->getId(),
|
'APPWRITE_FUNCTION_ID' => $function->getId(),
|
||||||
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''),
|
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''),
|
||||||
'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'],
|
'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'],
|
||||||
|
@ -166,14 +166,14 @@ class BuildsV1 extends Worker
|
||||||
]
|
]
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$tag->setAttribute('buildId', $buildId);
|
$deployment->setAttribute('buildId', $buildId);
|
||||||
$tag = $dbForProject->updateDocument('tags', $tagId, $tag);
|
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
|
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
Console::error($th->getMessage());
|
Console::error($th->getMessage());
|
||||||
$tag->setAttribute('status', 'failed');
|
$deployment->setAttribute('status', 'failed');
|
||||||
$tag->setAttribute('buildId', '');
|
$deployment->setAttribute('buildId', '');
|
||||||
$tag = $dbForProject->updateDocument('tags', $tagId, $tag);
|
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,13 +181,13 @@ class BuildsV1 extends Worker
|
||||||
// Build the Code
|
// Build the Code
|
||||||
try {
|
try {
|
||||||
Console::info("[ INFO ] Creating build with id: $buildId");
|
Console::info("[ INFO ] Creating build with id: $buildId");
|
||||||
$tag->setAttribute('status', 'building');
|
$deployment->setAttribute('status', 'building');
|
||||||
$tag = $dbForProject->updateDocument('tags', $tagId, $tag);
|
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
$this->triggerBuild($projectId, $buildId);
|
$this->triggerBuild($projectId, $buildId);
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
Console::error($th->getMessage());
|
Console::error($th->getMessage());
|
||||||
$tag->setAttribute('status', 'failed');
|
$deployment->setAttribute('status', 'failed');
|
||||||
$tag = $dbForProject->updateDocument('tags', $tagId, $tag);
|
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,8 +195,8 @@ class BuildsV1 extends Worker
|
||||||
|
|
||||||
// Update the schedule
|
// Update the schedule
|
||||||
$schedule = $function->getAttribute('schedule', '');
|
$schedule = $function->getAttribute('schedule', '');
|
||||||
$cron = (empty($function->getAttribute('tag')) && !empty($schedule)) ? new CronExpression($schedule) : null;
|
$cron = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null;
|
||||||
$next = (empty($function->getAttribute('tag')) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : 0;
|
$next = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : 0;
|
||||||
|
|
||||||
// Grab build
|
// Grab build
|
||||||
$build = $dbForProject->getDocument('builds', $buildId);
|
$build = $dbForProject->getDocument('builds', $buildId);
|
||||||
|
@ -206,10 +206,10 @@ class BuildsV1 extends Worker
|
||||||
throw new Exception('Build failed', 500);
|
throw new Exception('Build failed', 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($tag->getAttribute('automaticDeploy') === true) {
|
if ($deployment->getAttribute('deploy') === true) {
|
||||||
// Update the function document setting the tag as the active one
|
// Update the function document setting the deployment as the active one
|
||||||
$function
|
$function
|
||||||
->setAttribute('tag', $tag->getId())
|
->setAttribute('deployment', $deployment->getId())
|
||||||
->setAttribute('scheduleNext', (int)$next);
|
->setAttribute('scheduleNext', (int)$next);
|
||||||
|
|
||||||
$function = $dbForProject->updateDocument('functions', $functionId, $function);
|
$function = $dbForProject->updateDocument('functions', $functionId, $function);
|
||||||
|
@ -218,11 +218,11 @@ class BuildsV1 extends Worker
|
||||||
// Deploy Runtime Server
|
// Deploy Runtime Server
|
||||||
try {
|
try {
|
||||||
Console::info("[ INFO ] Creating runtime server");
|
Console::info("[ INFO ] Creating runtime server");
|
||||||
$this->triggerCreateRuntimeServer($projectId, $functionId, $tagId, $dbForProject);
|
$this->triggerCreateRuntimeServer($projectId, $functionId, $deploymentId, $dbForProject);
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
Console::error($th->getMessage());
|
Console::error($th->getMessage());
|
||||||
$tag->setAttribute('status', 'failed');
|
$deployment->setAttribute('status', 'failed');
|
||||||
$tag = $dbForProject->updateDocument('tags', $tagId, $tag);
|
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue