deployments wip

This commit is contained in:
2025-03-31 14:27:47 +00:00
parent d150f57c8f
commit 374ce90160
10 changed files with 147 additions and 22 deletions

View File

@@ -5,7 +5,7 @@ namespace App\Data\Deployments;
class Plan class Plan
{ {
/** /**
* @param Step[] $steps * @param PlannedStep[] $steps
*/ */
public function __construct( public function __construct(
public array $steps = [], public array $steps = [],

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Data\Deployments;
class PlannedStep
{
protected string $script;
public function __construct(
public string $name = 'Step',
protected array $secrets = [],
string|callable $script = 'echo "Incomplete Step"',
) {
if (is_callable($script)) {
$this->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;
}
}

View File

@@ -1,19 +0,0 @@
<?php
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;
}
}
}

View File

@@ -3,7 +3,7 @@
namespace App\Drivers\Postgres; namespace App\Drivers\Postgres;
use App\Data\Deployments\Plan; use App\Data\Deployments\Plan;
use App\Data\Deployments\Step; use App\Data\Deployments\PlannedStep as Step;
use App\Drivers\DatabaseDriver; use App\Drivers\DatabaseDriver;
class Postgres17Driver implements DatabaseDriver class Postgres17Driver implements DatabaseDriver
@@ -25,6 +25,9 @@ class Postgres17Driver implements DatabaseDriver
), ),
new Step( new Step(
name: 'Run the docker image', name: 'Run the docker image',
secrets: [
'defaultpassword' => $this->defaultPassword,
],
script: function () { script: function () {
$script = collect(); $script = collect();
if ($this->containerName) { if ($this->containerName) {
@@ -38,7 +41,7 @@ class Postgres17Driver implements DatabaseDriver
$runCommand .= " --name {$this->containerName}"; $runCommand .= " --name {$this->containerName}";
} }
if ($this->defaultPassword) { if ($this->defaultPassword) {
$runCommand .= " -e POSTGRES_PASSWORD={$this->defaultPassword}"; $runCommand .= " -e POSTGRES_PASSWORD=[!defaultpassword!]";
} }
if ($this->defaultUser) { if ($this->defaultUser) {
$runCommand .= " -e POSTGRES_USER={$this->defaultUser}"; $runCommand .= " -e POSTGRES_USER={$this->defaultUser}";
@@ -48,6 +51,8 @@ class Postgres17Driver implements DatabaseDriver
} }
$runCommand .= " -p 5432:5432 postgres:17"; $runCommand .= " -p 5432:5432 postgres:17";
return $runCommand;
} }
) )
]); ]);

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Enums;
enum DeploymentStatus: string
{
case PENDING = 'pending';
case IN_PROGRESS = 'in-progress';
case COMPLETED = 'completed';
case FAILED = 'failed';
}

20
app/Models/Deployment.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Deployment extends Model
{
public function steps(): HasMany
{
return $this->hasMany(Step::class);
}
public function deployable(): MorphTo
{
return $this->morphTo();
}
}

View File

@@ -9,6 +9,7 @@ use App\Enums\ServiceType;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
class Service extends Model class Service extends Model
{ {
@@ -33,6 +34,11 @@ class Service extends Model
return $this->hasMany(Slice::class); return $this->hasMany(Slice::class);
} }
public function deployments(): MorphMany
{
return $this->morphMany(Deployment::class, 'deployable');
}
public function driver()//: Driver public function driver()//: Driver
{ {
// @todo. This is the class that controls the service // @todo. This is the class that controls the service

14
app/Models/Step.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Step extends Model
{
public function deployment(): BelongsTo
{
return $this->belongsTo(Deployment::class);
}
}

View File

@@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('deployments', function (Blueprint $table) {
$table->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');
}
};

View File

@@ -0,0 +1,25 @@
<?php
use App\Models\Deployment;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('steps', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Deployment::class);
$table->longText('script');
$table->longText('logs')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('steps');
}
};