fix: file volume creation

fix: network_mode host compose
This commit is contained in:
Andras Bacsai
2024-02-14 15:00:24 +01:00
parent 6cdd87da41
commit 73e64d9052
3 changed files with 60 additions and 43 deletions

View File

@@ -41,7 +41,7 @@ class FileStorage extends Component
$this->fileStorage->content = null; $this->fileStorage->content = null;
} }
$this->fileStorage->save(); $this->fileStorage->save();
$this->fileStorage->saveStorageOnServer($this->service); $this->fileStorage->saveStorageOnServer();
$this->dispatch('success', 'File updated successfully.'); $this->dispatch('success', 'File updated successfully.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->fileStorage->setRawAttributes($original); $this->fileStorage->setRawAttributes($original);

View File

@@ -13,23 +13,34 @@ class LocalFileVolume extends BaseModel
protected static function booted() protected static function booted()
{ {
static::created(function (LocalFileVolume $fileVolume) { static::created(function (LocalFileVolume $fileVolume) {
$fileVolume->saveStorageOnServer($fileVolume->service); $fileVolume->load(['service']);
$fileVolume->saveStorageOnServer();
}); });
} }
public function service() public function service()
{ {
return $this->morphTo('resource'); return $this->morphTo('resource');
} }
public function saveStorageOnServer(ServiceApplication|ServiceDatabase $service) public function saveStorageOnServer()
{ {
$workdir = $service->service->workdir(); $workdir = $this->resource->service->workdir();
$server = $service->service->server; $server = $this->resource->service->server;
$commands = collect([ $commands = collect([
"mkdir -p $workdir > /dev/null 2>&1 || true", "mkdir -p $workdir > /dev/null 2>&1 || true",
"cd $workdir" "cd $workdir"
]); ]);
$is_directory = $this->is_directory;
if ($is_directory) {
$commands->push("mkdir -p $this->fs_path > /dev/null 2>&1 || true");
}
if (str($this->fs_path)->startsWith('.') || str($this->fs_path)->startsWith('/') || str($this->fs_path)->startsWith('~')) {
$parent_dir = str($this->fs_path)->beforeLast('/');
if ($parent_dir != '') {
$commands->push("mkdir -p $parent_dir > /dev/null 2>&1 || true");
}
}
$fileVolume = $this; $fileVolume = $this;
$path = Str::of(data_get($fileVolume, 'fs_path')); $path = str(data_get($fileVolume, 'fs_path'));
$content = data_get($fileVolume, 'content'); $content = data_get($fileVolume, 'content');
if ($path->startsWith('.')) { if ($path->startsWith('.')) {
$path = $path->after('.'); $path = $path->after('.');
@@ -38,17 +49,18 @@ class LocalFileVolume extends BaseModel
$isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server); $isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server);
$isDir = instant_remote_process(["test -d $path && echo OK || echo NOK"], $server); $isDir = instant_remote_process(["test -d $path && echo OK || echo NOK"], $server);
if ($isFile == 'OK' && $fileVolume->is_directory) { 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."); 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.");
} else if ($isDir == 'OK' && !$fileVolume->is_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."); throw new \Exception("The following file is a directory on the server, but you are trying to mark it as a file. <br><br>Please delete the directory on the server or mark it as directory.");
} }
if (!$fileVolume->is_directory && $isDir == 'NOK') { if (!$fileVolume->is_directory && $isDir == 'NOK') {
if ($content) {
$content = base64_encode($content); $content = base64_encode($content);
$commands->push("echo '$content' | base64 -d > $path"); $commands->push("echo '$content' | base64 -d > $path");
}
} else if ($isDir == 'NOK' && $fileVolume->is_directory) { } else if ($isDir == 'NOK' && $fileVolume->is_directory) {
$commands->push("mkdir -p $path > /dev/null 2>&1 || true"); $commands->push("mkdir -p $path > /dev/null 2>&1 || true");
} }
ray($commands->toArray());
return instant_remote_process($commands, $server); return instant_remote_process($commands, $server);
} }
} }

View File

@@ -125,6 +125,9 @@ function handleError(?Throwable $error = null, ?Livewire\Component $livewire = n
} }
if (isset($livewire)) { if (isset($livewire)) {
if (str($message)->length() > 20) {
return $livewire->dispatch('error', 'Error occured', $message);
}
return $livewire->dispatch('error', $message); return $livewire->dispatch('error', $message);
} }
throw new Exception($message); throw new Exception($message);
@@ -527,10 +530,10 @@ function getTopLevelNetworks(Service|Application $resource)
$definedNetwork = collect([$resource->uuid]); $definedNetwork = collect([$resource->uuid]);
$services = collect($services)->map(function ($service, $_) use ($topLevelNetworks, $definedNetwork) { $services = collect($services)->map(function ($service, $_) use ($topLevelNetworks, $definedNetwork) {
$serviceNetworks = collect(data_get($service, 'networks', [])); $serviceNetworks = collect(data_get($service, 'networks', []));
$hasNetworkMode = data_get($service, 'network_mode'); $hasHostNetworkMode = data_get($service, 'network_mode') === 'host' ? true : false;
// Only add 'networks' key if 'network_mode' is absent // Only add 'networks' key if 'network_mode' is not 'host'
if (!$hasNetworkMode) { if (!$hasHostNetworkMode) {
// Collect/create/update networks // Collect/create/update networks
if ($serviceNetworks->count() > 0) { if ($serviceNetworks->count() > 0) {
foreach ($serviceNetworks as $networkName => $networkDetails) { foreach ($serviceNetworks as $networkName => $networkDetails) {
@@ -632,6 +635,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$serviceNetworks = collect(data_get($service, 'networks', [])); $serviceNetworks = collect(data_get($service, 'networks', []));
$serviceVariables = collect(data_get($service, 'environment', [])); $serviceVariables = collect(data_get($service, 'environment', []));
$serviceLabels = collect(data_get($service, 'labels', [])); $serviceLabels = collect(data_get($service, 'labels', []));
$hasHostNetworkMode = data_get($service, 'network_mode') === 'host' ? true : false;
if ($serviceLabels->count() > 0) { if ($serviceLabels->count() > 0) {
$removedLabels = collect([]); $removedLabels = collect([]);
$serviceLabels = $serviceLabels->filter(function ($serviceLabel, $serviceLabelName) use ($removedLabels) { $serviceLabels = $serviceLabels->filter(function ($serviceLabel, $serviceLabelName) use ($removedLabels) {
@@ -702,7 +706,6 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$savedService->image = $image; $savedService->image = $image;
$savedService->save(); $savedService->save();
} }
// Collect/create/update networks // Collect/create/update networks
if ($serviceNetworks->count() > 0) { if ($serviceNetworks->count() > 0) {
foreach ($serviceNetworks as $networkName => $networkDetails) { foreach ($serviceNetworks as $networkName => $networkDetails) {
@@ -733,6 +736,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$savedService->ports = $collectedPorts->implode(','); $savedService->ports = $collectedPorts->implode(',');
$savedService->save(); $savedService->save();
if (!$hasHostNetworkMode) {
// Add Coolify specific networks // Add Coolify specific networks
$definedNetworkExists = $topLevelNetworks->contains(function ($value, $_) use ($definedNetwork) { $definedNetworkExists = $topLevelNetworks->contains(function ($value, $_) use ($definedNetwork) {
return $value == $definedNetwork; return $value == $definedNetwork;
@@ -764,6 +768,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$networks->put($network, null); $networks->put($network, null);
} }
data_set($service, 'networks', $networks->toArray()); data_set($service, 'networks', $networks->toArray());
}
// Collect/create/update volumes // Collect/create/update volumes
if ($serviceVolumes->count() > 0) { if ($serviceVolumes->count() > 0) {