fix: Service and database cloning and label generation
Databases: - clone tags - clone volumes - clone file and directory mounts - clone backup schedules Services: - clone tags - clone scheduled task - clone environment variables
This commit is contained in:
@@ -117,14 +117,20 @@ class CloneMe extends Component
|
|||||||
'additional_networks_count',
|
'additional_networks_count',
|
||||||
])->fill([
|
])->fill([
|
||||||
'uuid' => $uuid,
|
'uuid' => $uuid,
|
||||||
// 'fqdn' => generateFqdn($this->server, $uuid), - Proxy labels are also duplicated, so can we duplicate the domain as well?
|
'fqdn' => generateFqdn($this->server, $uuid), // this also needs a condition
|
||||||
'status' => 'exited',
|
'status' => 'exited',
|
||||||
'environment_id' => $environment->id,
|
'environment_id' => $environment->id,
|
||||||
'destination_id' => $this->selectedDestination,
|
'destination_id' => $this->selectedDestination,
|
||||||
]);
|
]);
|
||||||
$newApplication->save();
|
$newApplication->save();
|
||||||
|
|
||||||
$newApplication->settings()->delete(); // first delete the automatically created settings and fill in the old ones (to properly clone and avoid duplicates)
|
if ($newApplication->destination->server->proxyType() !== 'NONE' || ! $newApplication->application_settings->is_container_label_readonly_enabled) { // fix after switching this logic up
|
||||||
|
$customLabels = str(implode('|coolify|', generateLabelsApplication($newApplication)))->replace('|coolify|', "\n");
|
||||||
|
$newApplication->custom_labels = base64_encode($customLabels);
|
||||||
|
$newApplication->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
$newApplication->settings()->delete();
|
||||||
$applicationSettings = $application->settings;
|
$applicationSettings = $application->settings;
|
||||||
if ($applicationSettings) {
|
if ($applicationSettings) {
|
||||||
$newApplicationSettings = $applicationSettings->replicate([
|
$newApplicationSettings = $applicationSettings->replicate([
|
||||||
@@ -221,6 +227,65 @@ class CloneMe extends Component
|
|||||||
'destination_id' => $this->selectedDestination,
|
'destination_id' => $this->selectedDestination,
|
||||||
]);
|
]);
|
||||||
$newDatabase->save();
|
$newDatabase->save();
|
||||||
|
|
||||||
|
$tags = $database->tags;
|
||||||
|
foreach ($tags as $tag) {
|
||||||
|
$newDatabase->tags()->attach($tag->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$newDatabase->persistentStorages()->delete();
|
||||||
|
$persistentVolumes = $database->persistentStorages()->get();
|
||||||
|
foreach ($persistentVolumes as $volume) {
|
||||||
|
$originalName = $volume->name;
|
||||||
|
$newName = '';
|
||||||
|
|
||||||
|
if (str_starts_with($originalName, 'postgres-data-')) {
|
||||||
|
$newName = 'postgres-data-'.$newDatabase->uuid;
|
||||||
|
} else {
|
||||||
|
$newName = str($originalName)
|
||||||
|
->replaceFirst($database->uuid, $newDatabase->uuid)
|
||||||
|
->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
$newPersistentVolume = $volume->replicate([
|
||||||
|
'id',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
])->fill([
|
||||||
|
'name' => $newName,
|
||||||
|
'resource_id' => $newDatabase->id,
|
||||||
|
]);
|
||||||
|
$newPersistentVolume->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileStorages = $database->fileStorages()->get();
|
||||||
|
foreach ($fileStorages as $storage) {
|
||||||
|
$newStorage = $storage->replicate([
|
||||||
|
'id',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
])->fill([
|
||||||
|
'resource_id' => $newDatabase->id,
|
||||||
|
]);
|
||||||
|
$newStorage->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
$scheduledBackups = $database->scheduledBackups()->get();
|
||||||
|
foreach ($scheduledBackups as $backup) {
|
||||||
|
$uuid = (string) new Cuid2;
|
||||||
|
$newBackup = $backup->replicate([
|
||||||
|
'id',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
])->fill([
|
||||||
|
'uuid' => $uuid,
|
||||||
|
'database_id' => $newDatabase->id,
|
||||||
|
'database_type' => $newDatabase->getMorphClass(),
|
||||||
|
'team_id' => currentTeam()->id,
|
||||||
|
]);
|
||||||
|
$newBackup->save();
|
||||||
|
}
|
||||||
|
|
||||||
$environmentVaribles = $database->environment_variables()->get();
|
$environmentVaribles = $database->environment_variables()->get();
|
||||||
foreach ($environmentVaribles as $environmentVarible) {
|
foreach ($environmentVaribles as $environmentVarible) {
|
||||||
$payload = [];
|
$payload = [];
|
||||||
@@ -234,6 +299,7 @@ class CloneMe extends Component
|
|||||||
$newEnvironmentVariable->save();
|
$newEnvironmentVariable->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($services as $service) {
|
foreach ($services as $service) {
|
||||||
$uuid = (string) new Cuid2;
|
$uuid = (string) new Cuid2;
|
||||||
$newService = $service->replicate([
|
$newService = $service->replicate([
|
||||||
@@ -246,25 +312,65 @@ class CloneMe extends Component
|
|||||||
'destination_id' => $this->selectedDestination,
|
'destination_id' => $this->selectedDestination,
|
||||||
]);
|
]);
|
||||||
$newService->save();
|
$newService->save();
|
||||||
|
|
||||||
|
$tags = $service->tags;
|
||||||
|
foreach ($tags as $tag) {
|
||||||
|
$newService->tags()->attach($tag->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$scheduledTasks = $service->scheduled_tasks()->get();
|
||||||
|
foreach ($scheduledTasks as $task) {
|
||||||
|
$newTask = $task->replicate([
|
||||||
|
'id',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
])->fill([
|
||||||
|
'uuid' => (string) new Cuid2,
|
||||||
|
'service_id' => $newService->id,
|
||||||
|
'team_id' => currentTeam()->id,
|
||||||
|
]);
|
||||||
|
$newTask->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
$environmentVariables = $service->environment_variables()->get();
|
||||||
|
foreach ($environmentVariables as $environmentVariable) {
|
||||||
|
$newEnvironmentVariable = $environmentVariable->replicate([
|
||||||
|
'id',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
])->fill([
|
||||||
|
'resourceable_id' => $newService->id,
|
||||||
|
'resourceable_type' => $newService->getMorphClass(),
|
||||||
|
]);
|
||||||
|
$newEnvironmentVariable->save();
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($newService->applications() as $application) {
|
foreach ($newService->applications() as $application) {
|
||||||
$application->update([
|
$application->update([
|
||||||
'status' => 'exited',
|
'status' => 'exited',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($newService->databases() as $database) {
|
foreach ($newService->databases() as $database) {
|
||||||
$database->update([
|
$database->update([
|
||||||
'status' => 'exited',
|
'status' => 'exited',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$newService->parse();
|
$newService->parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect()->route('project.resource.index', [
|
|
||||||
'project_uuid' => $project->uuid,
|
|
||||||
'environment_uuid' => $environment->uuid,
|
|
||||||
]);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return handleError($e, $this);
|
handleError($e, $this);
|
||||||
|
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
if (! isset($e)) {
|
||||||
|
return redirect()->route('project.resource.index', [
|
||||||
|
'project_uuid' => $project->uuid,
|
||||||
|
'environment_uuid' => $environment->uuid,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user