Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(servers): validate new cloud servers automatically when they are running

Andras Bacsai56d0cf0e2790
diff --git a/app/Actions/Server/ValidateServer.php b/app/Actions/Server/ValidateServer.php
index f4855b9ffa..57be1a5a60 100644
--- a/app/Actions/Server/ValidateServer.php
+++ b/app/Actions/Server/ValidateServer.php
@@ -37,6 +37,19 @@ class ValidateServer
$server->update([
'validation_logs' => null,
]);
+ if ($server->hetzner_server_id) {
+ $status = $server->refreshHetznerState();
+ if (in_array($status, ['initializing', 'starting', 'stopping', 'off', 'deleting', 'rebuilding', 'migrating'], true)) {
+ $this->error = $status === 'off'
+ ? 'Hetzner server is off. Power it on before validating.'
+ : "Hetzner server is {$status}. Wait until it is running, then validate.";
+ $server->update([
+ 'validation_logs' => $this->error,
+ ]);
+ throw new \Exception($this->error);
+ }
+ }
+
if ($server->vultr_instance_id) {
$status = $server->refreshVultrState();
if (in_array($status, ['stopped', 'suspended', 'deleted'], true)) {
diff --git a/app/Http/Controllers/Api/Concerns/RequiresDeployAbility.php b/app/Http/Controllers/Api/Concerns/RequiresDeployAbility.php
index 28ccd598ed..9169c31de7 100644
--- a/app/Http/Controllers/Api/Concerns/RequiresDeployAbility.php
+++ b/app/Http/Controllers/Api/Concerns/RequiresDeployAbility.php
@@ -17,7 +17,7 @@ trait RequiresDeployAbility
*/
private function deployAbilityForbiddenResponse(Request $request, string $reason): ?JsonResponse
{
- if ($request->user()->tokenCan('deploy') || $request->user()->tokenCan('root')) {
+ if ($this->hasDeployAbility($request)) {
return null;
}
@@ -25,4 +25,9 @@ trait RequiresDeployAbility
'message' => "Missing required permissions: deploy. {$reason}",
], 403);
}
+
+ private function hasDeployAbility(Request $request): bool
+ {
+ return $request->user()->tokenCan('deploy') || $request->user()->tokenCan('root');
+ }
}
diff --git a/app/Http/Controllers/Api/DigitalOceanController.php b/app/Http/Controllers/Api/DigitalOceanController.php
index 7c7dd6e00b..4da5c3509c 100644
--- a/app/Http/Controllers/Api/DigitalOceanController.php
+++ b/app/Http/Controllers/Api/DigitalOceanController.php
@@ -2,12 +2,12 @@
namespace App\Http\Controllers\Api;
-use App\Actions\Server\ValidateServer;
use App\Enums\ProxyTypes;
use App\Exceptions\CloudProviderApiException;
use App\Exceptions\RateLimitException;
use App\Http\Controllers\Api\Concerns\RequiresDeployAbility;
use App\Http\Controllers\Controller;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
use App\Models\Server;
@@ -287,7 +287,13 @@ class DigitalOceanController extends Controller
$request->offsetSet('enable_ipv6', $request->boolean('enable_ipv6', true));
$request->offsetSet('monitoring', $request->boolean('monitoring', true));
$request->offsetSet('digitalocean_ssh_key_ids', $request->digitalocean_ssh_key_ids ?? []);
- $request->offsetSet('instant_validate', $request->boolean('instant_validate', false));
+ $request->offsetSet('instant_validate', $request->filled('instant_validate') ? $request->boolean('instant_validate') : $this->hasDeployAbility($request));
+ if ($request->instant_validate) {
+ $forbidden = $this->deployAbilityForbiddenResponse($request, 'Instant validation installs Docker and starts the proxy on the new server, so it needs a token with the deploy permission.');
+ if ($forbidden) {
+ return $forbidden;
+ }
+ }
$token = $this->digitalOceanToken($request, $teamId);
if ($token instanceof JsonResponse) {
@@ -365,7 +371,7 @@ class DigitalOceanController extends Controller
}
if ($request->instant_validate) {
- ValidateServer::dispatch($server);
+ ValidateNewCloudServerJob::dispatch($server);
}
auditLog('api.digitalocean_droplet.created', [
diff --git a/app/Http/Controllers/Api/HetznerController.php b/app/Http/Controllers/Api/HetznerController.php
index e59d85ba87..7b1011a254 100644
--- a/app/Http/Controllers/Api/HetznerController.php
+++ b/app/Http/Controllers/Api/HetznerController.php
@@ -2,12 +2,12 @@
namespace App\Http\Controllers\Api;
-use App\Actions\Server\ValidateServer;
use App\Enums\ProxyTypes;
use App\Exceptions\CloudProviderApiException;
use App\Exceptions\RateLimitException;
use App\Http\Controllers\Api\Concerns\RequiresDeployAbility;
use App\Http\Controllers\Controller;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
use App\Models\Server;
@@ -724,7 +724,7 @@ class HetznerController extends Controller
'hetzner_firewall_ids' => ['type' => 'array', 'items' => ['type' => 'integer'], 'description' => 'Existing Hetzner firewall IDs to apply during server creation'],
'hetzner_network_ids' => ['type' => 'array', 'items' => ['type' => 'integer'], 'description' => 'Existing Hetzner network IDs to attach during server creation'],
'cloud_init_script' => ['type' => 'string', 'description' => 'Cloud-init YAML script (optional). Needs a token with the deploy permission.'],
- 'instant_validate' => ['type' => 'boolean', 'example' => false, 'description' => 'Validate server immediately after creation'],
+ 'instant_validate' => ['type' => 'boolean', 'example' => false, 'description' => 'Validate the server and install Docker when it is running. Defaults to true for tokens with the deploy permission, and needs that permission.'],
],
),
),
@@ -872,8 +872,12 @@ class HetznerController extends Controller
if (is_null($request->hetzner_network_ids)) {
$request->offsetSet('hetzner_network_ids', []);
}
- if (is_null($request->instant_validate)) {
- $request->offsetSet('instant_validate', false);
+ $request->offsetSet('instant_validate', $request->filled('instant_validate') ? $request->boolean('instant_validate') : $this->hasDeployAbility($request));
+ if ($request->instant_validate) {
+ $forbidden = $this->deployAbilityForbiddenResponse($request, 'Instant validation installs Docker and starts the proxy on the new server, so it needs a token with the deploy permission.');
+ if ($forbidden) {
+ return $forbidden;
+ }
}
if (! $request->boolean('enable_ipv4') && ! $request->boolean('enable_ipv6')) {
@@ -1000,6 +1004,7 @@ class HetznerController extends Controller
'private_key_id' => $privateKey->id,
'cloud_provider_token_id' => $token->id,
'hetzner_server_id' => $hetznerServer['id'],
+ 'hetzner_server_status' => $hetznerServer['status'] ?? null,
]);
$server->proxy->set('status', 'exited');
@@ -1016,7 +1021,7 @@ class HetznerController extends Controller
// Validate server if requested
if ($request->instant_validate) {
- ValidateServer::dispatch($server);
+ ValidateNewCloudServerJob::dispatch($server);
}
auditLog('api.hetzner_server.created', [
diff --git a/app/Http/Controllers/Api/HostingerController.php b/app/Http/Controllers/Api/HostingerController.php
index 34eb2e94bf..1e28c93383 100644
--- a/app/Http/Controllers/Api/HostingerController.php
+++ b/app/Http/Controllers/Api/HostingerController.php
@@ -2,11 +2,11 @@
namespace App\Http\Controllers\Api;
-use App\Actions\Server\ValidateServer;
use App\Enums\ProxyTypes;
use App\Exceptions\RateLimitException;
use App\Http\Controllers\Api\Concerns\RequiresDeployAbility;
use App\Http\Controllers\Controller;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
use App\Models\Server;
@@ -193,6 +193,14 @@ class HostingerController extends Controller
}
}
+ $request->offsetSet('instant_validate', $request->filled('instant_validate') ? $request->boolean('instant_validate') : $this->hasDeployAbility($request));
+ if ($request->instant_validate) {
+ $forbidden = $this->deployAbilityForbiddenResponse($request, 'Instant validation installs Docker and starts the proxy on the new server, so it needs a token with the deploy permission.');
+ if ($forbidden) {
+ return $forbidden;
+ }
+ }
+
$team = Team::find($teamId);
if (Team::serverLimitReached($team)) {
return response()->json(['message' => 'Server limit reached for your subscription.'], 400);
@@ -267,8 +275,8 @@ class HostingerController extends Controller
$ipAddress = $server->fresh()->ip;
- if ($request->boolean('instant_validate') && ! $server->hasPlaceholderIp()) {
- ValidateServer::dispatch($server);
+ if ($request->instant_validate) {
+ ValidateNewCloudServerJob::dispatch($server);
}
auditLog('api.hostinger_virtual_machine.created', [
diff --git a/app/Http/Controllers/Api/VultrController.php b/app/Http/Controllers/Api/VultrController.php
index 86601b8e4d..e5c2664a96 100644
--- a/app/Http/Controllers/Api/VultrController.php
+++ b/app/Http/Controllers/Api/VultrController.php
@@ -2,12 +2,12 @@
namespace App\Http\Controllers\Api;
-use App\Actions\Server\ValidateServer;
use App\Enums\ProxyTypes;
use App\Exceptions\CloudProviderApiException;
use App\Exceptions\RateLimitException;
use App\Http\Controllers\Api\Concerns\RequiresDeployAbility;
use App\Http\Controllers\Controller;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
use App\Models\Server;
@@ -277,8 +277,12 @@ class VultrController extends Controller
if (is_null($request->vultr_ssh_key_ids)) {
$request->offsetSet('vultr_ssh_key_ids', []);
}
- if (is_null($request->instant_validate)) {
- $request->offsetSet('instant_validate', false);
+ $request->offsetSet('instant_validate', $request->filled('instant_validate') ? $request->boolean('instant_validate') : $this->hasDeployAbility($request));
+ if ($request->instant_validate) {
+ $forbidden = $this->deployAbilityForbiddenResponse($request, 'Instant validation installs Docker and starts the proxy on the new server, so it needs a token with the deploy permission.');
+ if ($forbidden) {
+ return $forbidden;
+ }
}
if ($request->disable_public_ipv4 && ! $request->enable_ipv6) {
@@ -373,7 +377,7 @@ class VultrController extends Controller
}
if ($request->instant_validate) {
- ValidateServer::dispatch($server);
+ ValidateNewCloudServerJob::dispatch($server);
}
auditLog('api.vultr_server.created', [
diff --git a/app/Jobs/ServerCloudProviderStatusCheckJob.php b/app/Jobs/ServerCloudProviderStatusCheckJob.php
index 46d0898c93..696adfe19c 100644
--- a/app/Jobs/ServerCloudProviderStatusCheckJob.php
+++ b/app/Jobs/ServerCloudProviderStatusCheckJob.php
@@ -33,25 +33,7 @@ class ServerCloudProviderStatusCheckJob implements ShouldBeEncrypted, ShouldQueu
public function handle(): void
{
try {
- if (! $this->server->cloudProviderToken) {
- return;
- }
-
- match ($this->server->cloudProviderToken->provider) {
- 'hetzner' => $this->server->hetzner_server_id
- ? $this->server->refreshHetznerState()
- : null,
- 'vultr' => $this->server->vultr_instance_id
- ? $this->server->refreshVultrState()
- : null,
- 'digitalocean' => $this->server->digitalocean_droplet_id
- ? $this->server->refreshDigitalOceanState()
- : null,
- 'hostinger' => $this->server->hostinger_virtual_machine_id
- ? $this->server->refreshHostingerState()
- : null,
- default => null,
- };
+ $this->server->refreshCloudProviderState();
} catch (\Throwable $e) {
Log::debug('Cloud provider status check failed', [
'server_id' => $this->server->id,
diff --git a/app/Jobs/ValidateNewCloudServerJob.php b/app/Jobs/ValidateNewCloudServerJob.php
new file mode 100644
index 0000000000..e1283f3937
--- /dev/null
+++ b/app/Jobs/ValidateNewCloudServerJob.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\Server;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeEncrypted;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+/**
+ * Waits until a server that Coolify just created at a cloud provider is running and accepts SSH,
+ * then validates it and installs Docker.
+ */
+class ValidateNewCloudServerJob implements ShouldBeEncrypted, ShouldQueue
+{
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ public const MAX_ATTEMPTS = 40;
+
+ public const RETRY_DELAY_SECONDS = 30;
+
+ public $tries = 1;
+
+ public $timeout = 60;
+
+ public bool $deleteWhenMissingModels = true;
+
+ public function __construct(
+ public Server $server,
+ public int $attempt = 1
+ ) {
+ $this->onQueue('high');
+ }
+
+ public function handle(): void
+ {
+ if (! $this->server->canBeValidated() || $this->server->is_validating || $this->server->isFunctional()) {
+ return;
+ }
+
+ if ($this->isReady()) {
+ ValidateAndInstallServerJob::dispatch($this->server);
+
+ return;
+ }
+
+ if ($this->attempt >= self::MAX_ATTEMPTS) {
+ $this->server->update([
+ 'validation_logs' => 'The server did not start or accept SSH connections within '.(self::MAX_ATTEMPTS * self::RETRY_DELAY_SECONDS / 60).' minutes after it was created. Validate it manually.',
+ ]);
+
+ return;
+ }
+
+ self::dispatch($this->server, $this->attempt + 1)->delay(now()->addSeconds(self::RETRY_DELAY_SECONDS));
+ }
+
+ private function isReady(): bool
+ {
+ try {
+ $status = $this->server->refreshCloudProviderState();
+ } catch (\Throwable) {
+ return false;
+ }
+
+ if (! in_array($status, ['running', 'active'], true) || $this->server->hasPlaceholderIp()) {
+ return false;
+ }
+
+ return $this->server->validateConnection()['uptime'];
+ }
+}
diff --git a/app/Livewire/Server/New/ByDigitalOcean.php b/app/Livewire/Server/New/ByDigitalOcean.php
index 95e07643ef..6c6f4a4fcd 100644
--- a/app/Livewire/Server/New/ByDigitalOcean.php
+++ b/app/Livewire/Server/New/ByDigitalOcean.php
@@ -3,6 +3,7 @@
namespace App\Livewire\Server\New;
use App\Enums\ProxyTypes;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudInitScript;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
@@ -519,6 +520,8 @@ class ByDigitalOcean extends Component
report($e);
}
+ ValidateNewCloudServerJob::dispatch($server);
+
if ($this->from_onboarding) {
currentTeam()->update([
'show_boarding' => false,
diff --git a/app/Livewire/Server/New/ByHetzner.php b/app/Livewire/Server/New/ByHetzner.php
index 3205daf96c..81ed3269ad 100644
--- a/app/Livewire/Server/New/ByHetzner.php
+++ b/app/Livewire/Server/New/ByHetzner.php
@@ -4,6 +4,7 @@ namespace App\Livewire\Server\New;
use App\Enums\ProxyTypes;
use App\Exceptions\CloudProviderApiException;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudInitScript;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
@@ -758,6 +759,8 @@ class ByHetzner extends Component
}
}
+ ValidateNewCloudServerJob::dispatch($server);
+
if ($this->from_onboarding) {
// Complete the boarding when server is successfully created via Hetzner
currentTeam()->update([
diff --git a/app/Livewire/Server/New/ByHostinger.php b/app/Livewire/Server/New/ByHostinger.php
index c05fcf7d5d..f12db08511 100644
--- a/app/Livewire/Server/New/ByHostinger.php
+++ b/app/Livewire/Server/New/ByHostinger.php
@@ -3,6 +3,7 @@
namespace App\Livewire\Server\New;
use App\Enums\ProxyTypes;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
use App\Models\Server;
@@ -377,6 +378,8 @@ class ByHostinger extends Component
report($e);
}
+ ValidateNewCloudServerJob::dispatch($server);
+
if ($this->from_onboarding) {
currentTeam()->update(['show_boarding' => false]);
refreshSession();
diff --git a/app/Livewire/Server/New/ByVultr.php b/app/Livewire/Server/New/ByVultr.php
index f3024f29da..5e27585b44 100644
--- a/app/Livewire/Server/New/ByVultr.php
+++ b/app/Livewire/Server/New/ByVultr.php
@@ -3,6 +3,7 @@
namespace App\Livewire\Server\New;
use App\Enums\ProxyTypes;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudInitScript;
use App\Models\CloudProviderToken;
use App\Models\PrivateKey;
@@ -487,6 +488,8 @@ class ByVultr extends Component
report($e);
}
+ ValidateNewCloudServerJob::dispatch($server);
+
if ($this->from_onboarding) {
currentTeam()->update([
'show_boarding' => false,
diff --git a/app/Livewire/Server/Show.php b/app/Livewire/Server/Show.php
index 796ddcc82c..45b617fab1 100644
--- a/app/Livewire/Server/Show.php
+++ b/app/Livewire/Server/Show.php
@@ -86,14 +86,6 @@ class Show extends Component
public ?string $hostingerVirtualMachineStatus = null;
- public bool $hetznerServerManuallyStarted = false;
-
- public bool $vultrInstanceManuallyStarted = false;
-
- public bool $digitalOceanDropletManuallyStarted = false;
-
- public bool $hostingerVirtualMachineManuallyStarted = false;
-
public bool $isValidating = false;
// Hetzner linking properties
@@ -780,7 +772,6 @@ class Show extends Component
$this->hetznerServerStatus = 'starting';
$this->server->update(['hetzner_server_status' => 'starting']);
- $this->hetznerServerManuallyStarted = true; // Set flag to trigger auto-validation when running
$this->dispatch('success', 'Hetzner server is starting...');
} catch (\Throwable $e) {
return handleError($e, $this);
@@ -803,7 +794,6 @@ class Show extends Component
$this->vultrInstanceStatus = 'starting';
$this->server->update(['vultr_instance_status' => 'starting']);
- $this->vultrInstanceManuallyStarted = true;
$this->dispatch('success', 'Vultr instance is starting...');
} catch (\Throwable $e) {
return handleError($e, $this);
@@ -826,7 +816,6 @@ class Show extends Component
$this->digitalOceanDropletStatus = 'new';
$this->server->update(['digitalocean_droplet_status' => 'new']);
- $this->digitalOceanDropletManuallyStarted = true;
$this->dispatch('success', 'DigitalOcean droplet is starting...');
} catch (\Throwable $e) {
return handleError($e, $this);
@@ -849,7 +838,6 @@ class Show extends Component
$this->hostingerVirtualMachineStatus = 'starting';
$this->server->update(['hostinger_virtual_machine_status' => 'starting']);
- $this->hostingerVirtualMachineManuallyStarted = true;
$this->dispatch('success', 'Hostinger VPS is starting...');
return null;
diff --git a/app/Livewire/Server/ValidateAndInstall.php b/app/Livewire/Server/ValidateAndInstall.php
index 84b2ffaa32..748c53ba2d 100644
--- a/app/Livewire/Server/ValidateAndInstall.php
+++ b/app/Livewire/Server/ValidateAndInstall.php
@@ -116,6 +116,22 @@ class ValidateAndInstall extends Component
try {
$this->authorize('update', $this->server);
Server::throttleValidation($this->server->team_id, $this->server->ip);
+ if ($this->server->hetzner_server_id) {
+ $status = $this->server->refreshHetznerState();
+ $this->server->refresh();
+
+ if (in_array($status, ['initializing', 'starting', 'stopping', 'off', 'deleting', 'rebuilding', 'migrating'], true)) {
+ $this->error = $status === 'off'
+ ? 'Hetzner server is off. Power it on before validating.'
+ : "Hetzner server is {$status}. Wait until it is running, then validate.";
+ $this->server->update([
+ 'validation_logs' => $this->error,
+ ]);
+
+ return;
+ }
+ }
+
if ($this->server->vultr_instance_id) {
$status = $this->server->refreshVultrState();
$this->server->refresh();
diff --git a/app/Models/Server.php b/app/Models/Server.php
index ae2d3b064a..d13f1f468f 100644
--- a/app/Models/Server.php
+++ b/app/Models/Server.php
@@ -462,6 +462,20 @@ class Server extends BaseModel
static::flushIdentityMap();
}
+ /**
+ * Refresh the status of the linked cloud server from its provider.
+ */
+ public function refreshCloudProviderState(): ?string
+ {
+ return match ($this->cloudProviderToken?->provider) {
+ 'hetzner' => $this->hetzner_server_id ? $this->refreshHetznerState() : null,
+ 'vultr' => $this->vultr_instance_id ? $this->refreshVultrState() : null,
+ 'digitalocean' => $this->digitalocean_droplet_id ? $this->refreshDigitalOceanState() : null,
+ 'hostinger' => $this->hostinger_virtual_machine_id ? $this->refreshHostingerState() : null,
+ default => null,
+ };
+ }
+
public function refreshHetznerState(): ?string
{
// Another instance manages the cloud server of an archive; keep the stored status.
diff --git a/openapi.json b/openapi.json
index 25d528b690..d732c04b2a 100644
--- a/openapi.json
+++ b/openapi.json
@@ -13101,7 +13101,7 @@
"instant_validate": {
"type": "boolean",
"example": false,
- "description": "Validate server immediately after creation"
+ "description": "Validate the server and install Docker when it is running. Defaults to true for tokens with the deploy permission, and needs that permission."
}
},
"type": "object"
diff --git a/openapi.yaml b/openapi.yaml
index 09caf34728..f471f09b8a 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -8419,7 +8419,7 @@ paths:
instant_validate:
type: boolean
example: false
- description: 'Validate server immediately after creation'
+ description: 'Validate the server and install Docker when it is running. Defaults to true for tokens with the deploy permission, and needs that permission.'
type: object
responses:
'201':
diff --git a/tests/Feature/Api/HetznerApiTest.php b/tests/Feature/Api/HetznerApiTest.php
index b792e00c17..dc4cc8942d 100644
--- a/tests/Feature/Api/HetznerApiTest.php
+++ b/tests/Feature/Api/HetznerApiTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
use App\Models\PrivateKey;
@@ -7,12 +8,15 @@ use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request as HttpRequest;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Once;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
config()->set('cache.default', 'array');
config()->set('app.maintenance.driver', 'file');
config()->set('app.maintenance.store', 'array');
@@ -411,6 +415,7 @@ describe('POST /api/v1/servers/hetzner', function () {
'server' => [
'id' => 456,
'name' => 'test-server',
+ 'status' => 'initializing',
'public_net' => [
'ipv4' => ['ip' => '1.2.3.4'],
'ipv6' => ['ip' => '2001:db8::1'],
@@ -443,9 +448,43 @@ describe('POST /api/v1/servers/hetzner', function () {
'ip' => '1.2.3.4',
'team_id' => $this->team->id,
'hetzner_server_id' => 456,
+ 'hetzner_server_status' => 'initializing',
]);
});
+ test('validates the new server when it runs by default for tokens with the deploy permission', function () {
+ fakeHetznerServerCreation();
+
+ $this->withHeaders(['Authorization' => 'Bearer '.$this->bearerToken])
+ ->postJson('/api/v1/servers/hetzner', hetznerCreatePayload($this))
+ ->assertStatus(201);
+
+ Bus::assertDispatched(ValidateNewCloudServerJob::class);
+ });
+
+ test('does not validate the new server by default for tokens without the deploy permission', function () {
+ fakeHetznerServerCreation();
+ $writeToken = $this->user->createToken('write-only', ['write'])->plainTextToken;
+
+ $this->withHeaders(['Authorization' => 'Bearer '.$writeToken])
+ ->postJson('/api/v1/servers/hetzner', hetznerCreatePayload($this))
+ ->assertStatus(201);
+
+ Bus::assertNotDispatched(ValidateNewCloudServerJob::class);
+ });
+
+ test('rejects instant validation for tokens without the deploy permission before it creates the server', function () {
+ Http::fake();
+ $writeToken = $this->user->createToken('write-only', ['write'])->plainTextToken;
+
+ $this->withHeaders(['Authorization' => 'Bearer '.$writeToken])
+ ->postJson('/api/v1/servers/hetzner', [...hetznerCreatePayload($this), 'instant_validate' => true])
+ ->assertStatus(403);
+
+ Http::assertNothingSent();
+ Bus::assertNotDispatched(ValidateNewCloudServerJob::class);
+ });
+
test('enables backups after creating a Hetzner server when requested', function () {
Http::fake(function (HttpRequest $request) {
if ($request->method() === 'GET' && str_starts_with($request->url(), 'https://api.hetzner.cloud/v1/ssh_keys')) {
@@ -1121,3 +1160,39 @@ describe('error responses do not leak exception details', function () {
expect($response->getContent())->not->toContain('INTERNAL_LEAK_TOKEN_abc');
});
});
+
+function fakeHetznerServerCreation(): void
+{
+ Http::fake([
+ 'https://api.hetzner.cloud/v1/ssh_keys' => Http::response([
+ 'ssh_key' => ['id' => 123, 'fingerprint' => 'aa:bb:cc:dd'],
+ ], 201),
+ 'https://api.hetzner.cloud/v1/ssh_keys*' => Http::response([
+ 'ssh_keys' => [],
+ 'meta' => ['pagination' => ['next_page' => null]],
+ ], 200),
+ 'https://api.hetzner.cloud/v1/servers' => Http::response([
+ 'server' => [
+ 'id' => 456,
+ 'name' => 'test-server',
+ 'status' => 'initializing',
+ 'public_net' => ['ipv4' => ['ip' => '203.0.113.10']],
+ ],
+ ], 201),
+ ]);
+}
+
+/**
+ * @return array<string, mixed>
+ */
+function hetznerCreatePayload(object $test): array
+{
+ return [
+ 'cloud_provider_token_id' => $test->hetznerToken->uuid,
+ 'location' => 'nbg1',
+ 'server_type' => 'cx11',
+ 'image' => 15512617,
+ 'name' => 'test-server',
+ 'private_key_uuid' => $test->privateKey->uuid,
+ ];
+}
diff --git a/tests/Feature/DigitalOceanApiTest.php b/tests/Feature/DigitalOceanApiTest.php
index d7233b85cc..bf80955e06 100644
--- a/tests/Feature/DigitalOceanApiTest.php
+++ b/tests/Feature/DigitalOceanApiTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
use App\Models\PrivateKey;
@@ -7,6 +8,7 @@ use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Exceptions;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Once;
@@ -14,6 +16,8 @@ use Illuminate\Support\Once;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
InstanceSettings::query()->whereKey(0)->delete();
$settings = new InstanceSettings(['is_api_enabled' => true]);
$settings->id = 0;
diff --git a/tests/Feature/DigitalOceanServerCreationTest.php b/tests/Feature/DigitalOceanServerCreationTest.php
index 1d9f629c38..d24192bc5f 100644
--- a/tests/Feature/DigitalOceanServerCreationTest.php
+++ b/tests/Feature/DigitalOceanServerCreationTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Jobs\ValidateNewCloudServerJob;
use App\Livewire\Server\New\ByDigitalOcean;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
@@ -8,12 +9,15 @@ use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
config([
'cache.default' => 'array',
'session.driver' => 'array',
diff --git a/tests/Feature/HetznerServerValidationTest.php b/tests/Feature/HetznerServerValidationTest.php
new file mode 100644
index 0000000000..e87fd380ba
--- /dev/null
+++ b/tests/Feature/HetznerServerValidationTest.php
@@ -0,0 +1,86 @@
+<?php
+
+use App\Actions\Server\ValidateServer;
+use App\Livewire\Server\ValidateAndInstall;
+use App\Models\CloudProviderToken;
+use App\Models\InstanceSettings;
+use App\Models\PrivateKey;
+use App\Models\Server;
+use App\Models\Team;
+use App\Models\User;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Http;
+use Livewire\Livewire;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ config([
+ 'app.maintenance.driver' => 'file',
+ 'cache.default' => 'array',
+ 'session.driver' => 'array',
+ ]);
+
+ InstanceSettings::unguarded(fn () => InstanceSettings::query()->create(['id' => 0]));
+
+ $this->team = Team::factory()->create();
+ $this->user = User::factory()->create();
+ $this->team->members()->attach($this->user->id, ['role' => 'owner']);
+
+ $this->actingAs($this->user);
+ session(['currentTeam' => $this->team]);
+
+ $token = CloudProviderToken::create([
+ 'team_id' => $this->team->id,
+ 'provider' => 'hetzner',
+ 'token' => 'test-hetzner-token',
+ 'name' => 'Test Hetzner Token',
+ ]);
+
+ $this->server = Server::factory()->create([
+ 'team_id' => $this->team->id,
+ 'private_key_id' => PrivateKey::factory()->create(['team_id' => $this->team->id])->id,
+ 'cloud_provider_token_id' => $token->id,
+ 'hetzner_server_id' => 456,
+ 'hetzner_server_status' => 'initializing',
+ 'ip' => '1.2.3.4',
+ ]);
+});
+
+function fakeHetznerServerStatus(string $status): void
+{
+ Http::fake([
+ 'https://api.hetzner.cloud/v1/servers/456' => Http::response([
+ 'server' => [
+ 'id' => 456,
+ 'status' => $status,
+ 'public_net' => ['ipv4' => ['ip' => '1.2.3.4']],
+ ],
+ ], 200),
+ ]);
+}
+
+it('blocks action validation while the Hetzner server is still initializing', function () {
+ fakeHetznerServerStatus('initializing');
+
+ expect(fn () => ValidateServer::run($this->server))
+ ->toThrow(Exception::class, 'Hetzner server is initializing. Wait until it is running, then validate.');
+
+ expect($this->server->fresh()->validation_logs)
+ ->toBe('Hetzner server is initializing. Wait until it is running, then validate.');
+});
+
+it('blocks validation modal connection checks when the Hetzner server is off', function () {
+ fakeHetznerServerStatus('off');
+
+ Livewire::test(ValidateAndInstall::class, ['server' => $this->server])
+ ->call('validateConnection')
+ ->assertSet('error', 'Hetzner server is off. Power it on before validating.')
+ ->assertNotDispatched('validateOS');
+
+ $this->assertDatabaseHas('servers', [
+ 'id' => $this->server->id,
+ 'hetzner_server_status' => 'off',
+ 'validation_logs' => 'Hetzner server is off. Power it on before validating.',
+ ]);
+});
diff --git a/tests/Feature/HostingerApiTest.php b/tests/Feature/HostingerApiTest.php
index 3351b2dd10..ad3a2cd356 100644
--- a/tests/Feature/HostingerApiTest.php
+++ b/tests/Feature/HostingerApiTest.php
@@ -1,17 +1,21 @@
<?php
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
use App\Models\PrivateKey;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Once;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
Http::preventStrayRequests();
InstanceSettings::query()->whereKey(0)->delete();
@@ -164,6 +168,7 @@ it('creates a Hostinger VPS server through the API', function () {
Http::assertSent(fn ($request) => $request->method() === 'POST'
&& $request->url() === 'https://developers.hostinger.com/api/vps/v1/virtual-machines'
&& $request['setup']['enable_backups'] === true);
+ Bus::assertDispatched(ValidateNewCloudServerJob::class);
});
it('turns paid Hostinger backups off when the API request does not enable them', function () {
diff --git a/tests/Feature/HostingerServerCreationTest.php b/tests/Feature/HostingerServerCreationTest.php
index 264fc523a4..ac39d73271 100644
--- a/tests/Feature/HostingerServerCreationTest.php
+++ b/tests/Feature/HostingerServerCreationTest.php
@@ -1,6 +1,7 @@
<?php
use App\Actions\Server\DeleteServer;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Livewire\Server\Create;
use App\Livewire\Server\CreatePage;
use App\Livewire\Server\Delete;
@@ -14,6 +15,7 @@ use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
@@ -22,6 +24,8 @@ use Lorisleiva\Actions\Decorators\JobDecorator;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
Http::preventStrayRequests();
config([
diff --git a/tests/Feature/Server/HetznerServerPlaceholderIpTest.php b/tests/Feature/Server/HetznerServerPlaceholderIpTest.php
index 7a44ebbfce..dfc0583aa3 100644
--- a/tests/Feature/Server/HetznerServerPlaceholderIpTest.php
+++ b/tests/Feature/Server/HetznerServerPlaceholderIpTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Jobs\ValidateNewCloudServerJob;
use App\Livewire\Server\New\ByHetzner;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
@@ -8,12 +9,15 @@ use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
config([
'cache.default' => 'array',
'session.driver' => 'array',
@@ -115,4 +119,5 @@ it('creates the server with the real IP when Hetzner assigns one immediately', f
'hetzner_server_id' => '777',
'hetzner_server_status' => 'running',
]);
+ Bus::assertDispatched(ValidateNewCloudServerJob::class);
});
diff --git a/tests/Feature/Server/ServerIpv6NormalizationTest.php b/tests/Feature/Server/ServerIpv6NormalizationTest.php
index 8beaa3921e..3397f98b65 100644
--- a/tests/Feature/Server/ServerIpv6NormalizationTest.php
+++ b/tests/Feature/Server/ServerIpv6NormalizationTest.php
@@ -1,6 +1,7 @@
<?php
use App\Jobs\ServerCloudProviderStatusCheckJob;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Livewire\Server\New\ByHetzner;
use App\Livewire\Server\New\ByIp;
use App\Livewire\Server\Show;
@@ -11,6 +12,7 @@ use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Process;
@@ -19,6 +21,8 @@ use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
Server::flushIdentityMap();
Process::fake();
config([
diff --git a/tests/Feature/ServerSettingsAuditTest.php b/tests/Feature/ServerSettingsAuditTest.php
index c9976c6657..de82f5e653 100644
--- a/tests/Feature/ServerSettingsAuditTest.php
+++ b/tests/Feature/ServerSettingsAuditTest.php
@@ -3,6 +3,7 @@
use App\Actions\Proxy\SaveProxyConfiguration;
use App\Actions\Server\StartSentinel;
use App\Helpers\SslHelper;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Livewire\Destination\New\Docker as NewDockerDestination;
use App\Livewire\Destination\Show as DestinationShow;
use App\Livewire\Project\Database\Postgresql\StatusInfo as PostgresqlStatusInfo;
@@ -36,6 +37,7 @@ use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Once;
@@ -44,6 +46,8 @@ use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
$this->withoutDefer();
Server::flushIdentityMap();
Queue::fake();
diff --git a/tests/Feature/ValidateNewCloudServerJobTest.php b/tests/Feature/ValidateNewCloudServerJobTest.php
new file mode 100644
index 0000000000..d46aa0d9aa
--- /dev/null
+++ b/tests/Feature/ValidateNewCloudServerJobTest.php
@@ -0,0 +1,100 @@
+<?php
+
+use App\Jobs\ValidateAndInstallServerJob;
+use App\Jobs\ValidateNewCloudServerJob;
+use App\Models\CloudProviderToken;
+use App\Models\InstanceSettings;
+use App\Models\PrivateKey;
+use App\Models\Server;
+use App\Models\Team;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Bus;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Process;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ InstanceSettings::unguarded(fn () => InstanceSettings::query()->create(['id' => 0]));
+ Bus::fake([ValidateNewCloudServerJob::class, ValidateAndInstallServerJob::class]);
+
+ $team = Team::factory()->create();
+ $token = CloudProviderToken::create([
+ 'team_id' => $team->id,
+ 'provider' => 'hetzner',
+ 'token' => 'test-hetzner-token',
+ 'name' => 'Test Hetzner Token',
+ ]);
+
+ $this->server = Server::factory()->create([
+ 'team_id' => $team->id,
+ 'private_key_id' => PrivateKey::factory()->create(['team_id' => $team->id])->id,
+ 'cloud_provider_token_id' => $token->id,
+ 'hetzner_server_id' => 456,
+ 'hetzner_server_status' => 'initializing',
+ 'ip' => Server::PLACEHOLDER_IP,
+ ]);
+});
+
+function fakeNewHetznerServer(string $status, ?string $ip = '203.0.113.10'): void
+{
+ Http::fake([
+ 'https://api.hetzner.cloud/v1/servers/456' => Http::response([
+ 'server' => [
+ 'id' => 456,
+ 'status' => $status,
+ 'public_net' => ['ipv4' => ['ip' => $ip]],
+ ],
+ ], 200),
+ ]);
+}
+
+it('validates and installs the server when it runs and accepts SSH', function () {
+ fakeNewHetznerServer('running');
+ Process::fake();
+
+ (new ValidateNewCloudServerJob($this->server))->handle();
+
+ Bus::assertDispatched(ValidateAndInstallServerJob::class, fn ($job) => $job->server->is($this->server));
+ Bus::assertNotDispatched(ValidateNewCloudServerJob::class);
+ expect($this->server->fresh()->ip)->toBe('203.0.113.10');
+});
+
+it('checks again later while the server is still starting', function () {
+ fakeNewHetznerServer('initializing');
+
+ (new ValidateNewCloudServerJob($this->server, attempt: 3))->handle();
+
+ Bus::assertDispatched(ValidateNewCloudServerJob::class, fn ($job) => $job->attempt === 4);
+ Bus::assertNotDispatched(ValidateAndInstallServerJob::class);
+});
+
+it('checks again later while SSH is not ready', function () {
+ fakeNewHetznerServer('running');
+ Process::fake(['*' => Process::result(errorOutput: 'Connection refused', exitCode: 255)]);
+
+ (new ValidateNewCloudServerJob($this->server))->handle();
+
+ Bus::assertDispatched(ValidateNewCloudServerJob::class, fn ($job) => $job->attempt === 2);
+ Bus::assertNotDispatched(ValidateAndInstallServerJob::class);
+});
+
+it('stops and tells the user to validate manually after the last attempt', function () {
+ fakeNewHetznerServer('initializing');
+
+ (new ValidateNewCloudServerJob($this->server, attempt: ValidateNewCloudServerJob::MAX_ATTEMPTS))->handle();
+
+ Bus::assertNotDispatched(ValidateNewCloudServerJob::class);
+ Bus::assertNotDispatched(ValidateAndInstallServerJob::class);
+ expect($this->server->fresh()->validation_logs)->toContain('Validate it manually.');
+});
+
+it('does nothing when the user already started a validation', function () {
+ $this->server->update(['is_validating' => true]);
+ Http::fake();
+
+ (new ValidateNewCloudServerJob($this->server))->handle();
+
+ Http::assertNothingSent();
+ Bus::assertNothingDispatched();
+});
diff --git a/tests/Feature/VultrApiTest.php b/tests/Feature/VultrApiTest.php
index 85a421e60e..2de416ffb9 100644
--- a/tests/Feature/VultrApiTest.php
+++ b/tests/Feature/VultrApiTest.php
@@ -1,6 +1,7 @@
<?php
use App\Http\Controllers\Api\VultrController;
+use App\Jobs\ValidateNewCloudServerJob;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
use App\Models\PrivateKey;
@@ -10,12 +11,15 @@ use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Exceptions;
use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
config([
'app.maintenance.driver' => 'file',
'cache.default' => 'array',
diff --git a/tests/Feature/VultrServerCreationTest.php b/tests/Feature/VultrServerCreationTest.php
index 4c506de26e..617355a701 100644
--- a/tests/Feature/VultrServerCreationTest.php
+++ b/tests/Feature/VultrServerCreationTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Jobs\ValidateNewCloudServerJob;
use App\Livewire\Server\New\ByVultr;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
@@ -9,12 +10,15 @@ use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
+ Bus::fake([ValidateNewCloudServerJob::class]);
+
config([
'app.maintenance.driver' => 'file',
'cache.default' => 'array',