From d150f57c8ff3738f70ab72c5cf5756df981921a1 Mon Sep 17 00:00:00 2001 From: "Harry (hjbdev)" Date: Mon, 31 Mar 2025 13:51:02 +0000 Subject: [PATCH] enum case consistency, wip creating service --- app/Actions/Services/CreateService.php | 31 +++++++ app/Data/Deployments/Step.php | 10 ++- app/Drivers/DatabaseDriver.php | 9 +- app/Drivers/Driver.php | 4 +- app/Drivers/Postgres/Postgres17Driver.php | 47 +++++++++- app/Enums/OrganisationRole.php | 4 +- app/Enums/RepositoryType.php | 2 +- app/Enums/ServerProvider.php | 4 +- app/Enums/ServiceCategory.php | 12 +++ app/Enums/ServiceStatus.php | 2 + app/Enums/ServiceType.php | 16 ++++ app/Models/Service.php | 4 + app/Models/User.php | 2 +- config/app.php | 101 ---------------------- config/keystone.php | 11 +++ database/seeders/DatabaseSeeder.php | 4 +- 16 files changed, 150 insertions(+), 113 deletions(-) create mode 100644 app/Actions/Services/CreateService.php create mode 100644 app/Enums/ServiceCategory.php create mode 100644 app/Enums/ServiceType.php create mode 100644 config/keystone.php diff --git a/app/Actions/Services/CreateService.php b/app/Actions/Services/CreateService.php new file mode 100644 index 0000000..99332ec --- /dev/null +++ b/app/Actions/Services/CreateService.php @@ -0,0 +1,31 @@ +services()->create([ + 'name' => $name, + 'category' => $category, + 'type' => $type, + 'version' => $version, // 17 + 'driver_name' => $driverName, // postgres + 'status' => ServiceStatus::NOT_INSTALLED, + ]); + } +} diff --git a/app/Data/Deployments/Step.php b/app/Data/Deployments/Step.php index 05c81ea..1c78ae7 100644 --- a/app/Data/Deployments/Step.php +++ b/app/Data/Deployments/Step.php @@ -4,8 +4,16 @@ namespace App\Data\Deployments; class Step { + public string $script; + public function __construct( + public string $name = 'Step', + string|callable $script = 'echo "Incomplete Step"', ) { - // + if (is_callable($script)) { + $this->script = $script(); + } else { + $this->script = $script; + } } } \ No newline at end of file diff --git a/app/Drivers/DatabaseDriver.php b/app/Drivers/DatabaseDriver.php index d545d4a..7a73eb2 100644 --- a/app/Drivers/DatabaseDriver.php +++ b/app/Drivers/DatabaseDriver.php @@ -4,5 +4,12 @@ namespace App\Drivers; interface DatabaseDriver extends Driver { - // + public string $defaultUser = 'keystone'; + public string $defaultDb = 'keystone'; + + public function __construct( + public ?string $containerName = null, + public ?string $containerId = null, + public ?string $defaultPassword = null, + ); } \ No newline at end of file diff --git a/app/Drivers/Driver.php b/app/Drivers/Driver.php index b02317a..a0d2ad7 100644 --- a/app/Drivers/Driver.php +++ b/app/Drivers/Driver.php @@ -2,7 +2,9 @@ namespace App\Drivers; +use App\Data\Deployments\Plan; + interface Driver { - // + public Plan $deploymentPlan { get; } } \ No newline at end of file diff --git a/app/Drivers/Postgres/Postgres17Driver.php b/app/Drivers/Postgres/Postgres17Driver.php index 70c46b4..1523af9 100644 --- a/app/Drivers/Postgres/Postgres17Driver.php +++ b/app/Drivers/Postgres/Postgres17Driver.php @@ -2,9 +2,54 @@ namespace App\Drivers\Postgres; +use App\Data\Deployments\Plan; +use App\Data\Deployments\Step; use App\Drivers\DatabaseDriver; class Postgres17Driver implements DatabaseDriver { - // @todo + public Plan $deploymentPlan; + public string $defaultUser = 'keystone'; + public string $defaultDb = 'keystone'; + + public function __construct( + public ?string $containerName = null, + public ?string $containerId = null, + public ?string $defaultPassword = null, + ) + { + $this->deploymentPlan = new Plan(steps: [ + new Step( + name: 'Check docker is installed', + script: 'command -v docker >/dev/null 2>&1 || { echo >&2 "Docker is not installed. Aborting."; exit 1; }', + ), + new Step( + name: 'Run the docker image', + script: function () { + $script = collect(); + if ($this->containerName) { + $script->push('docker stop ' . $this->containerName . ' || true'); + } else if ($this->containerId) { + $script->push('docker stop ' . $this->containerId . ' || true'); + } + + $runCommand = "docker run -d"; + if ($this->containerName) { + $runCommand .= " --name {$this->containerName}"; + } + if ($this->defaultPassword) { + $runCommand .= " -e POSTGRES_PASSWORD={$this->defaultPassword}"; + } + if ($this->defaultUser) { + $runCommand .= " -e POSTGRES_USER={$this->defaultUser}"; + } + if ($this->defaultDb) { + $runCommand .= " -e POSTGRES_DB={$this->defaultDb}"; + } + + $runCommand .= " -p 5432:5432 postgres:17"; + } + ) + ]); + } } \ No newline at end of file diff --git a/app/Enums/OrganisationRole.php b/app/Enums/OrganisationRole.php index 0edafe0..7b55a9d 100644 --- a/app/Enums/OrganisationRole.php +++ b/app/Enums/OrganisationRole.php @@ -4,6 +4,6 @@ namespace App\Enums; enum OrganisationRole: string { - case Admin = 'admin'; - case Member = 'member'; + case ADMIN = 'admin'; + case MEMBER = 'member'; } diff --git a/app/Enums/RepositoryType.php b/app/Enums/RepositoryType.php index 1bdb379..057968b 100644 --- a/app/Enums/RepositoryType.php +++ b/app/Enums/RepositoryType.php @@ -4,5 +4,5 @@ namespace App\Enums; enum RepositoryType: string { - case Git = 'git'; + case GIT = 'git'; } \ No newline at end of file diff --git a/app/Enums/ServerProvider.php b/app/Enums/ServerProvider.php index 3c33ed6..4f9a1dc 100644 --- a/app/Enums/ServerProvider.php +++ b/app/Enums/ServerProvider.php @@ -4,6 +4,6 @@ namespace App\Enums; enum ServerProvider: string { - case Hetzner = 'hetzner'; - case DigitalOcean = 'digital-ocean'; + case HETZNER = 'hetzner'; + case DIGITAL_OCEAN = 'digital-ocean'; } diff --git a/app/Enums/ServiceCategory.php b/app/Enums/ServiceCategory.php new file mode 100644 index 0000000..0b5c9dc --- /dev/null +++ b/app/Enums/ServiceCategory.php @@ -0,0 +1,12 @@ + ServiceStatus::class, + 'category' => ServiceCategory::class, + 'type' => ServiceType::class, ]; } diff --git a/app/Models/User.php b/app/Models/User.php index a409714..28a021b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -26,7 +26,7 @@ class User extends Authenticatable 'slug' => Organisation::createUniqueSlug($user->name), 'owner_id' => $user->id, ]); - $organisation->members()->attach($user, ['role' => OrganisationRole::Admin]); + $organisation->members()->attach($user, ['role' => OrganisationRole::ADMIN]); }); } diff --git a/config/app.php b/config/app.php index 324b513..57bade1 100644 --- a/config/app.php +++ b/config/app.php @@ -1,126 +1,25 @@ env('APP_NAME', 'Laravel'), - - /* - |-------------------------------------------------------------------------- - | Application Environment - |-------------------------------------------------------------------------- - | - | This value determines the "environment" your application is currently - | running in. This may determine how you prefer to configure various - | services the application utilizes. Set this in your ".env" file. - | - */ - 'env' => env('APP_ENV', 'production'), - - /* - |-------------------------------------------------------------------------- - | Application Debug Mode - |-------------------------------------------------------------------------- - | - | When your application is in debug mode, detailed error messages with - | stack traces will be shown on every error that occurs within your - | application. If disabled, a simple generic error page is shown. - | - */ - 'debug' => (bool) env('APP_DEBUG', false), - - /* - |-------------------------------------------------------------------------- - | Application URL - |-------------------------------------------------------------------------- - | - | This URL is used by the console to properly generate URLs when using - | the Artisan command line tool. You should set this to the root of - | the application so that it's available within Artisan commands. - | - */ - 'url' => env('APP_URL', 'http://localhost'), - - /* - |-------------------------------------------------------------------------- - | Application Timezone - |-------------------------------------------------------------------------- - | - | Here you may specify the default timezone for your application, which - | will be used by the PHP date and date-time functions. The timezone - | is set to "UTC" by default as it is suitable for most use cases. - | - */ - 'timezone' => 'UTC', - - /* - |-------------------------------------------------------------------------- - | Application Locale Configuration - |-------------------------------------------------------------------------- - | - | The application locale determines the default locale that will be used - | by Laravel's translation / localization methods. This option can be - | set to any locale for which you plan to have translation strings. - | - */ - 'locale' => env('APP_LOCALE', 'en'), - 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'), - 'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'), - /* - |-------------------------------------------------------------------------- - | Encryption Key - |-------------------------------------------------------------------------- - | - | This key is utilized by Laravel's encryption services and should be set - | to a random, 32 character string to ensure that all encrypted values - | are secure. You should do this prior to deploying the application. - | - */ - 'cipher' => 'AES-256-CBC', - 'key' => env('APP_KEY'), - 'previous_keys' => [ ...array_filter( explode(',', env('APP_PREVIOUS_KEYS', '')) ), ], - /* - |-------------------------------------------------------------------------- - | Maintenance Mode Driver - |-------------------------------------------------------------------------- - | - | These configuration options determine the driver used to determine and - | manage Laravel's "maintenance mode" status. The "cache" driver will - | allow maintenance mode to be controlled across multiple machines. - | - | Supported drivers: "file", "cache" - | - */ - 'maintenance' => [ 'driver' => env('APP_MAINTENANCE_DRIVER', 'file'), 'store' => env('APP_MAINTENANCE_STORE', 'database'), ], - ]; diff --git a/config/keystone.php b/config/keystone.php new file mode 100644 index 0000000..410826f --- /dev/null +++ b/config/keystone.php @@ -0,0 +1,11 @@ + [ + 'postgres' => [ + '17' => Postgres17Driver::class, + ] + ] +]; \ No newline at end of file diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0072cf8..6d951bd 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -31,12 +31,12 @@ class DatabaseSeeder extends Seeder 'owner_id' => 1, ]); - $organisation->members()->attach($user, ['role' => OrganisationRole::Admin]); + $organisation->members()->attach($user, ['role' => OrganisationRole::ADMIN]); $application = $organisation->applications()->create([ 'name' => 'ClipBin', 'repository_url' => 'git@github.com:hjbdev/clipbin.git', - 'repository_type' => RepositoryType::Git, + 'repository_type' => RepositoryType::GIT, ]); $application->environments()->create([