feat: volume cloning for ResourceOperations
This commit is contained in:
		@@ -2,6 +2,12 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace App\Livewire\Project\Shared;
 | 
					namespace App\Livewire\Project\Shared;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Actions\Application\StopApplication;
 | 
				
			||||||
 | 
					use App\Actions\Database\StartDatabase;
 | 
				
			||||||
 | 
					use App\Actions\Database\StopDatabase;
 | 
				
			||||||
 | 
					use App\Actions\Service\StartService;
 | 
				
			||||||
 | 
					use App\Actions\Service\StopService;
 | 
				
			||||||
 | 
					use App\Jobs\VolumeCloneJob;
 | 
				
			||||||
use App\Models\Environment;
 | 
					use App\Models\Environment;
 | 
				
			||||||
use App\Models\Project;
 | 
					use App\Models\Project;
 | 
				
			||||||
use App\Models\StandaloneDocker;
 | 
					use App\Models\StandaloneDocker;
 | 
				
			||||||
@@ -21,6 +27,8 @@ class ResourceOperations extends Component
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public $servers;
 | 
					    public $servers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public bool $cloneVolumeData = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function mount()
 | 
					    public function mount()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $parameters = get_route_parameters();
 | 
					        $parameters = get_route_parameters();
 | 
				
			||||||
@@ -30,6 +38,11 @@ class ResourceOperations extends Component
 | 
				
			|||||||
        $this->servers = currentTeam()->servers;
 | 
					        $this->servers = currentTeam()->servers;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function toggleVolumeCloning(bool $value)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->cloneVolumeData = $value;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function cloneTo($destination_id)
 | 
					    public function cloneTo($destination_id)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $new_destination = StandaloneDocker::find($destination_id);
 | 
					        $new_destination = StandaloneDocker::find($destination_id);
 | 
				
			||||||
@@ -118,19 +131,43 @@ class ResourceOperations extends Component
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            $persistentVolumes = $this->resource->persistentStorages()->get();
 | 
					            $persistentVolumes = $this->resource->persistentStorages()->get();
 | 
				
			||||||
            foreach ($persistentVolumes as $volume) {
 | 
					            foreach ($persistentVolumes as $volume) {
 | 
				
			||||||
                $volumeName = str($volume->name)->replace($this->resource->uuid, $new_resource->uuid)->value();
 | 
					                $newName = '';
 | 
				
			||||||
                if ($volumeName === $volume->name) {
 | 
					                if (str_starts_with($volume->name, $this->resource->uuid)) {
 | 
				
			||||||
                    $volumeName = $new_resource->uuid.'-'.str($volume->name)->afterLast('-');
 | 
					                    $newName = str($volume->name)->replace($this->resource->uuid, $new_resource->uuid);
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    $newName = $new_resource->uuid.'-'.str($volume->name)->afterLast('-');
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $newPersistentVolume = $volume->replicate([
 | 
					                $newPersistentVolume = $volume->replicate([
 | 
				
			||||||
                    'id',
 | 
					                    'id',
 | 
				
			||||||
                    'created_at',
 | 
					                    'created_at',
 | 
				
			||||||
                    'updated_at',
 | 
					                    'updated_at',
 | 
				
			||||||
                ])->fill([
 | 
					                ])->fill([
 | 
				
			||||||
                    'name' => $volumeName,
 | 
					                    'name' => $newName,
 | 
				
			||||||
                    'resource_id' => $new_resource->id,
 | 
					                    'resource_id' => $new_resource->id,
 | 
				
			||||||
                ]);
 | 
					                ]);
 | 
				
			||||||
                $newPersistentVolume->save();
 | 
					                $newPersistentVolume->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($this->cloneVolumeData) {
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        StopApplication::dispatch($this->resource, false, false);
 | 
				
			||||||
 | 
					                        $sourceVolume = $volume->name;
 | 
				
			||||||
 | 
					                        $targetVolume = $newPersistentVolume->name;
 | 
				
			||||||
 | 
					                        $server = $this->resource->destination->server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        VolumeCloneJob::dispatch($sourceVolume, $targetVolume, $server, $newPersistentVolume);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        queue_application_deployment(
 | 
				
			||||||
 | 
					                            deployment_uuid: (string) new Cuid2,
 | 
				
			||||||
 | 
					                            application: $this->resource,
 | 
				
			||||||
 | 
					                            server: $server,
 | 
				
			||||||
 | 
					                            destination: $this->resource->destination,
 | 
				
			||||||
 | 
					                            no_questions_asked: true
 | 
				
			||||||
 | 
					                        );
 | 
				
			||||||
 | 
					                    } catch (\Exception $e) {
 | 
				
			||||||
 | 
					                        \Log::error('Failed to copy volume data for '.$volume->name.': '.$e->getMessage());
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $fileStorages = $this->resource->fileStorages()->get();
 | 
					            $fileStorages = $this->resource->fileStorages()->get();
 | 
				
			||||||
@@ -217,9 +254,11 @@ class ResourceOperations extends Component
 | 
				
			|||||||
                } elseif (str_starts_with($originalName, 'dragonfly-data-')) {
 | 
					                } elseif (str_starts_with($originalName, 'dragonfly-data-')) {
 | 
				
			||||||
                    $newName = 'dragonfly-data-'.$new_resource->uuid;
 | 
					                    $newName = 'dragonfly-data-'.$new_resource->uuid;
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    $newName = str($originalName)
 | 
					                    if (str_starts_with($volume->name, $this->resource->uuid)) {
 | 
				
			||||||
                        ->replaceFirst($this->resource->uuid, $new_resource->uuid)
 | 
					                        $newName = str($volume->name)->replace($this->resource->uuid, $new_resource->uuid);
 | 
				
			||||||
                        ->toString();
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        $newName = $new_resource->uuid.'-'.$volume->name;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $newPersistentVolume = $volume->replicate([
 | 
					                $newPersistentVolume = $volume->replicate([
 | 
				
			||||||
@@ -231,6 +270,21 @@ class ResourceOperations extends Component
 | 
				
			|||||||
                    'resource_id' => $new_resource->id,
 | 
					                    'resource_id' => $new_resource->id,
 | 
				
			||||||
                ]);
 | 
					                ]);
 | 
				
			||||||
                $newPersistentVolume->save();
 | 
					                $newPersistentVolume->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($this->cloneVolumeData) {
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        StopDatabase::dispatch($this->resource);
 | 
				
			||||||
 | 
					                        $sourceVolume = $volume->name;
 | 
				
			||||||
 | 
					                        $targetVolume = $newPersistentVolume->name;
 | 
				
			||||||
 | 
					                        $server = $this->resource->destination->server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        VolumeCloneJob::dispatch($sourceVolume, $targetVolume, $server, $newPersistentVolume);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        StartDatabase::dispatch($this->resource);
 | 
				
			||||||
 | 
					                    } catch (\Exception $e) {
 | 
				
			||||||
 | 
					                        \Log::error('Failed to copy volume data for '.$volume->name.': '.$e->getMessage());
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $fileStorages = $this->resource->fileStorages()->get();
 | 
					            $fileStorages = $this->resource->fileStorages()->get();
 | 
				
			||||||
@@ -293,7 +347,7 @@ class ResourceOperations extends Component
 | 
				
			|||||||
                'name' => $this->resource->name.'-clone-'.$uuid,
 | 
					                'name' => $this->resource->name.'-clone-'.$uuid,
 | 
				
			||||||
                'destination_id' => $new_destination->id,
 | 
					                'destination_id' => $new_destination->id,
 | 
				
			||||||
                'destination_type' => $new_destination->getMorphClass(),
 | 
					                'destination_type' => $new_destination->getMorphClass(),
 | 
				
			||||||
                'server_id' => $new_destination->server_id, // server_id is probably not needed anymore because of the new polymorphic relationships (here it is needed for clone to work - but maybe we can drop the column)
 | 
					                'server_id' => $new_destination->server_id, // server_id is probably not needed anymore because of the new polymorphic relationships (here it is needed for clone to a different server to work - but maybe we can drop the column)
 | 
				
			||||||
            ]);
 | 
					            ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $new_resource->save();
 | 
					            $new_resource->save();
 | 
				
			||||||
@@ -334,12 +388,82 @@ class ResourceOperations extends Component
 | 
				
			|||||||
                $application->update([
 | 
					                $application->update([
 | 
				
			||||||
                    'status' => 'exited',
 | 
					                    'status' => 'exited',
 | 
				
			||||||
                ]);
 | 
					                ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $persistentVolumes = $application->persistentStorages()->get();
 | 
				
			||||||
 | 
					                foreach ($persistentVolumes as $volume) {
 | 
				
			||||||
 | 
					                    $newName = '';
 | 
				
			||||||
 | 
					                    if (str_starts_with($volume->name, $volume->resource->uuid)) {
 | 
				
			||||||
 | 
					                        $newName = str($volume->name)->replace($volume->resource->uuid, $application->uuid);
 | 
				
			||||||
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        $newName = $application->uuid.'-'.str($volume->name)->afterLast('-');
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    $newPersistentVolume = $volume->replicate([
 | 
				
			||||||
 | 
					                        'id',
 | 
				
			||||||
 | 
					                        'created_at',
 | 
				
			||||||
 | 
					                        'updated_at',
 | 
				
			||||||
 | 
					                    ])->fill([
 | 
				
			||||||
 | 
					                        'name' => $newName,
 | 
				
			||||||
 | 
					                        'resource_id' => $application->id,
 | 
				
			||||||
 | 
					                    ]);
 | 
				
			||||||
 | 
					                    $newPersistentVolume->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if ($this->cloneVolumeData) {
 | 
				
			||||||
 | 
					                        try {
 | 
				
			||||||
 | 
					                            StopService::dispatch($application, false, false);
 | 
				
			||||||
 | 
					                            $sourceVolume = $volume->name;
 | 
				
			||||||
 | 
					                            $targetVolume = $newPersistentVolume->name;
 | 
				
			||||||
 | 
					                            $server = $application->service->destination->server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            VolumeCloneJob::dispatch($sourceVolume, $targetVolume, $server, $newPersistentVolume);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            StartService::dispatch($application);
 | 
				
			||||||
 | 
					                        } catch (\Exception $e) {
 | 
				
			||||||
 | 
					                            \Log::error('Failed to copy volume data for '.$volume->name.': '.$e->getMessage());
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach ($new_resource->databases() as $database) {
 | 
					            foreach ($new_resource->databases() as $database) {
 | 
				
			||||||
                $database->update([
 | 
					                $database->update([
 | 
				
			||||||
                    'status' => 'exited',
 | 
					                    'status' => 'exited',
 | 
				
			||||||
                ]);
 | 
					                ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $persistentVolumes = $database->persistentStorages()->get();
 | 
				
			||||||
 | 
					                foreach ($persistentVolumes as $volume) {
 | 
				
			||||||
 | 
					                    $newName = '';
 | 
				
			||||||
 | 
					                    if (str_starts_with($volume->name, $volume->resource->uuid)) {
 | 
				
			||||||
 | 
					                        $newName = str($volume->name)->replace($volume->resource->uuid, $database->uuid);
 | 
				
			||||||
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        $newName = $database->uuid.'-'.str($volume->name)->afterLast('-');
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    $newPersistentVolume = $volume->replicate([
 | 
				
			||||||
 | 
					                        'id',
 | 
				
			||||||
 | 
					                        'created_at',
 | 
				
			||||||
 | 
					                        'updated_at',
 | 
				
			||||||
 | 
					                    ])->fill([
 | 
				
			||||||
 | 
					                        'name' => $newName,
 | 
				
			||||||
 | 
					                        'resource_id' => $database->id,
 | 
				
			||||||
 | 
					                    ]);
 | 
				
			||||||
 | 
					                    $newPersistentVolume->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if ($this->cloneVolumeData) {
 | 
				
			||||||
 | 
					                        try {
 | 
				
			||||||
 | 
					                            StopService::dispatch($database->service, false, false);
 | 
				
			||||||
 | 
					                            $sourceVolume = $volume->name;
 | 
				
			||||||
 | 
					                            $targetVolume = $newPersistentVolume->name;
 | 
				
			||||||
 | 
					                            $server = $database->service->destination->server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            VolumeCloneJob::dispatch($sourceVolume, $targetVolume, $server, $newPersistentVolume);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            StartService::dispatch($database->service);
 | 
				
			||||||
 | 
					                        } catch (\Exception $e) {
 | 
				
			||||||
 | 
					                            \Log::error('Failed to copy volume data for '.$volume->name.': '.$e->getMessage());
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $new_resource->parse();
 | 
					            $new_resource->parse();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,40 @@
 | 
				
			|||||||
<div>
 | 
					<div>
 | 
				
			||||||
    <h2>Resource Operations</h2>
 | 
					    <h2>Resource Operations</h2>
 | 
				
			||||||
    <div class="pb-4">You can easily make different kind of operations on this resource.</div>
 | 
					    <div>You can easily make different kind of operations on this resource.</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <div class="mt-8">
 | 
				
			||||||
 | 
					        <h3 class="text-lg font-bold mb-2">Clone Volume Data</h3>
 | 
				
			||||||
 | 
					        <div class="text-sm text-gray-600 dark:text-gray-300 mb-4">
 | 
				
			||||||
 | 
					            Clone your volume data to the new resources volumes. This process requires a brief container downtime to ensure data consistency.
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					        <div class="pb-4">
 | 
				
			||||||
 | 
					            @if(!$cloneVolumeData)
 | 
				
			||||||
 | 
					                <div wire:key="volume-disabled">
 | 
				
			||||||
 | 
					                    <x-modal-confirmation 
 | 
				
			||||||
 | 
					                        title="Enable Volume Data Cloning?" 
 | 
				
			||||||
 | 
					                        buttonTitle="Enable Volume Cloning" 
 | 
				
			||||||
 | 
					                        submitAction="toggleVolumeCloning(true)"
 | 
				
			||||||
 | 
					                        :actions="['This will temporarily stop all the containers to copy the volume data to the new resources to ensure data consistency.', 'The process runs in the background and may take a few minutes.']" 
 | 
				
			||||||
 | 
					                        :confirmWithPassword="false"
 | 
				
			||||||
 | 
					                        :confirmWithText="false"
 | 
				
			||||||
 | 
					                    />
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
 | 
					            @else
 | 
				
			||||||
 | 
					                <div wire:key="volume-enabled" class="max-w-md">
 | 
				
			||||||
 | 
					                    <x-forms.checkbox 
 | 
				
			||||||
 | 
					                        label="Clone Volume Data" 
 | 
				
			||||||
 | 
					                        id="cloneVolumeData" 
 | 
				
			||||||
 | 
					                        wire:model="cloneVolumeData"
 | 
				
			||||||
 | 
					                        wire:change="toggleVolumeCloning(false)"
 | 
				
			||||||
 | 
					                        :checked="$cloneVolumeData"
 | 
				
			||||||
 | 
					                        helper="Volume Data will be cloned to the new resources. Containers will be temporarily stopped during the cloning process." />
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
 | 
					            @endif
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <h3>Clone</h3>
 | 
					    <h3>Clone</h3>
 | 
				
			||||||
    <div class="pb-4">To another project / environment on a different server.</div>
 | 
					    <div class="pb-4">To another project / environment on a different / same server.</div>
 | 
				
			||||||
    <div class="pb-4">
 | 
					    <div class="pb-4">
 | 
				
			||||||
        <div class="flex flex-col flex-wrap gap-2">
 | 
					        <div class="flex flex-col flex-wrap gap-2">
 | 
				
			||||||
            @foreach ($servers->sortBy('id') as $server)
 | 
					            @foreach ($servers->sortBy('id') as $server)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user