From 374ce9016078d239df25609168a7cd8c3fde595a Mon Sep 17 00:00:00 2001 From: "Harry (hjbdev)" Date: Mon, 31 Mar 2025 14:27:47 +0000 Subject: [PATCH] deployments wip --- app/Data/Deployments/Plan.php | 2 +- app/Data/Deployments/PlannedStep.php | 38 +++++++++++++++++++ app/Data/Deployments/Step.php | 19 ---------- app/Drivers/Postgres/Postgres17Driver.php | 9 ++++- app/Enums/DeploymentStatus.php | 11 ++++++ app/Models/Deployment.php | 20 ++++++++++ app/Models/Service.php | 6 +++ app/Models/Step.php | 14 +++++++ ..._03_31_140110_create_deployments_table.php | 25 ++++++++++++ .../2025_03_31_141005_create_steps_table.php | 25 ++++++++++++ 10 files changed, 147 insertions(+), 22 deletions(-) create mode 100644 app/Data/Deployments/PlannedStep.php delete mode 100644 app/Data/Deployments/Step.php create mode 100644 app/Enums/DeploymentStatus.php create mode 100644 app/Models/Deployment.php create mode 100644 app/Models/Step.php create mode 100644 database/migrations/2025_03_31_140110_create_deployments_table.php create mode 100644 database/migrations/2025_03_31_141005_create_steps_table.php diff --git a/app/Data/Deployments/Plan.php b/app/Data/Deployments/Plan.php index 8bb3f6a..0153463 100644 --- a/app/Data/Deployments/Plan.php +++ b/app/Data/Deployments/Plan.php @@ -5,7 +5,7 @@ namespace App\Data\Deployments; class Plan { /** - * @param Step[] $steps + * @param PlannedStep[] $steps */ public function __construct( public array $steps = [], diff --git a/app/Data/Deployments/PlannedStep.php b/app/Data/Deployments/PlannedStep.php new file mode 100644 index 0000000..343c70f --- /dev/null +++ b/app/Data/Deployments/PlannedStep.php @@ -0,0 +1,38 @@ +script = $script(); + } else { + $this->script = $script; + } + } + + public function getSafeScript(): string + { + $script = $this->script; + foreach ($this->secrets as $key => $value) { + $script = str_replace("[!{$key}]", '********', $script); + } + return $script; + } + + public function getScript(): string + { + $script = $this->script; + foreach ($this->secrets as $key => $value) { + $script = str_replace("[!{$key}]", $value, $script); + } + return $script; + } +} \ No newline at end of file diff --git a/app/Data/Deployments/Step.php b/app/Data/Deployments/Step.php deleted file mode 100644 index 1c78ae7..0000000 --- a/app/Data/Deployments/Step.php +++ /dev/null @@ -1,19 +0,0 @@ -script = $script(); - } else { - $this->script = $script; - } - } -} \ No newline at end of file diff --git a/app/Drivers/Postgres/Postgres17Driver.php b/app/Drivers/Postgres/Postgres17Driver.php index 1523af9..84e813b 100644 --- a/app/Drivers/Postgres/Postgres17Driver.php +++ b/app/Drivers/Postgres/Postgres17Driver.php @@ -3,7 +3,7 @@ namespace App\Drivers\Postgres; use App\Data\Deployments\Plan; -use App\Data\Deployments\Step; +use App\Data\Deployments\PlannedStep as Step; use App\Drivers\DatabaseDriver; class Postgres17Driver implements DatabaseDriver @@ -25,6 +25,9 @@ class Postgres17Driver implements DatabaseDriver ), new Step( name: 'Run the docker image', + secrets: [ + 'defaultpassword' => $this->defaultPassword, + ], script: function () { $script = collect(); if ($this->containerName) { @@ -38,7 +41,7 @@ class Postgres17Driver implements DatabaseDriver $runCommand .= " --name {$this->containerName}"; } if ($this->defaultPassword) { - $runCommand .= " -e POSTGRES_PASSWORD={$this->defaultPassword}"; + $runCommand .= " -e POSTGRES_PASSWORD=[!defaultpassword!]"; } if ($this->defaultUser) { $runCommand .= " -e POSTGRES_USER={$this->defaultUser}"; @@ -48,6 +51,8 @@ class Postgres17Driver implements DatabaseDriver } $runCommand .= " -p 5432:5432 postgres:17"; + + return $runCommand; } ) ]); diff --git a/app/Enums/DeploymentStatus.php b/app/Enums/DeploymentStatus.php new file mode 100644 index 0000000..50c188b --- /dev/null +++ b/app/Enums/DeploymentStatus.php @@ -0,0 +1,11 @@ +hasMany(Step::class); + } + + public function deployable(): MorphTo + { + return $this->morphTo(); + } +} diff --git a/app/Models/Service.php b/app/Models/Service.php index 3132fe3..f82787d 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -9,6 +9,7 @@ use App\Enums\ServiceType; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; class Service extends Model { @@ -33,6 +34,11 @@ class Service extends Model return $this->hasMany(Slice::class); } + public function deployments(): MorphMany + { + return $this->morphMany(Deployment::class, 'deployable'); + } + public function driver()//: Driver { // @todo. This is the class that controls the service diff --git a/app/Models/Step.php b/app/Models/Step.php new file mode 100644 index 0000000..d3d636f --- /dev/null +++ b/app/Models/Step.php @@ -0,0 +1,14 @@ +belongsTo(Deployment::class); + } +} diff --git a/database/migrations/2025_03_31_140110_create_deployments_table.php b/database/migrations/2025_03_31_140110_create_deployments_table.php new file mode 100644 index 0000000..e5b4baa --- /dev/null +++ b/database/migrations/2025_03_31_140110_create_deployments_table.php @@ -0,0 +1,25 @@ +id(); + $table->morphs('target'); // server, service, etc. + $table->string('status'); + $table->dateTime('started_at')->nullable(); + $table->dateTime('finished_at')->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('deployments'); + } +}; diff --git a/database/migrations/2025_03_31_141005_create_steps_table.php b/database/migrations/2025_03_31_141005_create_steps_table.php new file mode 100644 index 0000000..2b3b284 --- /dev/null +++ b/database/migrations/2025_03_31_141005_create_steps_table.php @@ -0,0 +1,25 @@ +id(); + $table->foreignIdFor(Deployment::class); + $table->longText('script'); + $table->longText('logs')->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('steps'); + } +};