feat(resources): add cross-server resource migration (dev-only) (#11165)

This commit is contained in:
Andras Bacsai authored and GitHub committed 2026-08-07 23:01:31 +02:00
1 parent d83ba627cd
commit c15e3b35fd
15 files changed
+1354 -7

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Jobs;
use App\Actions\Shared\MigrateResourceToDestination;
use App\Models\Application;
use App\Models\Service;
use App\Models\StandaloneClickhouse;
use App\Models\StandaloneDocker;
use App\Models\StandaloneDragonfly;
use App\Models\StandaloneKeydb;
use App\Models\StandaloneMariadb;
use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
use App\Models\SwarmDocker;
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;
/**
* Final step of a server migration: update destination pointers after volume data is transferred.
*/
class FinalizeResourceMigrationJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $resource,
public StandaloneDocker|SwarmDocker $destination,
) {
$this->onQueue('high');
}
public function handle(): void
{
MigrateResourceToDestination::make()->applyDestination(
$this->resource->fresh(),
$this->destination
);
}
}
+132
View File
@@ -0,0 +1,132 @@
<?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;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
/**
* Copy a bind-mount host path from one Coolify-managed server to another.
*/
class HostPathCloneJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected string $cloneDir = '/data/coolify/clone';
public int $timeout = 3600;
public function __construct(
protected string $sourcePath,
protected string $targetPath,
protected Server $sourceServer,
protected Server $targetServer
) {
$this->onQueue('high');
}
public function handle(): void
{
if ($this->sourceServer->id === $this->targetServer->id && $this->sourcePath === $this->targetPath) {
return;
}
if ($this->sourceServer->id === $this->targetServer->id) {
$this->cloneLocalPath();
return;
}
$this->cloneRemotePath();
}
protected function cloneLocalPath(): void
{
$src = escapeshellarg($this->sourcePath);
$tgt = escapeshellarg($this->targetPath);
$tgtParent = escapeshellarg(dirname($this->targetPath));
instant_remote_process([
"mkdir -p {$tgtParent}",
"mkdir -p {$tgt}",
"docker run --rm -v {$src}:/source:ro -v {$tgt}:/target alpine sh -c 'cp -a /source/. /target/'",
], $this->sourceServer);
}
protected function cloneRemotePath(): void
{
$archiveName = 'hostpath-data.tar.gz';
$token = Str::uuid()->toString();
$sourceCloneDir = "{$this->cloneDir}/hostpath-{$token}";
$targetCloneDir = "{$this->cloneDir}/hostpath-{$token}";
$srcDir = escapeshellarg($sourceCloneDir);
$tgtDir = escapeshellarg($targetCloneDir);
$srcPath = escapeshellarg($this->sourcePath);
$tgtPath = escapeshellarg($this->targetPath);
$tgtParent = escapeshellarg(dirname($this->targetPath));
$localTempDir = storage_path('app/tmp/hostpath-clones/'.$token);
$localArchive = $localTempDir.'/'.$archiveName;
try {
File::ensureDirectoryExists($localTempDir, 0755);
instant_remote_process([
"mkdir -p {$srcDir}",
"chmod 777 {$srcDir}",
"test -e {$srcPath}",
"docker run --rm -v {$srcPath}:/source:ro -v {$srcDir}:/clone alpine sh -c 'cd /source && tar czf /clone/{$archiveName} .'",
], $this->sourceServer);
instant_remote_process([
"mkdir -p {$tgtDir}",
"chmod 777 {$tgtDir}",
], $this->targetServer);
instant_scp_from_server(
"{$sourceCloneDir}/{$archiveName}",
$localArchive,
$this->sourceServer
);
instant_scp(
$localArchive,
"{$targetCloneDir}/{$archiveName}",
$this->targetServer
);
instant_remote_process([
"mkdir -p {$tgtParent}",
"mkdir -p {$tgtPath}",
"docker run --rm -v {$tgtPath}:/target -v {$tgtDir}:/clone alpine sh -c 'cd /target && tar xzf /clone/{$archiveName}'",
], $this->targetServer);
} catch (\Exception $e) {
\Log::error("Failed to clone host path {$this->sourcePath} to {$this->targetPath}: ".$e->getMessage());
throw $e;
} finally {
try {
File::deleteDirectory($localTempDir);
} catch (\Exception $e) {
\Log::warning('Failed to clean up local host-path clone directory: '.$e->getMessage());
}
try {
instant_remote_process(["rm -rf {$srcDir}"], $this->sourceServer, false);
} catch (\Exception $e) {
\Log::warning('Failed to clean up source host-path clone directory: '.$e->getMessage());
}
try {
instant_remote_process(["rm -rf {$tgtDir}"], $this->targetServer, false);
} catch (\Exception $e) {
\Log::warning('Failed to clean up target host-path clone directory: '.$e->getMessage());
}
}
}
}
+31 -7
View File
@@ -10,6 +10,8 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
{
@@ -17,6 +19,8 @@ class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
protected string $cloneDir = '/data/coolify/clone';
public int $timeout = 3600;
public function __construct(
protected string $sourceVolume,
protected string $targetVolume,
@@ -27,7 +31,7 @@ class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
$this->onQueue('high');
}
public function handle()
public function handle(): void
{
try {
if (! $this->targetServer || $this->targetServer->id === $this->sourceServer->id) {
@@ -41,18 +45,23 @@ class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
}
}
protected function cloneLocalVolume()
protected function cloneLocalVolume(): void
{
$srcVol = escapeshellarg($this->sourceVolume);
$tgtVol = escapeshellarg($this->targetVolume);
// Same-name local copy is a no-op (used when only the network destination changes).
if ($this->sourceVolume === $this->targetVolume) {
return;
}
instant_remote_process([
"docker volume create {$tgtVol}",
"docker run --rm -v {$srcVol}:/source -v {$tgtVol}:/target alpine sh -c 'cp -a /source/. /target/ && chown -R 1000:1000 /target'",
], $this->sourceServer);
}
protected function cloneRemoteVolume()
protected function cloneRemoteVolume(): void
{
$srcVol = escapeshellarg($this->sourceVolume);
$tgtVol = escapeshellarg($this->targetVolume);
@@ -60,8 +69,12 @@ class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
$targetCloneDir = "{$this->cloneDir}/{$this->targetVolume}";
$srcDir = escapeshellarg($sourceCloneDir);
$tgtDir = escapeshellarg($targetCloneDir);
$localTempDir = storage_path('app/tmp/volume-clones/'.Str::uuid()->toString());
$localArchive = $localTempDir.'/volume-data.tar.gz';
try {
File::ensureDirectoryExists($localTempDir, 0755);
instant_remote_process([
"mkdir -p {$srcDir}",
"chmod 777 {$srcDir}",
@@ -73,10 +86,16 @@ class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
"chmod 777 {$tgtDir}",
], $this->targetServer);
// Coolify host is the intermediary: download from source, upload to target.
instant_scp_from_server(
"{$sourceCloneDir}/volume-data.tar.gz",
$localArchive,
$this->sourceServer
);
instant_scp(
"$sourceCloneDir/volume-data.tar.gz",
"$targetCloneDir/volume-data.tar.gz",
$this->sourceServer,
$localArchive,
"{$targetCloneDir}/volume-data.tar.gz",
$this->targetServer
);
@@ -84,11 +103,16 @@ class VolumeCloneJob implements ShouldBeEncrypted, ShouldQueue
"docker volume create {$tgtVol}",
"docker run --rm -v {$tgtVol}:/target -v {$tgtDir}:/clone alpine sh -c 'cd /target && tar xzf /clone/volume-data.tar.gz && chown -R 1000:1000 /target'",
], $this->targetServer);
} catch (\Exception $e) {
\Log::error("Failed to clone volume {$this->sourceVolume} to {$this->targetVolume}: ".$e->getMessage());
throw $e;
} finally {
try {
File::deleteDirectory($localTempDir);
} catch (\Exception $e) {
\Log::warning('Failed to clean up local volume clone directory: '.$e->getMessage());
}
try {
instant_remote_process([
"rm -rf {$srcDir}",