Files
api/services/nginx/app/classes/edge_gateway_install_service.php
T

81 lines
2.4 KiB
PHP

<?php
namespace classes;
use Exception;
class edge_gateway_install_service
{
private const ARTIFACTS = [
'agent.php' => 'application/x-httpd-php; charset=utf-8',
'lan-worker.php' => 'application/x-httpd-php; charset=utf-8',
'auto-updater.php' => 'application/x-httpd-php; charset=utf-8',
'docker-compose.gateway.yml' => 'text/yaml; charset=utf-8',
'Dockerfile.edge-agent' => 'text/plain; charset=utf-8',
'Dockerfile.lan-worker' => 'text/plain; charset=utf-8',
'Dockerfile.auto-updater' => 'text/plain; charset=utf-8',
'gateway-launcher.sh' => 'text/x-shellscript; charset=utf-8',
'truckwash-edge-gateway-stack.service' => 'text/plain; charset=utf-8',
'truckwash-edge-agent.service' => 'text/plain; charset=utf-8',
];
public function __construct(private readonly ?edge_gateway_manager $manager = null)
{
edge_gateway_schema_bootstrap::ensureTables();
}
public function buildInstallScript(string $plainToken): string
{
return $this->manager()->buildInstallScript($plainToken);
}
/**
* @throws Exception
*/
public function verifyInstallToken(string $plainToken): array
{
return $this->manager()->verifyInstallToken($plainToken);
}
/**
* @throws Exception
*/
public function readArtifact(string $fileName): string
{
$path = $this->artifactPath($fileName);
$contents = file_get_contents($path);
if ($contents === false) {
throw new Exception('Unable to read edge agent artifact');
}
return $contents;
}
public function contentType(string $fileName): string
{
return self::ARTIFACTS[$fileName] ?? 'application/octet-stream';
}
public function buildArtifactUrl(string $fileName): string
{
return rtrim($this->manager()->getApiBaseUrl(), '/') . '/edge-agent/artifacts/' . $fileName;
}
/**
* @throws Exception
*/
public function artifactPath(string $fileName): string
{
if (!array_key_exists($fileName, self::ARTIFACTS)) {
throw new Exception('Unknown edge agent artifact');
}
return edge_gateway_agent_artifact_locator::resolve($fileName);
}
private function manager(): edge_gateway_manager
{
return $this->manager ?? new edge_gateway_manager();
}
}