fix: file/dir based volumes are now read from the server

This commit is contained in:
Andras Bacsai
2023-09-29 21:38:11 +02:00
parent 23e205b6cd
commit 64ce41df0f
9 changed files with 91 additions and 34 deletions

View File

@@ -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);
}
}

View File

@@ -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()

View File

@@ -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);
}
}

View File

@@ -306,7 +306,7 @@ class Service extends BaseModel
]
);
}
$savedService->saveFileVolumes();
$savedService->getFilesFromServer();
}
}

View File

@@ -36,8 +36,8 @@ class ServiceApplication extends BaseModel
);
}
public function saveFileVolumes()
public function getFilesFromServer()
{
saveFileVolumesHelper($this);
getFilesystemVolumesFromServer($this);
}
}

View File

@@ -26,8 +26,8 @@ class ServiceDatabase extends BaseModel
{
return $this->morphMany(LocalFileVolume::class, 'resource');
}
public function saveFileVolumes()
public function getFilesFromServer()
{
saveFileVolumesHelper($this);
getFilesystemVolumesFromServer($this);
}
}