From 249e39ea7193f721ec2733159fe901742ad4abd5 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:26:59 +0100 Subject: [PATCH] fix: implement all the cloneMe changes for ResourceOperations as well --- .../Project/Shared/ResourceOperations.php | 212 ++++++++++++++++-- 1 file changed, 189 insertions(+), 23 deletions(-) diff --git a/app/Livewire/Project/Shared/ResourceOperations.php b/app/Livewire/Project/Shared/ResourceOperations.php index 635bf2b69..cb9f7db85 100644 --- a/app/Livewire/Project/Shared/ResourceOperations.php +++ b/app/Livewire/Project/Shared/ResourceOperations.php @@ -30,9 +30,7 @@ class ResourceOperations extends Component $this->servers = currentTeam()->servers; } - public function cloneTo($destination_id) // issues is applications table stuff is replciated but not the application_settings table stuff and also application_preveiws is not replicated, Also only volumes but not files an directory mounts are cloned - - // Also the next issue is that the thing is not coloned to the correct server + public function cloneTo($destination_id) { $new_destination = StandaloneDocker::find($destination_id); if (! $new_destination) { @@ -46,6 +44,12 @@ class ResourceOperations extends Component if ($this->resource->getMorphClass() === \App\Models\Application::class) { $name = 'clone-of-'.str($this->resource->name)->limit(20).'-'.$uuid; + $applicationSettings = $this->resource->settings; + $url = $this->resource->fqdn; + + if ($server->proxyType() !== 'NONE' && $applicationSettings->is_container_label_readonly_enabled === true) { + $url = generateFqdn($server, $uuid); + } $new_resource = $this->resource->replicate([ 'id', @@ -56,28 +60,62 @@ class ResourceOperations extends Component ])->fill([ 'uuid' => $uuid, 'name' => $name, - 'fqdn' => generateFqdn($server, $uuid), + 'fqdn' => $url, 'status' => 'exited', 'destination_id' => $new_destination->id, ]); $new_resource->save(); - if ($new_resource->destination->server->proxyType() !== 'NONE') { + + if ($new_resource->destination->server->proxyType() !== 'NONE' && $applicationSettings->is_container_label_readonly_enabled === true) { $customLabels = str(implode('|coolify|', generateLabelsApplication($new_resource)))->replace('|coolify|', "\n"); $new_resource->custom_labels = base64_encode($customLabels); $new_resource->save(); } - $environmentVaribles = $this->resource->environment_variables()->get(); - foreach ($environmentVaribles as $environmentVarible) { - $newEnvironmentVariable = $environmentVarible->replicate([ + + $new_resource->settings()->delete(); + if ($applicationSettings) { + $newApplicationSettings = $applicationSettings->replicate([ 'id', 'created_at', 'updated_at', ])->fill([ - 'resourceable_id' => $new_resource->id, - 'resourceable_type' => $new_resource->getMorphClass(), + 'application_id' => $new_resource->id, ]); - $newEnvironmentVariable->save(); + $newApplicationSettings->save(); } + + $tags = $this->resource->tags; + foreach ($tags as $tag) { + $new_resource->tags()->attach($tag->id); + } + + $scheduledTasks = $this->resource->scheduled_tasks()->get(); + foreach ($scheduledTasks as $task) { + $newTask = $task->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'uuid' => (string) new Cuid2, + 'application_id' => $new_resource->id, + 'team_id' => currentTeam()->id, + ]); + $newTask->save(); + } + + $applicationPreviews = $this->resource->previews()->get(); + foreach ($applicationPreviews as $preview) { + $newPreview = $preview->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'application_id' => $new_resource->id, + 'status' => 'exited', + ]); + $newPreview->save(); + } + $persistentVolumes = $this->resource->persistentStorages()->get(); foreach ($persistentVolumes as $volume) { $volumeName = str($volume->name)->replace($this->resource->uuid, $new_resource->uuid)->value(); @@ -94,6 +132,32 @@ class ResourceOperations extends Component ]); $newPersistentVolume->save(); } + + $fileStorages = $this->resource->fileStorages()->get(); + foreach ($fileStorages as $storage) { + $newStorage = $storage->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'resource_id' => $new_resource->id, + ]); + $newStorage->save(); + } + + $environmentVaribles = $this->resource->environment_variables()->get(); + foreach ($environmentVaribles as $environmentVarible) { + $newEnvironmentVariable = $environmentVarible->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'resourceable_id' => $new_resource->id, + 'resourceable_type' => $new_resource->getMorphClass(), + ]); + $newEnvironmentVariable->save(); + } + $route = route('project.application.configuration', [ 'project_uuid' => $this->projectUuid, 'environment_uuid' => $this->environmentUuid, @@ -124,20 +188,85 @@ class ResourceOperations extends Component 'destination_id' => $new_destination->id, ]); $new_resource->save(); + + $tags = $this->resource->tags; + foreach ($tags as $tag) { + $new_resource->tags()->attach($tag->id); + } + + $new_resource->persistentStorages()->delete(); + $persistentVolumes = $this->resource->persistentStorages()->get(); + foreach ($persistentVolumes as $volume) { + $originalName = $volume->name; + $newName = ''; + + if (str_starts_with($originalName, 'postgres-data-')) { + $newName = 'postgres-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'mysql-data-')) { + $newName = 'mysql-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'redis-data-')) { + $newName = 'redis-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'clickhouse-data-')) { + $newName = 'clickhouse-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'mariadb-data-')) { + $newName = 'mariadb-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'mongodb-data-')) { + $newName = 'mongodb-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'keydb-data-')) { + $newName = 'keydb-data-'.$new_resource->uuid; + } elseif (str_starts_with($originalName, 'dragonfly-data-')) { + $newName = 'dragonfly-data-'.$new_resource->uuid; + } else { + $newName = str($originalName) + ->replaceFirst($this->resource->uuid, $new_resource->uuid) + ->toString(); + } + + $newPersistentVolume = $volume->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'name' => $newName, + 'resource_id' => $new_resource->id, + ]); + $newPersistentVolume->save(); + } + + $fileStorages = $this->resource->fileStorages()->get(); + foreach ($fileStorages as $storage) { + $newStorage = $storage->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'resource_id' => $new_resource->id, + ]); + $newStorage->save(); + } + + $scheduledBackups = $this->resource->scheduledBackups()->get(); + foreach ($scheduledBackups as $backup) { + $uuid = (string) new Cuid2; + $newBackup = $backup->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'uuid' => $uuid, + 'database_id' => $new_resource->id, + 'database_type' => $new_resource->getMorphClass(), + 'team_id' => currentTeam()->id, + ]); + $newBackup->save(); + } + $environmentVaribles = $this->resource->environment_variables()->get(); foreach ($environmentVaribles as $environmentVarible) { - $payload = []; - if ($this->resource->type() === 'standalone-postgresql') { - $payload['standalone_postgresql_id'] = $new_resource->id; - } elseif ($this->resource->type() === 'standalone-redis') { - $payload['standalone_redis_id'] = $new_resource->id; - } elseif ($this->resource->type() === 'standalone-mongodb') { - $payload['standalone_mongodb_id'] = $new_resource->id; - } elseif ($this->resource->type() === 'standalone-mysql') { - $payload['standalone_mysql_id'] = $new_resource->id; - } elseif ($this->resource->type() === 'standalone-mariadb') { - $payload['standalone_mariadb_id'] = $new_resource->id; - } + $payload = [ + 'resourceable_id' => $new_resource->id, + 'resourceable_type' => $new_resource->getMorphClass(), + ]; $newEnvironmentVariable = $environmentVarible->replicate([ 'id', 'created_at', @@ -145,6 +274,7 @@ class ResourceOperations extends Component ])->fill($payload); $newEnvironmentVariable->save(); } + $route = route('project.database.configuration', [ 'project_uuid' => $this->projectUuid, 'environment_uuid' => $this->environmentUuid, @@ -167,17 +297,53 @@ class ResourceOperations extends Component ]); $new_resource->save(); + + $tags = $this->resource->tags; + foreach ($tags as $tag) { + $new_resource->tags()->attach($tag->id); + } + + $scheduledTasks = $this->resource->scheduled_tasks()->get(); + foreach ($scheduledTasks as $task) { + $newTask = $task->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'uuid' => (string) new Cuid2, + 'service_id' => $new_resource->id, + 'team_id' => currentTeam()->id, + ]); + $newTask->save(); + } + + $environmentVariables = $this->resource->environment_variables()->get(); + foreach ($environmentVariables as $environmentVariable) { + $newEnvironmentVariable = $environmentVariable->replicate([ + 'id', + 'created_at', + 'updated_at', + ])->fill([ + 'resourceable_id' => $new_resource->id, + 'resourceable_type' => $new_resource->getMorphClass(), + ]); + $newEnvironmentVariable->save(); + } + foreach ($new_resource->applications() as $application) { $application->update([ 'status' => 'exited', ]); } + foreach ($new_resource->databases() as $database) { $database->update([ 'status' => 'exited', ]); } + $new_resource->parse(); + $route = route('project.service.configuration', [ 'project_uuid' => $this->projectUuid, 'environment_uuid' => $this->environmentUuid,