refactor: clone project

This commit is contained in:
Andras Bacsai 2023-12-13 14:22:23 +01:00
parent e0289e2949
commit 52d84c5e9e
2 changed files with 35 additions and 31 deletions

View File

@ -19,12 +19,14 @@ class CloneProject extends Component
public $servers;
public ?Environment $environment = null;
public ?int $selectedServer = null;
public ?int $selectedDestination = null;
public ?Server $server = null;
public $resources = [];
public string $newProjectName = '';
protected $messages = [
'selectedServer' => 'Please select a server.',
'selectedDestination' => 'Please select a server & destination.',
'newProjectName' => 'Please enter a name for the new project.',
];
public function mount($project_uuid)
@ -34,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 = $this->project->name . ' (clone)';
$this->newProjectName = str($this->project->name . '-clone-' . (string)new Cuid2(7))->slug();
}
public function render()
@ -42,9 +44,10 @@ public function render()
return view('livewire.project.clone-project');
}
public function selectServer($server_id)
public function selectServer($server_id, $destination_id)
{
$this->selectedServer = $server_id;
$this->selectedDestination = $destination_id;
$this->server = $this->servers->where('id', $server_id)->first();
}
@ -52,7 +55,7 @@ public function clone()
{
try {
$this->validate([
'selectedServer' => 'required',
'selectedDestination' => 'required',
'newProjectName' => 'required',
]);
$foundProject = Project::where('name', $this->newProjectName)->first();
@ -81,7 +84,8 @@ public function clone()
'fqdn' => generateFqdn($this->server, $uuid),
'status' => 'exited',
'environment_id' => $newEnvironment->id,
'destination_id' => $this->selectedServer,
// This is not correct, but we need to set it to something
'destination_id' => $this->selectedDestination,
]);
$newApplication->save();
$environmentVaribles = $application->environment_variables()->get();
@ -107,7 +111,7 @@ public function clone()
'status' => 'exited',
'started_at' => null,
'environment_id' => $newEnvironment->id,
'destination_id' => $this->selectedServer,
'destination_id' => $this->selectedDestination,
]);
$newDatabase->save();
$environmentVaribles = $database->environment_variables()->get();
@ -133,7 +137,7 @@ public function clone()
$newService = $service->replicate()->fill([
'uuid' => $uuid,
'environment_id' => $newEnvironment->id,
'destination_id' => $this->selectedServer,
'destination_id' => $this->selectedDestination,
]);
$newService->save();
foreach ($newService->applications() as $application) {

View File

@ -1,36 +1,36 @@
<form wire:submit='clone'>
<div class="flex flex-col">
<div class="flex gap-2">
<h1>Clone</h1>
<x-forms.button type="submit">Clone to a New Project</x-forms.button>
</div>
<div class="subtitle ">Quickly clone a project</div>
<h1>Clone</h1>
<div class="subtitle ">Quickly clone all resources to a new project</div>
</div>
<div class="flex items-end gap-2">
<x-forms.input required id="newProjectName" label="New Project Name" />
<x-forms.button type="submit">Clone</x-forms.button>
</div>
<x-forms.input required id="newProjectName" label="New Project Name" />
<h3 class="pt-4 pb-2">Servers</h3>
<div class="grid gap-2 lg:grid-cols-3">
@foreach ($servers as $srv)
<div wire:click="selectServer('{{ $srv->id }}')"
class="cursor-pointer box-without-bg bg-coolgray-200 group"
:class="'{{ $selectedServer === $srv->id }}' && 'bg-coollabs'">
<div class="flex flex-col mx-6">
<div :class="'{{ $selectedServer === $srv->id }}' && 'text-white'"> {{ $srv->name }}</div>
@isset($selectedServer)
<div :class="'{{ $selectedServer === $srv->id }}' && 'text-white pt-2 text-xs font-bold'">
{{ $srv->description }}</div>
@else
<div class="description">
{{ $srv->description }}</div>
@endisset
<div class="flex flex-col gap-4">
@foreach ($servers->sortBy('id') as $server)
<div class="p-4 border border-coolgray-500">
<h3>{{ $server->name }}</h3>
<h5>{{ $server->description }}</h5>
<div class="pt-4 pb-2">Docker Networks</div>
<div class="grid grid-cols-1 gap-2 pb-4 lg:grid-cols-4">
@foreach ($server->destinations() as $destination)
<div class="cursor-pointer box-without-bg bg-coolgray-200 group"
:class="'{{ $selectedDestination === $destination->id }}' && 'bg-coollabs text-white'"
wire:click="selectServer('{{ $server->id }}', '{{ $destination->id }}')">
{{ $destination->name }}
</div>
@endforeach
</div>
</div>
@endforeach
</div>
<h3 class="pt-4 pb-2">Resources</h3>
<div class="grid grid-cols-1 gap-2">
<div class="grid grid-cols-1 gap-2 p-4 border border-coolgray-500">
@foreach ($environment->applications->sortBy('name') as $application)
<div class="p-2 border border-coolgray-200">
<div>
<div class="flex flex-col">
<div class="font-bold text-white">{{ $application->name }}</div>
<div class="description">{{ $application->description }}</div>
@ -38,7 +38,7 @@ class="cursor-pointer box-without-bg bg-coolgray-200 group"
</div>
@endforeach
@foreach ($environment->databases()->sortBy('name') as $database)
<div class="p-2 border border-coolgray-200">
<div>
<div class="flex flex-col">
<div class="font-bold text-white">{{ $database->name }}</div>
<div class="description">{{ $database->description }}</div>
@ -46,7 +46,7 @@ class="cursor-pointer box-without-bg bg-coolgray-200 group"
</div>
@endforeach
@foreach ($environment->services->sortBy('name') as $service)
<div class="p-2 border border-coolgray-200">
<div>
<div class="flex flex-col">
<div class="font-bold text-white">{{ $service->name }}</div>
<div class="description">{{ $service->description }}</div>