From a45fab4e43e8eef91230d50af1e4da0ae2692382 Mon Sep 17 00:00:00 2001 From: Everly Precia Suresh Date: Thu, 12 May 2022 22:01:53 +0000 Subject: [PATCH] better way to initialize storage libs --- app/executor.php | 37 ++++++++++++++++++++++++++++------ app/init.php | 44 +++++++++++++++++++++++++++++++---------- app/workers/deletes.php | 3 +-- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/app/executor.php b/app/executor.php index 6fc1aaf691..3d827f0a24 100644 --- a/app/executor.php +++ b/app/executor.php @@ -2,7 +2,6 @@ require_once __DIR__ . '/../vendor/autoload.php'; use Appwrite\Runtimes\Runtimes; -use Appwrite\Resque\Worker; use Swoole\ConnectionPool; use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Response as SwooleResponse; @@ -114,6 +113,34 @@ function logError(Throwable $error, string $action, Utopia\Route $route = null) Console::error('[Error] Line: ' . $error->getLine()); }; +function getStorageDevice($root): Device { + switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) { + case Storage::DEVICE_LOCAL:default: + return new Local($root); + case Storage::DEVICE_S3: + $s3AccessKey = App::getEnv('_APP_STORAGE_S3_ACCESS_KEY', ''); + $s3SecretKey = App::getEnv('_APP_STORAGE_S3_SECRET', ''); + $s3Region = App::getEnv('_APP_STORAGE_S3_REGION', ''); + $s3Bucket = App::getEnv('_APP_STORAGE_S3_BUCKET', ''); + $s3Acl = 'private'; + return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl); + case Storage::DEVICE_DO_SPACES: + $doSpacesAccessKey = App::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', ''); + $doSpacesSecretKey = App::getEnv('_APP_STORAGE_DO_SPACES_SECRET', ''); + $doSpacesRegion = App::getEnv('_APP_STORAGE_DO_SPACES_REGION', ''); + $doSpacesBucket = App::getEnv('_APP_STORAGE_DO_SPACES_BUCKET', ''); + $doSpacesAcl = 'private'; + return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl); + case Storage::DEVICE_BACKBLAZE: + $backblazeAccessKey = App::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', ''); + $backblazeSecretKey = App::getEnv('_APP_STORAGE_BACKBLAZE_SECRET', ''); + $backblazeRegion = App::getEnv('_APP_STORAGE_BACKBLAZE_REGION', ''); + $backblazeBucket = App::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', ''); + $backblazeAcl = 'private'; + return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl); + } +} + App::post('/v1/runtimes') ->desc("Create a new runtime server") ->param('runtimeId', '', new Text(64), 'Unique runtime ID.') @@ -154,8 +181,7 @@ App::post('/v1/runtimes') /** * Copy code files from source to a temporary location on the executor */ - $worker=new Worker(); - $sourceDevice = $worker->getDevice("/"); + $sourceDevice = getStorageDevice("/"); $localDevice = new Local(); $buffer = $sourceDevice->read($source); if(!$localDevice->write($tmpSource, $buffer)) { @@ -242,9 +268,8 @@ App::post('/v1/runtimes') if (!\file_exists($tmpBuild)) { throw new Exception('Something went wrong during the build process', 500); } - - $worker=new Worker(); - $destinationDevice = $worker->getDevice($destination); + + $destinationDevice = getStorageDevice($destination); $outputPath = $destinationDevice->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); $buffer = $localDevice->read($tmpBuild); diff --git a/app/init.php b/app/init.php index e3b364b0e6..170542effa 100644 --- a/app/init.php +++ b/app/init.php @@ -27,7 +27,6 @@ use Appwrite\Network\Validator\Email; use Appwrite\Network\Validator\IP; use Appwrite\Network\Validator\URL; use Appwrite\OpenSSL\OpenSSL; -use Appwrite\Resque\Worker; use Appwrite\Stats\Stats; use Appwrite\Utopia\View; use Utopia\App; @@ -53,10 +52,10 @@ use Swoole\Database\RedisPool; use Utopia\Database\Query; use Utopia\Storage\Device; use Utopia\Storage\Storage; -use Utopia\Storage\Device\Local; use Utopia\Storage\Device\Backblaze; -use Utopia\Storage\Device\S3; use Utopia\Storage\Device\DOSpaces; +use Utopia\Storage\Device\Local; +use Utopia\Storage\Device\S3; const APP_NAME = 'Appwrite'; const APP_DOMAIN = 'appwrite.io'; @@ -808,20 +807,45 @@ App::setResource('deviceLocal', function() { }); App::setResource('deviceFiles', function($project) { - $worker=new Worker(); - return $worker->getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); + return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); }, ['project']); App::setResource('deviceFunctions', function($project) { - $worker=new Worker(); - return $worker->getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); + return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); }, ['project']); App::setResource('deviceBuilds', function($project) { - $worker=new Worker(); - return $worker->getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); + return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); }, ['project']); +function getDevice($root): Device { + switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) { + case Storage::DEVICE_LOCAL:default: + return new Local($root); + case Storage::DEVICE_S3: + $s3AccessKey = App::getEnv('_APP_STORAGE_S3_ACCESS_KEY', ''); + $s3SecretKey = App::getEnv('_APP_STORAGE_S3_SECRET', ''); + $s3Region = App::getEnv('_APP_STORAGE_S3_REGION', ''); + $s3Bucket = App::getEnv('_APP_STORAGE_S3_BUCKET', ''); + $s3Acl = 'private'; + return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl); + case Storage::DEVICE_DO_SPACES: + $doSpacesAccessKey = App::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', ''); + $doSpacesSecretKey = App::getEnv('_APP_STORAGE_DO_SPACES_SECRET', ''); + $doSpacesRegion = App::getEnv('_APP_STORAGE_DO_SPACES_REGION', ''); + $doSpacesBucket = App::getEnv('_APP_STORAGE_DO_SPACES_BUCKET', ''); + $doSpacesAcl = 'private'; + return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl); + case Storage::DEVICE_BACKBLAZE: + $backblazeAccessKey = App::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', ''); + $backblazeSecretKey = App::getEnv('_APP_STORAGE_BACKBLAZE_SECRET', ''); + $backblazeRegion = App::getEnv('_APP_STORAGE_BACKBLAZE_REGION', ''); + $backblazeBucket = App::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', ''); + $backblazeAcl = 'private'; + return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl); + } +} + App::setResource('mode', function($request) { /** @var Appwrite\Utopia\Request $request */ @@ -836,4 +860,4 @@ App::setResource('mode', function($request) { App::setResource('geodb', function($register) { /** @var Utopia\Registry\Registry $register */ return $register->get('geodb'); -}, ['register']); +}, ['register']); \ No newline at end of file diff --git a/app/workers/deletes.php b/app/workers/deletes.php index b365adea81..f6d2a94960 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -545,8 +545,7 @@ class DeletesV1 extends Worker $dbForProject = $this->getProjectDB($projectId); $dbForProject->deleteCollection('bucket_' . $document->getInternalId()); - $worker=new Worker(); - $device=$worker->getDevice(APP_STORAGE_UPLOADS.'/app-'.$projectId); + $device=$this->getDevice(APP_STORAGE_UPLOADS.'/app-'.$projectId); $device->deletePath($document->getId()); }