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;
|
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
class CloneMe extends Component
|
2023-10-20 10:34:53 +00:00
|
|
|
{
|
|
|
|
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 = [];
|
2024-02-04 15:54:12 +00:00
|
|
|
public string $newName = '';
|
2023-10-20 10:34:53 +00:00
|
|
|
|
|
|
|
protected $messages = [
|
|
|
|
'selectedServer' => 'Please select a server.',
|
2023-12-13 13:22:23 +00:00
|
|
|
'selectedDestination' => 'Please select a server & destination.',
|
2024-02-04 15:54:12 +00:00
|
|
|
'newName' => 'Please enter a name for the new project or environment.',
|
2023-10-20 10:34:53 +00:00
|
|
|
];
|
|
|
|
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;
|
2024-02-04 15:54:12 +00:00
|
|
|
$this->newName = str($this->project->name . '-clone-' . (string)new Cuid2(7))->slug();
|
2023-10-20 10:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
return view('livewire.project.clone-me');
|
2023-10-20 10:34:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 13:22:23 +00:00
|
|
|
public function selectServer($server_id, $destination_id)
|
2023-10-20 10:34:53 +00:00
|
|
|
{
|
2024-02-04 15:54:12 +00:00
|
|
|
if ($server_id == $this->selectedServer && $destination_id == $this->selectedDestination) {
|
|
|
|
$this->selectedServer = null;
|
|
|
|
$this->selectedDestination = null;
|
|
|
|
$this->server = null;
|
|
|
|
return;
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2024-02-04 15:54:12 +00:00
|
|
|
public function clone(string $type)
|
2023-10-20 10:34:53 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->validate([
|
2023-12-13 13:22:23 +00:00
|
|
|
'selectedDestination' => 'required',
|
2024-02-04 15:54:12 +00:00
|
|
|
'newName' => 'required',
|
2023-10-20 10:34:53 +00:00
|
|
|
]);
|
2024-02-04 15:54:12 +00:00
|
|
|
if ($type === 'project') {
|
|
|
|
$foundProject = Project::where('name', $this->newName)->first();
|
|
|
|
if ($foundProject) {
|
|
|
|
throw new \Exception('Project with the same name already exists.');
|
|
|
|
}
|
|
|
|
$project = Project::create([
|
|
|
|
'name' => $this->newName,
|
|
|
|
'team_id' => currentTeam()->id,
|
|
|
|
'description' => $this->project->description . ' (clone)',
|
|
|
|
]);
|
|
|
|
if ($this->environment->name !== 'production') {
|
|
|
|
$project->environments()->create([
|
|
|
|
'name' => $this->environment->name,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
$environment = $project->environments->where('name', $this->environment->name)->first();
|
|
|
|
} else {
|
|
|
|
$foundEnv = $this->project->environments()->where('name', $this->newName)->first();
|
|
|
|
if ($foundEnv) {
|
|
|
|
throw new \Exception('Environment with the same name already exists.');
|
|
|
|
}
|
|
|
|
$project = $this->project;
|
|
|
|
$environment = $this->project->environments()->create([
|
|
|
|
'name' => $this->newName,
|
2023-10-20 10:34:53 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
$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',
|
2024-02-04 15:54:12 +00:00
|
|
|
'environment_id' => $environment->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,
|
2024-02-04 15:54:12 +00:00
|
|
|
'environment_id' => $environment->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,
|
2024-02-04 15:54:12 +00:00
|
|
|
'environment_id' => $environment->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();
|
|
|
|
}
|
2024-01-07 15:23:41 +00:00
|
|
|
return redirect()->route('project.resource.index', [
|
2024-02-04 15:54:12 +00:00
|
|
|
'project_uuid' => $project->uuid,
|
|
|
|
'environment_name' => $environment->name,
|
2023-12-27 15:45:01 +00:00
|
|
|
]);
|
2023-10-20 10:34:53 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|