From ea5101c81498e97b799870cef69008f4065766a2 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 5 Aug 2024 20:00:57 +0200 Subject: [PATCH] fix: file storages (dir/file mount) handled properly --- app/Events/FileStorageChanged.php | 32 +++++++++++++++++ app/Livewire/Project/Application/General.php | 2 +- app/Livewire/Project/Service/FileStorage.php | 19 ++++++++--- app/Livewire/Project/Service/Storage.php | 25 ++++++++++++-- app/Livewire/Project/Shared/Storages/Add.php | 4 +-- app/Livewire/Project/Shared/Storages/All.php | 2 +- app/Livewire/Project/Shared/Storages/Show.php | 2 +- app/Models/LocalFileVolume.php | 34 ++++++++++++------- .../project/service/file-storage.blade.php | 22 +++++++++--- .../project/service/storage.blade.php | 10 +++--- 10 files changed, 118 insertions(+), 34 deletions(-) create mode 100644 app/Events/FileStorageChanged.php diff --git a/app/Events/FileStorageChanged.php b/app/Events/FileStorageChanged.php new file mode 100644 index 000000000..27fdc6b5c --- /dev/null +++ b/app/Events/FileStorageChanged.php @@ -0,0 +1,32 @@ +teamId = $teamId; + } + + public function broadcastOn(): array + { + return [ + new PrivateChannel("team.{$this->teamId}"), + ]; + } +} diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 395c45524..77593bf0a 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -214,7 +214,7 @@ public function loadComposeFile($isInit = false) } $this->dispatch('success', 'Docker compose file loaded.'); $this->dispatch('compose_loaded'); - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); $this->dispatch('refreshEnvs'); } catch (\Throwable $e) { $this->application->docker_compose_location = $this->initialDockerComposeLocation; diff --git a/app/Livewire/Project/Service/FileStorage.php b/app/Livewire/Project/Service/FileStorage.php index 2eea0891f..2d9c95daa 100644 --- a/app/Livewire/Project/Service/FileStorage.php +++ b/app/Livewire/Project/Service/FileStorage.php @@ -26,6 +26,8 @@ class FileStorage extends Component public ?string $workdir = null; + public bool $permanently_delete = true; + protected $rules = [ 'fileStorage.is_directory' => 'required', 'fileStorage.fs_path' => 'required', @@ -56,7 +58,7 @@ public function convertToDirectory() } catch (\Throwable $e) { return handleError($e, $this); } finally { - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } } @@ -71,20 +73,27 @@ public function convertToFile() } catch (\Throwable $e) { return handleError($e, $this); } finally { - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } } public function delete() { try { - $this->fileStorage->deleteStorageOnServer(); + $message = 'File deleted.'; + if ($this->fileStorage->is_directory) { + $message = 'Directory deleted.'; + } + if ($this->permanently_delete) { + $message = 'Directory deleted from the server.'; + $this->fileStorage->deleteStorageOnServer(); + } $this->fileStorage->delete(); - $this->dispatch('success', 'File deleted.'); + $this->dispatch('success', $message); } catch (\Throwable $e) { return handleError($e, $this); } finally { - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } } diff --git a/app/Livewire/Project/Service/Storage.php b/app/Livewire/Project/Service/Storage.php index 161c38097..beccb2677 100644 --- a/app/Livewire/Project/Service/Storage.php +++ b/app/Livewire/Project/Service/Storage.php @@ -9,14 +9,35 @@ class Storage extends Component { public $resource; + public $fileStorage; + public function getListeners() { + $teamId = auth()->user()->currentTeam()->id; + return [ + "echo-private:team.{$teamId},FileStorageChanged" => 'refreshStoragesFromEvent', + 'refreshStorages' => '$refresh', 'addNewVolume', - 'refresh_storages' => '$refresh', ]; } + public function mount() + { + $this->refreshStorages(); + } + + public function refreshStoragesFromEvent() + { + $this->refreshStorages(); + $this->dispatch('warning', 'File storage changed. Usually it means that the file / directory is already defined on the server, so Coolify set it up for you properly on the UI.'); + } + + public function refreshStorages() + { + $this->fileStorage = $this->resource->fileStorages()->get(); + } + public function addNewVolume($data) { try { @@ -30,7 +51,7 @@ public function addNewVolume($data) $this->resource->refresh(); $this->dispatch('success', 'Storage added successfully'); $this->dispatch('clearAddStorage'); - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } catch (\Throwable $e) { return handleError($e, $this); } diff --git a/app/Livewire/Project/Shared/Storages/Add.php b/app/Livewire/Project/Shared/Storages/Add.php index c576e2eb9..27e0c6e44 100644 --- a/app/Livewire/Project/Shared/Storages/Add.php +++ b/app/Livewire/Project/Shared/Storages/Add.php @@ -96,7 +96,7 @@ public function submitFileStorage() 'resource_type' => get_class($this->resource), ], ); - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } catch (\Throwable $e) { return handleError($e, $this); } @@ -123,7 +123,7 @@ public function submitFileStorageDirectory() 'resource_type' => get_class($this->resource), ], ); - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } catch (\Throwable $e) { return handleError($e, $this); } diff --git a/app/Livewire/Project/Shared/Storages/All.php b/app/Livewire/Project/Shared/Storages/All.php index d2014694e..c26315d3b 100644 --- a/app/Livewire/Project/Shared/Storages/All.php +++ b/app/Livewire/Project/Shared/Storages/All.php @@ -8,5 +8,5 @@ class All extends Component { public $resource; - protected $listeners = ['refresh_storages' => '$refresh']; + protected $listeners = ['refreshStorages' => '$refresh']; } diff --git a/app/Livewire/Project/Shared/Storages/Show.php b/app/Livewire/Project/Shared/Storages/Show.php index b64cbfa11..08f51ce08 100644 --- a/app/Livewire/Project/Shared/Storages/Show.php +++ b/app/Livewire/Project/Shared/Storages/Show.php @@ -39,6 +39,6 @@ public function submit() public function delete() { $this->storage->delete(); - $this->dispatch('refresh_storages'); + $this->dispatch('refreshStorages'); } } diff --git a/app/Models/LocalFileVolume.php b/app/Models/LocalFileVolume.php index decfb1a8d..a436f5797 100644 --- a/app/Models/LocalFileVolume.php +++ b/app/Models/LocalFileVolume.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\FileStorageChanged; use Illuminate\Database\Eloquent\Factories\HasFactory; class LocalFileVolume extends BaseModel @@ -33,16 +34,23 @@ public function deleteStorageOnServer() $workdir = $this->resource->workdir(); $server = $this->resource->destination->server; } - $commands = collect([ - "cd $workdir", - ]); + $commands = collect([]); $fs_path = data_get($this, 'fs_path'); + $isFile = instant_remote_process(["test -f $fs_path && echo OK || echo NOK"], $server); + $isDir = instant_remote_process(["test -d $fs_path && echo OK || echo NOK"], $server); if ($fs_path && $fs_path != '/' && $fs_path != '.' && $fs_path != '..') { - $commands->push("rm -rf $fs_path"); - } - ray($commands); + ray($isFile, $isDir); + if ($isFile === 'OK') { + $commands->push("rm -rf $fs_path > /dev/null 2>&1 || true"); - return instant_remote_process($commands, $server); + } elseif ($isDir === 'OK') { + $commands->push("rm -rf $fs_path > /dev/null 2>&1 || true"); + $commands->push("rmdir $fs_path > /dev/null 2>&1 || true"); + } + } + if ($commands->count() > 0) { + return instant_remote_process($commands, $server); + } } public function saveStorageOnServer() @@ -55,13 +63,10 @@ public function saveStorageOnServer() $workdir = $this->resource->workdir(); $server = $this->resource->destination->server; } - $commands = collect([ - "mkdir -p $workdir > /dev/null 2>&1 || true", - "cd $workdir", - ]); - $is_directory = $this->is_directory; - if ($is_directory) { + $commands = collect([]); + if ($this->is_directory) { $commands->push("mkdir -p $this->fs_path > /dev/null 2>&1 || true"); + $commands->push("cd $workdir"); } if (str($this->fs_path)->startsWith('.') || str($this->fs_path)->startsWith('/') || str($this->fs_path)->startsWith('~')) { $parent_dir = str($this->fs_path)->beforeLast('/'); @@ -79,8 +84,11 @@ public function saveStorageOnServer() $isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server); $isDir = instant_remote_process(["test -d $path && echo OK || echo NOK"], $server); if ($isFile == 'OK' && $fileVolume->is_directory) { + $content = instant_remote_process(["cat $path"], $server, false); $fileVolume->is_directory = false; + $fileVolume->content = $content; $fileVolume->save(); + FileStorageChanged::dispatch(data_get($server, 'team_id')); throw new \Exception('The following file is a file on the server, but you are trying to mark it as a directory. Please delete the file on the server or mark it as directory.'); } elseif ($isDir == 'OK' && ! $fileVolume->is_directory) { $fileVolume->is_directory = true; diff --git a/resources/views/livewire/project/service/file-storage.blade.php b/resources/views/livewire/project/service/file-storage.blade.php index 12e4901b4..5680c94b3 100644 --- a/resources/views/livewire/project/service/file-storage.blade.php +++ b/resources/views/livewire/project/service/file-storage.blade.php @@ -14,16 +14,30 @@
@if ($fileStorage->is_directory) - This will delete all files in this directory. It is not reversible.
Please think again. +
This will delete all files in this directory. It is not reversible. Please think + again.

@else - This will convert this to a directory. If it was a file, it will be deleted. It is not reversible. -
Please think again. +
This will delete the file and make a directory instead. It is not reversible. + Please think + again.

+
@endif - This file / directory will be deleted. It is not reversible.
Please think again. +
This resource will be deleted. It is not reversible. Please think + again.

+

Actions

+ @if ($fileStorage->is_directory) + + @else + + @endif
@if (!$fileStorage->is_directory) diff --git a/resources/views/livewire/project/service/storage.blade.php b/resources/views/livewire/project/service/storage.blade.php index b71d7ea94..3a37f8091 100644 --- a/resources/views/livewire/project/service/storage.blade.php +++ b/resources/views/livewire/project/service/storage.blade.php @@ -25,7 +25,7 @@ Please modify storage layout in your Docker Compose file or reload the compose file to reread the storage layout. @else - @if ($resource->persistentStorages()->get()->count() === 0 && $resource->fileStorages()->get()->count() == 0) + @if ($resource->persistentStorages()->get()->count() === 0 && $fileStorage->count() == 0)
No storage found.
@endif @endif @@ -33,9 +33,9 @@ @if ($resource->persistentStorages()->get()->count() > 0) @endif - @if ($resource->fileStorages()->get()->count() > 0) + @if ($fileStorage->count() > 0)
- @foreach ($resource->fileStorages()->get()->sort() as $fileStorage) + @foreach ($fileStorage->sort() as $fileStorage) @endforeach @@ -48,9 +48,9 @@ @if ($resource->persistentStorages()->get()->count() > 0) @endif - @if ($resource->fileStorages()->get()->count() > 0) + @if ($fileStorage->count() > 0)
- @foreach ($resource->fileStorages()->get()->sort() as $fileStorage) + @foreach ($fileStorage->sort() as $fileStorage) @endforeach