From 1d59383c783dec57068c6189049ea693a11f7a21 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sun, 4 Feb 2024 16:54:12 +0100 Subject: [PATCH] feat: clone to env --- app/Livewire/Project/CloneMe.php | 64 ++++++++++++------- resources/css/app.css | 2 +- .../views/livewire/project/clone-me.blade.php | 20 +++--- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/app/Livewire/Project/CloneMe.php b/app/Livewire/Project/CloneMe.php index 993c9516c..29cf0bea8 100644 --- a/app/Livewire/Project/CloneMe.php +++ b/app/Livewire/Project/CloneMe.php @@ -22,12 +22,12 @@ class CloneMe extends Component public ?int $selectedDestination = null; public ?Server $server = null; public $resources = []; - public string $newProjectName = ''; + public string $newName = ''; protected $messages = [ 'selectedServer' => 'Please select a server.', 'selectedDestination' => 'Please select a server & destination.', - 'newProjectName' => 'Please enter a name for the new project.', + 'newName' => 'Please enter a name for the new project or environment.', ]; public function mount($project_uuid) { @@ -36,7 +36,7 @@ public function mount($project_uuid) $this->environment = $this->project->environments->where('name', $this->environment_name)->first(); $this->project_id = $this->project->id; $this->servers = currentTeam()->servers; - $this->newProjectName = str($this->project->name . '-clone-' . (string)new Cuid2(7))->slug(); + $this->newName = str($this->project->name . '-clone-' . (string)new Cuid2(7))->slug(); } public function render() @@ -46,34 +46,50 @@ public function render() public function selectServer($server_id, $destination_id) { + if ($server_id == $this->selectedServer && $destination_id == $this->selectedDestination) { + $this->selectedServer = null; + $this->selectedDestination = null; + $this->server = null; + return; + } $this->selectedServer = $server_id; $this->selectedDestination = $destination_id; $this->server = $this->servers->where('id', $server_id)->first(); } - public function clone() + public function clone(string $type) { try { $this->validate([ 'selectedDestination' => 'required', - 'newProjectName' => 'required', + 'newName' => 'required', ]); - $foundProject = Project::where('name', $this->newProjectName)->first(); - if ($foundProject) { - throw new \Exception('Project with the same name already exists.'); - } - $newProject = Project::create([ - 'name' => $this->newProjectName, - 'team_id' => currentTeam()->id, - 'description' => $this->project->description . ' (clone)', - ]); - if ($this->environment->name !== 'production') { - $newProject->environments()->create([ - 'name' => $this->environment->name, + 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, ]); } - $newEnvironment = $newProject->environments->where('name', $this->environment->name)->first(); - // Clone Applications $applications = $this->environment->applications; $databases = $this->environment->databases(); $services = $this->environment->services; @@ -83,7 +99,7 @@ public function clone() 'uuid' => $uuid, 'fqdn' => generateFqdn($this->server, $uuid), 'status' => 'exited', - 'environment_id' => $newEnvironment->id, + 'environment_id' => $environment->id, // This is not correct, but we need to set it to something 'destination_id' => $this->selectedDestination, ]); @@ -110,7 +126,7 @@ public function clone() 'uuid' => $uuid, 'status' => 'exited', 'started_at' => null, - 'environment_id' => $newEnvironment->id, + 'environment_id' => $environment->id, 'destination_id' => $this->selectedDestination, ]); $newDatabase->save(); @@ -136,7 +152,7 @@ public function clone() $uuid = (string)new Cuid2(7); $newService = $service->replicate()->fill([ 'uuid' => $uuid, - 'environment_id' => $newEnvironment->id, + 'environment_id' => $environment->id, 'destination_id' => $this->selectedDestination, ]); $newService->save(); @@ -153,8 +169,8 @@ public function clone() $newService->parse(); } return redirect()->route('project.resource.index', [ - 'project_uuid' => $newProject->uuid, - 'environment_name' => $newEnvironment->name, + 'project_uuid' => $project->uuid, + 'environment_name' => $environment->name, ]); } catch (\Exception $e) { return handleError($e, $this); diff --git a/resources/css/app.css b/resources/css/app.css index dbde2a2ad..1080a84f4 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -76,7 +76,7 @@ .icon:hover { } .box { - @apply flex p-2 transition-colors cursor-pointer min-h-[4rem] bg-coolgray-100 hover:bg-coollabs-100 hover:text-white hover:no-underline min-w-[24rem]; + @apply flex p-2 transition-colors cursor-pointer min-h-[4rem] bg-coolgray-100 hover:bg-coollabs-100 hover:text-white hover:no-underline; } .box-without-bg { diff --git a/resources/views/livewire/project/clone-me.blade.php b/resources/views/livewire/project/clone-me.blade.php index 948be9a5f..893b5cee5 100644 --- a/resources/views/livewire/project/clone-me.blade.php +++ b/resources/views/livewire/project/clone-me.blade.php @@ -1,19 +1,17 @@ -
+

Clone

-
Quickly clone all resources to a new project
-
-
- - Clone +
Quickly clone all resources to a new project or environment
+ + Clone to a new Project + Clone to a new Environment

Servers

Choose the server and network to clone the resources to.
@foreach ($servers->sortBy('id') as $server)

{{ $server->name }}

-
{{ $server->description }}
Docker Networks
@foreach ($server->destinations() as $destination) @@ -30,9 +28,9 @@

Resources

These will be cloned to the new project
-
+
@foreach ($environment->applications->sortBy('name') as $application) -
+
{{ $application->name }}
{{ $application->description }}
@@ -40,7 +38,7 @@
@endforeach @foreach ($environment->databases()->sortBy('name') as $database) -
+
{{ $database->name }}
{{ $database->description }}
@@ -48,7 +46,7 @@
@endforeach @foreach ($environment->services->sortBy('name') as $service) -
+
{{ $service->name }}
{{ $service->description }}