CluPilotCloud/app/Provisioning/Steps/Host/HostStep.php

53 lines
1.4 KiB
PHP

<?php
namespace App\Provisioning\Steps\Host;
use App\Models\Host;
use App\Models\ProvisioningRun;
use App\Models\RunResource;
use App\Provisioning\Contracts\ProvisioningStep;
use App\Services\Ssh\RemoteShell;
/**
* Shared helpers for host-onboarding steps: subject resolution, key login, and
* idempotency breadcrumbs (run_resources).
*/
abstract class HostStep implements ProvisioningStep
{
public function label(): string
{
return 'hosts.step.'.$this->key();
}
public function maxDuration(): int
{
return 300;
}
protected function host(ProvisioningRun $run): Host
{
/** @var Host $host */
$host = $run->subject;
return $host;
}
protected function keyLogin(RemoteShell $shell, Host $host): void
{
$shell->connectWithKey($host->public_ip, 'root', (string) config('provisioning.ssh.private_key'));
}
protected function recordResource(ProvisioningRun $run, Host $host, string $kind, string $externalId): void
{
RunResource::firstOrCreate(
['run_id' => $run->id, 'kind' => $kind],
['host_id' => $host->id, 'external_id' => $externalId],
);
}
protected function hasResource(ProvisioningRun $run, string $kind): bool
{
return RunResource::query()->where('run_id', $run->id)->where('kind', $kind)->exists();
}
}