coolify/app/Livewire/Project/Shared/ResourceOperations.php

216 lines
8.9 KiB
PHP
Raw Normal View History

2024-01-22 15:08:18 +00:00
<?php
namespace App\Livewire\Project\Shared;
2025-01-07 13:52:08 +00:00
use App\Models\Application;
2024-01-22 15:08:18 +00:00
use App\Models\Environment;
use App\Models\Project;
2025-01-07 13:52:08 +00:00
use App\Models\StandaloneClickhouse;
2024-01-22 15:08:18 +00:00
use App\Models\StandaloneDocker;
2025-01-07 13:52:08 +00:00
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;
2024-01-22 15:08:18 +00:00
use App\Models\SwarmDocker;
use Livewire\Component;
2025-01-07 13:52:08 +00:00
use Throwable;
2024-01-22 15:08:18 +00:00
use Visus\Cuid2\Cuid2;
class ResourceOperations extends Component
{
public $resource;
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public $projectUuid;
2024-06-10 20:43:34 +00:00
2024-11-22 15:03:20 +00:00
public $environmentUuid;
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public $projects;
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public $servers;
public function mount()
{
$parameters = get_route_parameters();
2024-01-31 13:22:48 +00:00
$this->projectUuid = data_get($parameters, 'project_uuid');
2024-11-22 15:03:20 +00:00
$this->environmentUuid = data_get($parameters, 'environment_uuid');
2024-01-22 15:08:18 +00:00
$this->projects = Project::ownedByCurrentTeam()->get();
$this->servers = currentTeam()->servers;
}
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public function cloneTo($destination_id)
{
2025-01-07 13:52:08 +00:00
$new_destination = StandaloneDocker::query()->find($destination_id);
2024-06-10 20:43:34 +00:00
if (! $new_destination) {
2025-01-07 13:52:08 +00:00
$new_destination = SwarmDocker::query()->find($destination_id);
2024-01-22 15:08:18 +00:00
}
2024-06-10 20:43:34 +00:00
if (! $new_destination) {
2024-01-22 15:08:18 +00:00
return $this->addError('destination_id', 'Destination not found.');
}
2024-07-25 11:31:59 +00:00
$uuid = (string) new Cuid2;
2024-01-22 15:08:18 +00:00
$server = $new_destination->server;
2025-01-07 13:52:08 +00:00
if ($this->resource->getMorphClass() === Application::class) {
$name = 'clone-of-'.str($this->resource->name)->limit(20).'-'.$uuid;
2024-01-22 15:08:18 +00:00
$new_resource = $this->resource->replicate()->fill([
'uuid' => $uuid,
'name' => $name,
2024-01-22 15:08:18 +00:00
'fqdn' => generateFqdn($server, $uuid),
'status' => 'exited',
'destination_id' => $new_destination->id,
]);
$new_resource->save();
2024-03-11 14:08:05 +00:00
if ($new_resource->destination->server->proxyType() !== 'NONE') {
2024-06-11 09:36:42 +00:00
$customLabels = str(implode('|coolify|', generateLabelsApplication($new_resource)))->replace('|coolify|', "\n");
$new_resource->custom_labels = base64_encode($customLabels);
$new_resource->save();
}
2024-01-22 15:08:18 +00:00
$environmentVaribles = $this->resource->environment_variables()->get();
foreach ($environmentVaribles as $environmentVarible) {
$newEnvironmentVariable = $environmentVarible->replicate()->fill([
'resourceable_id' => $new_resource->id,
'resourceable_type' => $new_resource->getMorphClass(),
2024-01-22 15:08:18 +00:00
]);
$newEnvironmentVariable->save();
}
$persistentVolumes = $this->resource->persistentStorages()->get();
2025-01-07 13:52:08 +00:00
foreach ($persistentVolumes as $persistentVolume) {
$volumeName = str($persistentVolume->name)->replace($this->resource->uuid, $new_resource->uuid)->value();
if ($volumeName === $persistentVolume->name) {
$volumeName = $new_resource->uuid.'-'.str($persistentVolume->name)->afterLast('-');
}
2025-01-07 13:52:08 +00:00
$newPersistentVolume = $persistentVolume->replicate()->fill([
'name' => $volumeName,
2024-01-22 15:08:18 +00:00
'resource_id' => $new_resource->id,
]);
$newPersistentVolume->save();
}
$route = route('project.application.configuration', [
'project_uuid' => $this->projectUuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this->environmentUuid,
2024-01-22 15:08:18 +00:00
'application_uuid' => $new_resource->uuid,
2024-06-10 20:43:34 +00:00
]).'#resource-operations';
2024-01-22 15:08:18 +00:00
return redirect()->to($route);
2025-01-07 13:52:08 +00:00
}
if ($this->resource->getMorphClass() === StandalonePostgresql::class ||
$this->resource->getMorphClass() === StandaloneMongodb::class ||
$this->resource->getMorphClass() === StandaloneMysql::class ||
$this->resource->getMorphClass() === StandaloneMariadb::class ||
$this->resource->getMorphClass() === StandaloneRedis::class ||
$this->resource->getMorphClass() === StandaloneKeydb::class ||
$this->resource->getMorphClass() === StandaloneDragonfly::class ||
$this->resource->getMorphClass() === StandaloneClickhouse::class) {
2024-07-25 11:31:59 +00:00
$uuid = (string) new Cuid2;
2024-01-22 15:08:18 +00:00
$new_resource = $this->resource->replicate()->fill([
'uuid' => $uuid,
2024-06-10 20:43:34 +00:00
'name' => $this->resource->name.'-clone-'.$uuid,
2024-01-22 15:08:18 +00:00
'status' => 'exited',
'started_at' => null,
'destination_id' => $new_destination->id,
]);
$new_resource->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;
2024-06-10 20:43:34 +00:00
} elseif ($this->resource->type() === 'standalone-redis') {
2024-01-22 15:08:18 +00:00
$payload['standalone_redis_id'] = $new_resource->id;
2024-06-10 20:43:34 +00:00
} elseif ($this->resource->type() === 'standalone-mongodb') {
2024-01-22 15:08:18 +00:00
$payload['standalone_mongodb_id'] = $new_resource->id;
2024-06-10 20:43:34 +00:00
} elseif ($this->resource->type() === 'standalone-mysql') {
2024-01-22 15:08:18 +00:00
$payload['standalone_mysql_id'] = $new_resource->id;
2024-06-10 20:43:34 +00:00
} elseif ($this->resource->type() === 'standalone-mariadb') {
2024-01-22 15:08:18 +00:00
$payload['standalone_mariadb_id'] = $new_resource->id;
}
2024-06-10 20:43:34 +00:00
$newEnvironmentVariable = $environmentVarible->replicate()->fill($payload);
2024-01-22 15:08:18 +00:00
$newEnvironmentVariable->save();
}
$route = route('project.database.configuration', [
'project_uuid' => $this->projectUuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this->environmentUuid,
2024-01-22 15:08:18 +00:00
'database_uuid' => $new_resource->uuid,
2024-06-10 20:43:34 +00:00
]).'#resource-operations';
2024-01-22 15:08:18 +00:00
return redirect()->to($route);
2025-01-07 13:52:08 +00:00
}
if ($this->resource->type() === 'service') {
2024-07-25 11:31:59 +00:00
$uuid = (string) new Cuid2;
2024-01-22 15:08:18 +00:00
$new_resource = $this->resource->replicate()->fill([
'uuid' => $uuid,
2024-06-10 20:43:34 +00:00
'name' => $this->resource->name.'-clone-'.$uuid,
2024-01-22 15:08:18 +00:00
'destination_id' => $new_destination->id,
]);
$new_resource->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,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this->environmentUuid,
2024-01-22 15:08:18 +00:00
'service_uuid' => $new_resource->uuid,
2024-06-10 20:43:34 +00:00
]).'#resource-operations';
2024-01-22 15:08:18 +00:00
return redirect()->to($route);
}
2025-01-07 13:52:08 +00:00
return null;
2024-01-22 15:08:18 +00:00
}
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public function moveTo($environment_id)
{
try {
2025-01-07 13:52:08 +00:00
$new_environment = Environment::query()->findOrFail($environment_id);
2024-01-22 15:08:18 +00:00
$this->resource->update([
2024-06-10 20:43:34 +00:00
'environment_id' => $environment_id,
2024-01-22 15:08:18 +00:00
]);
if ($this->resource->type() === 'application') {
$route = route('project.application.configuration', [
'project_uuid' => $new_environment->project->uuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $new_environment->uuid,
2024-01-22 15:08:18 +00:00
'application_uuid' => $this->resource->uuid,
2024-06-10 20:43:34 +00:00
]).'#resource-operations';
2024-01-22 15:08:18 +00:00
return redirect()->to($route);
2025-01-07 13:52:08 +00:00
}
if (str($this->resource->type())->startsWith('standalone-')) {
2024-01-22 15:08:18 +00:00
$route = route('project.database.configuration', [
'project_uuid' => $new_environment->project->uuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $new_environment->uuid,
2024-01-22 15:08:18 +00:00
'database_uuid' => $this->resource->uuid,
2024-06-10 20:43:34 +00:00
]).'#resource-operations';
2024-01-22 15:08:18 +00:00
return redirect()->to($route);
2025-01-07 13:52:08 +00:00
}
if ($this->resource->type() === 'service') {
2024-01-22 15:08:18 +00:00
$route = route('project.service.configuration', [
'project_uuid' => $new_environment->project->uuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $new_environment->uuid,
2024-01-22 15:08:18 +00:00
'service_uuid' => $this->resource->uuid,
2024-06-10 20:43:34 +00:00
]).'#resource-operations';
2024-01-22 15:08:18 +00:00
return redirect()->to($route);
}
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2024-01-22 15:08:18 +00:00
return handleError($e, $this);
}
2025-01-07 13:52:08 +00:00
return null;
2024-01-22 15:08:18 +00:00
}
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public function render()
{
return view('livewire.project.shared.resource-operations');
}
}