You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
7.1 KiB
PHTML
174 lines
7.1 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
namespace App\Controllers;
|
||
|
|
|
||
|
|
use App\Models\CloudHost;
|
||
|
|
use App\Services\Cloud\CloudAccess;
|
||
|
|
use App\Services\Cloud\CloudEngineClient;
|
||
|
|
use App\Services\Cloud\CloudError;
|
||
|
|
use App\Services\Cloud\CloudInput;
|
||
|
|
|
||
|
|
final class CloudAdminController
|
||
|
|
{
|
||
|
|
private function actor($request, bool $write = false): array
|
||
|
|
{
|
||
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor();
|
||
|
|
if (!$actor['is_admin']) {
|
||
|
|
throw new CloudError('需要管理员权限', 403);
|
||
|
|
}
|
||
|
|
if ($write) {
|
||
|
|
CloudAccess::manage($actor);
|
||
|
|
}
|
||
|
|
return $actor;
|
||
|
|
}
|
||
|
|
|
||
|
|
private function host($id): CloudHost
|
||
|
|
{
|
||
|
|
$host = CloudHost::find($id);
|
||
|
|
if ($host === null) {
|
||
|
|
throw new CloudError('宿主机不存在', 404);
|
||
|
|
}
|
||
|
|
return $host;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function scan($request, $response)
|
||
|
|
{
|
||
|
|
$actor = $this->actor($request, true);
|
||
|
|
$body = CloudInput::object($request);
|
||
|
|
CloudInput::only($body, ['host', 'port']);
|
||
|
|
return $response->withJson((new CloudEngineClient())->request('POST', '/internal/v1/hosts/scan', $actor, $body));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function hosts($request, $response, $args = [])
|
||
|
|
{
|
||
|
|
$write = $request->getMethod() !== 'GET';
|
||
|
|
$actor = $this->actor($request, $write);
|
||
|
|
$engine = new CloudEngineClient();
|
||
|
|
if (!$write && !isset($args['id'])) {
|
||
|
|
return $response->withJson(['hosts' => CloudHost::orderBy('id')->get()->toArray()]);
|
||
|
|
}
|
||
|
|
if (!$write) {
|
||
|
|
$host = $this->host($args['id']);
|
||
|
|
return $response->withJson(array_merge($host->toArray(), $engine->host((int) $host->id, 'GET', 'host', $actor)));
|
||
|
|
}
|
||
|
|
$body = CloudInput::object($request, 131072);
|
||
|
|
CloudInput::only($body, ['name', 'enabled', 'host', 'port', 'private_key', 'private_key_passphrase', 'host_key',
|
||
|
|
'confirmed_fingerprint', 'storage_pool', 'iso_pool', 'rescue_iso_pool', 'default_network', 'region',
|
||
|
|
'public_pool_cidr', 'public_gateway', 'public_dns_servers', 'public_reserved_ips', 'public_vm_prefix']);
|
||
|
|
$name = $body['name'] ?? '';
|
||
|
|
if (!is_string($name) || trim($name) === '' || mb_strlen($name) > 80) {
|
||
|
|
throw new CloudError('宿主机名称需为 1 至 80 字符');
|
||
|
|
}
|
||
|
|
$enabled = filter_var($body['enabled'] ?? true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||
|
|
if ($enabled === null) {
|
||
|
|
throw new CloudError('启用状态无效');
|
||
|
|
}
|
||
|
|
unset($body['name'], $body['enabled']);
|
||
|
|
$host = isset($args['id']) ? $this->host($args['id']) : new CloudHost();
|
||
|
|
if (!$host->exists) {
|
||
|
|
$host->name = trim($name);
|
||
|
|
$host->region = '';
|
||
|
|
$host->enabled = false;
|
||
|
|
$host->default_network = 'default';
|
||
|
|
$host->created_at = $host->updated_at = time();
|
||
|
|
$host->save();
|
||
|
|
}
|
||
|
|
// Secrets only transit this request. They are exclusively encrypted/stored by the engine.
|
||
|
|
$result = $engine->host((int) $host->id, 'PUT', 'host', $actor, $body);
|
||
|
|
$host->name = trim($name);
|
||
|
|
$host->region = (string) ($result['region'] ?? $body['region'] ?? '');
|
||
|
|
$host->default_network = (string) ($result['default_network'] ?? $body['default_network'] ?? 'default');
|
||
|
|
$host->enabled = $enabled;
|
||
|
|
$host->updated_at = time();
|
||
|
|
$host->save();
|
||
|
|
return $response->withJson(array_merge($result, $host->toArray()), isset($args['id']) ? 200 : 201);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test($request, $response, $args)
|
||
|
|
{
|
||
|
|
$actor = $this->actor($request, true);
|
||
|
|
$this->host($args['id']);
|
||
|
|
$body = CloudInput::object($request, 131072);
|
||
|
|
return $response->withJson((new CloudEngineClient())->host((int) $args['id'], 'POST', 'host/test', $actor, $body));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function templates($request, $response, $args = [])
|
||
|
|
{
|
||
|
|
$method = $request->getMethod();
|
||
|
|
$actor = $this->actor($request, $method !== 'GET');
|
||
|
|
$body = in_array($method, ['POST', 'PATCH', 'PUT'], true) ? CloudInput::object($request) : null;
|
||
|
|
$query = $request->getQueryParams();
|
||
|
|
$hostId = filter_var($query['host_id'] ?? $body['host_id'] ?? null, FILTER_VALIDATE_INT);
|
||
|
|
if (!$hostId) {
|
||
|
|
throw new CloudError('请选择宿主机');
|
||
|
|
}
|
||
|
|
$this->host($hostId);
|
||
|
|
if ($body !== null) {
|
||
|
|
unset($body['host_id']);
|
||
|
|
}
|
||
|
|
$kind = $args['kind'];
|
||
|
|
$resource = $kind === 'cloud' ? 'vm-templates' : 'rescue-templates';
|
||
|
|
if (isset($args['id'])) {
|
||
|
|
$resource .= '/' . rawurlencode($args['id']);
|
||
|
|
} elseif ($kind === 'cloud' && $method === 'POST') {
|
||
|
|
$resource .= '/import';
|
||
|
|
}
|
||
|
|
if (isset($args['job_id'])) {
|
||
|
|
$resource .= '/import-jobs/' . rawurlencode($args['job_id']);
|
||
|
|
}
|
||
|
|
return $response->withJson((new CloudEngineClient())->host($hostId, $method, $resource, $actor, $body));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function rescueUpload($request, $response)
|
||
|
|
{
|
||
|
|
$actor = $this->actor($request, true);
|
||
|
|
$query = $request->getQueryParams();
|
||
|
|
$hostId = filter_var($query['host_id'] ?? null, FILTER_VALIDATE_INT);
|
||
|
|
$name = $query['name'] ?? '';
|
||
|
|
if (!$hostId || !is_string($name) || trim($name) === '' || mb_strlen($name) > 64) {
|
||
|
|
throw new CloudError('请选择宿主机并填写救援模板名称');
|
||
|
|
}
|
||
|
|
$this->host($hostId);
|
||
|
|
if (strtolower(trim(explode(';', $request->getHeaderLine('Content-Type'))[0])) !== 'application/octet-stream') {
|
||
|
|
throw new CloudError('请直接上传 ISO 文件内容');
|
||
|
|
}
|
||
|
|
$temporary = tempnam(sys_get_temp_dir(), 'cloud-rescue-');
|
||
|
|
if ($temporary === false) {
|
||
|
|
throw new CloudError('无法准备私有上传文件', 503);
|
||
|
|
}
|
||
|
|
chmod($temporary, 0600);
|
||
|
|
$file = fopen($temporary, 'wb');
|
||
|
|
try {
|
||
|
|
if ($file === false) {
|
||
|
|
throw new CloudError('无法准备私有上传文件', 503);
|
||
|
|
}
|
||
|
|
$bytes = 0;
|
||
|
|
$stream = $request->getBody();
|
||
|
|
while (!$stream->eof()) {
|
||
|
|
$chunk = $stream->read(1048576);
|
||
|
|
$bytes += strlen($chunk);
|
||
|
|
if ($bytes > 8 * 1024 * 1024 * 1024) {
|
||
|
|
throw new CloudError('救援 ISO 超过 8 GiB', 413);
|
||
|
|
}
|
||
|
|
if ($chunk === '' && !$stream->eof()) {
|
||
|
|
throw new CloudError('上传中断');
|
||
|
|
}
|
||
|
|
if (fwrite($file, $chunk) !== strlen($chunk)) {
|
||
|
|
throw new CloudError('无法保存上传文件', 503);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fclose($file);
|
||
|
|
$file = null;
|
||
|
|
if ($bytes < 32768) {
|
||
|
|
throw new CloudError('ISO 文件内容无效');
|
||
|
|
}
|
||
|
|
return $response->withJson((new CloudEngineClient())->upload('/internal/v1/hosts/' . $hostId
|
||
|
|
. '/rescue-templates?name=' . rawurlencode(trim($name)), $actor, $temporary), 202);
|
||
|
|
} finally {
|
||
|
|
if (is_resource($file)) {
|
||
|
|
fclose($file);
|
||
|
|
}
|
||
|
|
unlink($temporary);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|