From b4670bfb2b200628c8b52264f04c34c1e0ced1bf Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Sun, 19 Mar 2023 02:18:31 +0530 Subject: [PATCH] CreateDeploymentUsingVcs endpoint working --- app/controllers/api/functions.php | 27 ++++---- app/workers/builds.php | 107 ++++++++++++++++++++---------- docker-compose.yml | 2 + 3 files changed, 87 insertions(+), 49 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 75ba9aa876..a8909e4596 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -822,19 +822,19 @@ App::post('/v1/functions/:functionId/deployments') $activate = (bool) filter_var($activate, FILTER_VALIDATE_BOOLEAN); - if ($activate) { - // Remove deploy for all other deployments. - $activeDeployments = $dbForProject->find('deployments', [ - Query::equal('activate', [true]), - Query::equal('resourceId', [$functionId]), - Query::equal('resourceType', ['functions']) - ]); + // if ($activate) { + // // Remove deploy for all other deployments. + // $activeDeployments = $dbForProject->find('deployments', [ + // Query::equal('activate', [true]), + // Query::equal('resourceId', [$functionId]), + // Query::equal('resourceType', ['functions']) + // ]); - foreach ($activeDeployments as $activeDeployment) { - $activeDeployment->setAttribute('activate', false); - $dbForProject->updateDocument('deployments', $activeDeployment->getId(), $activeDeployment); - } - } + // foreach ($activeDeployments as $activeDeployment) { + // $activeDeployment->setAttribute('activate', false); + // $dbForProject->updateDocument('deployments', $activeDeployment->getId(), $activeDeployment); + // } + // } $deployment = $dbForProject->getDocument('deployments', $deploymentId); @@ -855,7 +855,7 @@ App::post('/v1/functions/:functionId/deployments') ])); } else { - $deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment->setAttribute('size', null)->setAttribute('metadata', null)); + // $deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment->setAttribute('size', null)->setAttribute('metadata', null)); } // Start the build @@ -865,6 +865,7 @@ App::post('/v1/functions/:functionId/deployments') ->setResource($function) ->setDeployment($deployment) ->setProject($project) + ->trigger(); $events diff --git a/app/workers/builds.php b/app/workers/builds.php index 8e1aa435b6..a90e88bfad 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -86,56 +86,89 @@ class BuildsV1 extends Worker $startTime = DateTime::now(); if (empty($buildId)) { $buildId = ID::unique(); - // convert output to .tar.gz - // upload it to some storage - // pass the path to source attribute in the below method + + $isVcsEnabled = true; + if($isVcsEnabled){ + // convert output to .tar.gz + // upload it to some storage + // pass the path to source attribute in the below method - $gitCloneCommand = $deployment->getAttribute('path'); - $stdout = ''; - $stderr = ''; - Console::execute('mkdir /tmp/builds/' . $buildId, '', $stdout, $stderr); - Console::execute($gitCloneCommand . ' /tmp/builds/' . $buildId . '/code', '', $stdout, $stderr); - Console::execute('tar --exclude code.tar.gz -czf /tmp/builds/' . $buildId .'/code.tar.gz -C /tmp/builds/' . $buildId .'/code .', '', $stdout, $stderr); + $gitCloneCommand = $deployment->getAttribute('path'); + $stdout = ''; + $stderr = ''; + Console::execute('mkdir /tmp/builds/' . $buildId, '', $stdout, $stderr); + Console::execute($gitCloneCommand . ' /tmp/builds/' . $buildId . '/code', '', $stdout, $stderr); + Console::execute('tar --exclude code.tar.gz -czf /tmp/builds/' . $buildId .'/code.tar.gz -C /tmp/builds/' . $buildId .'/code .', '', $stdout, $stderr); - $deviceFunctions = $this->getFunctionsDevice($project->getId()); - var_dump($project->getId()); + $deviceFunctions = $this->getFunctionsDevice($project->getId()); + var_dump($project->getId()); - $fileName = 'code.tar.gz'; - $fileTmpName = '/tmp/builds/' . $buildId . '/code.tar.gz'; + $fileName = 'code.tar.gz'; + $fileTmpName = '/tmp/builds/' . $buildId . '/code.tar.gz'; - $deploymentId = $deployment->getId(); - $path = $deviceFunctions->getPath($deploymentId . '.' . \pathinfo($fileName, PATHINFO_EXTENSION)); + $deploymentId = $deployment->getId(); + $path = $deviceFunctions->getPath($deploymentId . '.' . \pathinfo($fileName, PATHINFO_EXTENSION)); - var_dump("path"); - var_dump($path); + // if (!\file_exists(\dirname($path))) { + // if (!@\mkdir(\dirname($path), 0755, true)) { + // throw new Exception("Failed to create temporary directory", 500); + // } + // } - $chunksUploaded = $deviceFunctions->upload($fileTmpName, $path); - var_dump($chunksUploaded); + var_dump("path"); + var_dump($path); - var_dump("hello"); - var_dump($path); + $result = $deviceFunctions->move($fileTmpName, $path); + var_dump($result); - $build = $dbForProject->createDocument('builds', new Document([ - '$id' => $buildId, - '$permissions' => [], - 'startTime' => $startTime, - 'deploymentId' => $deployment->getId(), - 'status' => 'processing', - 'outputPath' => '', - 'runtime' => $function->getAttribute('runtime'), - 'source' => $path, - 'sourceType' => strtolower(App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)), - 'stdout' => '', - 'stderr' => '', - 'endTime' => null, - 'duration' => 0 - ])); + if(!$result){ + throw new \Exception("Unable to move file"); + } + + var_dump("hello"); + var_dump($path); + } + + if($isVcsEnabled){ + $build = $dbForProject->createDocument('builds', new Document([ + '$id' => $buildId, + '$permissions' => [], + 'startTime' => $startTime, + 'deploymentId' => $deployment->getId(), + 'status' => 'processing', + 'outputPath' => '', + 'runtime' => $function->getAttribute('runtime'), + 'source' => $path, + 'sourceType' => strtolower(App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)), + 'stdout' => '', + 'stderr' => '', + 'endTime' => null, + 'duration' => 0 + ])); + }else{ + $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' => strtolower(App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)), + 'stdout' => '', + 'stderr' => '', + 'endTime' => null, + 'duration' => 0 + ])); + } $deployment->setAttribute('buildId', $buildId); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment); } else { $build = $dbForProject->getDocument('builds', $buildId); } + var_dump($build); /** Request the executor to build the code... */ $build->setAttribute('status', 'building'); $build = $dbForProject->updateDocument('builds', $buildId, $build); @@ -179,6 +212,8 @@ class BuildsV1 extends Worker ); $source = $deployment->getAttribute('path'); + if($isVcsEnabled) + $source = $path; $vars = array_reduce($function['vars'] ?? [], function (array $carry, Document $var) { $carry[$var->getAttribute('key')] = $var->getAttribute('value'); diff --git a/docker-compose.yml b/docker-compose.yml index 6249632864..89052eb7a0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -374,6 +374,8 @@ services: volumes: - ./app:/usr/src/code/app - ./src:/usr/src/code/src + - appwrite-functions:/storage/functions:rw + - appwrite-builds:/storage/builds:rw depends_on: - redis - mariadb