An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix: file/dir based volumes are now read from the server
Andras Bacsai64ce41df0f55
diff --git a/app/Http/Livewire/Project/Service/FileStorage.php b/app/Http/Livewire/Project/Service/FileStorage.phpindex 4341c6f510..e4103f6003 100644--- a/app/Http/Livewire/Project/Service/FileStorage.php+++ b/app/Http/Livewire/Project/Service/FileStorage.php@@ -33,12 +33,20 @@ class FileStorage extends Component}public function submit(){+ $original = $this->fileStorage->getOriginal();try {$this->validate();+ if ($this->fileStorage->is_directory) {+ $this->fileStorage->content = null;+ }$this->fileStorage->save();- $this->service->saveFileVolumes();+ $this->fileStorage->saveStorageOnServer($this->service);+ // ray($this->fileStorage);+ // $this->service->saveFileVolumes();$this->emit('success', 'File updated successfully.');} catch (\Throwable $e) {+ $this->fileStorage->setRawAttributes($original);+ $this->fileStorage->save();return handleError($e, $this);}}diff --git a/app/Http/Livewire/Project/Service/Show.php b/app/Http/Livewire/Project/Service/Show.phpindex 656adff0a0..6f48a13892 100644--- a/app/Http/Livewire/Project/Service/Show.php+++ b/app/Http/Livewire/Project/Service/Show.php@@ -27,8 +27,10 @@ class Show extends Component$service = $this->service->applications()->whereName($this->parameters['service_name'])->first();if ($service) {$this->serviceApplication = $service;+ $this->serviceApplication->getFilesFromServer();} else {$this->serviceDatabase = $this->service->databases()->whereName($this->parameters['service_name'])->first();+ $this->serviceDatabase->getFilesFromServer();}}public function generateDockerCompose()diff --git a/app/Models/LocalFileVolume.php b/app/Models/LocalFileVolume.phpindex 9077a36d5e..ebd64c6a27 100644--- a/app/Models/LocalFileVolume.php+++ b/app/Models/LocalFileVolume.php@@ -3,6 +3,7 @@namespace App\Models;use Illuminate\Database\Eloquent\Factories\HasFactory;+use Illuminate\Support\Str;class LocalFileVolume extends BaseModel{@@ -13,4 +14,40 @@ class LocalFileVolume extends BaseModel{return $this->morphTo('resource');}+ public function saveStorageOnServer(ServiceApplication|ServiceDatabase $service)+ {+ $workdir = $service->service->workdir();+ $server = $service->service->server;+ $commands = collect([+ "mkdir -p $workdir > /dev/null 2>&1 || true",+ "cd $workdir"+ ]);+ $fileVolume = $this;+ $path = Str::of(data_get($fileVolume, 'fs_path'));+ $content = data_get($fileVolume, 'content');+ if ($path->startsWith('.')) {+ $path = $path->after('.');+ $path = $workdir . $path;+ }+ $isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server);+ $isDir = instant_remote_process(["test -d $path && echo OK || echo NOK"], $server);+ ray($path);+ if ($isFile == 'OK' && $fileVolume->is_directory) {+ throw new \Exception("File $path 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.");+ } else if ($isDir == 'OK' && !$fileVolume->is_directory) {+ throw new \Exception("File $path is a directory on the server, but you are trying to mark it as a file. Please delete the directory on the server or mark it as directory.");+ }+ if (($isFile == 'NOK' && !$fileVolume->is_directory) || $isFile == 'OK') {+ $rootDir = Str::of($path)->dirname();+ $commands->push("mkdir -p $rootDir > /dev/null 2>&1 || true");+ $commands->push("touch $path > /dev/null 2>&1 || true");+ if ($content) {+ $content = base64_encode($content);+ $commands->push("echo '$content' | base64 -d > $path");+ }+ } else if ($isDir == 'NOK' && $fileVolume->is_directory) {+ $commands->push("mkdir -p $path > /dev/null 2>&1 || true");+ }+ return instant_remote_process($commands, $server);+ }}diff --git a/app/Models/Service.php b/app/Models/Service.phpindex cc60a8cadd..364e592489 100644--- a/app/Models/Service.php+++ b/app/Models/Service.php@@ -306,7 +306,7 @@ class Service extends BaseModel]);}- $savedService->saveFileVolumes();+ $savedService->getFilesFromServer();}}diff --git a/app/Models/ServiceApplication.php b/app/Models/ServiceApplication.phpindex f835ab753e..58569b6bd1 100644--- a/app/Models/ServiceApplication.php+++ b/app/Models/ServiceApplication.php@@ -36,8 +36,8 @@ class ServiceApplication extends BaseModel);}- public function saveFileVolumes()+ public function getFilesFromServer(){- saveFileVolumesHelper($this);+ getFilesystemVolumesFromServer($this);}}diff --git a/app/Models/ServiceDatabase.php b/app/Models/ServiceDatabase.phpindex 09b2162192..a94bca40a7 100644--- a/app/Models/ServiceDatabase.php+++ b/app/Models/ServiceDatabase.php@@ -26,8 +26,8 @@ class ServiceDatabase extends BaseModel{return $this->morphMany(LocalFileVolume::class, 'resource');}- public function saveFileVolumes()+ public function getFilesFromServer(){- saveFileVolumesHelper($this);+ getFilesystemVolumesFromServer($this);}}diff --git a/bootstrap/helpers/services.php b/bootstrap/helpers/services.phpindex 353e6853b2..56a4445e7a 100644--- a/bootstrap/helpers/services.php+++ b/bootstrap/helpers/services.php@@ -64,40 +64,48 @@ function serviceStatus(Service $service)}return 'exited';}-function saveFileVolumesHelper(ServiceApplication|ServiceDatabase $oneService)+function getFilesystemVolumesFromServer(ServiceApplication|ServiceDatabase $oneService){+ // TODO: make this asynctry {$workdir = $oneService->service->workdir();$server = $oneService->service->server;- $applicationFileVolume = $oneService->fileStorages()->get();+ $fileVolumes = $oneService->fileStorages()->get();$commands = collect(["mkdir -p $workdir > /dev/null 2>&1 || true","cd $workdir"]);- foreach ($applicationFileVolume as $fileVolume) {- $path = Str::of($fileVolume->fs_path);- if ($fileVolume->is_directory) {- $commands->push("test -f $path && rm -f $path > /dev/null 2>&1 || true");- $commands->push("mkdir -p $path > /dev/null 2>&1 || true");- continue;- }- $content = $fileVolume->content;- $dir = $path->beforeLast('/');- if ($dir->startsWith('.')) {- $dir = $dir->after('.');- $dir = $workdir . $dir;+ instant_remote_process($commands, $server);+ foreach ($fileVolumes as $fileVolume) {+ $path = Str::of(data_get($fileVolume, 'fs_path'));+ $content = data_get($fileVolume, 'content');+ $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') {+ $filesystemContent = instant_remote_process(["cat $path"], $server);+ if (base64_encode($filesystemContent) != base64_encode($content)) {+ $fileVolume->content = $filesystemContent;+ $fileVolume->save();+ }+ } else {+ if ($isDir == 'OK') {+ $fileVolume->content = null;+ $fileVolume->is_directory = true;+ $fileVolume->save();+ } else {+ $fileVolume->content = null;+ $fileVolume->is_directory = false;+ $fileVolume->save();+ }}- $content = base64_encode($content);- $commands->push("test -d $path && rm -rf $path > /dev/null 2>&1 || true");- $commands->push("mkdir -p $dir > /dev/null 2>&1 || true");- $commands->push("echo '$content' | base64 -d > $path");}- return instant_remote_process($commands, $server);} catch (\Throwable $e) {return handleError($e);}}-function updateCompose($resource) {+function updateCompose($resource)+{try {$name = data_get($resource, 'name');$dockerComposeRaw = data_get($resource, 'service.docker_compose_raw');@@ -111,7 +119,7 @@ function updateCompose($resource) {$variableName = "SERVICE_FQDN_" . Str::of($resource->name)->upper();ray($variableName);$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();- if ($generatedEnv){+ if ($generatedEnv) {$generatedEnv->value = $resource->fqdn;$generatedEnv->save();}diff --git a/resources/views/livewire/project/service/file-storage.blade.php b/resources/views/livewire/project/service/file-storage.blade.phpindex e38ab5f058..43f55bbf67 100644--- a/resources/views/livewire/project/service/file-storage.blade.php+++ b/resources/views/livewire/project/service/file-storage.blade.php@@ -1,23 +1,25 @@<x-collapsible><x-slot:title>- <div>{{ $fileStorage->mount_path }} </div>+ <div>{{ $fileStorage->fs_path }} -> {{ $fileStorage->mount_path }}</div></x-slot:title><x-slot:action><form wire:submit.prevent='submit' class="flex flex-col gap-2"><div class="w-64"><x-forms.checkbox instantSave label="Is directory?" id="fileStorage.is_directory"></x-forms.checkbox></div>- @if ($fileStorage->is_directory)+ {{-- @if ($fileStorage->is_directory)<x-forms.input readonly label="Directory on Filesystem (save files here)" id="fs_path"></x-forms.input>- @else- <div class="flex gap-2">+ @else --}}+ {{-- <div class="flex gap-2"><x-forms.input readonly label="File in Docker Compose file" id="fileStorage.fs_path"></x-forms.input><x-forms.input readonly label="File on Filesystem (save files here)" id="fs_path"></x-forms.input></div>- <x-forms.input readonly label="Mount (in container)" id="fileStorage.mount_path"></x-forms.input>+ <x-forms.input readonly label="Mount (in container)" id="fileStorage.mount_path"></x-forms.input> --}}+ @if (!$fileStorage->is_directory)<x-forms.textarea label="Content" rows="20" id="fileStorage.content"></x-forms.textarea><x-forms.button type="submit">Save</x-forms.button>@endif+ {{-- @endif --}}</form></x-slot:action></x-collapsible>diff --git a/resources/views/livewire/project/service/show.blade.php b/resources/views/livewire/project/service/show.blade.phpindex 7db827fcce..e0ab934a28 100644--- a/resources/views/livewire/project/service/show.blade.php+++ b/resources/views/livewire/project/service/show.blade.php@@ -22,7 +22,7 @@@if ($serviceApplication->fileStorages()->get()->count() > 0)<h3 class="py-4">Mounted Files (binds)</h3><div class="flex flex-col gap-4">- @foreach ($serviceApplication->fileStorages()->get() as $fileStorage)+ @foreach ($serviceApplication->fileStorages()->get()->sort() as $fileStorage)<livewire:project.service.file-storage :fileStorage="$fileStorage" wire:key="{{ $loop->index }}" />@endforeach</div>@@ -39,7 +39,7 @@@if ($serviceDatabase->fileStorages()->get()->count() > 0)<h3 class="py-4">Mounted Files (binds)</h3><div class="flex flex-col gap-4">- @foreach ($serviceDatabase->fileStorages()->get() as $fileStorage)+ @foreach ($serviceDatabase->fileStorages()->get()->sort() as $fileStorage)<livewire:project.service.file-storage :fileStorage="$fileStorage" wire:key="{{ $loop->index }}" />@endforeach</div>