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.
virt/web/app/Controllers/Admin/CloudPageController.php

369 lines
15 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudAdminBillingService;
use App\Services\Cloud\CloudAdminInstanceService;
use App\Services\Cloud\CloudAdminOperationsService;
use App\Services\Cloud\CloudAdminResourceService;
use App\Services\Cloud\CloudAdminVpcControlService;
use App\Services\Cloud\CloudCountryCatalog;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudMaintenanceNoticeService;
use App\Services\Cloud\CloudPlatformDefaultNetworkPolicy;
use App\Services\Cloud\CloudPlatformTestNetworkPolicy;
/** Admin pages use local or database-only first paints; JSON APIs repeat authorisation. */
final class CloudPageController extends AdminController
{
public function resources($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
}
$payload = null;
$error = '';
$status = 200;
try {
$payload = CloudAdminResourceService::page($request->getQueryParams());
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Admin resource snapshot failed: ' . get_class($exception));
$error = '资源调度数据暂时无法读取,请稍后重试。';
$status = 503;
}
$smarty = $this->view()->assign([
'resource_page' => $payload,
'resource_error' => $error,
'resource_bootstrap_json' => json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
]);
$response->getBody()->write($smarty->fetch('admin/cloud/resources.tpl'));
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
}
public function hosts($request, $response, $args)
{
return $this->view()->assign('cloud_country_options', CloudCountryCatalog::options())
->display('admin/hosts/index.tpl');
}
public function vpc($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)
->withHeader('Cache-Control', 'private, no-store');
}
$query = $request->getQueryParams();
$view = (string) ($query['view'] ?? 'overview');
if (!in_array($view, ['overview', 'networks', 'pools'], true)) {
$view = 'overview';
}
$payload = null;
$error = '';
$status = 200;
try {
$payload = CloudAdminVpcControlService::page(
$actor, $query
);
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Admin VPC control snapshot failed: '
. get_class($exception));
$error = 'VPC 控制面数据暂时无法读取,请稍后重试。';
$status = 503;
}
$json = json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
| JSON_THROW_ON_ERROR);
$smarty = $this->view()->assign([
'admin_vpc_page' => $payload,
'admin_vpc_error' => $error,
'admin_vpc_bootstrap_json' => $json,
'admin_vpc_view' => $view,
]);
$response->getBody()->write($smarty->fetch('admin/cloud/vpc.tpl'));
return $response->withStatus($status)
->withHeader('Cache-Control', 'private, no-store');
}
public function agents($request, $response, $args)
{
return $this->view()->display('admin/cloud/agents.tpl');
}
public function templates($request, $response, $args)
{
return $this->view()->display('admin/cloud/templates.tpl');
}
public function rescue($request, $response, $args)
{
return $this->view()->display('admin/cloud/rescue.tpl');
}
public function instances($request, $response, $args)
{
$actor = CloudAccess::actor(false);
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
}
$payload = null;
$error = '';
$status = 200;
try {
$payload = CloudAdminInstanceService::page($actor, $request->getQueryParams());
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Admin instance snapshot failed: ' . get_class($exception));
$error = '实例数据暂时无法读取,请稍后重试。';
$status = 503;
}
$smarty = $this->view()->assign([
'instance_page' => $payload,
'instance_error' => $error,
'instance_bootstrap_json' => json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
]);
$response->getBody()->write($smarty->fetch('admin/cloud/instances.tpl'));
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
}
public function orders($request, $response, $args)
{
return $response->withRedirect('/admin/billing?view=orders', 302);
}
public function billing($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
}
$payload = null;
$error = '';
$status = 200;
$requestedView = $request->getQueryParams()['view'] ?? 'invoices';
$view = is_string($requestedView) && in_array($requestedView, ['invoices', 'orders', 'payments'], true)
? $requestedView : 'invoices';
try {
$payload = CloudAdminBillingService::page($request->getQueryParams());
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Admin billing snapshot failed: ' . get_class($exception));
$error = '账单数据暂时无法读取,请稍后重试。';
$status = 503;
}
$smarty = $this->view()->assign([
'billing_page' => $payload, 'billing_error' => $error, 'billing_view' => $view,
'billing_bootstrap_json' => json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
]);
$response->getBody()->write($smarty->fetch('admin/billing.tpl'));
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
}
public function operations($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
}
$payload = null;
$error = '';
$status = 200;
try {
$payload = CloudAdminOperationsService::page($actor, $request->getQueryParams());
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Admin operations snapshot failed: ' . get_class($exception));
$error = '任务记录暂时无法读取,请稍后重试。';
$status = 503;
}
$smarty = $this->view()->assign([
'operations_page' => $payload, 'operations_error' => $error,
'operations_bootstrap_json' => json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
]);
$response->getBody()->write($smarty->fetch('admin/cloud/operations.tpl'));
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
}
public function maintenanceNotices($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)
->withHeader('Cache-Control', 'private, no-store');
}
$payload = null;
$error = '';
$status = 200;
try {
$service = new CloudMaintenanceNoticeService();
$catalog = $service->catalog();
$payload = [
'page' => $service->page($request->getQueryParams()),
'catalog' => $catalog,
'prefill' => $service->prefill(
$request->getQueryParams(),
$catalog
),
'can_manage' => (bool) ($actor['can_manage'] ?? false),
];
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Admin maintenance notice snapshot failed: '
. get_class($exception));
$error = '维护通知数据暂时无法读取,请稍后重试。';
$status = 503;
}
$json = json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
| JSON_THROW_ON_ERROR);
$smarty = $this->view()->assign([
'maintenance_notice_page' => $payload,
'maintenance_notice_error' => $error,
'maintenance_notice_bootstrap_json' => $json,
]);
$response->getBody()->write(
$smarty->fetch('admin/cloud/maintenance_notices.tpl')
);
return $response->withStatus($status)
->withHeader('Cache-Control', 'private, no-store');
}
public function platformDefaultPolicy($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)
->withHeader('Cache-Control', 'private, no-store');
}
$payload = null;
$testPayload = null;
$error = '';
$testError = '';
$status = 200;
try {
$payload = CloudPlatformDefaultNetworkPolicy::page();
} catch (\Throwable $exception) {
error_log('Admin platform policy manifest failed: '
. get_class($exception));
$error = '平台默认策略暂时无法读取,请检查部署文件。';
$status = 503;
}
try {
$testPayload = CloudPlatformTestNetworkPolicy::page();
} catch (\Throwable $exception) {
error_log('Admin platform test policy failed: '
. get_class($exception));
$testError = '手动测试配置暂时无法读取,请确认数据库已升级。';
$status = 503;
}
$smarty = $this->view()->assign([
'platform_policy' => $payload,
'platform_policy_error' => $error,
'platform_test_policy' => $testPayload,
'platform_test_policy_error' => $testError,
'platform_test_policy_bootstrap_json' => json_encode(
$testPayload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
| JSON_THROW_ON_ERROR
),
]);
$response->getBody()->write(
$smarty->fetch('admin/cloud/platform_default_policy.tpl')
);
return $response->withStatus($status)
->withHeader('Cache-Control', 'private, no-store');
}
public function systemLogs($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
if (!($actor['is_admin'] ?? false)) {
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
}
// Journal reads run asynchronously through the API. Keeping the
// first paint database-only prevents a slow or wedged journalctl child
// from holding the whole administrator page open.
$payload = null;
$error = '';
$status = 200;
$smarty = $this->view()->assign([
'system_logs_page' => $payload,
'system_logs_error' => $error,
'system_logs_timezone' => date_default_timezone_get(),
'system_logs_bootstrap_json' => json_encode($payload,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
| JSON_THROW_ON_ERROR),
]);
$response->getBody()->write($smarty->fetch('admin/cloud/system_logs.tpl'));
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
}
}