This commit is contained in:
2025-03-31 15:29:07 +00:00
parent 374ce90160
commit a62565d0ad
9 changed files with 147 additions and 6 deletions

View 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();
}
}
}
}

View 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();
}
}
}