2024-01-22 15:08:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire\Project\Shared;
|
|
|
|
|
|
|
|
use App\Models\Environment;
|
|
|
|
use App\Models\Project;
|
|
|
|
use App\Models\StandaloneDocker;
|
|
|
|
use App\Models\SwarmDocker;
|
|
|
|
use Livewire\Component;
|
|
|
|
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-01-22 15:08:18 +00:00
|
|
|
public $environmentName;
|
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');
|
|
|
|
$this->environmentName = data_get($parameters, 'environment_name');
|
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)
|
|
|
|
{
|
|
|
|
$new_destination = StandaloneDocker::find($destination_id);
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $new_destination) {
|
2024-01-22 15:08:18 +00:00
|
|
|
$new_destination = SwarmDocker::find($destination_id);
|
|
|
|
}
|
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-06-10 20:43:34 +00:00
|
|
|
$uuid = (string) new Cuid2(7);
|
2024-01-22 15:08:18 +00:00
|
|
|
$server = $new_destination->server;
|
|
|
|
if ($this->resource->getMorphClass() === 'App\Models\Application') {
|
|
|
|
$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
|
|
|
'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");
|
2024-01-31 08:03:54 +00:00
|
|
|
$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([
|
|
|
|
'application_id' => $new_resource->id,
|
|
|
|
]);
|
|
|
|
$newEnvironmentVariable->save();
|
|
|
|
}
|
|
|
|
$persistentVolumes = $this->resource->persistentStorages()->get();
|
|
|
|
foreach ($persistentVolumes as $volume) {
|
|
|
|
$newPersistentVolume = $volume->replicate()->fill([
|
2024-06-10 20:43:34 +00:00
|
|
|
'name' => $new_resource->uuid.'-'.str($volume->name)->afterLast('-'),
|
2024-01-22 15:08:18 +00:00
|
|
|
'resource_id' => $new_resource->id,
|
|
|
|
]);
|
|
|
|
$newPersistentVolume->save();
|
|
|
|
}
|
|
|
|
$route = route('project.application.configuration', [
|
|
|
|
'project_uuid' => $this->projectUuid,
|
|
|
|
'environment_name' => $this->environmentName,
|
|
|
|
'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);
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif (
|
2024-01-22 15:08:18 +00:00
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandalonePostgresql' ||
|
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneMongodb' ||
|
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneMysql' ||
|
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneMariadb' ||
|
2024-04-10 13:00:46 +00:00
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneRedis' ||
|
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneKeydb' ||
|
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneDragonfly' ||
|
|
|
|
$this->resource->getMorphClass() === 'App\Models\StandaloneClickhouse'
|
2024-01-22 15:08:18 +00:00
|
|
|
) {
|
2024-06-10 20:43:34 +00:00
|
|
|
$uuid = (string) new Cuid2(7);
|
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,
|
|
|
|
'environment_name' => $this->environmentName,
|
|
|
|
'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);
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->resource->type() === 'service') {
|
|
|
|
$uuid = (string) new Cuid2(7);
|
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,
|
|
|
|
'environment_name' => $this->environmentName,
|
|
|
|
'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);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
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 {
|
|
|
|
$new_environment = Environment::findOrFail($environment_id);
|
|
|
|
$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,
|
|
|
|
'environment_name' => $new_environment->name,
|
|
|
|
'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);
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif (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,
|
|
|
|
'environment_name' => $new_environment->name,
|
|
|
|
'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);
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->resource->type() === 'service') {
|
2024-01-22 15:08:18 +00:00
|
|
|
$route = route('project.service.configuration', [
|
|
|
|
'project_uuid' => $new_environment->project->uuid,
|
|
|
|
'environment_name' => $new_environment->name,
|
|
|
|
'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);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
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');
|
|
|
|
}
|
|
|
|
}
|