2023-10-20 10:34:53 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project;
|
2023-10-20 10:34:53 +00:00
|
|
|
|
|
|
|
use App\Models\Environment;
|
|
|
|
use App\Models\Project;
|
|
|
|
use App\Models\Server;
|
|
|
|
use Livewire\Component;
|
|
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
|
|
|
|
class CloneProject extends Component
|
|
|
|
{
|
|
|
|
public string $project_uuid;
|
|
|
|
public string $environment_name;
|
|
|
|
public int $project_id;
|
|
|
|
|
|
|
|
public Project $project;
|
|
|
|
public $environments;
|
|
|
|
public $servers;
|
|
|
|
public ?Environment $environment = null;
|
|
|
|
public ?int $selectedServer = null;
|
2023-12-13 13:22:23 +00:00
|
|
|
public ?int $selectedDestination = null;
|
2023-10-20 10:34:53 +00:00
|
|
|
public ?Server $server = null;
|
|
|
|
public $resources = [];
|
|
|
|
public string $newProjectName = '';
|
|
|
|
|
|
|
|
protected $messages = [
|
|
|
|
'selectedServer' => 'Please select a server.',
|
2023-12-13 13:22:23 +00:00
|
|
|
'selectedDestination' => 'Please select a server & destination.',
|
2023-10-20 10:34:53 +00:00
|
|
|
'newProjectName' => 'Please enter a name for the new project.',
|
|
|
|
];
|
|
|
|
public function mount($project_uuid)
|
|
|
|
{
|
|
|
|
$this->project_uuid = $project_uuid;
|
|
|
|
$this->project = Project::where('uuid', $project_uuid)->firstOrFail();
|
|
|
|
$this->environment = $this->project->environments->where('name', $this->environment_name)->first();
|
|
|
|
$this->project_id = $this->project->id;
|
|
|
|
$this->servers = currentTeam()->servers;
|
2023-12-13 13:22:23 +00:00
|
|
|
$this->newProjectName = str($this->project->name . '-clone-' . (string)new Cuid2(7))->slug();
|
2023-10-20 10:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.project.clone-project');
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:22:23 +00:00
|
|
|
public function selectServer($server_id, $destination_id)
|
2023-10-20 10:34:53 +00:00
|
|
|
{
|
|
|
|
$this->selectedServer = $server_id;
|
2023-12-13 13:22:23 +00:00
|
|
|
$this->selectedDestination = $destination_id;
|
2023-10-20 10:34:53 +00:00
|
|
|
$this->server = $this->servers->where('id', $server_id)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clone()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->validate([
|
2023-12-13 13:22:23 +00:00
|
|
|
'selectedDestination' => 'required',
|
2023-10-20 10:34:53 +00:00
|
|
|
'newProjectName' => 'required',
|
|
|
|
]);
|
2023-10-24 08:10:45 +00:00
|
|
|
$foundProject = Project::where('name', $this->newProjectName)->first();
|
|
|
|
if ($foundProject) {
|
|
|
|
throw new \Exception('Project with the same name already exists.');
|
|
|
|
}
|
2023-10-20 10:34:53 +00:00
|
|
|
$newProject = Project::create([
|
|
|
|
'name' => $this->newProjectName,
|
|
|
|
'team_id' => currentTeam()->id,
|
|
|
|
'description' => $this->project->description . ' (clone)',
|
|
|
|
]);
|
2023-10-24 08:10:45 +00:00
|
|
|
if ($this->environment->name !== 'production') {
|
2023-10-20 10:34:53 +00:00
|
|
|
$newProject->environments()->create([
|
|
|
|
'name' => $this->environment->name,
|
|
|
|
]);
|
|
|
|
}
|
2023-10-24 08:10:45 +00:00
|
|
|
$newEnvironment = $newProject->environments->where('name', $this->environment->name)->first();
|
2023-10-20 10:34:53 +00:00
|
|
|
// Clone Applications
|
|
|
|
$applications = $this->environment->applications;
|
|
|
|
$databases = $this->environment->databases();
|
|
|
|
$services = $this->environment->services;
|
|
|
|
foreach ($applications as $application) {
|
|
|
|
$uuid = (string)new Cuid2(7);
|
|
|
|
$newApplication = $application->replicate()->fill([
|
|
|
|
'uuid' => $uuid,
|
|
|
|
'fqdn' => generateFqdn($this->server, $uuid),
|
|
|
|
'status' => 'exited',
|
|
|
|
'environment_id' => $newEnvironment->id,
|
2023-12-13 13:22:23 +00:00
|
|
|
// This is not correct, but we need to set it to something
|
|
|
|
'destination_id' => $this->selectedDestination,
|
2023-10-20 10:34:53 +00:00
|
|
|
]);
|
|
|
|
$newApplication->save();
|
|
|
|
$environmentVaribles = $application->environment_variables()->get();
|
|
|
|
foreach ($environmentVaribles as $environmentVarible) {
|
|
|
|
$newEnvironmentVariable = $environmentVarible->replicate()->fill([
|
|
|
|
'application_id' => $newApplication->id,
|
|
|
|
]);
|
|
|
|
$newEnvironmentVariable->save();
|
|
|
|
}
|
|
|
|
$persistentVolumes = $application->persistentStorages()->get();
|
|
|
|
foreach ($persistentVolumes as $volume) {
|
|
|
|
$newPersistentVolume = $volume->replicate()->fill([
|
|
|
|
'name' => $newApplication->uuid . '-' . str($volume->name)->afterLast('-'),
|
|
|
|
'resource_id' => $newApplication->id,
|
|
|
|
]);
|
|
|
|
$newPersistentVolume->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($databases as $database) {
|
|
|
|
$uuid = (string)new Cuid2(7);
|
|
|
|
$newDatabase = $database->replicate()->fill([
|
|
|
|
'uuid' => $uuid,
|
2023-11-07 11:11:47 +00:00
|
|
|
'status' => 'exited',
|
2023-11-08 08:07:30 +00:00
|
|
|
'started_at' => null,
|
2023-10-20 10:34:53 +00:00
|
|
|
'environment_id' => $newEnvironment->id,
|
2023-12-13 13:22:23 +00:00
|
|
|
'destination_id' => $this->selectedDestination,
|
2023-10-20 10:34:53 +00:00
|
|
|
]);
|
|
|
|
$newDatabase->save();
|
|
|
|
$environmentVaribles = $database->environment_variables()->get();
|
|
|
|
foreach ($environmentVaribles as $environmentVarible) {
|
|
|
|
$payload = [];
|
2023-11-07 11:11:47 +00:00
|
|
|
if ($database->type() === 'standalone-postgresql') {
|
2023-10-20 10:34:53 +00:00
|
|
|
$payload['standalone_postgresql_id'] = $newDatabase->id;
|
2023-11-07 11:11:47 +00:00
|
|
|
} else if ($database->type() === 'standalone-redis') {
|
2023-10-20 10:34:53 +00:00
|
|
|
$payload['standalone_redis_id'] = $newDatabase->id;
|
2023-11-07 11:11:47 +00:00
|
|
|
} else if ($database->type() === 'standalone-mongodb') {
|
2023-10-20 10:34:53 +00:00
|
|
|
$payload['standalone_mongodb_id'] = $newDatabase->id;
|
2023-11-07 11:11:47 +00:00
|
|
|
} else if ($database->type() === 'standalone-mysql') {
|
2023-10-24 12:31:28 +00:00
|
|
|
$payload['standalone_mysql_id'] = $newDatabase->id;
|
2023-11-07 11:11:47 +00:00
|
|
|
} else if ($database->type() === 'standalone-mariadb') {
|
2023-10-24 12:31:28 +00:00
|
|
|
$payload['standalone_mariadb_id'] = $newDatabase->id;
|
2023-10-20 10:34:53 +00:00
|
|
|
}
|
|
|
|
$newEnvironmentVariable = $environmentVarible->replicate()->fill($payload);
|
|
|
|
$newEnvironmentVariable->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($services as $service) {
|
|
|
|
$uuid = (string)new Cuid2(7);
|
|
|
|
$newService = $service->replicate()->fill([
|
|
|
|
'uuid' => $uuid,
|
|
|
|
'environment_id' => $newEnvironment->id,
|
2023-12-13 13:22:23 +00:00
|
|
|
'destination_id' => $this->selectedDestination,
|
2023-10-20 10:34:53 +00:00
|
|
|
]);
|
|
|
|
$newService->save();
|
2023-11-07 11:11:47 +00:00
|
|
|
foreach ($newService->applications() as $application) {
|
|
|
|
$application->update([
|
|
|
|
'status' => 'exited',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
foreach ($newService->databases() as $database) {
|
|
|
|
$database->update([
|
|
|
|
'status' => 'exited',
|
|
|
|
]);
|
|
|
|
}
|
2023-10-20 10:34:53 +00:00
|
|
|
$newService->parse();
|
|
|
|
}
|
2023-12-07 21:56:55 +00:00
|
|
|
return $this->redirectRoute('project.resources', [
|
2023-10-20 10:34:53 +00:00
|
|
|
'project_uuid' => $newProject->uuid,
|
|
|
|
'environment_name' => $newEnvironment->name,
|
2023-12-07 21:56:55 +00:00
|
|
|
], navigate: true);
|
2023-10-20 10:34:53 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|