wip
This commit is contained in:
@@ -5,7 +5,7 @@ namespace App\Actions\Services;
|
|||||||
use App\Enums\ServiceCategory;
|
use App\Enums\ServiceCategory;
|
||||||
use App\Enums\ServiceStatus;
|
use App\Enums\ServiceStatus;
|
||||||
use App\Enums\ServiceType;
|
use App\Enums\ServiceType;
|
||||||
use App\Jobs\Services\InstallService;
|
use App\Jobs\Services\DeployService;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
@@ -27,5 +27,9 @@ class CreateService
|
|||||||
'driver_name' => $driverName, // postgres
|
'driver_name' => $driverName, // postgres
|
||||||
'status' => ServiceStatus::NOT_INSTALLED,
|
'status' => ServiceStatus::NOT_INSTALLED,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$defaultPassword = Str::random(16);
|
||||||
|
|
||||||
|
dispatch(new DeployService($service, $defaultPassword));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,9 @@ use App\Data\Deployments\Plan;
|
|||||||
interface Driver
|
interface Driver
|
||||||
{
|
{
|
||||||
public Plan $deploymentPlan { get; }
|
public Plan $deploymentPlan { get; }
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public ?string $containerName = null,
|
||||||
|
public ?string $containerId = null,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -41,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=[!defaultpassword!]";
|
$runCommand .= " -e POSTGRES_PASSWORD=[!defaultPassword!]";
|
||||||
}
|
}
|
||||||
if ($this->defaultUser) {
|
if ($this->defaultUser) {
|
||||||
$runCommand .= " -e POSTGRES_USER={$this->defaultUser}";
|
$runCommand .= " -e POSTGRES_USER={$this->defaultUser}";
|
||||||
|
|||||||
45
app/Jobs/Services/DeployService.php
Normal file
45
app/Jobs/Services/DeployService.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Services;
|
||||||
|
|
||||||
|
use App\Drivers\Driver;
|
||||||
|
use App\Enums\DeploymentStatus;
|
||||||
|
use App\Enums\ServiceStatus;
|
||||||
|
use App\Models\Service;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Queue\Queueable;
|
||||||
|
|
||||||
|
class DeployService implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public Service $service,
|
||||||
|
public ?string $defaultPassword = null,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$driver = $this->service->driver($this->defaultPassword);
|
||||||
|
/** @var \App\Models\Deployment $deployment */
|
||||||
|
$deployment = $this->service->deployments()->create([
|
||||||
|
'status' => DeploymentStatus::PENDING,
|
||||||
|
]);
|
||||||
|
foreach ($driver->deploymentPlan->steps as $index => $plannedStep) {
|
||||||
|
$step = $deployment->steps()->create([
|
||||||
|
'order' => $index + 1,
|
||||||
|
'status' => DeploymentStatus::PENDING,
|
||||||
|
'script' => $plannedStep->getSafeScript(),
|
||||||
|
'secrets' => [
|
||||||
|
'defaultPassword' => $this->defaultPassword,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
if ($index === 0) {
|
||||||
|
$step->dispatchJob();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
53
app/Jobs/Services/RunStep.php
Normal file
53
app/Jobs/Services/RunStep.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Services;
|
||||||
|
|
||||||
|
use App\Enums\DeploymentStatus;
|
||||||
|
use App\Models\Step;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Queue\Queueable;
|
||||||
|
use Spatie\Ssh\Ssh;
|
||||||
|
|
||||||
|
class RunStep implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected Step $step,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$this->step->load('service.server');
|
||||||
|
$this->step->update([
|
||||||
|
'status' => DeploymentStatus::IN_PROGRESS,
|
||||||
|
'started_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$server = $this->step->service->server;
|
||||||
|
|
||||||
|
$ssh = Ssh::create('root', $server->ipv4)
|
||||||
|
->usePrivateKey(storage_path('private/ssh/id_ed25519'))
|
||||||
|
->onOutput(function ($output) {
|
||||||
|
$this->step->update([
|
||||||
|
'logs' => $this->step->logs . "\n" . trim($output),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$ssh->execute($this->step->script);
|
||||||
|
|
||||||
|
$this->step->update([
|
||||||
|
'status' => DeploymentStatus::COMPLETED,
|
||||||
|
'finished_at' => now(),
|
||||||
|
'secrets' => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Dispatch the next step if available
|
||||||
|
if ($nextStep = $this->step->deployment->steps()->where('order', '>', $this->step->order)->orderBy('order', 'asc')->first()) {
|
||||||
|
$nextStep->dispatchJob();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,14 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|||||||
|
|
||||||
class Deployment extends Model
|
class Deployment extends Model
|
||||||
{
|
{
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'started_at' => 'datetime',
|
||||||
|
'finished_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function steps(): HasMany
|
public function steps(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Step::class);
|
return $this->hasMany(Step::class);
|
||||||
@@ -15,6 +23,6 @@ class Deployment extends Model
|
|||||||
|
|
||||||
public function deployable(): MorphTo
|
public function deployable(): MorphTo
|
||||||
{
|
{
|
||||||
return $this->morphTo();
|
return $this->morphTo('target');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,11 +36,17 @@ class Service extends Model
|
|||||||
|
|
||||||
public function deployments(): MorphMany
|
public function deployments(): MorphMany
|
||||||
{
|
{
|
||||||
return $this->morphMany(Deployment::class, 'deployable');
|
return $this->morphMany(Deployment::class, 'target');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function driver()//: Driver
|
public function driver(
|
||||||
|
?string $defaultPassword = null,
|
||||||
|
): Driver
|
||||||
{
|
{
|
||||||
// @todo. This is the class that controls the service
|
$class = config("keystone.drivers.{$this->driver_name}.{$this->version}");
|
||||||
|
if (!class_exists($class)) {
|
||||||
|
throw new \Exception("Driver class {$class} not found");
|
||||||
|
}
|
||||||
|
return new $class($this->container_name, $this->container_id, defaultPassword: $defaultPassword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,28 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Jobs\Services\RunStep;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
class Step extends Model
|
class Step extends Model
|
||||||
{
|
{
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'started_at' => 'datetime',
|
||||||
|
'finished_at' => 'datetime',
|
||||||
|
'secrets' => 'encrypted:array',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function deployment(): BelongsTo
|
public function deployment(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Deployment::class);
|
return $this->belongsTo(Deployment::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dispatchJob(): void
|
||||||
|
{
|
||||||
|
dispatch(new RunStep($this));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,13 @@ return new class extends Migration
|
|||||||
Schema::create('steps', function (Blueprint $table) {
|
Schema::create('steps', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->foreignIdFor(Deployment::class);
|
$table->foreignIdFor(Deployment::class);
|
||||||
|
$table->integer('order');
|
||||||
|
$table->string('status');
|
||||||
$table->longText('script');
|
$table->longText('script');
|
||||||
$table->longText('logs')->nullable();
|
$table->longText('logs')->nullable();
|
||||||
|
$table->text('secrets')->nullable();
|
||||||
|
$table->dateTime('started_at')->nullable();
|
||||||
|
$table->dateTime('finished_at')->nullable();
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user