Skip to content

Commit

Permalink
Merge pull request #37 from open-runtimes/feat-backawrds-storage-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron authored Aug 23, 2023
2 parents 2376110 + 41ac543 commit 85a715d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ OPR_EXECUTOR_DOCKER_HUB_PASSWORD=

> `OPR_EXECUTOR_CONNECTION_STORAGE` takes a DSN string that represents a connection to your storage device. If you would like to use your local filesystem, you can use `file://localhost`. If using S3 or any other provider for storage, use a DSN of the following format `s3://access_key:access_secret@host:port/bucket_name?region=us-east-1`

> For backwards compatibility, executor also supports `OPR_EXECUTOR_STORAGE_*` variables as replacement for `OPR_EXECUTOR_CONNECTION_STORAGE`, as seen in [Appwrite repository](https://github.com/appwrite/appwrite/blob/1.3.8/.env#L26-L46).

4. Start Docker container:

```bash
Expand Down
110 changes: 76 additions & 34 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,41 +178,83 @@ function logError(Log $log, Throwable $error, string $action, Logger $logger = n

function getStorageDevice(string $root): Device
{
$connection = \strval(App::getEnv('OPR_EXECUTOR_CONNECTION_STORAGE', ''));

$acl = 'private';
$device = '';
$accessKey = '';
$accessSecret = '';
$bucket = '';
$region = '';

try {
$dsn = new DSN($connection);
$device = $dsn->getScheme();
$accessKey = $dsn->getUser() ?? '';
$accessSecret = $dsn->getPassword() ?? '';
$bucket = $dsn->getPath() ?? '';
$region = $dsn->getParam('region');
} catch (\Exception $e) {
Console::warning('Defaulting to Local storage due to error: ' . $e->getMessage());
$device = 'Local';
}
$connection = App::getEnv('OPR_EXECUTOR_CONNECTION_STORAGE', '');

if (!empty($connection)) {
$acl = 'private';
$device = Storage::DEVICE_LOCAL;
$accessKey = '';
$accessSecret = '';
$bucket = '';
$region = '';

try {
$dsn = new DSN($connection);
$device = $dsn->getScheme();
$accessKey = $dsn->getUser() ?? '';
$accessSecret = $dsn->getPassword() ?? '';
$bucket = $dsn->getPath() ?? '';
$region = $dsn->getParam('region');
} catch (\Exception $e) {
Console::warning($e->getMessage() . 'Invalid DSN. Defaulting to Local device.');
}

switch ($device) {
case Storage::DEVICE_S3:
return new S3($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_DO_SPACES:
return new DOSpaces($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_BACKBLAZE:
return new Backblaze($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_LINODE:
return new Linode($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_WASABI:
return new Wasabi($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_LOCAL:
default:
return new Local($root);
switch ($device) {
case Storage::DEVICE_S3:
return new S3($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case STORAGE::DEVICE_DO_SPACES:
return new DOSpaces($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_BACKBLAZE:
return new Backblaze($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_LINODE:
return new Linode($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_WASABI:
return new Wasabi($root, $accessKey, $accessSecret, $bucket, $region, $acl);
case Storage::DEVICE_LOCAL:
default:
return new Local($root);
}
} else {
switch (strtolower(App::getEnv('OPR_EXECUTOR_STORAGE_DEVICE', Storage::DEVICE_LOCAL) ?? '')) {
case Storage::DEVICE_LOCAL:
default:
return new Local($root);
case Storage::DEVICE_S3:
$s3AccessKey = App::getEnv('OPR_EXECUTOR_STORAGE_S3_ACCESS_KEY', '') ?? '';
$s3SecretKey = App::getEnv('OPR_EXECUTOR_STORAGE_S3_SECRET', '') ?? '';
$s3Region = App::getEnv('OPR_EXECUTOR_STORAGE_S3_REGION', '') ?? '';
$s3Bucket = App::getEnv('OPR_EXECUTOR_STORAGE_S3_BUCKET', '') ?? '';
$s3Acl = 'private';
return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl);
case Storage::DEVICE_DO_SPACES:
$doSpacesAccessKey = App::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_ACCESS_KEY', '') ?? '';
$doSpacesSecretKey = App::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_SECRET', '') ?? '';
$doSpacesRegion = App::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_REGION', '') ?? '';
$doSpacesBucket = App::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_BUCKET', '') ?? '';
$doSpacesAcl = 'private';
return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl);
case Storage::DEVICE_BACKBLAZE:
$backblazeAccessKey = App::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_ACCESS_KEY', '') ?? '';
$backblazeSecretKey = App::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_SECRET', '') ?? '';
$backblazeRegion = App::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_REGION', '') ?? '';
$backblazeBucket = App::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_BUCKET', '') ?? '';
$backblazeAcl = 'private';
return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl);
case Storage::DEVICE_LINODE:
$linodeAccessKey = App::getEnv('OPR_EXECUTOR_STORAGE_LINODE_ACCESS_KEY', '') ?? '';
$linodeSecretKey = App::getEnv('OPR_EXECUTOR_STORAGE_LINODE_SECRET', '') ?? '';
$linodeRegion = App::getEnv('OPR_EXECUTOR_STORAGE_LINODE_REGION', '') ?? '';
$linodeBucket = App::getEnv('OPR_EXECUTOR_STORAGE_LINODE_BUCKET', '') ?? '';
$linodeAcl = 'private';
return new Linode($root, $linodeAccessKey, $linodeSecretKey, $linodeBucket, $linodeRegion, $linodeAcl);
case Storage::DEVICE_WASABI:
$wasabiAccessKey = App::getEnv('OPR_EXECUTOR_STORAGE_WASABI_ACCESS_KEY', '') ?? '';
$wasabiSecretKey = App::getEnv('OPR_EXECUTOR_STORAGE_WASABI_SECRET', '') ?? '';
$wasabiRegion = App::getEnv('OPR_EXECUTOR_STORAGE_WASABI_REGION', '') ?? '';
$wasabiBucket = App::getEnv('OPR_EXECUTOR_STORAGE_WASABI_BUCKET', '') ?? '';
$wasabiAcl = 'private';
return new Wasabi($root, $wasabiAccessKey, $wasabiSecretKey, $wasabiBucket, $wasabiRegion, $wasabiAcl);
}
}
}

Expand Down

0 comments on commit 85a715d

Please sign in to comment.