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 array $query;
public function mount() public function mount()
{ {
$this->parameters = get_route_parameters(); $this->parameters = get_route_parameters();
$this->query = request()->query(); $this->query = request()->query();
if (isDev()) { if (isDev()) {
@ -40,12 +39,17 @@ class DockerCompose extends Component
} }
public function submit() public function submit()
{ {
$server_id = $this->query['server_id'];
try { try {
$this->validate([ $this->validate([
'dockerComposeRaw' => 'required' 'dockerComposeRaw' => 'required'
]); ]);
$this->dockerComposeRaw = Yaml::dump(Yaml::parse($this->dockerComposeRaw), 10, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); $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(); $project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first(); $environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
@ -74,7 +78,6 @@ class DockerCompose extends Component
'environment_name' => $environment->name, 'environment_name' => $environment->name,
'project_uuid' => $project->uuid, 'project_uuid' => $project->uuid,
]); ]);
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }

View File

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

View File

@ -41,7 +41,6 @@ class StackForm extends Component
} }
public function saveCompose($raw) public function saveCompose($raw)
{ {
$this->service->docker_compose_raw = $raw; $this->service->docker_compose_raw = $raw;
$this->submit(); $this->submit();
} }
@ -55,6 +54,10 @@ class StackForm extends Component
{ {
try { try {
$this->validate(); $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->save();
$this->service->saveExtraFields($this->fields); $this->service->saveExtraFields($this->fields);
$this->service->parse(); $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(); 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);
}
}