fix: docker compose validation

This commit is contained in:
Andras Bacsai 2024-03-14 09:21:48 +01:00
parent 695d3b82b5
commit c8d48ccbff
4 changed files with 38 additions and 7 deletions

View File

@ -17,7 +17,6 @@ class DockerCompose extends Component
public array $query;
public function mount()
{
$this->parameters = get_route_parameters();
$this->query = request()->query();
if (isDev()) {
@ -40,12 +39,17 @@ public function mount()
}
public function submit()
{
$server_id = $this->query['server_id'];
try {
$this->validate([
'dockerComposeRaw' => 'required'
]);
$this->dockerComposeRaw = Yaml::dump(Yaml::parse($this->dockerComposeRaw), 10, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$server_id = $this->query['server_id'];
$isValid = validateComposeFile($this->dockerComposeRaw, $server_id);
if ($isValid !== 'OK') {
return $this->dispatch('error', "Invalid docker-compose file.\n$isValid");
}
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
@ -74,7 +78,6 @@ public function submit()
'environment_name' => $environment->name,
'project_uuid' => $project->uuid,
]);
} catch (\Throwable $e) {
return handleError($e, $this);
}

View File

@ -38,8 +38,12 @@ public function mount()
}
public function check_status()
{
dispatch_sync(new ContainerStatusJob($this->service->server));
$this->dispatch('refresh')->self();
$this->dispatch('serviceStatusChanged');
try {
dispatch_sync(new ContainerStatusJob($this->service->server));
$this->dispatch('refresh')->self();
$this->dispatch('serviceStatusChanged');
} catch (\Exception $e) {
return handleError($e, $this);
}
}
}

View File

@ -41,7 +41,6 @@ public function mount()
}
public function saveCompose($raw)
{
$this->service->docker_compose_raw = $raw;
$this->submit();
}
@ -55,6 +54,10 @@ public function submit()
{
try {
$this->validate();
$isValid = validateComposeFile($this->service->docker_compose_raw, $this->service->server->id);
if ($isValid !== 'OK') {
throw new \Exception("Invalid docker-compose file.\n$isValid");
}
$this->service->save();
$this->service->saveExtraFields($this->fields);
$this->service->parse();

View File

@ -556,3 +556,24 @@ function convert_docker_run_to_compose(?string $custom_docker_run_options = null
}
return $compose_options->toArray();
}
function validateComposeFile(string $compose, int $server_id): string|Throwable {
try {
$uuid = Str::random(10);
$server = Server::findOrFail($server_id);
$base64_compose = base64_encode($compose);
$output = instant_remote_process([
"echo {$base64_compose} | base64 -d > /tmp/{$uuid}.yml",
"docker compose -f /tmp/{$uuid}.yml config",
], $server);
ray($output);
return 'OK';
} catch (\Throwable $e) {
ray($e);
return $e->getMessage();
} finally {
instant_remote_process([
"rm /tmp/{$uuid}.yml",
], $server);
}
}