2022-01-24 11:25:46 +13:00
|
|
|
<?php
|
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
require_once __DIR__ . '/../worker.php';
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
use Appwrite\Event\Event;
|
2022-11-16 07:13:17 +13:00
|
|
|
use Appwrite\Event\Func;
|
2022-03-01 00:50:27 +13:00
|
|
|
use Appwrite\Messaging\Adapter\Realtime;
|
2022-04-19 04:21:45 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Deployment;
|
2022-02-04 14:29:40 +13:00
|
|
|
use Executor\Executor;
|
2022-08-09 18:28:38 +12:00
|
|
|
use Appwrite\Usage\Stats;
|
2022-07-13 01:32:39 +12:00
|
|
|
use Utopia\Database\DateTime;
|
2022-01-24 11:25:46 +13:00
|
|
|
use Utopia\App;
|
|
|
|
use Utopia\CLI\Console;
|
2022-08-15 02:22:38 +12:00
|
|
|
use Utopia\Database\ID;
|
2022-11-18 01:50:17 +13:00
|
|
|
use Utopia\DSN\DSN;
|
2022-01-24 11:25:46 +13:00
|
|
|
use Utopia\Database\Document;
|
|
|
|
use Utopia\Config\Config;
|
2022-11-16 23:33:11 +13:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2022-12-21 01:22:58 +13:00
|
|
|
use Utopia\Queue\Message;
|
2022-11-18 06:43:59 +13:00
|
|
|
use Utopia\Storage\Storage;
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
Authorization::disable();
|
|
|
|
Authorization::setDefaultStatus(false);
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
function buildDeployment(Document $project, Document $function, Document $deployment)
|
2022-04-14 00:39:31 +12:00
|
|
|
{
|
2022-12-21 01:22:58 +13:00
|
|
|
global $register;
|
|
|
|
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
|
|
|
|
|
|
|
|
$dbForProject = getProjectDB($project);
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$function = $dbForProject->getDocument('functions', $function->getId());
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
throw new Exception('Function not found', 404);
|
2022-01-24 11:25:46 +13:00
|
|
|
}
|
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$deployment = $dbForProject->getDocument('deployments', $deployment->getId());
|
|
|
|
if ($deployment->isEmpty()) {
|
|
|
|
throw new Exception('Deployment not found', 404);
|
2022-02-04 14:29:40 +13:00
|
|
|
}
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$runtimes = Config::getParam('runtimes', []);
|
|
|
|
$key = $function->getAttribute('runtime');
|
|
|
|
$runtime = isset($runtimes[$key]) ? $runtimes[$key] : null;
|
|
|
|
if (\is_null($runtime)) {
|
|
|
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported');
|
|
|
|
}
|
2022-04-14 00:39:31 +12:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$connection = App::getEnv('_APP_CONNECTIONS_STORAGE', ''); /** @TODO : move this to the registry or someplace else */
|
|
|
|
$device = Storage::DEVICE_LOCAL;
|
|
|
|
try {
|
|
|
|
$dsn = new DSN($connection);
|
|
|
|
$device = $dsn->getScheme();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Console::error($e->getMessage() . 'Invalid DSN. Defaulting to Local device.');
|
|
|
|
}
|
2022-01-24 12:01:42 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$buildId = $deployment->getAttribute('buildId', '');
|
|
|
|
$startTime = DateTime::now();
|
|
|
|
if (empty($buildId)) {
|
|
|
|
$buildId = ID::unique();
|
|
|
|
$build = $dbForProject->createDocument('builds', new Document([
|
|
|
|
'$id' => $buildId,
|
|
|
|
'$permissions' => [],
|
|
|
|
'startTime' => $startTime,
|
|
|
|
'deploymentId' => $deployment->getId(),
|
|
|
|
'status' => 'processing',
|
|
|
|
'outputPath' => '',
|
|
|
|
'runtime' => $function->getAttribute('runtime'),
|
|
|
|
'source' => $deployment->getAttribute('path'),
|
|
|
|
'sourceType' => $device,
|
|
|
|
'stdout' => '',
|
|
|
|
'stderr' => '',
|
|
|
|
'endTime' => null,
|
|
|
|
'duration' => 0
|
|
|
|
]));
|
|
|
|
$deployment->setAttribute('buildId', $buildId);
|
|
|
|
$deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
|
|
|
|
} else {
|
|
|
|
$build = $dbForProject->getDocument('builds', $buildId);
|
2022-01-24 11:25:46 +13:00
|
|
|
}
|
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/** Request the executor to build the code... */
|
|
|
|
$build->setAttribute('status', 'building');
|
|
|
|
$build = $dbForProject->updateDocument('builds', $buildId, $build);
|
|
|
|
|
|
|
|
/** Trigger Webhook */
|
|
|
|
$deploymentModel = new Deployment();
|
|
|
|
|
|
|
|
$pools = $register->get('pools');
|
|
|
|
$connection = $pools->get('queue')->pop()->getResource();
|
|
|
|
|
|
|
|
$deploymentUpdate = new Event(Event::WEBHOOK_QUEUE_NAME, Event::WEBHOOK_CLASS_NAME, $connection);
|
|
|
|
$deploymentUpdate
|
|
|
|
->setProject($project)
|
|
|
|
->setEvent('functions.[functionId].deployments.[deploymentId].update')
|
|
|
|
->setParam('functionId', $function->getId())
|
|
|
|
->setParam('deploymentId', $deployment->getId())
|
|
|
|
->setPayload($deployment->getArrayCopy(array_keys($deploymentModel->getRules())))
|
|
|
|
->trigger();
|
|
|
|
|
|
|
|
/** Trigger Functions */
|
|
|
|
$pools = $register->get('pools');
|
|
|
|
$connection = $pools->get('queue')->pop();
|
|
|
|
|
|
|
|
$functions = new Func($connection->getResource());
|
|
|
|
$functions
|
|
|
|
->from($deploymentUpdate)
|
|
|
|
->trigger();
|
|
|
|
|
|
|
|
$connection->reclaim();
|
|
|
|
|
|
|
|
/** Trigger Realtime */
|
|
|
|
$allEvents = Event::generateEvents('functions.[functionId].deployments.[deploymentId].update', [
|
|
|
|
'functionId' => $function->getId(),
|
|
|
|
'deploymentId' => $deployment->getId()
|
|
|
|
]);
|
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $allEvents[0],
|
|
|
|
payload: $build,
|
|
|
|
project: $project
|
|
|
|
);
|
|
|
|
|
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $build->getArrayCopy(),
|
|
|
|
events: $allEvents,
|
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles']
|
|
|
|
);
|
|
|
|
|
|
|
|
$source = $deployment->getAttribute('path');
|
|
|
|
|
|
|
|
$vars = array_reduce($function->getAttribute('vars', []), function (array $carry, Document $var) {
|
|
|
|
$carry[$var->getAttribute('key')] = $var->getAttribute('value');
|
|
|
|
return $carry;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $executor->createRuntime(
|
|
|
|
projectId: $project->getId(),
|
|
|
|
deploymentId: $deployment->getId(),
|
|
|
|
source: $source,
|
|
|
|
image: $runtime['image'],
|
|
|
|
remove: true,
|
|
|
|
entrypoint: $deployment->getAttribute('entrypoint'),
|
|
|
|
workdir: '/usr/code',
|
|
|
|
destination: APP_STORAGE_BUILDS . "/app-{$project->getId()}",
|
|
|
|
variables: $vars,
|
|
|
|
commands: [
|
|
|
|
'sh', '-c',
|
|
|
|
'tar -zxf /tmp/code.tar.gz -C /usr/code && \
|
|
|
|
cd /usr/local/src/ && ./build.sh'
|
|
|
|
]
|
|
|
|
);
|
2022-11-17 01:19:29 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$endTime = new \DateTime();
|
|
|
|
$endTime->setTimestamp($response['endTimeUnix']);
|
2022-04-14 00:39:31 +12:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/** Update the build document */
|
|
|
|
$build->setAttribute('endTime', DateTime::format($endTime));
|
|
|
|
$build->setAttribute('duration', \intval($response['duration']));
|
|
|
|
$build->setAttribute('status', $response['status']);
|
|
|
|
$build->setAttribute('outputPath', $response['outputPath']);
|
|
|
|
$build->setAttribute('stderr', $response['stderr']);
|
|
|
|
$build->setAttribute('stdout', $response['stdout']);
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/* Also update the deployment buildTime */
|
|
|
|
$deployment->setAttribute('buildTime', $response['duration']);
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
Console::success("Build id: $buildId created");
|
2022-01-24 11:25:46 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
$function->setAttribute('scheduleUpdatedAt', DateTime::now());
|
2022-11-17 20:44:54 +13:00
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/** Set auto deploy */
|
|
|
|
if ($deployment->getAttribute('activate') === true) {
|
|
|
|
$function->setAttribute('deployment', $deployment->getId());
|
|
|
|
$function = $dbForProject->updateDocument('functions', $function->getId(), $function);
|
2022-02-03 07:58:03 +13:00
|
|
|
}
|
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/** Update function schedule */
|
|
|
|
$dbForConsole = getConsoleDB();
|
|
|
|
$schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId'));
|
|
|
|
$schedule->setAttribute('resourceUpdatedAt', $function->getAttribute('scheduleUpdatedAt'));
|
|
|
|
|
|
|
|
$schedule
|
|
|
|
->setAttribute('schedule', $function->getAttribute('schedule'))
|
|
|
|
->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment')));
|
|
|
|
|
|
|
|
|
|
|
|
Authorization::skip(fn () => $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule));
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$endTime = DateTime::now();
|
|
|
|
$interval = (new \DateTime($endTime))->diff(new \DateTime($startTime));
|
|
|
|
$build->setAttribute('endTime', $endTime);
|
|
|
|
$build->setAttribute('duration', $interval->format('%s') + 0);
|
|
|
|
$build->setAttribute('status', 'failed');
|
|
|
|
$build->setAttribute('stderr', $th->getMessage());
|
|
|
|
Console::error($th->getMessage());
|
|
|
|
} finally {
|
2022-02-03 07:58:03 +13:00
|
|
|
$build = $dbForProject->updateDocument('builds', $buildId, $build);
|
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/**
|
|
|
|
* Send realtime Event
|
|
|
|
*/
|
2022-05-10 00:36:29 +12:00
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $allEvents[0],
|
|
|
|
payload: $build,
|
|
|
|
project: $project
|
|
|
|
);
|
2022-03-01 00:50:27 +13:00
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $build->getArrayCopy(),
|
2022-04-19 04:21:45 +12:00
|
|
|
events: $allEvents,
|
2022-03-01 00:50:27 +13:00
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles']
|
|
|
|
);
|
|
|
|
|
2022-12-21 01:22:58 +13:00
|
|
|
/** Update usage stats */
|
|
|
|
if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') {
|
|
|
|
$statsd = $register->get('statsd');
|
|
|
|
$usage = new Stats($statsd);
|
|
|
|
$usage
|
|
|
|
->setParam('projectInternalId', $project->getInternalId())
|
|
|
|
->setParam('projectId', $project->getId())
|
|
|
|
->setParam('functionId', $function->getId())
|
|
|
|
->setParam('builds.{scope}.compute', 1)
|
|
|
|
->setParam('buildStatus', $build->getAttribute('status', ''))
|
|
|
|
->setParam('buildTime', $build->getAttribute('duration'))
|
|
|
|
->setParam('networkRequestSize', 0)
|
|
|
|
->setParam('networkResponseSize', 0)
|
|
|
|
->submit();
|
2022-02-14 10:26:36 +13:00
|
|
|
}
|
2022-01-24 11:25:46 +13:00
|
|
|
}
|
|
|
|
}
|
2022-12-21 01:22:58 +13:00
|
|
|
|
|
|
|
$server->job()
|
|
|
|
->inject('message')
|
|
|
|
->action(function (Message $message) {
|
|
|
|
$payload = $message->getPayload() ?? [];
|
|
|
|
|
|
|
|
if (empty($payload)) {
|
|
|
|
throw new Exception('Missing payload');
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $payload['type'] ?? '';
|
|
|
|
$project = new Document($payload['project'] ?? []);
|
|
|
|
$resource = new Document($payload['resource'] ?? []);
|
|
|
|
$deployment = new Document($payload['deployment'] ?? []);
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case BUILD_TYPE_DEPLOYMENT:
|
|
|
|
case BUILD_TYPE_RETRY:
|
|
|
|
Console::info('Creating build for deployment: ' . $deployment->getId());
|
|
|
|
buildDeployment($project, $resource, $deployment);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Exception('Invalid build type');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$server->workerStart();
|
|
|
|
$server->start();
|