mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-14 02:15:51 +00:00
feat(resources): add cross-server resource migration (dev-only) (#11165)
This commit is contained in:
1 parent
d83ba627cd
commit
c15e3b35fd
15 files changed
+1354
-7
No files matched your search
@@ -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}",
|
||||
|
||||
Reference in new issue
Block a user