Add prepaid lifecycle and traffic enforcement

dev
admin 2 months ago
parent 74b89051a7
commit 772fa83ae4

@ -18,9 +18,12 @@
- PHP owns accounts, wallet purchases, orders, public-key ownership and public
HTTP authorization. The Python engine owns libvirt, host credentials and VM
operations. Keep the engine private and authenticate every internal request.
- Purchases provision one VM from the plan snapshot. This release does not
implement expiry or renewal. Never retry an ambiguous destructive operation
or refund it without confirmed cleanup; reconcile its operation identifier.
- Purchases provision one VM from the plan snapshot. Each purchase or renewal
creates an independent prepaid service period with its own traffic quota.
An exhausted quota disconnects VM networking until the next paid period;
final expiry suspends the service. Never retry an ambiguous destructive
operation or refund it without confirmed cleanup; reconcile its operation
identifier.
- Users share the original VM-panel styling; administrator pages retain venix.
- See `docs/PRUNING_REPORT.md` for retained/removed feature boundaries and
`docs/INTEGRATION_PLAN.md` for the planned Python engine integration.

@ -5,8 +5,9 @@ PHP 8.5 网站与私有 Python/libvirt 引擎集成的虚拟机销售面板。
## 本版功能
- PHP 统一注册、邮箱登录、账户、充值、支付、SMTP、工单;注册后无需创建审核。
- 用户购买套餐开通 VM,余额扣款、库存扣减、确认创建失败退款;不包含到期、续费和周期扣费。
- 套餐绑定宿主机,提供 CPU、内存、磁盘、网络带宽规格,用户不能随意修改套餐资源。
- 用户购买套餐开通 VM,余额扣款、库存扣减、确认创建失败退款;可手动续费,续费从已购期限末尾顺延。
- 套餐绑定宿主机,提供 CPU、内存、磁盘、网络带宽、账期天数和每期流量额度,用户不能随意修改套餐资源。
- 下载 RX 与上传 TX 按字节累加计费;当期额度用尽只断开 VM 网卡,不关机,下一已付费账期开始自动归零当期用量并恢复网络;无后续账期的最终到期才停机。
- 管理员管理多宿主机、云镜像和独立救援 ISO;用户选择已有模板及自己的 SSH 公钥。
- 用户仅操作自己的 VM:控制台、开关机、软/硬重启、重装、救援、删除、初始 root 密码查看/复制。
- CPU、内存、磁盘 I/O、网络带宽图表;后台持续采集,写入日志库,最长保留 30 天。
@ -19,7 +20,7 @@ PHP 8.5 网站与私有 Python/libvirt 引擎集成的虚拟机销售面板。
virt-cloud/
web/ PHP 网站、页面、SQL、测试
engine/ 私有 FastAPI/libvirt 引擎及测试
deploy/ 引擎与开通 worker 的 systemd 配置
deploy/ 引擎、开通 worker、账期 worker 与策略 worker 的 systemd 配置
runtime/ 私有配置、密钥、引擎数据(安装生成,不入 Git)
docs/ 架构、部署、原清理记录
```

@ -0,0 +1,24 @@
[Unit]
Description=Virt Cloud service-period and traffic lifecycle worker
After=network-online.target virt-cloud-engine.service
Wants=network-online.target
[Service]
Type=simple
User=apache
Group=apache
WorkingDirectory=/home/virt-cloud/web
ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudLifecycle
Restart=on-failure
RestartSec=5
UMask=0077
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=false
ProtectSystem=strict
ReadWritePaths=-/home/virt-cloud/runtime/php
ReadWritePaths=-/home/virt-cloud/web/storage
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target

@ -0,0 +1,24 @@
[Unit]
Description=Virt Cloud enforcement policy worker %i
After=network-online.target virt-cloud-engine.service
Wants=network-online.target
[Service]
Type=simple
User=apache
Group=apache
WorkingDirectory=/home/virt-cloud/web
ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudWorker --policy-only
Restart=on-failure
RestartSec=3
UMask=0077
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=false
ProtectSystem=strict
ReadWritePaths=-/home/virt-cloud/runtime/php
ReadWritePaths=-/home/virt-cloud/web/storage
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target

@ -5,7 +5,7 @@
## 1. 自行安装依赖
- PHP 8.5 CLI/FPM:PDO MySQL、sodium、mbstring、curl、XML/DOM、OpenSSL、session 等扩展,Composer 2。
- MariaDB/MySQL:业务库、日志库,初始化账号需要建表权限。
- MariaDB:业务库、日志库,初始化账号需要建表和执行幂等升级 DDL 的权限;PHP/PDO 中驱动名仍为 `mysql`。当前升级脚本使用 MariaDB 的 `ADD COLUMN IF NOT EXISTS`,不承诺兼容 Oracle MySQL。
- Redis 或兼容的 Valkey:沿用 venix 会话、验证码与限流。
- Python 3.12 或更新的项目支持版本、venv/pip、libvirt 客户端及开发头文件、C 编译工具、pkg-config、OpenSSH 客户端。
- Nginx/Apache、HTTPS、PHP-FPM,网站配置由你自行管理。
@ -41,6 +41,7 @@ cp config/.config.php.example config/config.php
- `enable_log` 设置为字符串 `'true'`;`log_db_host/log_db_port/log_db_database/log_db_user/log_db_password`:日志库。空日志库地址沿用本机默认,远程数据库请填写明确地址。
- `db_prefix` 留空,网站域名、HTTPS Cookie、Redis/Valkey、账户安全、SMTP 按 venix 设置。
- `cloud_*` 默认路径适配 `/home/virt-cloud`,私有密钥自动生成;宿主机凭据稍后在页面设置。
- `cloud_max_renewal_periods_ahead` 限制单台实例可排队的未来续费账期,默认 24;不要设成无限值。
- `cloud_introspection_url` 必须指向 PHP 网站,不是引擎 8000。默认 `http://127.0.0.1/api/internal/cloud/session-introspection`;多虚拟主机可设置 `cloud_introspection_host` 为网站域名,或改为正确的本机 HTTPS URL。
在**两个全新空数据库**分别导入,替换命令中的地址、端口、账号、库名;`-p` 提示密码,不把密码写入历史:
@ -100,18 +101,37 @@ cd /home/virt-cloud/web
sudo -u apache php xcat cloudWorker
```
策略专用 worker 调试(正式部署至少运行两个实例,避免慢镜像任务阻塞到期/流量策略):
```bash
cd /home/virt-cloud/web
sudo -u apache php xcat cloudWorker --policy-only
```
账期、到期和流量限额前台调试:
```bash
cd /home/virt-cloud/web
sudo -u apache php xcat cloudLifecycle
```
常驻 service:
```bash
cp /home/virt-cloud/deploy/virt-cloud-engine.service /etc/systemd/system/virt-cloud-engine.service
cp /home/virt-cloud/deploy/virt-cloud-worker.service /etc/systemd/system/virt-cloud-worker.service
cp /home/virt-cloud/deploy/virt-cloud-lifecycle.service /etc/systemd/system/virt-cloud-lifecycle.service
cp /home/virt-cloud/deploy/virt-cloud-policy-worker@.service /etc/systemd/system/virt-cloud-policy-worker@.service
systemctl daemon-reload
systemctl enable --now virt-cloud-engine.service virt-cloud-worker.service
systemctl status virt-cloud-engine.service virt-cloud-worker.service --no-pager
journalctl -u virt-cloud-engine -u virt-cloud-worker -n 100 --no-pager
systemctl enable --now virt-cloud-engine.service virt-cloud-worker.service virt-cloud-lifecycle.service \
virt-cloud-policy-worker@1.service virt-cloud-policy-worker@2.service
systemctl status virt-cloud-engine.service virt-cloud-worker.service virt-cloud-lifecycle.service \
virt-cloud-policy-worker@1.service virt-cloud-policy-worker@2.service --no-pager
journalctl -u virt-cloud-engine -u virt-cloud-worker -u virt-cloud-lifecycle \
-u 'virt-cloud-policy-worker@*' -n 100 --no-pager
```
worker 使用 `/usr/bin/php`,须为 PHP 8.5,否则修改 unit。引擎使用一个 worker。购买排队不执行时先检查 `virt-cloud-worker`。PHP 网站由你现有的 Web/FPM 服务提供。
worker 使用 `/usr/bin/php`,须为 PHP 8.5,否则修改 unit。引擎使用一个 Uvicorn worker。普通 worker 负责购买、重装等任务,并可在策略 worker 缺失时兜底;两个策略 worker 专门处理到期停机、断网与恢复,不能省略。购买排队不执行时先检查 `virt-cloud-worker`;流量用尽不断网、账期不切换或到期不停机时同时检查 `virt-cloud-lifecycle` 与 `virt-cloud-policy-worker@1/@2`。PHP 网站由你现有的 Web/FPM 服务提供。
## 6. 网站与控制台
@ -143,9 +163,10 @@ location = /engine-console {
1. 管理员添加宿主机:现有 SSH/root/私钥口令、核验指纹,设置区域、网桥、目录、地址池/网关/DNS/网卡前缀,测试连接。
2. 导入云镜像与救援模板;无已有模板不能开通云镜像 VM。云镜像 SHA-256 可留空;留空时仍会在下载后计算并保存实际摘要,填写时则会严格比对。预存其他系统图标不代表已实现其云模板导入;其他系统可用合适 ISO 安装,云镜像适配后续扩展。
3. 套餐设置规格、价格、库存、已启用宿主机。本版价格是一次开通金额,不是自动月费。
4. 测试用户注册/充值/购买,验证任务、VM 控制台、SSH、重装、救援、删除与图表。
5. 日志库 `engine_vm_metric_samples` 应在所有用户退出后继续新增;首批图表需等待采样间隔。
3. 套餐设置规格、每期价格、账期天数、每期流量额度、库存和已启用宿主机。本版为手动余额续费,不自动扣费。
4. 测试用户注册/充值/购买/提前续费,验证任务、VM 控制台、SSH、重装、救援、删除与图表。
5. 日志库 `engine_vm_metric_samples` 应在所有用户退出后继续新增,`engine_vm_traffic_totals` 应持续累加 RX/TX 字节;稳定 MAC 用于跨 QEMU/tap 重启衔接计数,首批数据需等待采样间隔。
6. 用测试小额度验收:下载与上传合计达额度后 VM 仍运行但所有网卡断开;下一已付费周期开始后当期用量归零且网卡自动恢复。无下一周期时,最终到期才停机。
先不要启用旧 `web/crontab`,付款扫描/备份任务按保留业务单独核对。真实 MariaDB/KVM/SMTP/支付未在开发环境联调,先验收测试环境。`uncertain` 状态先核对宿主机,不直接改库退款或重复毁盘任务。
@ -158,7 +179,13 @@ cd /home/virt-cloud
git pull --ff-only origin venix
cd /home/virt-cloud/web
php xcat cloudInit
systemctl restart virt-cloud-engine.service virt-cloud-worker.service
cp /home/virt-cloud/deploy/virt-cloud-lifecycle.service /etc/systemd/system/virt-cloud-lifecycle.service
cp /home/virt-cloud/deploy/virt-cloud-policy-worker@.service /etc/systemd/system/virt-cloud-policy-worker@.service
systemctl daemon-reload
systemctl enable --now virt-cloud-lifecycle.service \
virt-cloud-policy-worker@1.service virt-cloud-policy-worker@2.service
systemctl restart virt-cloud-engine.service virt-cloud-worker.service virt-cloud-lifecycle.service \
virt-cloud-policy-worker@1.service virt-cloud-policy-worker@2.service
```
已存在的宿主机需在管理页重新保存一次。面板会扫描宿主机,将尚未记录在当前业务库和归属库的存量虚拟机导入业务库,并归属给当前超级管理员;这也包括带有旧版面板元数据、但不存在于当前 PHP 业务库的虚拟机。重复保存不会复制记录,也不会改写已有归属。
@ -166,5 +193,6 @@ systemctl restart virt-cloud-engine.service virt-cloud-worker.service
订单状态为“待核对”时不要重复下单或手动退款。worker 会使用原操作编号向引擎核对,确认成功后关联实例,确认未创建后才退款。管理页会显示格式化时间、尝试次数和安全的错误摘要;更详细日志使用:
```bash
journalctl -u virt-cloud-worker.service -u virt-cloud-engine.service -n 200 --no-pager
journalctl -u virt-cloud-worker.service -u virt-cloud-engine.service \
-u virt-cloud-lifecycle.service -u 'virt-cloud-policy-worker@*' -n 200 --no-pager
```

@ -11,7 +11,8 @@
└─ WSS /engine-console ──────────────────→ Python → SSH 隧道 → VM VNC
PHP cloudWorker → 持久任务 → Python operation_id → 实例关联/核对/退款
Python 后台采集 → 日志库 engine_vm_metric_samples → 详情页四项图表
Python 后台采集 → 日志库指标/持久流量基线 → 图表与字节计量
PHP cloudLifecycle → 独立账期/流量限额 → 幂等断网、恢复或最终到期停机
```
PHP 是唯一账户入口,所有用户页面使用原虚拟化面板 CSS;后台保留 venix。没有第二套 Python 注册、SMTP、数据库设置或审核流程。
@ -21,10 +22,11 @@ PHP 是唯一账户入口,所有用户页面使用原虚拟化面板 CSS;后
| 数据 | 权威写入方 | 存储 |
| --- | --- | --- |
| 用户、钱包、套餐、原财务/工单 | PHP | 原业务库表 |
| 宿主机索引、订单、实例、公钥、会话、任务 | PHP | `cloud_hosts/orders/instances/ssh_keys/sessions/jobs` |
| 宿主机索引、订单、实例、账期、续费、公钥、会话、任务 | PHP | `cloud_hosts/orders/instances/service_periods/renewals/ssh_keys/sessions/jobs` |
| 宿主机凭据、VM 元数据、镜像、操作进度 | Python | 主库 `engine_records`,敏感记录加密 |
| 引擎签名防重放 | Python | 主库 `engine_request_nonces` |
| 四项性能指标样本 | Python | 日志库 `engine_vm_metric_samples`,保留 30 天 |
| VM RX/TX 持久累计与网卡基线 | Python | 日志库 `engine_vm_traffic_totals`,不随 30 天图表样本清理 |
多个宿主机使用稳定 `host_id`,不共用一个可切换的全局“当前宿主机”。模板、IP 地址池、网关、DNS、虚拟机网卡前缀、磁盘/ISO/救援目录均按宿主机设置。地址池与网卡前缀分开:供应商地址池可以为 /28,虚拟机网卡可以为 /24,不能因此把售卖范围扩大为 /24。
@ -38,7 +40,11 @@ PHP 不直接解密 Python 记录或执行任意 virsh 命令。主库与日志
4. 成功关联实例和凭据;确认创建失败且无残留时只退款、恢复库存一次。
5. 超时、引擎中断或残留无法确认时保持待核对,只查询原操作,不重复创建或盲目退款。
金额通过精确小数与整数最小单位换算处理,不用浮点完成扣款比较。套餐带宽转换成 libvirt 网卡双向 KiB/s 限制。本轮没有续费、月结、到期停机、自动回收或套餐升级。
金额通过精确小数与整数最小单位换算处理,不用浮点完成扣款比较。套餐带宽转换成 libvirt 网卡双向 KiB/s 限制。开通和每次手动续费都生成一个独立预付费周期;提前续费只排在已购周期末尾,不会提前刷新当期流量。每期额度为下载 RX + 上传 TX;用尽只持久断开网卡,VM 不关机;下一已付费周期开始时以新的累计基线归零当期用量并自动恢复网卡。只有无后续周期的最终到期才禁用自启并停机;到期后续费恢复服务和自启,但不会擅自开启用户已关机的 VM。
单台实例默认最多排队 24 个未来账期,可通过 `cloud_max_renewal_periods_ahead` 调整(服务端限制为 1–120)。这个上限在实例行锁内校验,避免免费套餐或自动化重复请求无限写入续费与账期记录。
本轮不含自动扣费、套餐升级、按日比例退款或自动删除到期 VM。流量依赖约 60 秒的宿主机采样,因此限额可能有一个采样间隔的少量超额;日志库不可读时采取 fail-open,不会因未知流量误停租户。
## 安全与控制台

@ -2,8 +2,8 @@
2026-09-27,集成代码的隔离验证,不代表生产验收。
- Python 3.12:445 passed、10 skipped、39 subtests passed。覆盖原虚拟化逻辑及私有 API、签名/重放、多宿主机归属、持久幂等、一次性 VNC、退出后断权、带宽 XML、救援上传摘要。
- 隔离 PHP 8.5.10:源码语法、70 个 Smarty 模板、178 个路由目标、精确金额/HMAC、SQLite 购买开通/退款/超时核对/配额/租户隔离、Cookie/Redis 会话撤销及内省、统一用户资源和 52 个 noVNC 模块检查通过。
- 当前 macOS 工作区 Python 3.9.6 分组回归:485 passed、10 skipped。覆盖原虚拟化逻辑及私有 API、签名/重放、多宿主机归属、持久幂等、一次性 VNC、退出后断权、带宽 XML、救援上传摘要,以及服务到期停机/恢复、同实例变更串行、多网卡 live+config 断网/恢复、退出救援保持计费断网、流量累计检查点、停机前状态持久化失败时的安全中止和崩溃后的目标状态核对。生产仍按部署文档使用 Python 3.12 或更新的受支持版本复测。
- 隔离 PHP 8.5.10:源码语法、Smarty 模板、路由目标、精确金额/HMAC、SQLite 购买开通/退款/超时核对/配额/租户隔离、Cookie/Redis 会话撤销及内省、统一用户资源和 noVNC 模块检查通过。本次新增独立账期用例,覆盖提前续费、精确扣款幂等、RX+TX 额度、断网不关机、下期恢复和最终到期停机;当前 macOS 工作区没有 PHP 可执行文件,该新用例需在下方 PHP 8.5 环境命令中复跑。
- Chrome 使用模拟 API 检查套餐购买、VM 详情/指标/凭据、公钥、重装/救援、后台宿主机/镜像操作与 CSRF;用户账户及登录等页面进行桌面/手机布局检查。不连接真实数据库或宿主机。
- PHP 隔离环境不提供 sodium:`CloudSecretsTest` 明确跳过,原 Paytaro/易支付密钥加密测试因同一缺失无法运行;财务/会话测试的加密存储仅用明确标注的测试替身,不作为实际加密验证。生产必须安装 sodium,没有弱加密回退。
- Python 的跳过项需要真实 libvirt/网络环境。没有验证真实 MariaDB 并发、AlmaLinux systemd、宿主机 VM 操作、SMTP、支付和大文件上传。

@ -177,11 +177,13 @@ class FakeVMService:
self.vm["rescue_volume_name"] = volume_name
return {"vm": self.vm}
def exit_rescue_vm(self, vm_id, original_xml):
self.calls.append(("exit_rescue_vm", vm_id, original_xml))
def exit_rescue_vm(self, vm_id, original_xml, *, network_suspended=False):
self.calls.append(("exit_rescue_vm", vm_id, original_xml,
network_suspended))
self.vm["rescue_mode"] = False
self.vm["rescue_staged"] = False
self.vm["rescue_volume_name"] = None
self.vm["network_suspended"] = network_suspended
self.vm["state"] = "running"
return {"vm": self.vm}
@ -205,6 +207,30 @@ class FakeVMService:
self.vm["rescue_staged"] = False
return self.vm
def service_suspend(self, vm_id):
self.calls.append(("service_suspend", vm_id))
was_running = self.vm["state"] == "running"
self.vm["state"] = "shutoff"
self.vm["autostart"] = False
return {**self.vm, "service_was_running": was_running}
def service_resume(self, vm_id, *, start_if_suspended=False):
self.calls.append(("service_resume", vm_id, start_if_suspended))
self.vm["autostart"] = True
if start_if_suspended:
self.vm["state"] = "running"
return self.vm
def network_suspend(self, vm_id):
self.calls.append(("network_suspend", vm_id))
self.vm["network_suspended"] = True
return self.vm
def network_resume(self, vm_id):
self.calls.append(("network_resume", vm_id))
self.vm["network_suspended"] = False
return self.vm
def delete_vm(self, vm_id, delete_disks):
self.calls.append(("delete_vm", vm_id, delete_disks))
return {"uuid": vm_id, "deleted": True, "deleted_disks": []}
@ -324,6 +350,15 @@ def test_first_start_creates_token_and_restarts_reuse_it(tmp_path):
assert not state.token_file.exists()
@pytest.mark.parametrize("operation", [
"service-suspend", "service-resume", "network-suspend", "network-resume",
])
def test_service_target_routes_are_not_exposed_by_browser_panel(web, operation):
client, *_ = web
response = client.post(f"/api/v1/vms/{VM_ID}/{operation}")
assert response.status_code == 404
def test_lifespan_closes_real_manager_interface(tmp_path):
state = PanelState(tmp_path / "panel-data")
settings = Settings(data_dir=state.data_dir, static_dir=STATIC_DIR)
@ -821,8 +856,9 @@ def test_rescue_iso_upload_enter_exit_and_safe_lifecycle(web):
assert client.delete(f"/api/v1/rescue-templates/{template['id']}",
headers=headers).status_code == 409
exited = client.post(f"{rescue_url}/exit", json={"confirmation_name": "alma-test"},
headers=json_headers)
exited = client.post(f"{rescue_url}/exit",
json={"confirmation_name": "alma-test",
"network_suspended": True}, headers=json_headers)
assert exited.status_code == 202, exited.text
for _ in range(100):
exit_job = client.get(f"/api/v1/rescue-jobs/{exited.json()['job_id']}").json()
@ -831,6 +867,9 @@ def test_rescue_iso_upload_enter_exit_and_safe_lifecycle(web):
time.sleep(0.01)
assert exit_job["status"] == "completed", exit_job
assert client.get(rescue_url).json()["active"] is False
assert service.vm["network_suspended"] is True
assert any(call[0] == "exit_rescue_vm" and call[3] is True
for call in service.calls)
# If a worker dies after saving its recovery record but the original
# persistent definition is still present, reading status safely clears

@ -329,6 +329,226 @@ def test_power_action_has_persistent_idempotency(boundary):
assert operation["status"] == "succeeded" and operation["safe_to_refund"] is False
@pytest.mark.parametrize(("route", "method", "target"), [
("service-suspend", "service_suspend", {"state": "shutoff", "autostart": False}),
("service-resume", "service_resume", {"autostart": True}),
("network-suspend", "network_suspend", {"network_suspended": True}),
("network-resume", "network_resume", {"network_suspended": False}),
])
def test_private_target_state_operations_are_owned_idempotent_mutations(
boundary, route, method, target):
client, engine, _, _, services = boundary
base = f"/internal/v1/hosts/hk/vms/{VM_ID}/{route}"
call(client, "GET", "/internal/v1/hosts/hk/vms")
component = engine.registry.components["hk"]
component.state.users.assign_vm(host_scope(component.manager.profile), VM_ID, 41)
assert call(client, "POST", base, USER, {}).status_code == 403
assert call(client, "POST", base, {**USER, "can_manage": False,
"operation_id": f"blocked-{route}"}, {}).status_code == 403
assert call(client, "POST", base, {**USER, "user_id": 42,
"operation_id": f"foreign-{route}"}, {}).status_code == 404
actor = {**USER, "operation_id": f"target-{route}"}
first = call(client, "POST", base, actor, {})
assert first.status_code == 200, first.text
assert first.json()["status"] == "succeeded"
assert all(first.json()[key] == value for key, value in target.items())
repeated = call(client, "POST", base, actor, {})
assert repeated.status_code == 200
assert len([item for item in services["hk"].calls if item[0] == method]) == 1
@pytest.mark.parametrize(("route", "method", "drift"), [
("service-suspend", "service_suspend", {"state": "running", "autostart": True}),
("service-resume", "service_resume", {"autostart": False}),
("network-suspend", "network_suspend", {"network_suspended": False}),
("network-resume", "network_resume", {"network_suspended": True}),
])
def test_target_state_operation_replays_after_lost_response(
boundary, route, method, drift):
client, engine, store, _, services = boundary
base = f"/internal/v1/hosts/hk/vms/{VM_ID}/{route}"
call(client, "GET", "/internal/v1/hosts/hk/vms")
component = engine.registry.components["hk"]
component.state.users.assign_vm(host_scope(component.manager.profile), VM_ID, 41)
actor = {**USER, "operation_id": f"replay-{route}"}
assert call(client, "POST", base, actor, {}).status_code == 200
saved = json.loads(store.get("operation", actor["operation_id"]))
saved.update(status="uncertain", result=None, error="response lost")
store.set("operation", actor["operation_id"], json.dumps(saved))
services["hk"].vm.update(drift)
engine.active_operations.clear()
replayed = call(client, "GET", f"/internal/v1/operations/{actor['operation_id']}",
actor)
assert replayed.status_code == 200, replayed.text
assert replayed.json()["status"] == "succeeded"
assert len([item for item in services["hk"].calls if item[0] == method]) == 2
saved = json.loads(store.get("operation", actor["operation_id"]))
saved.update(status="uncertain", result=None, error="response lost again")
store.set("operation", actor["operation_id"], json.dumps(saved))
services["hk"].vm.update(drift)
replayed_post = call(client, "POST", base, actor, {})
assert replayed_post.status_code == 200, replayed_post.text
assert replayed_post.json()["status"] == "succeeded"
assert len([item for item in services["hk"].calls if item[0] == method]) == 3
def test_non_reconciling_operation_lookup_never_replays_target_state(boundary):
client, engine, store, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
component = engine.registry.components["hk"]
component.state.users.assign_vm(host_scope(component.manager.profile), VM_ID, 41)
path = f"/internal/v1/hosts/hk/vms/{VM_ID}/network-suspend"
actor = {**USER, "operation_id": "peek-target-state"}
assert call(client, "POST", path, actor, {}).status_code == 200
saved = json.loads(store.get("operation", actor["operation_id"]))
saved.update(status="uncertain", result=None, error="response lost")
store.set("operation", actor["operation_id"], json.dumps(saved))
services["hk"].vm["network_suspended"] = False
engine.active_operations.clear()
before = len(services["hk"].calls)
peeked = call(client, "GET",
"/internal/v1/operations/peek-target-state?reconcile=false", actor)
assert peeked.status_code == 200
assert peeked.json()["status"] == "uncertain"
assert peeked.json()["executor_active"] is False
assert len(services["hk"].calls) == before
assert services["hk"].vm["network_suspended"] is False
engine.active_operations.add(actor["operation_id"])
active = call(client, "GET",
"/internal/v1/operations/peek-target-state?reconcile=false", actor)
assert active.status_code == 200
assert active.json()["executor_active"] is True
assert len(services["hk"].calls) == before
engine.active_operations.discard(actor["operation_id"])
def test_target_state_lock_serializes_same_vm_and_not_other_vms(boundary):
_, engine, *_ = boundary
async def exercise():
first = engine._target_lock("hk", VM_ID)
same = engine._target_lock("hk", VM_ID)
other = engine._target_lock("hk", "22222222-2222-4222-8222-222222222222")
assert first is same
assert first is not other
entered = []
release = asyncio.Event()
async def owner():
async with first:
entered.append("owner")
await release.wait()
entered.append("owner-finished")
async def waiter():
async with same:
entered.append("waiter")
owner_task = asyncio.create_task(owner())
await asyncio.sleep(0)
waiter_task = asyncio.create_task(waiter())
await asyncio.sleep(0)
assert entered == ["owner"]
release.set()
await asyncio.gather(owner_task, waiter_task)
assert entered == ["owner", "owner-finished", "waiter"]
errors = []
def run_in_isolated_loop():
try:
asyncio.run(exercise())
except BaseException as exc: # propagate assertion failures to pytest
errors.append(exc)
thread = threading.Thread(target=run_in_isolated_loop)
thread.start()
thread.join(timeout=5)
assert not thread.is_alive()
if errors:
raise errors[0]
def test_expiry_target_waits_for_inflight_user_mutation_on_same_vm(boundary):
client, engine, _, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
components = engine.registry.components["hk"]
components.state.users.assign_vm(
host_scope(components.manager.profile), VM_ID, USER["user_id"])
service = services["hk"]
action_entered = threading.Event()
release_action = threading.Event()
network_entered = threading.Event()
original_action = service.action
original_network_suspend = service.network_suspend
def slow_action(vm_id, action):
action_entered.set()
assert release_action.wait(timeout=5), "test did not release user mutation"
return original_action(vm_id, action)
def observed_network_suspend(vm_id):
network_entered.set()
return original_network_suspend(vm_id)
service.action = slow_action
service.network_suspend = observed_network_suspend
responses = {}
def user_request():
responses["action"] = call(
client, "POST", f"/internal/v1/hosts/hk/vms/{VM_ID}/actions",
{**USER, "operation_id": "concurrent-user-action"}, {"action": "reset"})
def policy_request():
responses["policy"] = call(
client, "POST", f"/internal/v1/hosts/hk/vms/{VM_ID}/network-suspend",
{**USER, "operation_id": "concurrent-expiry-policy"}, {})
first = threading.Thread(target=user_request)
first.start()
assert action_entered.wait(timeout=5)
second = threading.Thread(target=policy_request)
second.start()
assert not network_entered.wait(timeout=0.2)
release_action.set()
first.join(timeout=5)
second.join(timeout=5)
assert not first.is_alive() and not second.is_alive()
assert responses["action"].status_code == 200
assert responses["policy"].status_code == 200
assert network_entered.is_set()
mutations = [item[0] for item in service.calls
if item[0] in {"action", "network_suspend"}]
assert mutations == ["action", "network_suspend"]
assert service.vm["network_suspended"] is True
def test_target_state_reconciliation_does_not_bypass_original_owner(boundary):
client, engine, store, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
component = engine.registry.components["hk"]
component.state.users.assign_vm(host_scope(component.manager.profile), VM_ID, 41)
store.set("operation", "unsafe-target", json.dumps({
"operation_id": "unsafe-target", "host_id": "hk", "user_id": 42,
"kind": "mutation", "resource": f"vms/{VM_ID}/network-suspend",
"vm_uuid": VM_ID, "status": "uncertain", "result": None,
"actor_can_manage": True, "actor_is_admin": False,
"safe_to_refund": False,
}))
response = call(client, "GET", "/internal/v1/operations/unsafe-target",
{**USER, "user_id": 42})
assert response.status_code == 200
assert response.json()["status"] == "failed"
assert not any(item[0] == "network_suspend" for item in services["hk"].calls)
def test_async_mutation_operation_survives_lost_job_response_and_does_not_retry(boundary):
client, engine, store, *_ = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
@ -349,7 +569,7 @@ def test_async_mutation_operation_survives_lost_job_response_and_does_not_retry(
assert not completed["safe_to_refund"]
def test_interrupted_async_job_stays_uncertain(boundary):
def test_interrupted_async_job_becomes_terminal_without_replay(boundary):
client, engine, store, *_ = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
components = engine.registry.components["hk"]
@ -360,8 +580,148 @@ def test_interrupted_async_job_stays_uncertain(boundary):
"host_id": "hk", "user_id": 41, "kind": "mutation", "resource": f"vms/{VM_ID}/reinstall",
"vm_uuid": VM_ID, "status": "pending", "result": None, "safe_to_refund": False}))
recovered = call(client, "GET", "/internal/v1/operations/interrupted-job").json()
assert recovered["status"] == "uncertain"
assert recovered["status"] == "failed"
assert "中断" in recovered["error"]
@pytest.mark.parametrize("resource", [
f"vms/{VM_ID}/actions",
f"vms/{VM_ID}",
f"vms/{VM_ID}/reinstall",
f"vms/{VM_ID}/rescue",
])
def test_orphaned_mutation_becomes_terminal_without_replay(boundary, resource):
client, _, store, *_ = boundary
operation_id = "orphan-" + resource.rsplit("/", 1)[-1].replace("?", "-")
store.set("operation", operation_id, json.dumps({
"operation_id": operation_id, "host_id": "hk", "user_id": 41,
"kind": "mutation", "resource": resource, "vm_uuid": VM_ID,
"status": "running", "result": None, "safe_to_refund": False,
"created_at": 1,
}))
recovered = call(client, "GET", f"/internal/v1/operations/{operation_id}").json()
assert recovered["status"] == "failed"
assert "不会自动重放" in recovered["error"]
def test_service_resume_replays_exact_start_target_after_lost_response(boundary):
client, engine, store, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
components = engine.registry.components["hk"]
components.state.users.assign_vm(
host_scope(components.manager.profile), VM_ID, USER["user_id"])
services["hk"].vm.update(state="shutoff", autostart=False)
actor = {**USER, "operation_id": "resume-start-target"}
path = f"/internal/v1/hosts/hk/vms/{VM_ID}/service-resume"
first = call(client, "POST", path, actor, {"start_if_suspended": True})
assert first.status_code == 200, first.text
assert services["hk"].vm["state"] == "running"
assert services["hk"].calls[-1] == ("service_resume", VM_ID, True)
saved = json.loads(store.get("operation", actor["operation_id"]))
saved.update(status="uncertain", result=None, error="response lost")
store.set("operation", actor["operation_id"], json.dumps(saved))
services["hk"].vm.update(state="shutoff", autostart=False)
engine.active_operations.clear()
replayed = call(client, "GET", "/internal/v1/operations/resume-start-target", actor)
assert replayed.status_code == 200
assert replayed.json()["status"] == "succeeded"
assert services["hk"].vm["state"] == "running"
assert services["hk"].calls[-1] == ("service_resume", VM_ID, True)
def test_service_suspend_replay_preserves_original_running_state(boundary):
client, engine, store, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
components = engine.registry.components["hk"]
components.state.users.assign_vm(
host_scope(components.manager.profile), VM_ID, USER["user_id"])
actor = {**USER, "operation_id": "suspend-running-hint"}
path = f"/internal/v1/hosts/hk/vms/{VM_ID}/service-suspend"
assert call(client, "POST", path, actor, {}).status_code == 200
saved = json.loads(store.get("operation", actor["operation_id"]))
assert saved["service_was_running_hint"] is True
# Model a hard crash after libvirt stopped the guest but before the
# successful result was durable. Reconciliation now observes shutoff, yet
# must retain the original running state for the billing resume policy.
saved.update(status="uncertain", result=None, error="engine crashed")
store.set("operation", actor["operation_id"], json.dumps(saved))
services["hk"].vm.update(state="shutoff", autostart=False)
engine.active_operations.clear()
reconciled = call(client, "GET",
"/internal/v1/operations/suspend-running-hint", actor)
assert reconciled.status_code == 200
assert reconciled.json()["status"] == "succeeded"
assert reconciled.json()["result"]["service_was_running"] is True
def test_service_suspend_replay_without_running_hint_fails_closed(boundary):
client, engine, store, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
components = engine.registry.components["hk"]
components.state.users.assign_vm(
host_scope(components.manager.profile), VM_ID, USER["user_id"])
operation_id = "suspend-missing-running-hint"
store.set("operation", operation_id, json.dumps({
"operation_id": operation_id,
"host_id": "hk",
"user_id": USER["user_id"],
"vm_uuid": VM_ID,
"kind": "mutation",
"resource": f"vms/{VM_ID}/service-suspend",
"method": "POST",
"status": "uncertain",
"actor_can_manage": True,
"actor_is_admin": False,
"created_at": 1,
}))
before_calls = list(services["hk"].calls)
response = call(
client, "GET", f"/internal/v1/operations/{operation_id}", USER)
assert response.status_code == 200
assert response.json()["status"] == "failed"
assert "已取消服务暂停" in response.json()["error"]
assert services["hk"].calls == before_calls
def test_service_suspend_does_not_stop_vm_when_running_hint_cannot_persist(
boundary, monkeypatch):
client, engine, store, _, services = boundary
call(client, "GET", "/internal/v1/hosts/hk/vms")
components = engine.registry.components["hk"]
components.state.users.assign_vm(
host_scope(components.manager.profile), VM_ID, USER["user_id"])
operation_id = "suspend-hint-write-failed"
original_set = store.set
def fail_operation_write(kind, key, value):
if kind == "operation" and key == operation_id:
raise RuntimeError("database unavailable")
return original_set(kind, key, value)
monkeypatch.setattr(store, "set", fail_operation_write)
failed = call(
client, "POST",
f"/internal/v1/hosts/hk/vms/{VM_ID}/service-suspend",
{**USER, "operation_id": operation_id}, {})
assert failed.status_code == 503
assert services["hk"].vm["state"] == "running"
assert not any(
item[0] == "service_suspend" for item in services["hk"].calls)
monkeypatch.setattr(store, "set", original_set)
recovered = call(
client, "GET", f"/internal/v1/operations/{operation_id}", USER)
assert recovered.status_code == 200
assert recovered.json()["status"] == "failed"
assert services["hk"].vm["state"] == "running"
assert not any(
item[0] == "service_suspend" for item in services["hk"].calls)
def test_metrics_query_uses_log_store_and_owner_permission(boundary):
client, engine, *_ = boundary
@ -458,4 +818,10 @@ def test_main_and_log_schema_do_not_create_python_accounts_or_cross_database_tab
cursor.reset_mock()
LogStore({**config, "database": "logs"}, b"m" * 32)
statements = [call.args[0] for call in cursor.execute.call_args_list]
assert len(statements) == 1 and "engine_vm_metric_samples" in statements[0]
assert "engine_vm_metric_samples" in statements[0]
assert "engine_vm_traffic_totals" in statements[1]
assert "engine_vm_traffic_checkpoints" in statements[2]
assert all("ALTER TABLE engine_vm_metric_samples" in statement
for statement in statements[3:])
assert LogStore.TRAFFIC_TABLE == "engine_vm_traffic_totals"
assert LogStore.TRAFFIC_CHECKPOINT_TABLE == "engine_vm_traffic_checkpoints"

@ -260,6 +260,36 @@ class FakeDomain:
self.auto_start = bool(enabled)
self.backend.domain_autostart_changed.append(self._uuid)
@staticmethod
def _interface_identity(interface):
mac = interface.find("mac")
target = interface.find("target")
source = interface.find("source")
return (
mac.get("address") if mac is not None else None,
target.get("dev") if target is not None else None,
tuple(sorted(source.attrib.items())) if source is not None else (),
)
def updateDeviceFlags(self, xml, flags):
assert flags in (1, 2)
replacement = self._parse_xml(xml)
roots = [self.live_root] if flags == 1 else [self.inactive_root]
identity = self._interface_identity(replacement)
for root in roots:
if root is None:
raise FakeLibvirtError("requested domain definition is unavailable")
devices = root.find("devices")
for index, interface in enumerate(devices.findall("interface")):
if self._interface_identity(interface) == identity:
devices.remove(interface)
devices.insert(index, copy.deepcopy(replacement))
state = replacement.find("link").get("state")
self.backend.device_updates.append((self._uuid, state, flags))
break
else:
raise FakeLibvirtError("interface not found")
def isActive(self):
return self.running
@ -468,6 +498,7 @@ class FakeBackend:
self.started = []
self.autostart_changed = []
self.domain_autostart_changed = []
self.device_updates = []
self.destroyed = []
self.undefined = []
self.undefine_flags = []
@ -512,6 +543,8 @@ class VMServiceTests(unittest.TestCase):
fake_libvirt.VIR_DOMAIN_UNDEFINE_NVRAM = 4
fake_libvirt.VIR_DOMAIN_UNDEFINE_KEEP_NVRAM = 8
fake_libvirt.VIR_DOMAIN_XML_INACTIVE = 1
fake_libvirt.VIR_DOMAIN_AFFECT_LIVE = 1
fake_libvirt.VIR_DOMAIN_AFFECT_CONFIG = 2
fake_libvirt.open = self.backend.open
module_spec = importlib.util.spec_from_file_location("service_under_test", SERVICE_PATH)
self.assertIsNotNone(module_spec)
@ -1174,6 +1207,127 @@ class VMServiceTests(unittest.TestCase):
self.assertFalse(self.backend.pools["images"].volumes)
self.assertFalse(self.backend.domains)
def test_service_suspend_and_resume_are_idempotent_target_states(self):
vm = self.service.create_vm(self.spec())
domain = self.backend.domains[0]
self.assertTrue(domain.autostart())
suspended = self.service.service_suspend(vm["uuid"])
self.assertEqual(suspended["state"], "shutoff")
self.assertFalse(suspended["autostart"])
self.assertTrue(suspended["service_was_running"])
self.assertEqual(self.backend.shutdown_calls, 1)
self.assertEqual(self.backend.destroy_calls, 0)
repeated = self.service.service_suspend(vm["uuid"])
self.assertEqual(repeated["state"], "shutoff")
self.assertFalse(repeated["service_was_running"])
self.assertEqual(self.backend.shutdown_calls, 1)
self.assertEqual(self.backend.destroy_calls, 0)
resumed = self.service.service_resume(vm["uuid"])
self.assertEqual(resumed["state"], "shutoff")
self.assertTrue(resumed["autostart"])
self.service.service_resume(vm["uuid"])
self.assertEqual(self.backend.domain_starts, 0)
# Creation, suspension and resumption each change autostart once;
# target-state retries do not issue duplicate writes.
self.assertEqual(self.backend.domain_autostart_changed,
[vm["uuid"], vm["uuid"], vm["uuid"]])
def test_service_resume_starts_only_when_expiry_policy_stopped_running_vm(self):
vm = self.service.create_vm(self.spec())
domain = self.backend.domains[0]
self.service.service_suspend(vm["uuid"])
resumed = self.service.service_resume(
vm["uuid"], start_if_suspended=True)
self.assertEqual(resumed["state"], "running")
self.assertTrue(resumed["autostart"])
self.assertEqual(self.backend.domain_starts, 1)
# Replaying the same target state after a lost response is idempotent.
self.service.service_resume(vm["uuid"], start_if_suspended=True)
self.assertEqual(self.backend.domain_starts, 1)
self.service.service_suspend(vm["uuid"])
self.service.service_resume(vm["uuid"], start_if_suspended=False)
self.assertFalse(domain.isActive())
self.assertEqual(self.backend.domain_starts, 1)
def test_service_suspend_forces_stop_after_graceful_timeout(self):
vm = self.service.create_vm(self.spec())
self.backend.shutdown_stalls = True
with patch.object(self.service_module, "SERVICE_SUSPEND_SHUTDOWN_TIMEOUT", 0):
suspended = self.service.service_suspend(vm["uuid"])
self.assertEqual(suspended["state"], "shutoff")
self.assertEqual(self.backend.shutdown_calls, 1)
self.assertEqual(self.backend.destroy_calls, 1)
self.assertFalse(self.backend.domains[0].autostart())
def test_service_suspend_rejects_transient_vm_before_stopping_it(self):
vm = self.service.create_vm(self.spec())
domain = self.backend.domains[0]
domain.persistent = False
with self.assertRaises(self.error_type) as error:
self.service.service_suspend(vm["uuid"])
self.assertEqual(error.exception.status, 409)
self.assertTrue(domain.isActive())
self.assertEqual(self.backend.shutdown_calls, 0)
def test_network_target_states_cover_all_nics_live_and_persistent(self):
vm = self.service.create_vm(self.spec())
domain = self.backend.domains[0]
for root in (domain.live_root, domain.inactive_root):
first = root.find("./devices/interface")
first.insert(0, ET.Element("mac", address="52:54:00:00:00:01"))
second = copy.deepcopy(first)
second.find("mac").set("address", "52:54:00:00:00:02")
root.find("devices").append(second)
autostart_changes = list(self.backend.domain_autostart_changed)
suspended = self.service.network_suspend(vm["uuid"])
self.assertTrue(domain.isActive())
self.assertTrue(suspended["network_suspended"])
for root in (domain.live_root, domain.inactive_root):
self.assertEqual(
[node.find("link").get("state")
for node in root.findall("./devices/interface")],
["down", "down"],
)
self.assertEqual(len(self.backend.device_updates), 4)
self.service.network_suspend(vm["uuid"])
self.assertEqual(len(self.backend.device_updates), 4)
resumed = self.service.network_resume(vm["uuid"])
self.assertTrue(domain.isActive())
self.assertFalse(resumed["network_suspended"])
for root in (domain.live_root, domain.inactive_root):
self.assertEqual(
[node.find("link").get("state")
for node in root.findall("./devices/interface")],
["up", "up"],
)
self.service.network_resume(vm["uuid"])
self.assertEqual(len(self.backend.device_updates), 8)
self.assertEqual(self.backend.domain_autostart_changed, autostart_changes)
def test_network_target_state_on_stopped_vm_changes_config_only(self):
vm = self.service.create_vm(self.spec())
self.service.action(vm["uuid"], "shutdown")
domain = self.backend.domains[0]
self.assertFalse(domain.isActive())
suspended = self.service.network_suspend(vm["uuid"])
self.assertEqual(suspended["state"], "shutoff")
self.assertEqual(self.backend.device_updates[-1][1:], ("down", 2))
self.assertEqual(len(self.backend.device_updates), 1)
resumed = self.service.network_resume(vm["uuid"])
self.assertEqual(resumed["state"], "shutoff")
self.assertEqual(self.backend.device_updates[-1][1:], ("up", 2))
self.assertEqual(len(self.backend.device_updates), 2)
def test_legacy_panel_uuid_disk_can_still_be_deleted(self):
vm = self.service.create_vm(self.spec())
self.service.action(vm["uuid"], "shutdown")
@ -1889,6 +2043,31 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(self.backend.destroyed, [])
self.assertTrue(service.delete_rescue_iso(RESCUE_NAME)["deleted"])
def test_traffic_suspension_survives_rescue_exit_until_network_resume(self):
service = self.cloud_service()
created = self.create_cloud_vm(service, self.cloud_spec())
self.rescue_volume()
original_xml = service.prepare_rescue_vm(created["uuid"], RESCUE_NAME)
service.enter_rescue_vm(created["uuid"], RESCUE_NAME, original_xml)
service.network_suspend(created["uuid"])
exited = service.exit_rescue_vm(
created["uuid"], original_xml, network_suspended=True)
self.assertFalse(exited["vm"]["rescue_mode"])
domain = self.backend.domains[0]
inactive = ET.fromstring(domain.XMLDesc(
self.service_module.libvirt.VIR_DOMAIN_XML_INACTIVE))
live = ET.fromstring(domain.XMLDesc(0))
self.assertTrue(service._interface_links_match(inactive, "down"))
self.assertTrue(service._interface_links_match(live, "down"))
service.network_resume(created["uuid"])
inactive = ET.fromstring(domain.XMLDesc(
self.service_module.libvirt.VIR_DOMAIN_XML_INACTIVE))
live = ET.fromstring(domain.XMLDesc(0))
self.assertTrue(service._interface_links_match(inactive, "up"))
self.assertTrue(service._interface_links_match(live, "up"))
def test_stopped_vm_can_start_directly_in_rescue_mode(self):
service = self.cloud_service()
created = self.create_cloud_vm(service, self.cloud_spec())
@ -2234,12 +2413,17 @@ class VMServiceTests(unittest.TestCase):
UUIDString=lambda: "00000000-0000-4000-8000-000000000001",
info=lambda: (1, 2048 * 1024, 2048 * 1024, 2, 0),
ID=lambda: 7,
XMLDesc=lambda _flags: ("<domain><devices><interface>"
"<mac address='52:54:00:12:34:56'/>"
"<target dev='vnet9'/>"
"</interface></devices></domain>"),
)
calls = []
def get_stats(_connection, groups, flags):
calls.append((groups, flags))
return [(domain, {"cpu.time": 123, "net.0.rx.bytes": 456})]
return [(domain, {"cpu.time": 123, "net.count": 1,
"net.0.name": "vnet9", "net.0.rx.bytes": 456})]
with patch.object(FakeConnection, "getAllDomainStats", get_stats, create=True):
result = self.service.metric_counters()
@ -2248,6 +2432,8 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(result[0].generation, 7)
self.assertEqual(result[0].vcpus, 2)
self.assertEqual(result[0].values["net.0.rx.bytes"], 456)
self.assertEqual(result[0].values["net.0.identity"],
"mac:52:54:00:12:34:56")
self.assertEqual(self.backend.opened, self.backend.closed)
def test_metric_counters_skip_vm_disappearing_after_bulk_read(self):

@ -2,6 +2,7 @@
from __future__ import annotations
import json
import threading
import pytest
@ -178,6 +179,12 @@ class FakeCursor:
self.commands = []
self.delete_counts = list(delete_counts)
self.rowcount = 0
self.one = {
"last_observed_at": None,
"network_baseline_json": "{}",
"download_rx_bytes": 0,
"upload_tx_bytes": 0,
}
def __enter__(self):
return self
@ -197,6 +204,9 @@ class FakeCursor:
def fetchall(self):
return self.rows
def fetchone(self):
return self.one
class FakeConnection:
def __init__(self, rows=(), delete_counts=()):
@ -222,13 +232,17 @@ def test_metric_and_user_tables_created_with_record_table():
store = DatabaseStore.__new__(DatabaseStore)
store._connect = lambda: connection
store._initialize_schema()
assert len(connection.fake_cursor.commands) == 6
assert len(connection.fake_cursor.commands) == 13
assert "vm_metric_samples" in connection.fake_cursor.commands[1][0]
assert "vm_metric_retention" in connection.fake_cursor.commands[1][0]
assert "CREATE TABLE IF NOT EXISTS users" in connection.fake_cursor.commands[2][0]
assert "CREATE TABLE IF NOT EXISTS registration_codes" in connection.fake_cursor.commands[3][0]
assert "CREATE TABLE IF NOT EXISTS vm_ownership" in connection.fake_cursor.commands[4][0]
assert "CREATE TABLE IF NOT EXISTS user_ssh_keys" in connection.fake_cursor.commands[5][0]
assert "CREATE TABLE IF NOT EXISTS vm_traffic_totals" in connection.fake_cursor.commands[2][0]
assert "CREATE TABLE IF NOT EXISTS vm_traffic_checkpoints" in connection.fake_cursor.commands[3][0]
assert all("ALTER TABLE vm_metric_samples" in connection.fake_cursor.commands[index][0]
for index in range(4, 9))
assert "CREATE TABLE IF NOT EXISTS users" in connection.fake_cursor.commands[9][0]
assert "CREATE TABLE IF NOT EXISTS registration_codes" in connection.fake_cursor.commands[10][0]
assert "CREATE TABLE IF NOT EXISTS vm_ownership" in connection.fake_cursor.commands[11][0]
assert "CREATE TABLE IF NOT EXISTS user_ssh_keys" in connection.fake_cursor.commands[12][0]
assert connection.commits == 1 and connection.closed
@ -249,14 +263,20 @@ def test_metric_store_prunes_physically_and_queries_only_scoped_recent_data():
point = {"vm_id": VM_ID, "timestamp": 1000, "cpu_percent": 12.5,
"memory_used_mib": 1024, "memory_total_mib": 2048,
"disk_read_bps": None, "disk_write_bps": None,
"network_rx_bps": 256, "network_tx_bps": 0}
"network_rx_bps": 256, "network_tx_bps": 0,
"network_devices": {}}
store.save_metric_samples(scope, [point])
store.prune_metric_samples(500)
result = store.query_metric_samples(scope, VM_ID, since=500, until=1100,
interval_seconds=60, limit=600)
assert result[0]["cpu_percent"] == 12.5
assert result[0]["disk_read_bps"] is None
insert, delete, query = connection.fake_cursor.commands
traffic_insert, locked, traffic_update, checkpoint, insert, delete, query = connection.fake_cursor.commands
assert "INSERT IGNORE INTO vm_traffic_totals" in traffic_insert[0]
assert "FOR UPDATE" in locked[0]
assert "UPDATE vm_traffic_totals" in traffic_update[0]
assert "INSERT IGNORE INTO vm_traffic_checkpoints" in checkpoint[0]
assert checkpoint[1] == (scope, VM_ID, 0, None, None, 1000, 0, 0)
assert "INSERT INTO vm_metric_samples" in insert[0]
assert "DELETE FROM vm_metric_samples WHERE sampled_at < %s" in delete[0]
assert delete[1] == (500, 5000)
@ -266,6 +286,215 @@ def test_metric_store_prunes_physically_and_queries_only_scoped_recent_data():
assert connection.commits == 2 and connection.closed
class TrafficBackend:
"""Tiny transaction fake for durable accounting behavior tests."""
def __init__(self):
self.states = {}
self.samples = {}
self.checkpoints = {}
self.commands = []
self.commits = 0
def connection(self):
backend = self
class Cursor:
selected = None
def __enter__(self):
return self
def __exit__(self, *_args):
return None
def execute(self, sql, args=None):
backend.commands.append((sql, args))
if sql.startswith("INSERT IGNORE INTO vm_traffic_totals"):
key = args[0], args[1]
backend.states.setdefault(key, {
"host_id": args[2], "user_id": args[3],
"last_observed_at": None,
"network_baseline_json": "{}",
"download_rx_bytes": 0, "upload_tx_bytes": 0,
})
elif sql.startswith("SELECT last_observed_at"):
self.selected = backend.states[(args[0], args[1])]
elif sql.startswith("UPDATE vm_traffic_totals"):
key = args[-2], args[-1]
backend.states[key].update({
"host_id": args[0], "user_id": args[1],
"last_observed_at": args[2],
"network_baseline_json": args[4],
"download_rx_bytes": args[5], "upload_tx_bytes": args[6],
})
elif sql.startswith("INSERT IGNORE INTO vm_traffic_checkpoints"):
key = args[0], args[1], args[2]
backend.checkpoints.setdefault(key, {
"host_id": args[3], "user_id": args[4],
"first_observed_at": args[5],
"download_rx_bytes": args[6], "upload_tx_bytes": args[7],
})
elif sql.startswith("INSERT INTO vm_metric_samples"):
key = args[0], args[1], args[4]
# The production ON DUPLICATE clause deliberately keeps
# the first interval byte deltas immutable.
backend.samples.setdefault(key, {
"network_rx_bytes": args[-2], "network_tx_bytes": args[-1],
"host_id": args[2], "user_id": args[3],
})
def fetchone(self):
return dict(self.selected)
class Connection:
def cursor(self):
return Cursor()
def commit(self):
backend.commits += 1
def rollback(self):
raise AssertionError("unexpected rollback")
def close(self):
return None
return Connection()
def traffic_point(timestamp, devices, *, host_id="hk", user_id=41):
return {
"vm_id": VM_ID, "timestamp": timestamp,
"cpu_percent": None, "memory_used_mib": None, "memory_total_mib": None,
"disk_read_bps": None, "disk_write_bps": None,
"network_rx_bps": None, "network_tx_bps": None,
"network_devices": devices, "host_id": host_id, "user_id": user_id,
}
def test_durable_traffic_baseline_survives_store_restart_and_is_idempotent():
scope = host_scope({**HOST, "host_id": "hk"})
backend = TrafficBackend()
first = DatabaseStore.__new__(DatabaseStore)
first._connect = backend.connection
first.save_metric_samples(scope, [traffic_point(1000, {"vnet0": [1000, 2000]})])
# A fresh object represents an engine restart; the baseline is in MariaDB.
restarted = DatabaseStore.__new__(DatabaseStore)
restarted._connect = backend.connection
restarted.save_metric_samples(scope, [traffic_point(1060, {"vnet0": [1250, 2400]})])
state = backend.states[(scope, VM_ID)]
assert state["download_rx_bytes"] == 250 # guest download
assert state["upload_tx_bytes"] == 400 # guest upload
assert json.loads(state["network_baseline_json"]) == {"vnet0": [1250, 2400]}
assert backend.samples[(scope, VM_ID, 1060)] == {
"network_rx_bytes": 250, "network_tx_bytes": 400,
"host_id": "hk", "user_id": 41,
}
assert backend.checkpoints[(scope, VM_ID, 0)] == {
"host_id": "hk", "user_id": 41, "first_observed_at": 1000,
"download_rx_bytes": 0, "upload_tx_bytes": 0,
}
# Duplicate and older observations neither advance totals nor replace the
# durable baseline used by the next valid interval.
restarted.save_metric_samples(scope, [traffic_point(1060, {"vnet0": [9999, 9999]})])
restarted.save_metric_samples(scope, [traffic_point(1030, {"vnet0": [8000, 8000]})])
restarted.save_metric_samples(scope, [traffic_point(1120, {"vnet0": [1300, 2500]})])
state = backend.states[(scope, VM_ID)]
assert state["download_rx_bytes"] == 300
assert state["upload_tx_bytes"] == 500
assert backend.samples[(scope, VM_ID, 1060)]["network_rx_bytes"] == 250
def test_incomplete_network_frame_does_not_replace_durable_baseline():
scope = host_scope({**HOST, "host_id": "hk"})
backend = TrafficBackend()
store = DatabaseStore.__new__(DatabaseStore)
store._connect = backend.connection
identity = "mac:52:54:00:12:34:56"
store.save_metric_samples(scope, [traffic_point(1000, {identity: [1000, 2000]})])
store.save_metric_samples(scope, [traffic_point(1060, None)])
state = backend.states[(scope, VM_ID)]
assert state["last_observed_at"] == 1000
assert json.loads(state["network_baseline_json"]) == {identity: [1000, 2000]}
assert backend.samples[(scope, VM_ID, 1060)]["network_rx_bytes"] is None
store.save_metric_samples(scope, [traffic_point(1120, {identity: [1400, 2600]})])
state = backend.states[(scope, VM_ID)]
assert state["download_rx_bytes"] == 400
assert state["upload_tx_bytes"] == 600
def test_same_mac_counter_reset_and_host_tap_rename_still_accumulates():
previous = {"mac:52:54:00:12:34:56": [9000, 12000]}
current = {"mac:52:54:00:12:34:56": [700, 1100]}
assert DatabaseStore._traffic_increment(previous, current) == (700, 1100)
def test_traffic_rollbacks_new_and_removed_nics_do_not_hide_stable_nics():
previous = {"vnet0": [1000, 2000], "vnet1": [500, 800], "removed": [9, 9]}
current = {"vnet0": [10, 20], "vnet1": [650, 1100], "new": [9999, 9999]}
# vnet0 is a known stable identity whose counters restarted at zero, so
# its post-reset bytes remain billable. New/removed NICs establish/no
# longer have a baseline, while the stable vnet1 increment is exact.
assert DatabaseStore._traffic_increment(previous, current) == (160, 320)
def test_sampler_rejects_frame_with_duplicate_network_identity():
sampler = MetricSampler()
point = sampler.observe(host_scope(HOST), raw(extra={
"net.count": 4,
"net.1.name": "vnet2", "net.1.rx.bytes": 3000, "net.1.tx.bytes": 4000,
"net.2.name": "duplicate", "net.2.rx.bytes": 1, "net.2.tx.bytes": 2,
"net.3.name": "duplicate", "net.3.rx.bytes": 3, "net.3.tx.bytes": 4,
}), timestamp=1000, monotonic=10)
assert point["network_devices"] is None
def test_sampler_prefers_stable_network_identity_over_renamed_host_tap():
sampler = MetricSampler()
point = sampler.observe(host_scope(HOST), raw(extra={
"net.0.identity": "mac:52:54:00:12:34:56",
}), timestamp=1000, monotonic=10)
assert point["network_devices"] == {
"mac:52:54:00:12:34:56": [1000, 2000],
}
def test_collector_associates_host_and_current_owner_without_hiding_unowned_vms():
saved = []
class Store:
def save_metric_samples(self, scope, points):
saved.append((scope, points))
def prune_metric_samples(self, _cutoff):
return 0
class Service:
def metric_counters(self):
second = raw()
return [second, RawVMStats(
"00000000-0000-4000-8000-000000000002", second.generation,
second.vcpus, second.values)]
owners = {VM_ID: 41}
profile = {**HOST, "host_id": "hk", "key_locked": False}
collector = MetricsCollector(
lambda: Store(), lambda: profile, lambda _profile: Service(),
owner_getter=lambda _scope, vm_id: owners.get(vm_id),
)
collector.sample_once(timestamp=1000, monotonic=10)
_, points = saved[0]
assert [(point["host_id"], point["user_id"]) for point in points] == [
("hk", 41), ("hk", None),
]
assert all(point["network_devices"] == {"vnet1": [1000, 2000]}
for point in points)
def test_metric_prune_is_one_index_ordered_batch_per_transaction():
connection = FakeConnection(delete_counts=[5000] * 10)
store = DatabaseStore.__new__(DatabaseStore)

@ -256,6 +256,9 @@ class ExitRescueBody(BaseModel):
model_config = ConfigDict(extra="forbid")
confirmation_name: str = Field(min_length=1, max_length=63)
# PHP's billing ledger supplies the authoritative target. The original
# XML was saved before rescue mode and may predate a traffic-quota block.
network_suspended: bool = False
class ForceRescueBody(BaseModel):
@ -271,6 +274,15 @@ class ActionBody(BaseModel):
action: Literal["start", "shutdown", "reboot", "poweroff", "reset"]
class ServiceResumeBody(BaseModel):
model_config = ConfigDict(extra="forbid")
# Only restart a guest that the expiry policy itself stopped. A tenant
# that had already shut the VM down before expiry must remain shut down
# after renewal.
start_if_suspended: bool = False
def _public_host(profile: dict | None) -> dict:
if profile is None:
return {"configured": False}
@ -731,6 +743,7 @@ def create_app(
lambda: metric_store if metric_store is not None else data_store,
metric_profile,
lambda profile: new_service(profile),
owner_getter=lambda scope, vm_id: state.users.owner_of_vm(scope, vm_id),
) if database_mode else None
@asynccontextmanager
@ -897,6 +910,11 @@ def create_app(
raise HTTPException(status_code=403, detail="账号等待管理员批准")
return session
def require_internal_service_write(request: Request) -> Session:
if not internal_mode:
raise HTTPException(status_code=404, detail="Not Found")
return require_approved_write(request)
def require_vm_access(session: Session, profile: dict, vm_id: str) -> str:
if not database_mode:
return vm_id
@ -2161,7 +2179,8 @@ def create_app(
return {"acknowledged": True}
def run_rescue_job(job_id: str, profile: dict, vm_id: str,
operation: Literal["enter", "exit", "force"]) -> None:
operation: Literal["enter", "exit", "force"],
network_suspended: bool = False) -> None:
vm_scope = host_scope(profile)
key = (vm_scope, vm_id)
completed = False
@ -2188,7 +2207,9 @@ def create_app(
result = service.force_start_staged_rescue_vm(
vm_id, state_record["original_xml"])
else:
result = service.exit_rescue_vm(vm_id, state_record["original_xml"])
result = service.exit_rescue_vm(
vm_id, state_record["original_xml"],
network_suspended=network_suspended)
completed = True
staged = (operation == "enter" and isinstance(result, dict)
and result.get("pending_manual_restart") is True)
@ -2417,7 +2438,9 @@ def create_app(
save_rescue_state(profile, canonical_id, record)
pending_rescues.add(key)
try:
rescue_executor.submit(run_rescue_job, job_id, profile, canonical_id, "exit")
rescue_executor.submit(
run_rescue_job, job_id, profile, canonical_id, "exit",
body.network_suspended)
except RuntimeError as exc:
pending_rescues.discard(key)
record["status"] = "active"
@ -2443,6 +2466,55 @@ def create_app(
return {key: value for key, value in job.items()
if key not in {"scope", "owner", "vm_id"}}
def apply_private_vm_target(vm_id: str, session: Session,
method: str, label: str, **options) -> dict:
with reinstall_lock, rescue_lock:
forbid_long_job_during_update()
profile = current_profile()
canonical_id = require_vm_access(session, profile, vm_id)
if (template_scope(profile), canonical_id) in pending_reinstalls:
raise VMError(f"虚拟机正在重装系统,暂时不能执行{label}", 409)
if (host_scope(profile), canonical_id) in pending_rescues:
raise VMError(f"虚拟机正在切换救援模式,暂时不能执行{label}", 409)
vm = getattr(new_service(profile), method)(canonical_id, **options)
LOG.info("vm target state applied: uuid=%s target=%s actor=%s",
vm.get("uuid"), method, session.username)
return vm
@app.post("/api/v1/vms/{vm_id}/service-suspend")
def service_suspend_vm(
vm_id: str,
_session: Session = Depends(require_internal_service_write),
):
return apply_private_vm_target(
vm_id, _session, "service_suspend", "服务暂停")
@app.post("/api/v1/vms/{vm_id}/service-resume")
def service_resume_vm(
vm_id: str,
body: ServiceResumeBody,
_session: Session = Depends(require_internal_service_write),
):
return apply_private_vm_target(
vm_id, _session, "service_resume", "服务恢复",
start_if_suspended=body.start_if_suspended)
@app.post("/api/v1/vms/{vm_id}/network-suspend")
def network_suspend_vm(
vm_id: str,
_session: Session = Depends(require_internal_service_write),
):
return apply_private_vm_target(
vm_id, _session, "network_suspend", "网络暂停")
@app.post("/api/v1/vms/{vm_id}/network-resume")
def network_resume_vm(
vm_id: str,
_session: Session = Depends(require_internal_service_write),
):
return apply_private_vm_target(
vm_id, _session, "network_resume", "网络恢复")
@app.post("/api/v1/vms/{vm_id}/actions")
def action(vm_id: str, body: ActionBody, _session: Session = Depends(require_approved_write)):
with reinstall_lock, rescue_lock:

@ -8,6 +8,7 @@ non-secret executable wrapper is written to the panel's data directory.
from __future__ import annotations
import hashlib
import hmac
import json
import logging
@ -72,6 +73,8 @@ _METRIC_SCHEMA = """
CREATE TABLE IF NOT EXISTS vm_metric_samples (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
user_id BIGINT UNSIGNED NULL,
sampled_at BIGINT UNSIGNED NOT NULL,
cpu_percent DOUBLE NULL,
memory_used_mib DOUBLE NULL,
@ -80,10 +83,56 @@ CREATE TABLE IF NOT EXISTS vm_metric_samples (
disk_write_bps DOUBLE NULL,
network_rx_bps DOUBLE NULL,
network_tx_bps DOUBLE NULL,
network_rx_bytes BIGINT UNSIGNED NULL,
network_tx_bytes BIGINT UNSIGNED NULL,
PRIMARY KEY (host_scope, vm_uuid, sampled_at),
KEY vm_metric_retention (sampled_at)
KEY vm_metric_retention (sampled_at),
KEY vm_metric_owner (user_id, host_id, sampled_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_TRAFFIC_SCHEMA = """
CREATE TABLE IF NOT EXISTS vm_traffic_totals (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
user_id BIGINT UNSIGNED NULL,
last_observed_at BIGINT UNSIGNED NULL,
network_signature CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
network_baseline_json JSON NOT NULL,
download_rx_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
upload_tx_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
updated_at BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (host_scope, vm_uuid),
KEY vm_traffic_owner (user_id, host_id),
KEY vm_traffic_host (host_id, vm_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_TRAFFIC_CHECKPOINT_SCHEMA = """
CREATE TABLE IF NOT EXISTS vm_traffic_checkpoints (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
checkpoint_hour BIGINT UNSIGNED NOT NULL,
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
user_id BIGINT UNSIGNED NULL,
first_observed_at BIGINT UNSIGNED NOT NULL,
download_rx_bytes BIGINT UNSIGNED NOT NULL,
upload_tx_bytes BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (host_scope, vm_uuid, checkpoint_hour),
KEY vm_traffic_checkpoint_owner (user_id, host_id, first_observed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_METRIC_MIGRATIONS = (
"ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS "
"host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL AFTER vm_uuid",
"ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS "
"user_id BIGINT UNSIGNED NULL AFTER host_id",
"ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS "
"network_rx_bytes BIGINT UNSIGNED NULL AFTER network_tx_bps",
"ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS "
"network_tx_bytes BIGINT UNSIGNED NULL AFTER network_rx_bytes",
"ALTER TABLE {metric_table} ADD INDEX IF NOT EXISTS "
"vm_metric_owner (user_id, host_id, sampled_at)",
)
_USERS_SCHEMA = """
CREATE TABLE IF NOT EXISTS users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
@ -142,7 +191,9 @@ _METRIC_COLUMNS = (
"disk_write_bps", "network_rx_bps", "network_tx_bps",
)
_HOST_SCOPE = re.compile(r"[0-9a-f]{64}\Z")
_HOST_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}\Z")
_METRIC_PRUNE_BATCH_SIZE = 5000
_MAX_UNSIGNED_BIGINT = (1 << 64) - 1
LOG = logging.getLogger(__name__)
@ -164,6 +215,8 @@ class DatabaseStore:
RECORD_TABLE = "panel_records"
METRIC_TABLE = "vm_metric_samples"
TRAFFIC_TABLE = "vm_traffic_totals"
TRAFFIC_CHECKPOINT_TABLE = "vm_traffic_checkpoints"
def __init__(self, config: Mapping[str, Any], master_key: bytes) -> None:
if pymysql is None:
@ -229,6 +282,10 @@ class DatabaseStore:
with connection.cursor() as cursor:
cursor.execute(_SCHEMA)
cursor.execute(_METRIC_SCHEMA)
cursor.execute(_TRAFFIC_SCHEMA)
cursor.execute(_TRAFFIC_CHECKPOINT_SCHEMA)
for statement in _METRIC_MIGRATIONS:
cursor.execute(statement.format(metric_table=self.METRIC_TABLE))
cursor.execute(_USERS_SCHEMA)
cursor.execute(_REGISTRATION_CODE_SCHEMA)
cursor.execute(_VM_OWNERSHIP_SCHEMA)
@ -257,7 +314,8 @@ class DatabaseStore:
for point in points:
vm_id = self._metric_identity(host_scope, point["vm_id"])
timestamp = point["timestamp"]
if isinstance(timestamp, bool) or not isinstance(timestamp, int) or timestamp < 0:
if (isinstance(timestamp, bool) or not isinstance(timestamp, int)
or timestamp < 0 or timestamp > _MAX_UNSIGNED_BIGINT):
raise ValueError("Metric timestamp is invalid")
metrics = []
for name in _METRIC_COLUMNS:
@ -267,25 +325,174 @@ class DatabaseStore:
or not math.isfinite(value) or value < 0)):
raise ValueError("Metric value is invalid")
metrics.append(value)
rows.append((host_scope, vm_id, timestamp, *metrics))
host_id = point.get("host_id")
if host_id is not None and (not isinstance(host_id, str)
or not _HOST_ID.fullmatch(host_id)):
raise ValueError("Metric host ID is invalid")
user_id = point.get("user_id")
if (user_id is not None and (isinstance(user_id, bool)
or not isinstance(user_id, int)
or user_id < 1
or user_id > _MAX_UNSIGNED_BIGINT)):
raise ValueError("Metric user ID is invalid")
raw_devices = point.get("network_devices")
# None means libvirt did not return a complete NIC counter frame.
# Never replace a durable baseline with incomplete data, otherwise
# the next healthy sample would look like a brand-new NIC and the
# whole gap would become free traffic. An explicit empty mapping
# remains a valid observation of a VM with no interfaces.
devices = (None if raw_devices is None
else self._normalize_traffic_devices(raw_devices))
rows.append((host_scope, vm_id, timestamp, host_id, user_id, metrics, devices))
# Keep accounting deterministic if a caller ever submits more than one
# observation for the same VM in a batch.
rows.sort(key=lambda row: (row[0], row[1], row[2]))
connection = self._connect()
try:
with connection.cursor() as cursor:
cursor.executemany(
f"INSERT INTO {self.METRIC_TABLE} (host_scope, vm_uuid, sampled_at, "
+ ", ".join(_METRIC_COLUMNS) + ") VALUES ("
+ ", ".join(["%s"] * (3 + len(_METRIC_COLUMNS))) + ") "
"ON DUPLICATE KEY UPDATE "
+ ", ".join(f"{name}=VALUES({name})" for name in _METRIC_COLUMNS),
rows,
)
for scope, vm_id, timestamp, host_id, user_id, metrics, devices in rows:
cursor.execute(
f"INSERT IGNORE INTO {self.TRAFFIC_TABLE} "
"(host_scope,vm_uuid,host_id,user_id,last_observed_at,network_signature,"
"network_baseline_json,download_rx_bytes,upload_tx_bytes,updated_at) "
"VALUES(%s,%s,%s,%s,NULL,%s,%s,0,0,%s)",
(scope, vm_id, host_id, user_id,
self._traffic_signature({}), "{}", timestamp),
)
cursor.execute(
f"SELECT last_observed_at,network_baseline_json,"
"download_rx_bytes,upload_tx_bytes "
f"FROM {self.TRAFFIC_TABLE} WHERE host_scope=%s AND vm_uuid=%s FOR UPDATE",
(scope, vm_id),
)
state = cursor.fetchone()
if not isinstance(state, Mapping):
raise DatabaseError("无法锁定虚拟机流量计量状态")
previous_at = state.get("last_observed_at")
if (previous_at is not None
and (isinstance(previous_at, bool)
or not isinstance(previous_at, int)
or previous_at < 0
or previous_at > _MAX_UNSIGNED_BIGINT)):
raise DatabaseError("虚拟机流量计量时间戳损坏")
rx_delta = tx_delta = None
if devices is not None and (previous_at is None or timestamp > previous_at):
previous = self._decode_traffic_baseline(
state.get("network_baseline_json"))
if previous_at is not None:
rx_delta, tx_delta = self._traffic_increment(previous, devices)
rx_total = self._traffic_total(state.get("download_rx_bytes"))
tx_total = self._traffic_total(state.get("upload_tx_bytes"))
rx_total = self._checked_traffic_sum(rx_total, rx_delta or 0)
tx_total = self._checked_traffic_sum(tx_total, tx_delta or 0)
cursor.execute(
f"UPDATE {self.TRAFFIC_TABLE} SET host_id=%s,user_id=%s,"
"last_observed_at=%s,network_signature=%s,network_baseline_json=%s,"
"download_rx_bytes=%s,upload_tx_bytes=%s,updated_at=%s "
"WHERE host_scope=%s AND vm_uuid=%s",
(host_id, user_id, timestamp, self._traffic_signature(devices),
json.dumps(devices, sort_keys=True, separators=(",", ":")),
rx_total, tx_total, timestamp, scope, vm_id),
)
# Full chart samples are retained for 30 days. This
# compact hourly ledger is retained independently so a
# long PHP lifecycle outage cannot erase the cumulative
# counter near a future prepaid-period boundary. The
# first observation in each hour is immutable.
cursor.execute(
f"INSERT IGNORE INTO {self.TRAFFIC_CHECKPOINT_TABLE} "
"(host_scope,vm_uuid,checkpoint_hour,host_id,user_id,"
"first_observed_at,download_rx_bytes,upload_tx_bytes) "
"VALUES(%s,%s,%s,%s,%s,%s,%s,%s)",
(scope, vm_id, timestamp - timestamp % 3600,
host_id, user_id, timestamp, rx_total, tx_total),
)
cursor.execute(
f"INSERT INTO {self.METRIC_TABLE} "
"(host_scope,vm_uuid,host_id,user_id,sampled_at,"
+ ",".join(_METRIC_COLUMNS)
+ ",network_rx_bytes,network_tx_bytes) VALUES ("
+ ",".join(["%s"] * (7 + len(_METRIC_COLUMNS))) + ") "
"ON DUPLICATE KEY UPDATE "
+ ",".join(f"{name}=VALUES({name})" for name in _METRIC_COLUMNS),
(scope, vm_id, host_id, user_id, timestamp, *metrics,
rx_delta, tx_delta),
)
connection.commit()
except pymysql.MySQLError as exc:
connection.rollback()
raise DatabaseError("无法保存虚拟机指标") from exc
except (DatabaseError, ValueError):
connection.rollback()
raise
finally:
connection.close()
@staticmethod
def _traffic_total(value: Any) -> int:
if (isinstance(value, bool) or not isinstance(value, int)
or value < 0 or value > _MAX_UNSIGNED_BIGINT):
raise DatabaseError("虚拟机流量计量状态损坏")
return value
@staticmethod
def _checked_traffic_sum(total: int, increment: int) -> int:
value = total + increment
if value > _MAX_UNSIGNED_BIGINT:
raise DatabaseError("虚拟机累计流量超过可计量范围")
return value
@staticmethod
def _normalize_traffic_devices(value: Any) -> dict[str, list[int]]:
if value is None:
return {}
if not isinstance(value, Mapping) or len(value) > 1024:
raise ValueError("VM network counters are invalid")
result: dict[str, list[int]] = {}
for name, counters in value.items():
if (not isinstance(name, str) or not 1 <= len(name) <= 255
or any(ord(char) < 32 or ord(char) == 127 for char in name)):
raise ValueError("VM network interface name is invalid")
if (not isinstance(counters, (list, tuple)) or len(counters) != 2
or any(isinstance(counter, bool) or not isinstance(counter, int)
or counter < 0 or counter > _MAX_UNSIGNED_BIGINT
for counter in counters)):
raise ValueError("VM network counters are invalid")
result[name] = [counters[0], counters[1]]
return result
@classmethod
def _decode_traffic_baseline(cls, value: Any) -> dict[str, list[int]]:
try:
decoded = json.loads(value) if isinstance(value, (str, bytes, bytearray)) else value
return cls._normalize_traffic_devices(decoded)
except (TypeError, ValueError, UnicodeError, json.JSONDecodeError) as exc:
raise DatabaseError("虚拟机流量计量基线损坏") from exc
@staticmethod
def _traffic_signature(devices: Mapping[str, list[int]]) -> str:
names = json.dumps(sorted(devices), ensure_ascii=False,
separators=(",", ":")).encode("utf-8")
return hashlib.sha256(names).hexdigest()
@staticmethod
def _traffic_increment(previous: Mapping[str, list[int]],
current: Mapping[str, list[int]]) -> tuple[int, int]:
"""Account stable NICs independently; rx is download and tx is upload."""
rx_total = tx_total = 0
for name, (rx, tx) in current.items():
old = previous.get(name)
# A genuinely new NIC establishes a baseline. For an existing
# stable identity a lower counter means QEMU/tap counters reset
# (normally a VM restart); bytes since zero are still billable.
# Treat directions independently because drivers need not reset
# both counters in exactly the same observation.
if old is None:
continue
rx_total += rx - old[0] if rx >= old[0] else rx
tx_total += tx - old[1] if tx >= old[1] else tx
return rx_total, tx_total
def prune_metric_samples(self, cutoff: int) -> int:
if isinstance(cutoff, bool) or not isinstance(cutoff, int) or cutoff < 0:
raise ValueError("Metric retention cutoff is invalid")

@ -10,7 +10,8 @@ from typing import Any
from .auth import Session
from .database import (DatabaseError, DatabaseRemoteHostManager, DatabaseStore,
DatabaseTemplateStore, _METRIC_SCHEMA, _SCHEMA, pymysql)
DatabaseTemplateStore, _METRIC_MIGRATIONS, _METRIC_SCHEMA,
_SCHEMA, _TRAFFIC_CHECKPOINT_SCHEMA, _TRAFFIC_SCHEMA, pymysql)
TRUSTED_ACTOR: ContextVar[dict | None] = ContextVar("cloud_trusted_actor", default=None)
@ -79,12 +80,19 @@ class MainStore(DatabaseStore):
class LogStore(DatabaseStore):
METRIC_TABLE = "engine_vm_metric_samples"
TRAFFIC_TABLE = "engine_vm_traffic_totals"
TRAFFIC_CHECKPOINT_TABLE = "engine_vm_traffic_checkpoints"
def _initialize_schema(self) -> None:
connection = self._connect()
try:
with connection.cursor() as cursor:
cursor.execute(_METRIC_SCHEMA.replace("vm_metric_samples", self.METRIC_TABLE))
cursor.execute(_TRAFFIC_SCHEMA.replace("vm_traffic_totals", self.TRAFFIC_TABLE))
cursor.execute(_TRAFFIC_CHECKPOINT_SCHEMA.replace(
"vm_traffic_checkpoints", self.TRAFFIC_CHECKPOINT_TABLE))
for statement in _METRIC_MIGRATIONS:
cursor.execute(statement.format(metric_table=self.METRIC_TABLE))
connection.commit()
except pymysql.MySQLError as exc:
connection.rollback()

@ -43,6 +43,7 @@ READ_PATHS = (
WRITE_PATHS = {
"POST": (r"host/(?:scan|unlock|test|discover-vms)", r"vm-templates/import", r"rescue-templates",
r"vms", rf"vms/{VM_ID}/(?:actions|credential|reinstall|rescue|console-ticket)",
rf"vms/{VM_ID}/(?:service|network)-(?:suspend|resume)",
rf"vms/{VM_ID}/rescue/(?:exit|force-start)", r"reinstall-jobs/[A-Za-z0-9_-]+/ack-password"),
"PUT": (r"host",),
"PATCH": (r"vm-templates/[A-Za-z0-9_-]+",),
@ -153,8 +154,22 @@ class EngineApplication:
self.registry = registry or HostRegistry(config, store, log_store)
self.introspection = introspection
self.active_operations: set[str] = set()
# Target-state operations are idempotent individually, but two
# different operation IDs can request opposite states at the same
# time (for example traffic suspension ending at a period boundary).
# Serialize those mutations per VM so the later request always wins
# on the hypervisor as well as in the billing database.
self.target_locks: dict[tuple[str, str], asyncio.Lock] = {}
self.console_check_interval = 15
def _target_lock(self, host_id: str, vm_id: str) -> asyncio.Lock:
key = (host_id, vm_id)
lock = self.target_locks.get(key)
if lock is None:
lock = asyncio.Lock()
self.target_locks[key] = lock
return lock
async def __call__(self, scope, receive, send):
if scope["type"] == "lifespan":
await self._lifespan(receive, send)
@ -283,7 +298,11 @@ class EngineApplication:
return
op_match = re.fullmatch(r"/internal/v1/operations/([A-Za-z0-9_-]{8,80})", path)
if scope["method"] == "GET" and op_match:
operation = await self._operation(op_match[1], delegated)
query = scope.get("query_string", b"")
if query not in {b"", b"reconcile=false"}:
raise EngineAuthError("操作查询参数无效", 400)
operation = await self._operation(
op_match[1], delegated, reconcile=query != b"reconcile=false")
await json_response(send, 200, self._public_operation(operation))
return
match = re.fullmatch(r"/internal/v1/hosts/([A-Za-z0-9][A-Za-z0-9_-]{0,63})/(.+)", path)
@ -300,7 +319,10 @@ class EngineApplication:
create = scope["method"] == "POST" and resource == "vms"
mutation = scope["method"] == "DELETE" and resource.startswith("vms/") or (
scope["method"] == "POST" and bool(re.fullmatch(
rf"vms/{VM_ID}/(?:actions|reinstall|rescue(?:/exit|/force-start)?)", resource)))
rf"vms/{VM_ID}/(?:actions|reinstall|"
rf"(?:service|network)-(?:suspend|resume)|"
rf"rescue(?:/exit|/force-start)?)",
resource)))
if create or mutation:
if not OP_ID.fullmatch(delegated.get("operation_id", "")):
raise EngineAuthError("虚拟机操作缺少持久幂等编号", 403)
@ -312,6 +334,9 @@ class EngineApplication:
await json_response(send, 422, self._public_operation(operation))
return
if not fresh:
if (operation["status"] in {"running", "pending", "uncertain"}
and self._service_target_method(operation.get("resource", ""))):
operation = await self._mutation_operation(operation)
if operation["status"] == "succeeded":
await json_response(send, 201 if create else 200,
{**operation["result"], **self._public_operation(operation)})
@ -334,7 +359,19 @@ class EngineApplication:
async def capture(message):
messages.append(message)
try:
await app(child_scope, receive, capture)
target_method = self._service_target_method(operation.get("resource", ""))
# Every VM mutation shares the same lock, not just billing target
# state. A start/reset/delete that timed out at PHP may still be
# executing here when expiry enforcement arrives. Serializing the
# later policy call guarantees that its desired state wins. The
# reinstall/rescue endpoints register their per-VM pending guard
# before returning 202; child endpoints reject every other
# mutation until those background jobs remove that guard.
async with self._target_lock(host_id, operation["vm_uuid"]):
if target_method == "service_suspend":
await self._capture_service_was_running_hint(
host_id, operation, delegated)
await app(child_scope, receive, capture)
status = next(item["status"] for item in messages if item["type"] == "http.response.start")
payload = json.loads(b"".join(item.get("body", b"") for item in messages
if item["type"] == "http.response.body"))
@ -390,8 +427,17 @@ class EngineApplication:
"request_hash": hmac.new(self.secret, method.encode() + b"\n" + target.encode() + b"\n" + (body or b""), hashlib.sha256).hexdigest(),
"vm_uuid": str(uuid.uuid4()) if create else str(uuid.UUID(resource.split("/")[1])),
"vm_name": value.get("name"), "kind": "create" if create else "mutation",
"actor_can_manage": bool(delegated.get("can_manage")),
"actor_is_admin": bool(delegated.get("is_admin")),
"resource": resource, "method": method, "status": "running", "result": None,
"safe_to_refund": False, "created_at": int(time.time())}
if resource.endswith("/service-resume"):
# Store only the allowlisted target-state option. It is needed to
# replay the exact same convergence request after a lost response;
# never persist arbitrary request bodies in the operation table.
record["target_options"] = {
"start_if_suspended": value.get("start_if_suspended") is True,
}
if error:
# No service, task, VM or volume mutation has been invoked.
record.update(status="failed", prevalidation_failed=True,
@ -441,11 +487,74 @@ class EngineApplication:
async def _mutation_operation(self, record):
await self.registry.get(record["host_id"])
components = self.registry.components[record["host_id"]]
target_method = self._service_target_method(record.get("resource", ""))
if target_method is not None:
if record["operation_id"] in self.active_operations:
return record
if (target_method == "service_suspend"
and not isinstance(record.get("service_was_running_hint"), bool)):
# Suspending without a durable pre-stop state can permanently
# lose whether a later paid renewal is allowed to restart the
# guest. Fail closed instead of replaying a destructive target
# after the database recovers.
record.update(
status="failed",
error="停机前运行状态未安全保存,已取消服务暂停",
finished_at=int(time.time()),
)
await asyncio.to_thread(
self.store.set, "operation", record["operation_id"],
json.dumps(record))
return record
profile = components.manager.current()
if profile is None or profile.get("key_locked"):
return record
if (not record.get("actor_can_manage")
or (not record.get("actor_is_admin")
and not components.state.users.owns_vm(
self._host_scope(profile), record["vm_uuid"], record["user_id"]))):
record.update(status="failed", error="虚拟机不存在", finished_at=int(time.time()))
await asyncio.to_thread(self.store.set, "operation", record["operation_id"],
json.dumps(record))
return record
self.active_operations.add(record["operation_id"])
try:
async with self._target_lock(record["host_id"], record["vm_uuid"]):
options = record.get("target_options", {}) \
if target_method == "service_resume" else {}
if not isinstance(options, dict) or any(
key != "start_if_suspended" for key in options):
options = {}
result = await asyncio.to_thread(
getattr(self._service(profile), target_method), record["vm_uuid"],
**options)
if (target_method == "service_suspend"
and isinstance(record.get("service_was_running_hint"), bool)):
result["service_was_running"] = (
bool(result.get("service_was_running"))
or record["service_was_running_hint"])
except Exception as exc:
from .libvirt_service import VMError
if isinstance(exc, VMError) and exc.status < 500:
record.update(status="failed", error=str(exc), finished_at=int(time.time()))
else:
record.update(status="uncertain",
error="服务状态尚未确认,引擎将安全重试")
else:
record.update(status="succeeded", result=result, error=None,
finished_at=int(time.time()))
finally:
self.active_operations.discard(record["operation_id"])
await asyncio.to_thread(self.store.set, "operation", record["operation_id"],
json.dumps(record))
return record
kind = "reinstall_job" if record["resource"].endswith("/reinstall") else "rescue_job" if "/rescue" in record["resource"] else None
if kind is None:
# A reset/shutdown/delete cannot be safely replayed merely because
# its response disappeared. Keep its uncertainty explicit.
return record
# its response disappeared. Once no executor can still own the
# operation, terminate it visibly instead of allowing a permanent
# running/uncertain row to defeat expiry enforcement.
return await self._finish_orphan_mutation(record)
scoped = components.store
job = None
job_id = record.get("job_id")
@ -460,7 +569,7 @@ class EngineApplication:
job_id, job = candidate_id, candidate
break
if job is None:
return record
return await self._finish_orphan_mutation(record)
if job.get("owner") != f"php:{record['user_id']}" or job.get("vm_id") != record["vm_uuid"]:
return record
record["job_id"] = job_id
@ -469,12 +578,69 @@ class EngineApplication:
status = job.get("status")
record["status"] = "succeeded" if status in {"completed", "succeeded"} else "running" if status in {"queued", "running"} else "failed"
if "面板重启" in job.get("phase", "") or "面板在任务执行期间重启" in job.get("error", ""):
record["status"] = "uncertain"
# The asynchronous job registry explicitly confirms that this job
# is no longer running. Replaying a reinstall/rescue mutation is
# unsafe, but leaving it uncertain forever would also grant free
# service by permanently blocking expiry enforcement.
record["status"] = "failed"
record["error"] = job.get("error") or "引擎重启导致任务中断,请核查虚拟机状态后重试"
if job.get("error"):
record["error"] = job["error"]
await asyncio.to_thread(self.store.set, "operation", record["operation_id"], json.dumps(record))
return record
async def _finish_orphan_mutation(self, record):
if (record["operation_id"] in self.active_operations
or int(time.time()) - int(record.get("created_at", 0)) < 120):
return record
record.update(
status="failed",
error="任务执行器已不存在,不会自动重放;请核查虚拟机实际状态",
finished_at=int(time.time()),
)
await asyncio.to_thread(
self.store.set, "operation", record["operation_id"], json.dumps(record))
return record
@staticmethod
def _service_target_method(resource: str) -> str | None:
match = re.fullmatch(
rf"vms/{VM_ID}/((?:service|network)-(?:suspend|resume))", resource)
if match is None:
return None
return {
"service-suspend": "service_suspend",
"service-resume": "service_resume",
"network-suspend": "network_suspend",
"network-resume": "network_resume",
}[match[1]]
@staticmethod
def _host_scope(profile):
from .metrics import host_scope
return host_scope(profile)
async def _capture_service_was_running_hint(self, host_id, operation, delegated):
# Run this inside the same per-VM lock as service_suspend. If the
# engine crashes after stopping QEMU but before saving the response, a
# replay otherwise sees only a shutoff guest and cannot know whether a
# later renewal is allowed to start it automatically.
components = self.registry.components[host_id]
profile = components.manager.current()
owns = (profile is not None and not profile.get("key_locked")
and (delegated.get("is_admin") or components.state.users.owns_vm(
self._host_scope(profile), operation["vm_uuid"], delegated["user_id"])))
if not owns:
return
before = await asyncio.to_thread(
self._service(profile).get_vm, operation["vm_uuid"])
operation["service_was_running_hint"] = before.get("state") == "running"
# This write is a prerequisite for stopping the guest. Propagate any
# failure so the child endpoint is never called without a durable hint.
await asyncio.to_thread(
self.store.set, "operation", operation["operation_id"],
json.dumps(operation))
async def _check_absent(self, operation):
operation["safe_to_refund"] = False
components = self.registry.components.get(operation["host_id"])
@ -501,10 +667,16 @@ class EngineApplication:
if key.startswith(prefix) and json.loads(raw).get("vm_uuid") == vm_id:
components.store.delete("ip_claim", key)
@staticmethod
def _public_operation(record):
return {key: record[key] for key in ("operation_id", "status", "result", "error", "safe_to_refund", "created_at", "finished_at")
if key in record}
def _public_operation(self, record):
result = {key: record[key] for key in (
"operation_id", "status", "result", "error", "safe_to_refund",
"service_was_running_hint", "created_at", "finished_at")
if key in record}
# This is deliberately live process state, not a persisted claim. PHP
# must not close a stale policy job while its original executor can
# still mutate libvirt. It retries after the executor becomes terminal.
result["executor_active"] = record.get("operation_id") in self.active_operations
return result
@staticmethod
def _service(profile):

@ -37,6 +37,8 @@ RESCUE_VOLUME_RE = re.compile(r"panel-rescue-[0-9a-f]{32}\.iso\Z")
_CREATE_LOCK = threading.RLock()
REINSTALL_SHUTDOWN_TIMEOUT = 120
REINSTALL_SHUTDOWN_POLL_INTERVAL = 1
SERVICE_SUSPEND_SHUTDOWN_TIMEOUT = 30
SERVICE_SUSPEND_SHUTDOWN_POLL_INTERVAL = 0.5
MIN_RESCUE_ISO_BYTES = 1024 * 1024
MAX_RESCUE_ISO_BYTES = 8 * GIB
STATES = {
@ -457,6 +459,36 @@ class VMService:
continue
if info[0] != 1 or generation < 0:
continue
# ``net.N.name`` is normally the host tap device (vnet0,
# vnet1, ...), which can change every time QEMU starts. Add
# the guest NIC MAC as a stable billing identity so a reboot
# cannot make the first interval on the renamed tap free.
values = dict(values)
try:
root = ET.fromstring(domain.XMLDesc(0))
by_target: dict[str, str] = {}
for interface in root.findall("./devices/interface"):
target = interface.find("target")
mac = interface.find("mac")
target_name = target.get("dev") if target is not None else None
address = mac.get("address") if mac is not None else None
if (isinstance(target_name, str) and target_name
and isinstance(address, str)
and re.fullmatch(r"[0-9A-Fa-f]{2}(?::[0-9A-Fa-f]{2}){5}",
address)):
by_target[target_name] = "mac:" + address.lower()
count = values.get("net.count")
if isinstance(count, int) and not isinstance(count, bool):
for number in range(min(max(0, count), 1024)):
name = values.get(f"net.{number}.name")
identity = by_target.get(name) if isinstance(name, str) else None
if identity is not None:
values[f"net.{number}.identity"] = identity
except (AttributeError, ET.ParseError, libvirt.libvirtError):
# Metrics remain useful on older/unusual domains whose
# live XML cannot be read; accounting falls back to the
# reported interface name for that sample.
pass
rows.append(RawVMStats(
vm_id=vm_id, generation=generation,
vcpus=info[3], values=values,
@ -1187,8 +1219,15 @@ class VMService:
return {"vm": vm}
def exit_rescue_vm(self, vm_id: str, original_xml: str, *,
network_suspended: bool = False,
progress: Callable[[str], None] | None = None) -> dict[str, Any]:
"""Gracefully stop rescue mode, restore original XML, and start OS."""
"""Gracefully stop rescue mode, restore original XML, and start OS.
``original_xml`` predates rescue mode and can therefore contain a
stale NIC link state. The billing ledger passes its current target so
quota suspension survives the definition swap atomically; a later
network_resume is the only operation that reconnects the guest.
"""
def phase(value: str) -> None:
if progress is not None:
progress(value)
@ -1205,6 +1244,13 @@ class VMService:
if (original_root.findtext("uuid") != vm_uuid
or original_root.find(f"./metadata/{{{PANEL_NS}}}rescue") is not None):
raise VMError("原虚拟机配置备份与目标虚拟机不匹配", 409)
target_link_state = "down" if network_suspended else "up"
for interface in original_root.findall("./devices/interface"):
link = interface.find("link")
if link is None:
link = ET.SubElement(interface, "link")
link.set("state", target_link_state)
restored_xml = ET.tostring(original_root, encoding="unicode")
rescue_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
try:
rescue_root = ET.fromstring(rescue_xml)
@ -1235,7 +1281,7 @@ class VMService:
switched = False
try:
phase("restoring")
normal_domain = conn.defineXML(original_xml)
normal_domain = conn.defineXML(restored_xml)
if normal_domain is None:
raise VMError("宿主机未能恢复原虚拟机配置", 502)
switched = True
@ -1267,7 +1313,7 @@ class VMService:
normal_domain = None
try:
phase("starting")
normal_domain = conn.defineXML(original_xml)
normal_domain = conn.defineXML(restored_xml)
if normal_domain is None:
raise VMError("宿主机未能恢复原虚拟机配置", 502)
switched = True
@ -1809,6 +1855,133 @@ class VMService:
getattr(domain, methods[action])()
return self._vm_dict(domain)
def service_suspend(self, uuid: str) -> dict[str, Any]:
"""Converge a persistent VM to the administratively stopped state.
Autostart is disabled before requesting guest shutdown so a host reboot
cannot undo a service-expiry suspension. The operation is safe to
repeat after a lost response: an already stopped VM is still verified
and returned without issuing another shutdown.
"""
with self._connect() as conn:
domain = self._domain(conn, uuid)
if not domain.isPersistent():
raise VMError("只能暂停持久化虚拟机", 409)
was_running = bool(domain.isActive())
if bool(domain.autostart()):
domain.setAutostart(0)
if domain.isActive():
try:
domain.shutdown()
except libvirt.libvirtError as exc:
# A failed ACPI request is not allowed to prevent an
# administrative suspension. Recheck the target state,
# then fall through to the bounded force-stop path.
LOG.warning("Could not gracefully stop VM %s: %s", uuid, exc)
deadline = time.monotonic() + SERVICE_SUSPEND_SHUTDOWN_TIMEOUT
while domain.isActive() and time.monotonic() < deadline:
time.sleep(SERVICE_SUSPEND_SHUTDOWN_POLL_INTERVAL)
if domain.isActive():
try:
domain.destroy()
except libvirt.libvirtError as exc:
# destroy() may have reached libvirt even when its
# response was lost. Only fail if the VM is still on.
if domain.isActive():
raise VMError("虚拟机强制停机失败", 502) from exc
if domain.isActive():
raise VMError("虚拟机停机状态未确认", 502)
result = self._vm_dict(domain)
# PHP uses this durable result to distinguish a VM stopped by the
# expiry policy from one the tenant had already stopped. Only the
# former may be started automatically when a paid period begins.
result["service_was_running"] = was_running
return result
def service_resume(self, uuid: str, *, start_if_suspended: bool = False) -> dict[str, Any]:
"""Remove an administrative suspension and optionally restore runtime.
``start_if_suspended`` is supplied only when the billing ledger proves
the expiry policy stopped a previously running guest. It is a target
state and therefore remains safe to replay after a lost response.
"""
with self._connect() as conn:
domain = self._domain(conn, uuid)
if not domain.isPersistent():
raise VMError("只能恢复持久化虚拟机", 409)
if not bool(domain.autostart()):
domain.setAutostart(1)
if start_if_suspended and not domain.isActive():
try:
domain.create()
except libvirt.libvirtError as exc:
# create() may have succeeded while its transport response
# was lost. Confirm the target state before reporting a
# retryable failure.
if not domain.isActive():
raise VMError("虚拟机服务恢复后启动失败", 502) from exc
return self._vm_dict(domain)
@staticmethod
def _interface_link_state(interface: ET.Element) -> str:
link = interface.find("link")
# libvirt omits the element for its default, connected state.
return link.get("state", "up") if link is not None else "up"
@staticmethod
def _interface_with_link_state(interface: ET.Element, state: str) -> str:
updated = ET.fromstring(ET.tostring(interface, encoding="unicode"))
link = updated.find("link")
if link is None:
link = ET.SubElement(updated, "link")
link.set("state", state)
return ET.tostring(updated, encoding="unicode")
@classmethod
def _update_interface_links(cls, domain: Any, root: ET.Element,
state: str, flags: int) -> None:
for interface in root.findall("./devices/interface"):
if cls._interface_link_state(interface) == state:
continue
domain.updateDeviceFlags(
cls._interface_with_link_state(interface, state), flags)
@classmethod
def _interface_links_match(cls, root: ET.Element, state: str) -> bool:
return all(cls._interface_link_state(interface) == state
for interface in root.findall("./devices/interface"))
def _network_service_state(self, uuid: str, *, suspended: bool) -> dict[str, Any]:
"""Persist and, when running, apply one link state to every VM NIC."""
target = "down" if suspended else "up"
with self._connect() as conn:
domain = self._domain(conn, uuid)
if not domain.isPersistent():
raise VMError("只能修改持久化虚拟机的网络状态", 409)
inactive = ET.fromstring(domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
self._update_interface_links(
domain, inactive, target, libvirt.VIR_DOMAIN_AFFECT_CONFIG)
inactive = ET.fromstring(domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
if not self._interface_links_match(inactive, target):
raise VMError("虚拟机持久网络状态未确认", 502)
if domain.isActive():
live = ET.fromstring(domain.XMLDesc(0))
self._update_interface_links(
domain, live, target, libvirt.VIR_DOMAIN_AFFECT_LIVE)
if domain.isActive():
live = ET.fromstring(domain.XMLDesc(0))
if not self._interface_links_match(live, target):
raise VMError("虚拟机实时网络状态未确认", 502)
return {**self._vm_dict(domain), "network_suspended": suspended}
def network_suspend(self, uuid: str) -> dict[str, Any]:
"""Disconnect every NIC without stopping the VM or changing autostart."""
return self._network_service_state(uuid, suspended=True)
def network_resume(self, uuid: str) -> dict[str, Any]:
"""Reconnect every NIC without starting the VM or changing autostart."""
return self._network_service_state(uuid, suspended=False)
@staticmethod
def _disk_sources(root: ET.Element) -> list[ET.Element]:
return [source for disk in root.findall("./devices/disk")

@ -1,7 +1,8 @@
"""Periodic libvirt VM measurements and safe counter-to-rate conversion.
The collector runs independently of browser visits. A process restart deliberately
loses counter baselines: the first subsequent rate sample is unknown, not zero.
The collector runs independently of browser visits. Graph-rate baselines are
process-local, while billing byte-counter baselines are committed by the log
store so traffic accounting survives an engine restart.
"""
from __future__ import annotations
@ -26,6 +27,7 @@ RANGES = {"1h": 3600, "24h": 24 * 3600, "7d": 7 * 24 * 3600,
"30d": RETENTION_SECONDS}
_COUNTER = re.compile(r"^(block|net)\.(\d+)\.(rd|wr|rx|tx)\.bytes$")
_MEMORY_MAX_AGE_SECONDS = 180
_MAX_UNSIGNED_BIGINT = (1 << 64) - 1
def host_scope(profile: Mapping[str, Any]) -> str:
@ -84,6 +86,37 @@ def _device_counters(stats: Mapping[str, Any], group: str,
return signature, counters_by_direction
def _network_counter_devices(stats: Mapping[str, Any]) -> dict[str, list[int]] | None:
"""Return validated cumulative counters keyed by stable guest NIC identity.
libvirt reports rx/tx from the guest's point of view: rx is downloaded by
the guest and tx is uploaded by it. metric_counters enriches each row
with the NIC MAC; the host tap name is only a compatibility fallback.
Device indexes are deliberately not identities because their order can
change when another NIC is attached.
"""
count = _nonnegative_int(stats.get("net.count"))
if count is None or count > 1024:
return None
candidates: dict[str, list[int] | None] = {}
for number in range(count):
name = stats.get(f"net.{number}.name")
identity = stats.get(f"net.{number}.identity", name)
rx = _nonnegative_int(stats.get(f"net.{number}.rx.bytes"))
tx = _nonnegative_int(stats.get(f"net.{number}.tx.bytes"))
if (not isinstance(identity, str) or not 1 <= len(identity) <= 255
or any(ord(char) < 32 or ord(char) == 127 for char in identity)
or rx is None or tx is None
or rx > _MAX_UNSIGNED_BIGINT or tx > _MAX_UNSIGNED_BIGINT):
return None
# Duplicate names cannot be safely matched to a previous observation.
if identity in candidates:
return None
candidates[identity] = [rx, tx]
return {identity: counters for identity, counters in candidates.items()
if counters is not None}
def _memory_mib(stats: Mapping[str, Any], sampled_at: int) -> tuple[float | None,
float | None]:
available = _nonnegative_int(stats.get("balloon.available"))
@ -173,6 +206,10 @@ class MetricSampler:
seconds, network_same),
"network_tx_bps": self._rate(network["tx"], previous.network["tx"] if previous else None,
seconds, network_same),
# Raw per-NIC cumulative counters are never summed into billing
# totals here. The database store locks and advances its durable
# baseline in the same transaction as this sample.
"network_devices": _network_counter_devices(values),
}
self._previous[key] = _Baseline(monotonic, raw.generation, raw.vcpus, cpu_time,
disk_signature, disk, network_signature, network)
@ -190,10 +227,12 @@ class MetricsCollector:
def __init__(self, store_getter: Callable[[], Any],
profile_getter: Callable[[], Mapping[str, Any] | None],
service_factory: Callable[[Mapping[str, Any]], Any],
*, interval_seconds: int = SAMPLE_INTERVAL_SECONDS) -> None:
*, owner_getter: Callable[[str, str], int | None] | None = None,
interval_seconds: int = SAMPLE_INTERVAL_SECONDS) -> None:
self._store_getter = store_getter
self._profile_getter = profile_getter
self._service_factory = service_factory
self._owner_getter = owner_getter
self._interval_seconds = interval_seconds
self._sampler = MetricSampler()
self._stop = threading.Event()
@ -214,8 +253,15 @@ class MetricsCollector:
scope = host_scope(profile)
rows = self._service_factory(profile).metric_counters()
monotonic = time.monotonic() if monotonic is None else monotonic
points = [self._sampler.observe(scope, row, timestamp=timestamp,
monotonic=monotonic) for row in rows]
points = []
host_id = profile.get("host_id")
for row in rows:
point = self._sampler.observe(scope, row, timestamp=timestamp,
monotonic=monotonic)
point["host_id"] = host_id if isinstance(host_id, str) else None
point["user_id"] = (self._owner_getter(scope, point["vm_id"])
if self._owner_getter is not None else None)
points.append(point)
self._sampler.forget_unseen(scope, {str(uuid.UUID(row.vm_id)) for row in rows})
if points:
store.save_metric_samples(scope, points)

@ -1,7 +1,7 @@
# Virt Cloud PHP 开发副本
仅保留 venix 主题及通用账户、邮箱、工单、公告、充值和财务能力。
用户侧节点列表已经删除;管理员 `/admin/hosts` 是未接入的宿主机准备页。
商城套餐现在是 CPU、内存、带宽和硬盘规格,接入 Python 引擎前不允许购买或扣款。
用户侧旧节点列表已经删除;管理员在 `/admin/hosts` 配置宿主机。
商城套餐现在是 CPU、内存、带宽、硬盘、账期天数和每期流量额度,通过私有 Python/libvirt 引擎开通实例。支持手动续费、到期停机以及流量用尽仅断网;生产必须同时运行账期 worker 与独立策略 worker。
详见上层 `docs/PRUNING_REPORT.md`。这不是已集成的生产销售面板,不要对现有生产数据库执行此副本的 SQL。
部署与安全边界见上层 `docs/DEPLOYMENT.md` 和 `docs/INTEGRATION_PLAN.md`。真实数据库、宿主机与支付通道仍必须在测试环境验收后再开放销售。

@ -0,0 +1,28 @@
<?php
namespace App\Command;
use App\Services\Cloud\CloudLifecycleService;
final class CloudLifecycle
{
public static function run(array $argv = []): int
{
$once = in_array('--once', $argv, true);
$service = new CloudLifecycleService();
do {
try {
$service->tick();
} catch (\Throwable $error) {
fwrite(STDERR, 'cloud lifecycle temporarily unavailable: '
. get_class($error) . PHP_EOL);
if ($once) {
return 1;
}
}
if (!$once) {
usleep(15000000);
}
} while (!$once);
return 0;
}
}

@ -36,12 +36,59 @@ final class CloudSetup
$table->unsignedBigInteger('host_id')->nullable();
});
}
$shopSchema = $connection->getSchemaBuilder();
if (!$shopSchema->hasColumn('shop', 'validity_days')) {
$shopSchema->table('shop', static function ($table) {
$table->unsignedInteger('validity_days')->default(30);
});
}
if (!$shopSchema->hasColumn('shop', 'traffic_quota_bytes')) {
$shopSchema->table('shop', static function ($table) {
$table->unsignedBigInteger('traffic_quota_bytes')->default(0);
});
}
self::upgradeCloudInstance($connection);
User::getConnectionResolver()->connection('log')->unprepared(self::schema('cloud_log.sql'));
self::exportEngineConfig();
echo "虚拟化业务表、指标日志表与私有引擎配置已准备。未连接或修改任何宿主机。\n";
return true;
}
private static function upgradeCloudInstance($connection): void
{
$schema = $connection->getSchemaBuilder();
$columns = [
'plan_id' => 'BIGINT UNSIGNED NULL',
'current_period_id' => 'BIGINT UNSIGNED NULL',
'service_status' => "VARCHAR(32) NOT NULL DEFAULT 'exempt'",
'network_status' => "VARCHAR(32) NOT NULL DEFAULT 'active'",
'expires_at' => 'BIGINT UNSIGNED NULL',
'renewal_price' => 'DECIMAL(12,2) NULL',
'renewal_days' => 'INT UNSIGNED NOT NULL DEFAULT 0',
'renewal_traffic_bytes' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'traffic_quota_bytes' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'traffic_download_bytes' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'traffic_upload_bytes' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'lifetime_download_bytes' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'lifetime_upload_bytes' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'traffic_synced_at' => 'BIGINT UNSIGNED NOT NULL DEFAULT 0',
'suspended_at' => 'BIGINT UNSIGNED NULL',
'network_suspended_at' => 'BIGINT UNSIGNED NULL',
];
foreach ($columns as $name => $definition) {
if (!$schema->hasColumn('cloud_instances', $name)) {
$connection->statement('ALTER TABLE cloud_instances ADD COLUMN ' . $name . ' ' . $definition);
}
}
if (!$schema->hasColumn('cloud_renewals', 'period_id')) {
$connection->statement('ALTER TABLE cloud_renewals ADD COLUMN period_id BIGINT UNSIGNED NULL AFTER plan_id');
}
$connection->statement('ALTER TABLE cloud_instances ADD INDEX IF NOT EXISTS '
. 'cloud_instance_lifecycle (service_status, expires_at, id)');
$connection->statement('ALTER TABLE cloud_jobs ADD INDEX IF NOT EXISTS '
. 'cloud_job_instance_state (instance_id, status, id)');
}
public static function exportEngineConfig(): bool
{
if (Config::get('enable_log', 'false') !== 'true') {
@ -81,7 +128,7 @@ final class CloudSetup
private static function database(array $config): array
{
if ($config['driver'] !== 'mysql') {
throw new \RuntimeException('虚拟化引擎需要 MariaDB/MySQL 数据库');
throw new \RuntimeException('虚拟化引擎需要 MariaDB 数据库(PDO 驱动名为 mysql)');
}
if ((string) $config['database'] === '' || (string) $config['username'] === '') {
throw new \RuntimeException('请完整配置业务和日志数据库');

@ -8,7 +8,8 @@ final class CloudWorker
public static function run(array $argv = []): int
{
$once = in_array('--once', $argv, true);
$worker = new CloudWorkerService();
$policyOnly = in_array('--policy-only', $argv, true);
$worker = new CloudWorkerService(null, $policyOnly);
do {
try {
$worked = $worker->tick();

@ -23,6 +23,7 @@ class XCat
'cloudInit' => CloudSetup::initialize(),
'cloudExportConfig' => CloudSetup::exportEngineConfig(),
'cloudWorker' => CloudWorker::run($this->argv) === 0,
'cloudLifecycle' => CloudLifecycle::run($this->argv) === 0,
'setTelegram' => $this->setTelegram(),
'backup' => Job::backup(),
'backupJob' => Job::backupJob(),
@ -97,7 +98,8 @@ class XCat
. " php xcat createAdmin <email> [name]\n"
. " php xcat cloudInit\n"
. " php xcat cloudExportConfig\n"
. " php xcat cloudWorker [--once]\n"
. " php xcat cloudWorker [--once] [--policy-only]\n"
. " php xcat cloudLifecycle [--once]\n"
. " php xcat setTelegram\n"
. " php xcat backup\n"
. " php xcat backupJob\n"

@ -16,7 +16,8 @@ class ShopController extends AdminController
{
$columns = ['op' => '操作', 'id' => 'ID', 'name' => '套餐名称', 'host_id' => '宿主机 ID',
'cpu_cores' => 'CPU 核数', 'memory_mib' => '内存 MiB', 'bandwidth_mbps' => '带宽 Mbps',
'disk_gib' => '磁盘 GiB', 'price' => '价格', 'status' => '状态', 'stock' => '库存', 'sort' => '排序'];
'disk_gib' => '磁盘 GiB', 'validity_days' => '有效期(天)', 'traffic_gib' => '每期流量 GiB',
'price' => '每期价格', 'status' => '状态', 'stock' => '库存', 'sort' => '排序'];
return $this->view()->assign('table_config', [
'total_column' => $columns, 'default_show_column' => array_keys($columns), 'ajax_url' => 'shop/ajax',
@ -60,8 +61,9 @@ class ShopController extends AdminController
}
try {
$input = [];
foreach (['name', 'host_id', 'price', 'cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib', 'status', 'stock', 'sort'] as $field) {
$default = $shop->exists ? $shop->$field : null;
foreach (['name', 'host_id', 'price', 'cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib',
'validity_days', 'traffic_gib', 'status', 'stock', 'sort'] as $field) {
$default = $shop->exists ? ($field === 'traffic_gib' ? $shop->trafficGiB() : $shop->$field) : null;
$input[$field] = $request->getParam($field, $default);
}
$values = Shop::validatePlan($input);
@ -102,7 +104,9 @@ class ShopController extends AdminController
public function ajax_shop($request, $response, $args)
{
$table = new Datatables(new DatatablesHelper());
$table->query('SELECT id AS op,id,name,host_id,cpu_cores,memory_mib,bandwidth_mbps,disk_gib,price,status,stock,sort FROM shop');
$table->query('SELECT id AS op,id,name,host_id,cpu_cores,memory_mib,bandwidth_mbps,disk_gib,'
. 'validity_days,CASE WHEN traffic_quota_bytes=0 THEN 0 ELSE FLOOR(traffic_quota_bytes/1073741824) END AS traffic_gib,'
. 'price,status,stock,sort FROM shop');
$table->edit('op', static function ($data) {
if (!AdminPermission::isSuperAdmin()) {
return '<span class="text-muted">只读</span>';

@ -5,16 +5,19 @@ use App\Models\CloudHost;
use App\Models\CloudInstance;
use App\Models\CloudJob;
use App\Models\CloudOrder;
use App\Models\CloudServicePeriod;
use App\Models\CloudSSHKey;
use App\Models\Shop;
use App\Services\Auth;
use App\Services\AdminCsrf;
use App\Services\Config;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudEngineClient;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudJobService;
use App\Services\Cloud\CloudOrderService;
use App\Services\Cloud\CloudRenewalService;
use App\Services\Cloud\CloudSecrets;
use App\Services\Cloud\CloudSSHKeys;
@ -36,7 +39,8 @@ final class CloudApiController
$actor = CloudAccess::actor();
$user = Auth::getUser();
return $this->json($response, ['authenticated' => true, 'username' => $user->user_name,
'email' => $user->email, 'admin' => $actor['is_admin'], 'can_manage' => $actor['can_manage'],
'user_id' => (int) $user->id, 'email' => $user->email,
'admin' => $actor['is_admin'], 'can_manage' => $actor['can_manage'],
'approved' => true, 'csrf_token' => AdminCsrf::token(), 'balance' => (string) $user->money]);
} catch (CloudError $error) {
return $this->json($response, ['authenticated' => false, 'detail' => $error->getMessage()], $error->httpStatus === 401 ? 200 : $error->httpStatus);
@ -52,6 +56,9 @@ final class CloudApiController
$plans[] = ['id' => (int) $plan->id, 'name' => $plan->name, 'price' => (string) $plan->price,
'cpu_cores' => (int) $plan->cpu_cores, 'memory_mib' => (int) $plan->memory_mib,
'bandwidth_mbps' => (int) $plan->bandwidth_mbps, 'disk_gib' => (int) $plan->disk_gib,
'validity_days' => (int) $plan->validity_days,
'traffic_quota_bytes' => (int) $plan->traffic_quota_bytes,
'traffic_gib' => $plan->trafficGiB(),
'stock' => (int) $plan->stock, 'host_id' => (int) $host->id, 'host_name' => $host->name, 'region' => $host->region];
}
}
@ -89,6 +96,23 @@ final class CloudApiController
return $this->json($response, ['order' => CloudOrderService::display($order)]);
}
public function orderRequest($request, $response, $args)
{
$actor = $this->actor($request);
$key = (string) ($args['key'] ?? '');
if (preg_match('/^[A-Za-z0-9_.-]{16,128}$/D', $key) !== 1) {
throw new CloudError('订单请求标识无效');
}
// Deliberately owner-only even for administrators: this endpoint is
// used to reconcile a browser's possibly lost purchase response.
$order = CloudOrder::where('user_id', $actor['user_id'])
->where('idempotency_key', $key)->first();
if ($order === null) {
throw new CloudError('订单请求尚未建立', 404);
}
return $this->json($response, ['order' => CloudOrderService::display($order)]);
}
public function job($request, $response, $args)
{
$actor = $this->actor($request);
@ -105,6 +129,9 @@ final class CloudApiController
private function vmData(CloudInstance $instance, array $actor): array
{
$owner = (int) $instance->user_id === (int) $actor['user_id'];
$global = ($actor['is_admin'] ?? false) && ($actor['is_super_admin'] ?? false)
&& ($actor['can_manage'] ?? false);
$spec = json_decode($instance->spec_json, true) ?: [];
try {
$vm = (new CloudEngineClient())->host((int) $instance->host_id, 'GET', 'vms/' . rawurlencode($instance->uuid), $actor);
@ -115,11 +142,38 @@ final class CloudApiController
$createdAt = ($spec['record_source'] ?? '') === 'discovered'
? (is_string($vm['created_at'] ?? null) && trim($vm['created_at']) !== '' ? $vm['created_at'] : null)
: gmdate('Y-m-d\TH:i:s\Z', (int) $instance->created_at);
$period = $instance->current_period_id === null ? null
: CloudServicePeriod::where('id', $instance->current_period_id)
->where('instance_id', $instance->id)->first();
$futurePeriods = CloudServicePeriod::where('instance_id', $instance->id)
->where('status', 'scheduled')->count();
$maximumFuturePeriods = max(1, min(120,
(int) Config::get('cloud_max_renewal_periods_ahead', 24)));
return array_merge(CloudInput::safeResult($spec), CloudInput::safeResult($vm), ['id' => (string) $instance->id, 'uuid' => $instance->uuid,
'user_id' => (int) $instance->user_id, 'owner_user_id' => (int) $instance->user_id,
'display_uuid' => $instance->uuid, 'host_id' => (int) $instance->host_id, 'region' => $host?->region ?? '',
'created_at' => $createdAt,
'has_initial_root_password' => $instance->credential_cipher !== null]);
'service_status' => (string) $instance->service_status,
'network_status' => (string) $instance->network_status,
'service_expires_at' => $instance->expires_at === null ? null : (int) $instance->expires_at,
'current_period_started_at' => $period === null ? null : (int) $period->starts_at,
'current_period_ends_at' => $period === null ? null : (int) $period->ends_at,
'prepaid_periods' => $futurePeriods,
'traffic_download_bytes' => (int) $instance->traffic_download_bytes,
'traffic_upload_bytes' => (int) $instance->traffic_upload_bytes,
'traffic_used_bytes' => (int) $instance->traffic_download_bytes + (int) $instance->traffic_upload_bytes,
'traffic_quota_bytes' => (int) $instance->traffic_quota_bytes,
'renewal_price' => $instance->renewal_price === null ? null : (string) $instance->renewal_price,
'renewal_days' => (int) $instance->renewal_days,
'renewal_traffic_bytes' => (int) $instance->renewal_traffic_bytes,
'is_owner' => $owner,
'can_mutate' => $owner || $global,
'can_view_credential' => $owner || $global,
'can_renew' => $owner
&& $instance->expires_at !== null && (int) $instance->renewal_days > 0
&& $instance->renewal_price !== null && $futurePeriods < $maximumFuturePeriods,
'has_initial_root_password' => ($owner || $global)
&& $instance->credential_cipher !== null]);
}
public function vms($request, $response)
@ -166,6 +220,7 @@ final class CloudApiController
{
$actor = $this->actor($request);
$instance = CloudAccess::instance($args['id'], $actor, true);
$this->requireService($instance, $actor);
$actor = CloudAccess::vmActor($instance, $actor);
return $this->json($response, (new CloudEngineClient())->host((int) $instance->host_id, 'POST',
'vms/' . rawurlencode($instance->uuid) . '/console-ticket', $actor, []));
@ -181,6 +236,9 @@ final class CloudApiController
if (!in_array($body['action'] ?? '', ['start', 'shutdown', 'reboot', 'poweroff', 'reset'], true)) {
throw new CloudError('操作无效');
}
if (in_array($body['action'], ['start', 'reboot', 'reset'], true)) {
$this->requireService($instance, $actor);
}
$job = CloudJobService::enqueue($instance, $actor, 'action', 'POST', '/actions', $body);
return $this->json($response, ['job_id' => (string) $job->id, 'status_url' => '/api/v1/jobs/' . $job->id], 202);
}
@ -189,6 +247,7 @@ final class CloudApiController
{
$actor = $this->actor($request);
$instance = CloudAccess::instance($args['id'], $actor, true);
$this->requireService($instance, $actor);
$body = CloudInput::object($request);
CloudInput::only($body, ['template_id', 'ssh_key_id', 'root_password', 'confirmation_name']);
if (($body['confirmation_name'] ?? '') !== $instance->name) {
@ -214,15 +273,40 @@ final class CloudApiController
return $this->json($response, (new CloudEngineClient())->host((int) $instance->host_id, 'GET',
'vms/' . rawurlencode($instance->uuid) . $suffix, $actor));
}
$this->requireService($instance, $actor);
$body = CloudInput::object($request);
CloudInput::only($body, ['template_id', 'confirmation_name', 'ack_data_loss']);
if (($body['confirmation_name'] ?? '') !== $instance->name) {
throw new CloudError('请填写虚拟机名称确认操作');
}
if ($suffix === '/rescue/exit') {
// The saved original XML may predate a traffic-quota block. Pass
// the current billing target so the engine restores every NIC in
// the correct link state before it starts the normal system.
$body['network_suspended'] = in_array(
(string) $instance->network_status,
['blocking', 'blocked_traffic', 'block_failed'], true);
}
$job = CloudJobService::enqueue($instance, $actor, 'rescue', 'POST', $suffix, $body);
return $this->json($response, ['job_id' => (string) $job->id, 'status_url' => '/api/v1/jobs/' . $job->id], 202);
}
public function renew($request, $response, $args)
{
$actor = $this->actor($request);
$instance = CloudAccess::instance($args['id'], $actor, true);
$renewal = CloudRenewalService::renew($instance, CloudInput::object($request), $actor);
return $this->json($response, ['renewal' => CloudRenewalService::display($renewal)], 201);
}
private function requireService(CloudInstance $instance, array $actor): void
{
$global = ($actor['is_super_admin'] ?? false) && ($actor['can_manage'] ?? false);
if (!$global && !$instance->serviceAvailable()) {
throw new CloudError('实例服务已到期或正在恢复,请先续费并等待恢复完成', 409);
}
}
public function delete($request, $response, $args)
{
$actor = $this->actor($request);

@ -5,4 +5,15 @@ final class CloudInstance extends Model
protected $connection = 'default';
protected $table = 'cloud_instances';
protected $hidden = ['credential_cipher'];
public function serviceAvailable(?int $now = null): bool
{
if ((string) $this->service_status === 'exempt') {
return true;
}
$now ??= time();
return (string) $this->service_status === 'active'
&& $this->expires_at !== null
&& (int) $this->expires_at > $now;
}
}

@ -0,0 +1,10 @@
<?php
namespace App\Models;
final class CloudRenewal extends Model
{
protected $connection = 'default';
protected $table = 'cloud_renewals';
protected $casts = ['amount' => 'decimal:2'];
protected $hidden = ['request_hash'];
}

@ -0,0 +1,8 @@
<?php
namespace App\Models;
final class CloudServicePeriod extends Model
{
protected $connection = 'default';
protected $table = 'cloud_service_periods';
}

@ -4,20 +4,34 @@ namespace App\Models;
class Shop extends Model
{
public const GIB = 1073741824;
protected $connection = 'default';
protected $table = 'shop';
protected $casts = [
'host_id' => 'int',
'cpu_cores' => 'int', 'memory_mib' => 'int', 'bandwidth_mbps' => 'int',
'disk_gib' => 'int', 'status' => 'int', 'stock' => 'int', 'sort' => 'int',
'disk_gib' => 'int', 'validity_days' => 'int', 'traffic_quota_bytes' => 'int',
'status' => 'int', 'stock' => 'int', 'sort' => 'int',
'price' => 'decimal:2',
];
public function content()
{
return sprintf('%d 核 CPU · %d MiB 内存 · %d Mbps 带宽 · %d GiB 磁盘',
$this->cpu_cores, $this->memory_mib, $this->bandwidth_mbps, $this->disk_gib);
return sprintf('%d 核 CPU · %d MiB 内存 · %d Mbps 带宽 · %d GiB 磁盘 · %d 天 · %s',
$this->cpu_cores, $this->memory_mib, $this->bandwidth_mbps, $this->disk_gib,
$this->validity_days, $this->trafficQuotaText());
}
public function trafficGiB(): int
{
return intdiv(max(0, (int) $this->traffic_quota_bytes), self::GIB);
}
public function trafficQuotaText(): string
{
return (int) $this->traffic_quota_bytes === 0 ? '不限流量' : $this->trafficGiB() . ' GiB 流量';
}
public static function validatePlan(array $input): array
@ -40,6 +54,7 @@ class Shop extends Model
foreach ([
'cpu_cores' => [1, 64], 'memory_mib' => [512, 262144],
'bandwidth_mbps' => [1, 100000], 'disk_gib' => [1, 4096],
'validity_days' => [1, 3650], 'traffic_gib' => [0, 1048576],
'status' => [0, 1], 'stock' => [-1, 2147483647], 'sort' => [-1000000, 1000000],
] as $field => [$minimum, $maximum]) {
$raw = $input[$field] ?? (['status' => 1, 'stock' => -1, 'sort' => 0][$field] ?? null);
@ -49,6 +64,9 @@ class Shop extends Model
}
$data[$field] = $value;
}
$trafficGiB = $data['traffic_gib'];
unset($data['traffic_gib']);
$data['traffic_quota_bytes'] = $trafficGiB * self::GIB;
$price = $input['price'] ?? '';
if ((!is_string($price) && !is_int($price)) || preg_match('/^\d{1,10}(?:\.\d{1,2})?$/D', (string) $price) !== 1) {
throw new \InvalidArgumentException('价格需为非负金额,最多两位小数');

@ -62,6 +62,8 @@ final class CloudInstanceDiscovery
$instance->order_id = null;
$instance->name = $name;
$instance->status = 'active';
$instance->service_status = 'exempt';
$instance->network_status = 'active';
$instance->spec_json = json_encode(
CloudInput::safeResult($spec),
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE

@ -15,6 +15,19 @@ final class CloudJobService
if ($locked === null || $locked->status === 'deleted') {
throw new CloudError('虚拟机不存在', 404);
}
$policyService = ['suspending_expired', 'resume_pending', 'resuming',
'suspend_failed', 'resume_failed'];
$policyNetwork = ['blocking', 'restore_pending', 'restoring',
'block_failed', 'restore_failed'];
$superAdmin = ($actor['is_super_admin'] ?? false)
&& ($actor['can_manage'] ?? false);
if ($kind !== 'delete' && !$superAdmin
&& (in_array((string) $locked->service_status, $policyService, true)
|| in_array((string) $locked->network_status, $policyNetwork, true)
|| ((string) $locked->service_status !== 'exempt'
&& ($locked->expires_at === null || (int) $locked->expires_at <= time())))) {
throw new CloudError('实例正在执行到期或流量策略,请稍后重试', 409);
}
if (CloudJob::where('instance_id', $locked->id)->whereIn('status', ['queued', 'running', 'uncertain'])->exists()) {
throw new CloudError('虚拟机已有任务,请等待完成', 409);
}

@ -0,0 +1,274 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\CloudJob;
use App\Models\CloudServicePeriod;
final class CloudLifecycleService
{
public function tick(int $limit = 200): int
{
$batchSize = max(1, min(1000, $limit));
try {
CloudTrafficService::sync($batchSize);
} catch (\Throwable $error) {
// Unknown traffic is fail-open. Time expiry remains enforceable.
error_log('cloud traffic sync unavailable: ' . get_class($error));
}
$queued = 0;
CloudInstance::where('status', '<>', 'deleted')
->where('service_status', '<>', 'exempt')
->select('id')->chunkById($batchSize, function ($instances) use (&$queued) {
foreach ($instances as $instance) {
try {
$queued += $this->reconcile((int) $instance->id) ? 1 : 0;
} catch (\Throwable $error) {
// One corrupt or temporarily contended record must not
// starve every higher instance ID on every daemon pass.
error_log('cloud lifecycle instance ' . (int) $instance->id
. ' unavailable: ' . get_class($error));
}
}
});
return $queued;
}
private function reconcile(int $instanceId): bool
{
return (new CloudInstance())->getConnection()->transaction(function () use ($instanceId) {
$now = time();
$instance = CloudInstance::where('id', $instanceId)->lockForUpdate()->first();
if ($instance === null || $instance->status === 'deleted'
|| $instance->service_status === 'exempt') {
return false;
}
$period = $instance->current_period_id === null ? null
: CloudServicePeriod::where('id', $instance->current_period_id)
->where('instance_id', $instance->id)->lockForUpdate()->first();
if ($period !== null && in_array((string) $period->status, ['active', 'exhausted'], true)) {
$this->updateUsage($instance, $period, $now);
if ((int) $period->ends_at <= $now) {
$period->status = 'finished';
$period->updated_at = $now;
$period->save();
$period = null;
$instance->current_period_id = null;
}
} else {
$period = null;
$instance->current_period_id = null;
}
if ($period === null) {
// The lifecycle daemon may be stopped for longer than one
// prepaid period. Never briefly activate an already expired
// historical period (which would also restore its network).
CloudServicePeriod::where('instance_id', $instance->id)
->where('status', 'scheduled')->where('ends_at', '<=', $now)
->update(['status' => 'finished', 'updated_at' => $now]);
$period = CloudServicePeriod::where('instance_id', $instance->id)
->where('status', 'scheduled')->where('starts_at', '<=', $now)
->where('ends_at', '>', $now)
->orderBy('starts_at')->orderBy('id')->lockForUpdate()->first();
if ($period !== null) {
$period->status = 'active';
$period->activated_at = $now;
$baseline = CloudTrafficService::periodBaseline(
$instance, (int) $period->starts_at);
if ($baseline === null
&& (int) $instance->traffic_synced_at >= (int) $period->starts_at) {
$baseline = [
'download' => max(0, (int) $instance->lifetime_download_bytes),
'upload' => max(0, (int) $instance->lifetime_upload_bytes),
];
}
$period->download_base_bytes = $baseline['download'] ?? null;
$period->upload_base_bytes = $baseline['upload'] ?? null;
$period->download_used_bytes = $baseline === null ? 0
: max(0, (int) $instance->lifetime_download_bytes
- (int) $baseline['download']);
$period->upload_used_bytes = $baseline === null ? 0
: max(0, (int) $instance->lifetime_upload_bytes
- (int) $baseline['upload']);
$period->updated_at = $now;
$period->save();
$instance->current_period_id = $period->id;
$instance->traffic_quota_bytes = max(0, (int) $period->traffic_quota_bytes);
$instance->traffic_download_bytes = (int) $period->download_used_bytes;
$instance->traffic_upload_bytes = (int) $period->upload_used_bytes;
if (!in_array((string) $instance->service_status, ['active', 'exempt'], true)) {
$instance->service_status = 'resume_pending';
}
if ((string) $instance->network_status !== 'active') {
$instance->network_status = 'restore_pending';
$instance->network_suspended_at = null;
}
}
}
if ($period !== null) {
$usageKnown = $this->updateUsage($instance, $period, $now);
$used = (int) $period->download_used_bytes + (int) $period->upload_used_bytes;
$quota = max(0, (int) $period->traffic_quota_bytes);
if ($usageKnown && $quota > 0 && $used >= $quota) {
if ((string) $period->status !== 'exhausted') {
$period->status = 'exhausted';
$period->updated_at = $now;
$period->save();
}
if ((string) $instance->network_status === 'block_failed'
&& $this->policyRetryReady($instance, 'network_suspend', $now)) {
$instance->network_status = 'blocking';
} elseif (!in_array((string) $instance->network_status,
['blocking', 'blocked_traffic', 'block_failed'], true)) {
$instance->network_status = 'blocking';
}
} elseif ($usageKnown) {
if ((string) $instance->network_status === 'restore_failed'
&& $this->policyRetryReady($instance, 'network_resume', $now)) {
$instance->network_status = 'restore_pending';
$instance->network_suspended_at = null;
} elseif (!in_array((string) $instance->network_status,
['active', 'restore_failed'], true)) {
$instance->network_status = 'restore_pending';
$instance->network_suspended_at = null;
}
}
if ((string) $instance->service_status === 'resume_failed'
&& $this->policyRetryReady($instance, 'service_resume', $now)) {
$instance->service_status = 'resume_pending';
}
} elseif ($instance->expires_at !== null && (int) $instance->expires_at <= $now
&& (string) $instance->service_status !== 'suspended_expired') {
if ((string) $instance->service_status === 'suspend_failed') {
if ($this->policyRetryReady($instance, 'service_suspend', $now)) {
$instance->service_status = 'suspending_expired';
}
} elseif ((string) $instance->service_status !== 'suspending_expired') {
$instance->service_status = 'suspending_expired';
}
}
$instance->updated_at = $now;
$instance->save();
if ($this->hasConflictingJob($instance)) {
return false;
}
$service = (string) $instance->service_status;
$network = (string) $instance->network_status;
if ($service === 'suspending_expired') {
return $this->enqueue($instance, 'service_suspend', 'service-suspend', 'expired');
}
if (in_array($service, ['resume_pending', 'resuming'], true)) {
$instance->service_status = 'resuming';
$instance->save();
return $this->enqueue($instance, 'service_resume', 'service-resume', null,
['start_if_suspended' => $instance->suspended_at !== null]);
}
if ($network === 'blocking') {
return $this->enqueue($instance, 'network_suspend', 'network-suspend', 'traffic');
}
if (in_array($network, ['restore_pending', 'restoring'], true)) {
$instance->network_status = 'restoring';
$instance->save();
return $this->enqueue($instance, 'network_resume', 'network-resume');
}
return false;
}, 3);
}
private function hasConflictingJob(CloudInstance $instance): bool
{
return CloudJob::where('instance_id', $instance->id)
->where(function ($query) {
$query->whereIn('status', ['queued', 'running'])
->orWhere(function ($uncertain) {
// Reset/shutdown/delete style mutations cannot be
// replayed after a lost response and may legitimately
// remain uncertain forever. They must not permanently
// disable expiry and traffic policy. Reinstall,
// rescue and target-state policy operations can still
// converge, so their uncertainty remains a conflict.
$uncertain->where('status', 'uncertain')
->whereNotIn('kind', ['action', 'delete']);
});
})->exists();
}
private function policyRetryReady(CloudInstance $instance, string $kind, int $now): bool
{
$failed = CloudJob::where('instance_id', $instance->id)
->where('kind', $kind)->where('status', 'failed')
->orderByDesc('updated_at')->orderByDesc('id')->first();
if ($failed === null) {
return true;
}
$recentFailures = CloudJob::where('instance_id', $instance->id)
->where('kind', $kind)->where('status', 'failed')
->where('updated_at', '>=', $now - 3600)->count();
// Retry temporary host/SSH failures without creating a 15-second job
// storm. Enforcement always converges again after an administrator
// repairs the host; the delay is capped at five minutes.
$exponent = min(5, max(0, $recentFailures - 1));
$delay = min(300, 15 * (1 << $exponent));
return (int) $failed->updated_at + $delay <= $now;
}
private function updateUsage(CloudInstance $instance, CloudServicePeriod $period, int $now): bool
{
if ($period->download_base_bytes === null || $period->upload_base_bytes === null) {
// At a period boundary, an old cached total would charge bytes
// from the previous period to the new one. Wait for the first
// successful sample at/after the boundary and fail open until it
// establishes both baselines.
if ((int) $instance->traffic_synced_at < (int) $period->starts_at) {
$period->download_base_bytes = null;
$period->upload_base_bytes = null;
$period->download_used_bytes = 0;
$period->upload_used_bytes = 0;
$period->updated_at = $now;
$period->save();
$instance->traffic_quota_bytes = max(0, (int) $period->traffic_quota_bytes);
$instance->traffic_download_bytes = 0;
$instance->traffic_upload_bytes = 0;
return false;
}
$period->download_base_bytes = max(0, (int) $instance->lifetime_download_bytes);
$period->upload_base_bytes = max(0, (int) $instance->lifetime_upload_bytes);
}
$download = max(0, (int) $instance->lifetime_download_bytes - (int) $period->download_base_bytes);
$upload = max(0, (int) $instance->lifetime_upload_bytes - (int) $period->upload_base_bytes);
$period->download_used_bytes = $download;
$period->upload_used_bytes = $upload;
$period->updated_at = $now;
$period->save();
$instance->traffic_quota_bytes = max(0, (int) $period->traffic_quota_bytes);
$instance->traffic_download_bytes = $download;
$instance->traffic_upload_bytes = $upload;
return true;
}
private function enqueue(CloudInstance $instance, string $kind, string $suffix,
?string $reason = null, array $body = []): bool
{
$job = new CloudJob();
$job->operation_id = bin2hex(random_bytes(16));
$job->instance_id = $instance->id;
$job->user_id = $instance->user_id;
$job->host_id = $instance->host_id;
$job->kind = $kind;
$job->status = 'queued';
$job->payload_cipher = CloudSecrets::encrypt([
'method' => 'POST',
'resource' => 'vms/' . rawurlencode($instance->uuid) . '/' . $suffix,
'body' => $body, 'owner_id' => (int) $instance->user_id,
'system_policy' => true, 'suspend_reason' => $reason,
]);
$job->available_at = $job->created_at = $job->updated_at = time();
$job->save();
return true;
}
}

@ -86,6 +86,10 @@ final class CloudOrderService
}
$safeSpec = CloudInput::safeResult($payload);
$safeSpec['plan_name'] = $plan->name;
$safeSpec['plan_id'] = (int) $plan->id;
$safeSpec['validity_days'] = (int) $plan->validity_days;
$safeSpec['traffic_quota_bytes'] = (int) $plan->traffic_quota_bytes;
$safeSpec['renewal_price'] = CloudMoney::decimal($price);
$order = new CloudOrder();
$order->user_id = $user->id;
$order->plan_id = $plan->id;

@ -0,0 +1,174 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\CloudJob;
use App\Models\CloudRenewal;
use App\Models\CloudServicePeriod;
use App\Models\User;
final class CloudRenewalService
{
public static function renew(CloudInstance $instance, array $input, array $actor): CloudRenewal
{
CloudInput::only($input, ['idempotency_key']);
$idempotency = $input['idempotency_key'] ?? '';
if (!is_string($idempotency) || preg_match('/^[A-Za-z0-9_.-]{16,128}$/D', $idempotency) !== 1) {
throw new CloudError('请提供 16 至 128 字符的续费幂等标识');
}
if ((int) $instance->user_id !== (int) $actor['user_id']) {
throw new CloudError('只能为自己持有的实例续费', 403);
}
$requestHash = hash_hmac('sha256', 'renew:' . (int) $instance->id,
CloudSecrets::key('payload_key'));
return (new User())->getConnection()->transaction(function () use ($instance, $actor, $idempotency, $requestHash) {
$user = User::where('id', (int) $actor['user_id'])->lockForUpdate()->first();
if ($user === null || (int) $user->enable !== 1) {
throw new CloudError('账户不可用', 403);
}
if ((int) $user->restrict_balance_purchase === 1) {
throw new CloudError('账户套餐购买服务已被管理员停用', 403);
}
$existing = CloudRenewal::where('user_id', $user->id)
->where('idempotency_key', $idempotency)->first();
if ($existing !== null) {
if (!hash_equals((string) $existing->request_hash, $requestHash)) {
throw new CloudError('同一请求标识不能用于不同实例', 409);
}
return $existing;
}
$locked = CloudInstance::where('id', $instance->id)->lockForUpdate()->first();
if ($locked === null || $locked->status === 'deleted'
|| (int) $locked->user_id !== (int) $user->id) {
throw new CloudError('实例不存在或无权续费', 404);
}
if (CloudJob::where('instance_id', $locked->id)->where('kind', 'delete')
->whereIn('status', ['queued', 'running', 'uncertain'])->exists()) {
throw new CloudError('实例正在删除或等待核对,不能续费', 409);
}
$days = (int) $locked->renewal_days;
$price = $locked->renewal_price === null ? -1 : CloudMoney::cents((string) $locked->renewal_price);
if ($days < 1 || $price < 0 || $locked->expires_at === null) {
throw new CloudError('此实例没有可续费的套餐快照', 409);
}
$balance = CloudMoney::cents((string) $user->money);
if ($balance < $price) {
throw new CloudError('账户余额不足', 409);
}
$now = time();
$currentPeriod = $locked->current_period_id === null ? null
: CloudServicePeriod::where('id', $locked->current_period_id)
->where('instance_id', $locked->id)->lockForUpdate()->first();
if ($currentPeriod !== null
&& in_array((string) $currentPeriod->status, ['active', 'exhausted'], true)
&& (int) $currentPeriod->ends_at <= $now) {
if ($currentPeriod->download_base_bytes !== null
&& $currentPeriod->upload_base_bytes !== null) {
$currentPeriod->download_used_bytes = max(0,
(int) $locked->lifetime_download_bytes - (int) $currentPeriod->download_base_bytes);
$currentPeriod->upload_used_bytes = max(0,
(int) $locked->lifetime_upload_bytes - (int) $currentPeriod->upload_base_bytes);
}
$currentPeriod->status = 'finished';
$currentPeriod->updated_at = $now;
$currentPeriod->save();
$locked->current_period_id = null;
}
CloudServicePeriod::where('instance_id', $locked->id)
->where('status', 'scheduled')->where('ends_at', '<=', $now)
->update(['status' => 'finished', 'updated_at' => $now]);
$maximumScheduled = max(1, min(120,
(int) \App\Services\Config::get('cloud_max_renewal_periods_ahead', 24)));
if (CloudServicePeriod::where('instance_id', $locked->id)
->where('status', 'scheduled')->count() >= $maximumScheduled) {
throw new CloudError('提前续费周期已达到上限,请在后续账期开始后再续费', 409);
}
$lastPeriod = CloudServicePeriod::where('instance_id', $locked->id)
->orderByDesc('ends_at')->orderByDesc('id')->lockForUpdate()->first();
if ($lastPeriod === null) {
throw new CloudError('实例缺少有效的服务周期,无法续费', 409);
}
$previousExpiry = max(0, (int) $locked->expires_at, (int) $lastPeriod->ends_at);
$startsAt = max($now, $previousExpiry);
$newExpiry = $startsAt + $days * 86400;
if ($newExpiry < $startsAt) {
throw new CloudError('续费时间超出系统范围', 409);
}
$addedTraffic = max(0, (int) $locked->renewal_traffic_bytes);
$renewal = new CloudRenewal();
$renewal->instance_id = $locked->id;
$renewal->user_id = $user->id;
$renewal->plan_id = $locked->plan_id;
$renewal->idempotency_key = $idempotency;
$renewal->request_hash = $requestHash;
$renewal->amount = CloudMoney::decimal($price);
$renewal->validity_days = $days;
$renewal->traffic_bytes_added = $addedTraffic;
$renewal->previous_expires_at = $previousExpiry;
$renewal->new_expires_at = $newExpiry;
$renewal->created_at = $now;
$renewal->save();
$period = new CloudServicePeriod();
$period->instance_id = $locked->id;
$period->user_id = $user->id;
$period->plan_id = $locked->plan_id;
$period->renewal_id = $renewal->id;
$period->starts_at = $startsAt;
$period->ends_at = $newExpiry;
$period->traffic_quota_bytes = $addedTraffic;
$period->status = $startsAt <= $now ? 'active' : 'scheduled';
$period->activated_at = $startsAt <= $now ? $now : null;
if ($period->status === 'active') {
$baselineReady = (int) $locked->traffic_synced_at >= $startsAt;
$period->download_base_bytes = $baselineReady
? max(0, (int) $locked->lifetime_download_bytes) : null;
$period->upload_base_bytes = $baselineReady
? max(0, (int) $locked->lifetime_upload_bytes) : null;
}
$period->created_at = $period->updated_at = $now;
$period->save();
$renewal->period_id = $period->id;
$renewal->save();
$user->money = CloudMoney::decimal($balance - $price);
$user->save();
$locked->expires_at = $newExpiry;
if ($period->status === 'active') {
$locked->current_period_id = $period->id;
$locked->traffic_quota_bytes = $addedTraffic;
$locked->traffic_download_bytes = 0;
$locked->traffic_upload_bytes = 0;
if (!in_array((string) $locked->service_status, ['active', 'exempt'], true)) {
$locked->service_status = 'resume_pending';
}
if ((string) $locked->network_status !== 'active') {
$locked->network_status = 'restore_pending';
$locked->network_suspended_at = null;
}
}
$locked->updated_at = $now;
$locked->save();
return $renewal;
}, 3);
}
public static function display(CloudRenewal $renewal): array
{
return [
'id' => (string) $renewal->id,
'instance_id' => (string) $renewal->instance_id,
'amount' => (string) $renewal->amount,
'validity_days' => (int) $renewal->validity_days,
'traffic_bytes_added' => (int) $renewal->traffic_bytes_added,
'period_id' => $renewal->period_id === null ? null : (string) $renewal->period_id,
'previous_expires_at' => (int) $renewal->previous_expires_at,
'new_expires_at' => (int) $renewal->new_expires_at,
'created_at' => (int) $renewal->created_at,
];
}
}

@ -116,7 +116,16 @@ final class CloudSessionService
$superAdmin = (bool) $user->is_admin && in_array((int) $user->id,
array_map('intval', explode(',', (string) Config::get('super_admin'))), true);
if (!$superAdmin) {
$query->where('user_id', $user->id);
$now = time();
$query->where('user_id', $user->id)
->where(function ($available) use ($now) {
$available->where('service_status', 'exempt')
->orWhere(function ($paid) use ($now) {
$paid->where('service_status', 'active')
->whereNotNull('expires_at')
->where('expires_at', '>', $now);
});
});
}
return $query->exists();
}

@ -0,0 +1,152 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\User;
final class CloudTrafficService
{
public static function sync(int $limit = 500): int
{
$batchSize = max(1, min(2000, $limit));
$updated = 0;
CloudInstance::where('status', '<>', 'deleted')
->where('service_status', '<>', 'exempt')
->select(['id', 'host_id', 'user_id', 'uuid'])
->chunkById($batchSize, function ($instances) use (&$updated) {
$updated += self::syncBatch($instances);
});
return $updated;
}
private static function syncBatch($instances): int
{
if ($instances->isEmpty()) {
return 0;
}
$groups = [];
foreach ($instances as $instance) {
$groups[(int) $instance->host_id][] = $instance;
}
$updated = 0;
$log = User::getConnectionResolver()->connection('log');
foreach ($groups as $hostId => $owned) {
$scope = hash('sha256', 'virt-cloud/host/' . $hostId);
$uuids = array_map(static fn ($instance) => (string) $instance->uuid, $owned);
$rows = $log->table('engine_vm_traffic_totals')
->where('host_scope', $scope)->whereIn('vm_uuid', $uuids)->get();
$byUuid = [];
foreach ($rows as $row) {
$byUuid[(string) $row->vm_uuid] = $row;
}
foreach ($owned as $instance) {
$row = $byUuid[(string) $instance->uuid] ?? null;
// An absent or changed owner is unknown usage, not grounds to
// stop another tenant's VM. The next successful sample repairs it.
if ($row === null || $row->user_id === null
|| (int) $row->user_id !== (int) $instance->user_id
|| ($row->host_id !== null && (string) $row->host_id !== (string) $hostId)) {
continue;
}
$download = max(0, (int) $row->download_rx_bytes);
$upload = max(0, (int) $row->upload_tx_bytes);
$observed = max(0, (int) ($row->last_observed_at ?? $row->updated_at ?? 0));
$changed = CloudInstance::where('id', $instance->id)
->where('host_id', $hostId)->where('user_id', $instance->user_id)
->where('uuid', $instance->uuid)
->where('traffic_synced_at', '<=', $observed)
// Engine totals are durable monotonic counters. A rebuilt,
// restored or corrupt log database must never roll paid
// usage backwards and accidentally restore a blocked NIC.
->where('lifetime_download_bytes', '<=', $download)
->where('lifetime_upload_bytes', '<=', $upload)
->update(['lifetime_download_bytes' => $download,
'lifetime_upload_bytes' => $upload, 'traffic_synced_at' => $observed]);
$updated += $changed;
}
}
return $updated;
}
/**
* Reconstruct a conservative period boundary from durable cumulative
* checkpoints. Chart samples are intentionally retained for only 30
* days, while the engine keeps the first cumulative total in every hour
* indefinitely. Using the first observation *after* the boundary excludes
* the straddling interval and bounds uncharged traffic to less than one
* checkpoint hour even after a long lifecycle-daemon outage.
*/
public static function periodBaseline(CloudInstance $instance,
int $startsAt): ?array
{
if ((int) $instance->traffic_synced_at < $startsAt) {
return null;
}
$log = User::getConnectionResolver()->connection('log');
$scope = hash('sha256', 'virt-cloud/host/' . (int) $instance->host_id);
if ($log->getSchemaBuilder()->hasTable('engine_vm_traffic_checkpoints')) {
$checkpoint = $log->table('engine_vm_traffic_checkpoints')
->where('host_scope', $scope)->where('vm_uuid', (string) $instance->uuid)
->where('user_id', (int) $instance->user_id)
->where('host_id', (string) (int) $instance->host_id)
->where('first_observed_at', '>', $startsAt)
->where('first_observed_at', '<=', (int) $instance->traffic_synced_at)
->orderBy('first_observed_at')->first([
'download_rx_bytes', 'upload_tx_bytes',
]);
if ($checkpoint !== null) {
$download = max(0, (int) $checkpoint->download_rx_bytes);
$upload = max(0, (int) $checkpoint->upload_tx_bytes);
if ($download <= (int) $instance->lifetime_download_bytes
&& $upload <= (int) $instance->lifetime_upload_bytes) {
return ['download' => $download, 'upload' => $upload];
}
return null;
}
}
// Upgrade compatibility for a boundary still covered by the old
// 30-day sample table. Refuse partial retained history: otherwise a
// long outage would subtract only the tail and silently undercount.
if (!$log->getSchemaBuilder()->hasTable('engine_vm_metric_samples')) {
return null;
}
$rows = $log->table('engine_vm_metric_samples')
->where('host_scope', $scope)->where('vm_uuid', (string) $instance->uuid)
->where('user_id', (int) $instance->user_id)
->where('host_id', (string) (int) $instance->host_id)
->where('sampled_at', '>', $startsAt)
->where('sampled_at', '<=', (int) $instance->traffic_synced_at)
->orderBy('sampled_at')->get([
'sampled_at', 'network_rx_bytes', 'network_tx_bytes',
]);
$download = 0;
$upload = 0;
$skippedBoundaryInterval = false;
foreach ($rows as $row) {
if ($row->network_rx_bytes === null || $row->network_tx_bytes === null) {
continue;
}
if (!$skippedBoundaryInterval) {
// The legacy sample fallback is safe only when retention still
// covers the boundary. Normal collection is every 60 seconds;
// five minutes tolerates jitter without accepting a truncated
// 30-day tail as complete history.
if ((int) $row->sampled_at > $startsAt + 300) {
return null;
}
$skippedBoundaryInterval = true;
continue;
}
$download += max(0, (int) $row->network_rx_bytes);
$upload += max(0, (int) $row->network_tx_bytes);
}
$lifetimeDownload = max(0, (int) $instance->lifetime_download_bytes);
$lifetimeUpload = max(0, (int) $instance->lifetime_upload_bytes);
if ($download > $lifetimeDownload || $upload > $lifetimeUpload) {
return null;
}
return ['download' => $lifetimeDownload - $download,
'upload' => $lifetimeUpload - $upload];
}
}

@ -4,20 +4,29 @@ namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\CloudJob;
use App\Models\CloudOrder;
use App\Models\CloudServicePeriod;
use App\Models\Shop;
use App\Models\User;
final class CloudWorkerService
{
private const ENGINE_TIMEOUT_SECONDS = 900.0;
private const POLICY_ENGINE_TIMEOUT_SECONDS = 120.0;
private const LEASE_SECONDS = 930;
private const POLICY_KINDS = ['service_suspend', 'service_resume',
'network_suspend', 'network_resume'];
public function __construct(private ?CloudEngineClient $engine = null)
private bool $policyOnly;
public function __construct(private ?CloudEngineClient $engine = null,
bool $policyOnly = false)
{
$this->policyOnly = $policyOnly;
// A cloud-image clone can legitimately take minutes on remote storage.
// Keep the worker lease longer than the HTTP timeout so another worker
// cannot claim the same job while the first request is still active.
$this->engine ??= new CloudEngineClient(null, self::ENGINE_TIMEOUT_SECONDS);
$this->engine ??= new CloudEngineClient(null, $policyOnly
? self::POLICY_ENGINE_TIMEOUT_SECONDS : self::ENGINE_TIMEOUT_SECONDS);
}
public static function disposition(array $operation, bool $creating): string
@ -39,14 +48,24 @@ final class CloudWorkerService
public function tick(): bool
{
$job = (new CloudJob())->getConnection()->transaction(function () {
$job = CloudJob::whereIn('status', ['queued', 'running', 'uncertain'])
->where('available_at', '<=', time())->where('lease_until', '<=', time())
->orderBy('id')->lockForUpdate()->first();
$query = CloudJob::whereIn('status', ['queued', 'running', 'uncertain'])
->where('available_at', '<=', time())->where('lease_until', '<=', time());
if ($this->policyOnly) {
$query->whereIn('kind', self::POLICY_KINDS);
} else {
// The regular worker remains a fail-safe policy consumer when
// the dedicated workers are not installed, but always claims
// target-state policy ahead of slow image/provisioning jobs.
$quoted = "'" . implode("','", self::POLICY_KINDS) . "'";
$query->orderByRaw('CASE WHEN kind IN (' . $quoted . ') THEN 0 ELSE 1 END');
}
$job = $query->orderBy('id')->lockForUpdate()->first();
if ($job === null) {
return null;
}
$job->lease_token = bin2hex(random_bytes(16));
$job->lease_until = time() + self::LEASE_SECONDS;
$job->lease_until = time() + (in_array($job->kind, self::POLICY_KINDS, true)
? 150 : self::LEASE_SECONDS);
$job->attempts++;
$job->save();
return $job;
@ -58,10 +77,12 @@ final class CloudWorkerService
$dispatched = $job->dispatched_at !== null;
try {
$payload = CloudSecrets::decrypt($job->payload_cipher);
$systemPolicy = ($payload['system_policy'] ?? false) === true
&& in_array($job->kind, self::POLICY_KINDS, true);
$actor = ['user_id' => (int) $job->user_id, 'is_admin' => false, 'is_super_admin' => false,
'can_manage' => false, 'session_id' => '', 'operation_id' => $job->operation_id];
$user = User::find($job->user_id);
if ($user === null || (int) $user->enable !== 1) {
if (!$systemPolicy && ($user === null || (int) $user->enable !== 1)) {
if ($job->dispatched_at === null) {
$this->finish($job, ['status' => 'failed', 'safe_to_refund' => true], $payload);
return true;
@ -70,21 +91,117 @@ final class CloudWorkerService
if ($job->kind === 'create') {
$actor['can_provision'] = true;
$actor['can_manage'] = true;
} elseif ((int) ($payload['owner_id'] ?? 0) === (int) $job->user_id) {
} elseif ($systemPolicy) {
$actor['can_manage'] = true;
} elseif (($payload['can_manage'] ?? false) === true && $user !== null
&& (bool) $user->is_admin && in_array((int) $user->id,
array_map('intval', explode(',', (string) \App\Services\Config::get('super_admin'))), true)) {
$actor['is_admin'] = $actor['is_super_admin'] = $actor['can_manage'] = true;
} elseif ((int) ($payload['owner_id'] ?? 0) === (int) $job->user_id) {
$actor['can_manage'] = true;
}
if ($job->kind !== 'create' && !$actor['can_manage'] && $job->dispatched_at === null) {
$this->finish($job, ['status' => 'failed'], $payload);
return true;
}
if ($systemPolicy && $job->dispatched_at === null && $job->instance_id !== null) {
$instance = CloudInstance::find($job->instance_id);
$desired = self::policyDesired($instance, (string) $job->kind);
if (!$desired) {
$this->finish($job, ['status' => 'succeeded', 'result' => ['skipped' => true]], $payload);
return true;
}
}
if (!$systemPolicy && $job->kind !== 'create' && $job->dispatched_at === null
&& $job->instance_id !== null) {
$instance = CloudInstance::find($job->instance_id);
$policyTransition = $instance !== null
&& (in_array((string) $instance->service_status,
['suspending_expired', 'resume_pending', 'resuming',
'suspend_failed', 'resume_failed'], true)
|| in_array((string) $instance->network_status,
['blocking', 'restore_pending', 'restoring',
'block_failed', 'restore_failed'], true));
$globalOverride = $actor['is_super_admin'] && $actor['can_manage'];
if ($instance === null || ($job->kind !== 'delete' && !$globalOverride
&& ($policyTransition || !$instance->serviceAvailable()))) {
$this->finish($job, ['status' => 'failed',
'error' => '实例服务已到期或正在执行策略,不能执行该操作'], $payload);
return true;
}
}
if ($job->dispatched_at !== null) {
// A crash after dispatch is ambiguous. Never submit a second create or refund on a 404.
$operation = $this->engine->request('GET', '/internal/v1/operations/' . $job->operation_id,
$actor, null, $job->operation_id);
// A crash after dispatch is ambiguous. Destructive/user jobs
// are never replayed. Policy lookups are explicitly read-only:
// the engine's ordinary GET reconciles target state and could
// otherwise apply a stale suspend before PHP rechecks that a
// new paid period now desires resume.
if ($systemPolicy) {
try {
$operation = $this->engine->request('GET',
'/internal/v1/operations/' . $job->operation_id
. '?reconcile=false',
$actor, null, $job->operation_id);
} catch (CloudError $error) {
if ($error->httpStatus !== 404) {
throw $error;
}
$operation = null;
}
$terminal = is_array($operation) && in_array(
(string) ($operation['status'] ?? ''), ['succeeded', 'failed'], true);
if (!$terminal) {
// Do not close or reverse a stale policy while its
// original engine executor can still touch libvirt.
// Keeping this job uncertain also prevents lifecycle
// from enqueueing the opposite target until the next
// reconciliation pass observes a terminal operation.
if (($operation['executor_active'] ?? false) === true) {
throw new CloudError('引擎仍在执行当前实例策略,请稍后核对', 503);
}
// Re-read desired state after the read-only lookup. If
// an engine operation record exists, always converge
// that old target first with the same operation ID,
// even when billing now wants the opposite state. The
// reverse transition stays pending in CloudInstance
// and is dispatched afterwards. This ordering remains
// safe across an engine crash while ACPI shutdown or a
// live NIC change was only partly applied.
$current = $job->instance_id === null ? null
: CloudInstance::find($job->instance_id);
$desired = self::policyDesired($current, (string) $job->kind);
if ($operation === null && !$desired) {
$result = ['skipped' => true];
if ($job->kind === 'service_suspend'
&& ($operation['service_was_running_hint'] ?? false) === true) {
$result['service_was_running'] = true;
}
$this->finish($job, ['status' => 'succeeded',
'result' => $result], $payload);
return true;
}
$operation = $this->engine->host((int) $job->host_id,
$payload['method'], $payload['resource'], $actor,
$payload['body'], $job->operation_id);
}
} else {
try {
$operation = $this->engine->request('GET',
'/internal/v1/operations/' . $job->operation_id,
$actor, null, $job->operation_id);
} catch (CloudError $error) {
if ($job->kind === 'create' || $error->httpStatus !== 404) {
throw $error;
}
// Never replay destructive/user mutations. An
// authoritative missing operation means there is no
// engine registry entry left to reconcile; terminate
// the local job visibly so it cannot block expiry.
$this->finish($job, ['status' => 'failed',
'error' => '引擎中已无此任务记录,请核查虚拟机状态后重试'], $payload);
return true;
}
}
} else {
$updated = CloudJob::where('id', $job->id)->where('lease_token', $job->lease_token)
->whereNull('dispatched_at')->update(['dispatched_at' => time(), 'status' => 'running', 'updated_at' => time()]);
@ -94,9 +211,10 @@ final class CloudWorkerService
$dispatched = true;
$operation = $this->engine->host((int) $job->host_id, $payload['method'],
$payload['resource'], $actor, $payload['body'], $job->operation_id);
if (isset($operation['uuid']) && !array_key_exists('result', $operation)) {
$operation = ['status' => $operation['status'] ?? 'succeeded', 'result' => $operation];
}
}
if (isset($operation['uuid']) && !array_key_exists('result', $operation)) {
$operation = ['status' => $operation['status'] ?? 'succeeded',
'result' => $operation];
}
$this->finish($job, $operation, $payload);
} catch (\Throwable $error) {
@ -131,6 +249,19 @@ final class CloudWorkerService
return true;
}
private static function policyDesired(?CloudInstance $instance, string $kind): bool
{
return $instance !== null && $instance->status !== 'deleted' && match ($kind) {
'service_suspend' => $instance->service_status === 'suspending_expired',
'service_resume' => in_array((string) $instance->service_status,
['resume_pending', 'resuming'], true),
'network_suspend' => $instance->network_status === 'blocking',
'network_resume' => in_array((string) $instance->network_status,
['restore_pending', 'restoring'], true),
default => false,
};
}
private function finish(CloudJob $claimed, array $operation, array $payload): void
{
$status = self::disposition($operation, $claimed->kind === 'create');
@ -156,13 +287,60 @@ final class CloudWorkerService
$instance->host_id = $order->host_id;
$instance->user_id = $order->user_id;
$instance->order_id = $order->id;
$spec = json_decode((string) $order->spec_json, true) ?: [];
$now = time();
$billingSnapshot = array_key_exists('validity_days', $spec)
&& array_key_exists('traffic_quota_bytes', $spec)
&& (int) $spec['validity_days'] >= 1;
$days = $billingSnapshot ? (int) $spec['validity_days'] : 0;
$traffic = $billingSnapshot
? max(0, (int) $spec['traffic_quota_bytes']) : 0;
$instance->plan_id = $order->plan_id;
$instance->name = $payload['body']['name'];
$instance->status = 'active';
// Orders submitted by a pre-billing release do not contain an
// immutable term/quota snapshot. Never invent a 30-day expiry
// while such an order is in flight during upgrade; preserve
// its historical unlimited/exempt behavior instead.
$instance->service_status = $billingSnapshot ? 'active' : 'exempt';
$instance->network_status = 'active';
$instance->expires_at = $billingSnapshot ? $now + $days * 86400 : null;
$instance->renewal_price = $billingSnapshot ? $order->amount : null;
$instance->renewal_days = $days;
$instance->renewal_traffic_bytes = $traffic;
$instance->traffic_quota_bytes = $traffic;
$instance->traffic_download_bytes = 0;
$instance->traffic_upload_bytes = 0;
$instance->lifetime_download_bytes = 0;
$instance->lifetime_upload_bytes = 0;
$instance->traffic_synced_at = 0;
$instance->spec_json = $order->spec_json;
$instance->credential_cipher = isset($payload['credential']) ? CloudSecrets::encrypt($payload['credential']) : null;
$instance->created_at = $order->created_at;
$instance->updated_at = time();
$instance->save();
if ($billingSnapshot) {
$period = CloudServicePeriod::where('order_id', $order->id)->first()
?? new CloudServicePeriod();
$period->instance_id = $instance->id;
$period->user_id = $order->user_id;
$period->plan_id = $order->plan_id;
$period->order_id = $order->id;
$period->starts_at = $now;
$period->ends_at = $instance->expires_at;
$period->traffic_quota_bytes = $traffic;
$period->download_base_bytes = 0;
$period->upload_base_bytes = 0;
$period->download_used_bytes = 0;
$period->upload_used_bytes = 0;
$period->status = 'active';
$period->activated_at = $now;
$period->created_at = $period->created_at ?: $now;
$period->updated_at = $now;
$period->save();
$instance->current_period_id = $period->id;
$instance->save();
}
$job->instance_id = $order->instance_id = $instance->id;
$safe = $safe ?? [];
$safe['id'] = (string) $instance->id;
@ -179,6 +357,36 @@ final class CloudWorkerService
$plan->save();
}
$order->refunded_at = time();
} elseif ($status === 'failed' && $job->instance_id !== null
&& in_array($job->kind,
['service_suspend', 'service_resume', 'network_suspend', 'network_resume'], true)) {
// Record a visible policy error. The lifecycle reconciler
// retries this desired state with bounded exponential backoff
// after a temporary host/SSH failure is repaired.
$instance = CloudInstance::where('id', $job->instance_id)->lockForUpdate()->first();
if ($instance !== null) {
$expected = [
'service_suspend' => ['suspending_expired'],
'service_resume' => ['resume_pending', 'resuming'],
'network_suspend' => ['blocking'],
'network_resume' => ['restore_pending', 'restoring'],
][$job->kind];
if ($job->kind === 'service_suspend'
&& in_array((string) $instance->service_status, $expected, true)) {
$instance->service_status = 'suspend_failed';
} elseif ($job->kind === 'service_resume'
&& in_array((string) $instance->service_status, $expected, true)) {
$instance->service_status = 'resume_failed';
} elseif ($job->kind === 'network_suspend'
&& in_array((string) $instance->network_status, $expected, true)) {
$instance->network_status = 'block_failed';
} elseif ($job->kind === 'network_resume'
&& in_array((string) $instance->network_status, $expected, true)) {
$instance->network_status = 'restore_failed';
}
$instance->updated_at = time();
$instance->save();
}
} elseif ($status === 'succeeded' && $job->instance_id !== null) {
$instance = CloudInstance::where('id', $job->instance_id)->lockForUpdate()->first();
if ($instance !== null) {
@ -187,6 +395,32 @@ final class CloudWorkerService
$instance->credential_cipher = null;
} elseif ($job->kind === 'reinstall' && isset($payload['credential'])) {
$instance->credential_cipher = CloudSecrets::encrypt($payload['credential']);
} elseif ($job->kind === 'service_suspend'
&& in_array((string) $instance->service_status,
['suspending_expired', 'resume_pending', 'resuming'], true)) {
// A renewal may commit while the already-dispatched
// expiry shutdown is still finishing. Preserve proof
// that policy stopped a running guest so the following
// resume target may start it again. Never auto-start a
// VM that the tenant had already stopped themselves.
if (($result['service_was_running'] ?? false) === true) {
$instance->suspended_at = time();
}
if ($instance->service_status === 'suspending_expired') {
$instance->service_status = 'suspended_expired';
}
} elseif ($job->kind === 'service_resume'
&& in_array((string) $instance->service_status, ['resuming', 'resume_pending'], true)) {
$instance->service_status = 'active';
$instance->suspended_at = null;
} elseif ($job->kind === 'network_suspend'
&& $instance->network_status === 'blocking') {
$instance->network_status = 'blocked_traffic';
$instance->network_suspended_at = time();
} elseif ($job->kind === 'network_resume'
&& in_array((string) $instance->network_status, ['restoring', 'restore_pending'], true)) {
$instance->network_status = 'active';
$instance->network_suspended_at = null;
}
$instance->updated_at = time();
$instance->save();

@ -48,8 +48,10 @@ $System_Config['cloud_engine_data_dir'] = dirname(__DIR__, 2) . '/runtime/engine
# VNC 持续检查 PHP 会话。设为面板服务器本机可访问的网站 URL,不要指向宿主机。
$System_Config['cloud_introspection_url'] = 'http://127.0.0.1/api/internal/cloud/session-introspection';
$System_Config['cloud_introspection_host'] = '';
# 本轮只按套餐价格购买开通,不包含到期、续费或在线更新。
# 每个账户最多持有的非删除实例数。
$System_Config['cloud_max_instances_per_user'] = 20;
# 单台实例最多排队的未来续费账期,防止免费套餐或自动化请求无限写入数据库。
$System_Config['cloud_max_renewal_periods_ahead'] = 24;
# redis
$System_Config['redis_scheme'] = 'tcp';
$System_Config['redis_host'] = '127.0.0.1';

@ -5,6 +5,8 @@ $app->post('/api/internal/cloud/session-introspection', 'App\Controllers\CloudIn
$app->group('/api/v1', function () {
$this->get('/plans', 'App\Controllers\CloudApiController:plans');
$this->map(['GET', 'POST'], '/orders', 'App\Controllers\CloudApiController:orders');
$this->get('/orders/by-request/{key:[A-Za-z0-9_.-]+}',
'App\Controllers\CloudApiController:orderRequest');
$this->get('/orders/{id:[0-9]+}', 'App\Controllers\CloudApiController:order');
foreach (['jobs', 'reinstall-jobs', 'rescue-jobs'] as $name) {
$this->get('/' . $name . '/{id:[0-9]+}', 'App\Controllers\CloudApiController:job');
@ -16,6 +18,7 @@ $app->group('/api/v1', function () {
$this->post('/vms/{id}/credential', 'App\Controllers\CloudApiController:credential');
$this->post('/vms/{id}/console-ticket', 'App\Controllers\CloudApiController:consoleTicket');
$this->post('/vms/{id}/actions', 'App\Controllers\CloudApiController:actions');
$this->post('/vms/{id}/renewals', 'App\Controllers\CloudApiController:renew');
$this->post('/vms/{id}/reinstall', 'App\Controllers\CloudApiController:reinstall');
$this->map(['GET', 'POST'], '/vms/{id}/rescue', 'App\Controllers\CloudApiController:rescue');
$this->post('/vms/{id}/rescue/force-start', 'App\Controllers\CloudApiController:rescue')->setArgument('rescue_action', '/force-start');

@ -1,5 +1,6 @@
# Edit the PHP binary and project path for your deployment before installing.
# No account expiry, entitlement reset, proxy-node or automatic record-deletion jobs.
# VM service-period and traffic enforcement is the virt-cloud-lifecycle systemd service, not cron.
# No account expiry, proxy-node or automatic record-deletion jobs.
0 6 * * * /usr/bin/php /home/www/virt-cloud/web/xcat backupJob >> /home/www/virt-cloud/web/storage/logs/database_backup.log 2>&1
* * * * * /usr/bin/php /home/www/virt-cloud/web/xcat usdtScanJob 50 >> /home/www/virt-cloud/web/storage/logs/usdt_scan.log 2>&1
*/5 * * * * /usr/bin/php /home/www/virt-cloud/web/xcat siteAnalyticsJob 2 >> /home/www/virt-cloud/web/storage/logs/site_analytics.log 2>&1

@ -1,7 +1,9 @@
// PHP owns authentication, authorisation, ownership and purchasing. No engine
// address, token, host credential or unrestricted create API exists in this UI.
const byId = (id) => document.getElementById(id);
const state = { csrfToken: '', username: '', admin: false, vms: [], plans: [], sshKeys: [], templates: [], resources: null, view: 'list', selectedVmId: null, metricsRange: '24h', metricsPayload: null, metricsRequest: null, metricsPollTimer: null, metricsResizeTimer: null, credentialPassword: '', credentialVisible: false, credentialTimer: null, rfb: null, consoleToken: 0, loadingVMs: false, selectedKeys: { create: '', reinstall: '' }, formEpoch: 0, resourceController: null, orderPoll: null, mutationBusy: false, rescueActive: false, rescueStaged: false };
const state = { csrfToken: '', userId: '', username: '', admin: false, vms: [], plans: [], sshKeys: [], templates: [], resources: null, view: 'list', selectedVmId: null, metricsRange: '24h', metricsPayload: null, metricsRequest: null, metricsPollTimer: null, metricsResizeTimer: null, credentialPassword: '', credentialVisible: false, credentialTimer: null, rfb: null, consoleToken: 0, loadingVMs: false, selectedKeys: { create: '', reinstall: '' }, formEpoch: 0, resourceController: null, orderPoll: null, mutationBusy: false, orderIdempotency: '', orderFingerprint: '', renewalIdempotency: {}, rescueActive: false, rescueStaged: false };
const RENEWAL_STORAGE_PREFIX = 'virt-cloud:pending-renewal:';
const ORDER_STORAGE_PREFIX = 'virt-cloud:pending-order:';
const text = (id, value) => { byId(id).textContent = value ?? '—'; };
const items = (payload) => Array.isArray(payload) ? payload : Array.isArray(payload?.items) ? payload.items : Array.isArray(payload?.plans) ? payload.plans : Array.isArray(payload?.orders) ? payload.orders : Array.isArray(payload?.templates) ? payload.templates : Array.isArray(payload?.keys) ? payload.keys : Array.isArray(payload?.vms) ? payload.vms : [];
function showError(id, message) { text(id, message); byId(id).hidden = !message; }
@ -17,10 +19,116 @@ async function api(path, options = {}) {
let payload = null;
if (response.status !== 204) { try { payload = await response.json(); } catch { throw new Error('服务返回了非 JSON 数据,请检查 PHP 及反代配置。'); } }
if (response.status === 401) { clearCredential(); closeConsole(); location.assign('/auth/login'); throw new Error('登录已过期。'); }
if (!response.ok || payload?.ret === 0) { const detail = Array.isArray(payload?.detail) ? payload.detail.map(item => item.msg || '输入错误').join(';') : payload?.detail; throw new Error(String(detail || payload?.message || payload?.msg || payload?.error || `请求失败 (${response.status})`)); }
if (!response.ok || payload?.ret === 0) { const detail = Array.isArray(payload?.detail) ? payload.detail.map(item => item.msg || '输入错误').join(';') : payload?.detail; const error = new Error(String(detail || payload?.message || payload?.msg || payload?.error || `请求失败 (${response.status})`)); error.status = response.status; throw error; }
return payload;
}
function normalizeVm(vm) { return { ...vm, uuid: String(vm.uuid || vm.id || ''), api_id: String(vm.id || vm.uuid || ''), vcpu: vm.vcpu ?? vm.cpu_cores, memory_mib: vm.memory_mib, public_ip_cidr: vm.public_ip_cidr || vm.public_ip || '', password_available: vm.password_available === true || vm.has_initial_root_password === true }; }
function normalizeVm(vm) {
const canMutate = vm.can_mutate !== false;
const canViewCredential = vm.can_view_credential !== false;
return { ...vm, uuid: String(vm.uuid || vm.id || ''), api_id: String(vm.id || vm.uuid || ''), vcpu: vm.vcpu ?? vm.cpu_cores,
memory_mib: vm.memory_mib, public_ip_cidr: vm.public_ip_cidr || vm.public_ip || '', can_mutate: canMutate,
can_view_credential: canViewCredential, password_available: canViewCredential
&& (vm.password_available === true || vm.has_initial_root_password === true) };
}
function firstNumber(...values) {
for (const value of values) {
if (value === null || value === undefined || value === '') continue;
const number = Number(value);
if (Number.isFinite(number) && number >= 0) return number;
}
return null;
}
function firstBoolean(...values) {
for (const value of values) {
if (value === true || value === false) return value;
if (value === 1 || value === '1' || value === 'true') return true;
if (value === 0 || value === '0' || value === 'false') return false;
}
return null;
}
function firstText(...values) {
for (const value of values) {
if (value !== null && value !== undefined && String(value).trim() !== '') return String(value).trim();
}
return '';
}
function vmServiceData(vm) {
const period = vm?.current_period && typeof vm.current_period === 'object' ? vm.current_period
: vm?.service_period && typeof vm.service_period === 'object' ? vm.service_period : {};
const service = vm?.service && typeof vm.service === 'object' ? vm.service : period;
const billing = vm?.billing && typeof vm.billing === 'object' ? vm.billing : {};
const traffic = vm?.traffic && typeof vm.traffic === 'object' ? vm.traffic : (service.traffic && typeof service.traffic === 'object' ? service.traffic : {});
const trafficGib = firstNumber(vm?.traffic_gib, service.traffic_gib, period.traffic_gib, billing.traffic_gib);
const quota = firstNumber(vm?.traffic_quota_bytes, service.traffic_quota_bytes, period.traffic_quota_bytes, billing.traffic_quota_bytes, traffic.quota_bytes,
trafficGib === null ? null : trafficGib * 1024 ** 3);
const rx = firstNumber(vm?.traffic_download_bytes, vm?.traffic_rx_bytes, vm?.download_rx_bytes, vm?.network_rx_total_bytes,
service.traffic_download_bytes, service.traffic_rx_bytes, period.traffic_download_bytes, period.traffic_rx_bytes, traffic.rx_bytes, traffic.download_bytes);
const tx = firstNumber(vm?.traffic_upload_bytes, vm?.traffic_tx_bytes, vm?.upload_tx_bytes, vm?.network_tx_total_bytes,
service.traffic_upload_bytes, service.traffic_tx_bytes, period.traffic_upload_bytes, period.traffic_tx_bytes, traffic.tx_bytes, traffic.upload_bytes);
const used = firstNumber(vm?.traffic_used_bytes, vm?.traffic_total_bytes, service.traffic_used_bytes, period.traffic_used_bytes, traffic.used_bytes, traffic.total_bytes,
rx !== null && tx !== null ? rx + tx : null);
const remaining = firstNumber(vm?.traffic_remaining_bytes, service.traffic_remaining_bytes, period.traffic_remaining_bytes, traffic.remaining_bytes,
quota !== null && used !== null ? Math.max(0, quota - used) : null);
const validityDays = firstNumber(vm?.renewal_days, vm?.validity_days, service.renewal_days, service.validity_days, period.renewal_days, period.validity_days, billing.renewal_days, billing.validity_days);
const renewalPrice = firstNumber(vm?.renewal_price, service.renewal_price, period.renewal_price, billing.renewal_price);
const renewalQuota = firstNumber(vm?.renewal_traffic_bytes, service.renewal_traffic_bytes, period.renewal_traffic_bytes, billing.renewal_traffic_bytes, quota);
const periodStartsAt = vm?.current_period_starts_at ?? vm?.current_period_started_at ?? vm?.current_period_start ?? service.current_period_starts_at ?? service.current_period_started_at ?? service.current_period_start ?? period.started_at ?? period.starts_at ?? period.start_at ?? null;
const periodEndsAt = vm?.current_period_ends_at ?? vm?.current_period_end ?? service.current_period_ends_at ?? service.current_period_end ?? period.ends_at ?? period.end_at ?? null;
const expiresAt = vm?.service_expires_at ?? vm?.final_expires_at ?? vm?.expires_at ?? vm?.valid_until ?? service.service_expires_at ?? service.final_expires_at ?? service.expires_at ?? service.valid_until ?? period.final_expires_at ?? period.expires_at ?? billing.expires_at ?? null;
const prepaidPeriods = firstNumber(vm?.prepaid_future_periods, vm?.prepaid_periods, vm?.future_period_count, vm?.scheduled_period_count,
service.prepaid_future_periods, service.prepaid_periods, service.future_period_count, period.prepaid_future_periods, period.prepaid_periods, billing.prepaid_periods);
const status = firstText(vm?.service_status, service.status, period.status, billing.status, vm?.subscription_status);
const networkStatus = firstText(vm?.network_status, service.network_status, period.network_status, traffic.network_status);
const renewable = firstBoolean(vm?.can_renew, vm?.renewable, service.can_renew, service.renewable, period.can_renew, period.renewable, billing.can_renew, billing.renewable);
return { quota, rx, tx, used, remaining, validityDays, renewalPrice, renewalQuota, periodStartsAt, periodEndsAt, expiresAt, prepaidPeriods, status, networkStatus, renewable,
available: Boolean(status || networkStatus || expiresAt !== null || periodStartsAt !== null || periodEndsAt !== null || [quota, rx, tx, used, remaining, validityDays, renewalPrice, renewalQuota, prepaidPeriods].some(value => value !== null) || renewable !== null) };
}
function formatBytes(value) {
if (value === null || !Number.isFinite(value) || value < 0) return '暂未提供';
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
let amount = value; let unit = 0;
while (amount >= 1024 && unit < units.length - 1) { amount /= 1024; unit += 1; }
const precision = unit === 0 ? 0 : amount >= 100 ? 0 : amount >= 10 ? 1 : 2;
return `${amount.toFixed(precision)} ${units[unit]}`;
}
function formatTrafficQuota(value) { return value === 0 ? '不限流量' : formatBytes(value); }
function serviceTimestamp(value) {
if (value === null || value === undefined || value === '') return null;
if (typeof value === 'number' || /^\d+$/.test(String(value).trim())) {
const numeric = Number(value);
if (!Number.isFinite(numeric) || numeric <= 0) return null;
return numeric < 1e12 ? numeric * 1000 : numeric;
}
const parsed = Date.parse(String(value));
return Number.isFinite(parsed) ? parsed : null;
}
function formatServiceDate(value) {
const timestamp = serviceTimestamp(value);
return timestamp === null ? '暂未提供' : new Date(timestamp).toLocaleString('zh-CN', { hour12: false });
}
function serviceStatusInfo(data) {
let status = data.status.toLowerCase().replace(/[\s-]+/g, '_');
if (!status && serviceTimestamp(data.expiresAt) !== null) status = serviceTimestamp(data.expiresAt) <= Date.now() ? 'expired' : 'active';
const labels = {
active: ['服务有效', 'active'], pending: ['待生效', 'warning'], renewing: ['续费处理中', 'warning'], grace: ['宽限期', 'warning'],
resume_pending: ['等待恢复服务', 'warning'], resuming: ['正在恢复服务', 'warning'], resume_failed: ['服务恢复失败,请联系管理员', 'stopped'], suspending: ['正在暂停', 'warning'],
suspending_expired: ['到期停机中', 'warning'], suspended_expired: ['已因到期停机', 'stopped'], expired: ['已到期', 'stopped'],
suspend_failed: ['到期停机失败,请联系管理员', 'stopped'],
suspended_traffic: ['已因流量用尽暂停', 'stopped'], traffic_exhausted: ['流量已用尽', 'stopped'], exempt: ['不受套餐限制', 'active'],
};
const [label, className] = labels[status] || [data.status || '暂未提供', 'unknown'];
return { label, className };
}
function networkStatusInfo(value) {
const status = value.toLowerCase().replace(/[\s-]+/g, '_');
const labels = {
active: ['网络正常', 'active'], blocking: ['正在暂停网络', 'warning'], blocked_traffic: ['本期流量用尽,已断网', 'stopped'],
block_failed: ['流量断网失败,请联系管理员', 'stopped'], restore_pending: ['等待恢复网络', 'warning'], restoring: ['正在恢复网络', 'warning'],
restore_failed: ['网络恢复失败,请联系管理员', 'stopped'], exempt: ['网络不受套餐限制', 'active'],
};
const [label, className] = labels[status] || [value || '暂未提供', 'unknown'];
return { label, className };
}
function selectedVm() { return state.vms.find(vm => vm.uuid === state.selectedVmId); }
function vmPath(vm = selectedVm()) { if (!vm) throw new Error('请先选择虚拟机。'); return `/api/v1/vms/${encodeURIComponent(vm.api_id)}`; }
function clearCredential() { clearTimeout(state.credentialTimer); state.credentialPassword = ''; state.credentialVisible = false; text('vm-detail-root-password', '********'); byId('vm-detail-password-reveal').setAttribute('aria-pressed', 'false'); }
@ -59,19 +167,111 @@ function renderVmDetail(vm) {
text('vm-detail-header-created-at', vmCreatedAgo(vm)); const system = vmSystemInfo(vm); text('vm-detail-os-label', system.label); text('vm-detail-os', system.value);
text('vm-detail-spec', `${vm.vcpu ?? '—'} vCPU · ${vm.memory_mib ?? '—'} MiB`); text('vm-detail-disk', diskLabel(vm)); text('vm-detail-ssh-user', vm.login_name || (vm.os_source === 'cloud_template' ? 'root' : '未记录'));
byId('vm-detail-ip-copy').hidden = vmIp(vm) === '未获取';
text('vm-detail-root-password', vm.password_available ? (state.credentialVisible ? state.credentialPassword : '********') : '未记录');
text('vm-detail-root-password', !vm.can_view_credential ? '无权查看' : vm.password_available ? (state.credentialVisible ? state.credentialPassword : '********') : '未记录');
for (const id of ['vm-detail-password-reveal', 'vm-detail-password-copy']) byId(id).hidden = !vm.password_available;
renderVmService(vm);
const running = stateCategory(vm.state) === 'running'; const stopped = stateCategory(vm.state) === 'stopped';
const primary = byId('vm-detail-primary-actions'); const danger = byId('vm-detail-danger-actions'); primary.replaceChildren(); danger.replaceChildren();
primary.append(actionButton('控制台', () => openConsole(vm), { disabled: !running }), actionButton('开机', () => powerAction('start'), { disabled: !stopped }), actionButton('正常关机', () => powerAction('shutdown'), { disabled: !running }), actionButton('重启', () => powerAction('reboot'), { disabled: !running }));
danger.append(actionButton('重装系统', () => openReinstall(vm), { variant: 'danger', disabled: vm.os_source !== 'cloud_template' }), actionButton('紧急救援', () => openRescue(vm), { variant: 'danger' }), actionButton('强制重置', () => powerAction('reset', true), { variant: 'danger', disabled: !running }), actionButton('强制断电', () => powerAction('poweroff', true), { variant: 'danger', disabled: stopped }), actionButton('删除虚拟机', deleteVm, { variant: 'danger', disabled: !stopped }));
if (state.mutationBusy) document.querySelectorAll('#vm-detail-primary-actions button,#vm-detail-danger-actions button').forEach(button => { button.disabled = true; });
danger.closest('section').hidden = !vm.can_mutate;
if (vm.can_mutate) {
primary.append(actionButton('控制台', () => openConsole(vm), { disabled: !running }), actionButton('开机', () => powerAction('start'), { disabled: !stopped }), actionButton('正常关机', () => powerAction('shutdown'), { disabled: !running }), actionButton('重启', () => powerAction('reboot'), { disabled: !running }));
danger.append(actionButton('重装系统', () => openReinstall(vm), { variant: 'danger', disabled: vm.os_source !== 'cloud_template' }), actionButton('紧急救援', () => openRescue(vm), { variant: 'danger' }), actionButton('强制重置', () => powerAction('reset', true), { variant: 'danger', disabled: !running }), actionButton('强制断电', () => powerAction('poweroff', true), { variant: 'danger', disabled: stopped }), actionButton('删除虚拟机', deleteVm, { variant: 'danger', disabled: !stopped }));
} else {
primary.append(makeElement('p', 'muted', '这是其他用户的实例;当前管理员账户只有查看权限。'));
}
if (state.mutationBusy) document.querySelectorAll('#vm-detail-primary-actions button,#vm-detail-danger-actions button,#vm-detail-renew-button').forEach(button => { button.disabled = true; });
}
function renderVmService(vm) {
const data = vmServiceData(vm);
const status = serviceStatusInfo(data);
const statusElement = byId('vm-detail-service-status');
statusElement.textContent = status.label;
statusElement.className = `service-pill ${status.className}`;
const networkStatus = networkStatusInfo(data.networkStatus);
const networkStatusElement = byId('vm-detail-network-status');
networkStatusElement.textContent = networkStatus.label;
networkStatusElement.className = `service-pill ${networkStatus.className}`;
text('vm-detail-period-start', formatServiceDate(data.periodStartsAt));
text('vm-detail-period-end', formatServiceDate(data.periodEndsAt));
text('vm-detail-expires-at', formatServiceDate(data.expiresAt));
text('vm-detail-prepaid-periods', data.prepaidPeriods === null ? '暂未提供' : `${Math.floor(data.prepaidPeriods)} 个`);
text('vm-detail-traffic-rx', formatBytes(data.rx));
text('vm-detail-traffic-tx', formatBytes(data.tx));
text('vm-detail-traffic-used', formatBytes(data.used));
text('vm-detail-traffic-quota', formatTrafficQuota(data.quota));
text('vm-detail-traffic-remaining', data.quota === 0 ? '不限流量' : formatBytes(data.remaining));
const progress = byId('vm-detail-traffic-progress');
const ratio = data.quota !== null && data.quota > 0 && data.used !== null ? Math.max(0, Math.min(100, data.used / data.quota * 100)) : null;
progress.hidden = ratio === null;
if (ratio !== null) {
progress.setAttribute('aria-valuenow', String(Math.round(ratio * 10) / 10));
byId('vm-detail-traffic-progress-bar').style.width = `${ratio}%`;
progress.classList.toggle('is-exhausted', ratio >= 100);
} else {
progress.removeAttribute('aria-valuenow');
byId('vm-detail-traffic-progress-bar').style.width = '0';
progress.classList.remove('is-exhausted');
}
const renewButton = byId('vm-detail-renew-button');
const hasRenewalMetadata = data.renewable !== null || data.validityDays !== null || data.renewalPrice !== null;
renewButton.hidden = vm.is_owner === false || !data.available || !hasRenewalMetadata;
renewButton.disabled = state.mutationBusy || data.renewable === false;
renewButton.textContent = data.renewalPrice !== null && data.validityDays !== null
? `续费 ${Math.floor(data.validityDays)} 天 · ¥${data.renewalPrice.toFixed(2)}` : '立即续费';
renewButton.title = data.renewable === false ? '当前实例暂不能续费' : '购买下一个服务周期';
let note = '接口暂未提供服务周期与流量信息。';
if (data.available) {
note = '流量按当前账期单独计算;用尽时仅暂停网络,下一账期开始流量归零并自动恢复网络。';
if (data.prepaidPeriods !== null && data.prepaidPeriods > 0) note += ` 已预付 ${Math.floor(data.prepaidPeriods)} 个未来账期。`;
if (data.renewable !== false) note += ' 续费购买的下一账期会从当前已购期限末尾开始。';
}
text('vm-detail-service-note', note);
}
async function powerAction(action, dangerous = false) { const vm = selectedVm(); if (!vm?.can_mutate) return toast('当前账户只有查看权限。', 'error'); if (state.mutationBusy) return; if (dangerous && !confirm('此操作会强制断电,可能丢失未保存的数据。确认继续?')) return; state.mutationBusy = true; setDetailBusy(true); try { const result = await api(`${vmPath()}/actions`, { method: 'POST', body: { action } }); toast('电源任务已提交,正在执行…'); if (result.job_id) await pollJob('power', result.job_id); toast('电源操作已完成。', 'success'); await loadVms(); } catch (error) { toast(error.message, 'error'); } finally { state.mutationBusy = false; setDetailBusy(false); } }
async function deleteVm() { const vm = selectedVm(); if (!vm?.can_mutate) return toast('当前账户只有查看权限。', 'error'); if (state.mutationBusy) return; const confirmation = prompt(`永久删除“${vm.name}”及面板管理的虚拟磁盘,此操作不可恢复。请输入完整虚拟机名称确认:`); if (confirmation === null) return; if (confirmation !== vm.name) return toast('虚拟机名称不匹配,未执行删除。', 'error'); state.mutationBusy = true; setDetailBusy(true); try { const result = await api(vmPath(vm), { method: 'DELETE', body: { confirmation_name: vm.name, delete_disks: true } }); toast('删除任务已提交,正在执行…'); if (result.job_id) await pollJob('delete', result.job_id); toast('虚拟机删除已完成。', 'success'); state.selectedVmId = null; showView('list'); await loadVms(); } catch (error) { toast(error.message, 'error'); } finally { state.mutationBusy = false; setDetailBusy(false); } }
function setDetailBusy(busy) { if (!busy && selectedVm()) renderVmDetail(selectedVm()); else if (busy) document.querySelectorAll('#vm-detail-primary-actions button,#vm-detail-danger-actions button,#vm-detail-renew-button').forEach(button => { button.disabled = true; }); }
function renewalStorageKey(vmId) { return `${RENEWAL_STORAGE_PREFIX}${vmId}`; }
function pendingRenewalKey(vmId) {
if (state.renewalIdempotency[vmId]) return state.renewalIdempotency[vmId];
let key = '';
try { key = sessionStorage.getItem(renewalStorageKey(vmId)) || ''; } catch {}
if (!/^[A-Za-z0-9_.-]{16,128}$/.test(key)) key = crypto.randomUUID();
state.renewalIdempotency[vmId] = key;
try { sessionStorage.setItem(renewalStorageKey(vmId), key); } catch {}
return key;
}
function clearPendingRenewalKey(vmId) {
delete state.renewalIdempotency[vmId];
try { sessionStorage.removeItem(renewalStorageKey(vmId)); } catch {}
}
async function renewVm() {
const vm = selectedVm();
if (!vm || state.mutationBusy) return;
const service = vmServiceData(vm);
if (service.renewable === false) return toast('当前实例暂不能续费。', 'error');
const details = [];
if (service.renewalPrice !== null) details.push(`从账户余额支付 ¥${service.renewalPrice.toFixed(2)}`);
if (service.validityDays !== null) details.push(`购买下一个 ${Math.floor(service.validityDays)} 天账期`);
if (service.renewalQuota !== null) details.push(`下一账期流量额度 ${formatTrafficQuota(service.renewalQuota)}`);
const prompt = `${details.length ? details.join(',') : '确认购买下一个服务账期'}。下一账期将从当前已购买期限末尾生效,确认续费?`;
if (!confirm(prompt)) return;
state.mutationBusy = true;
setDetailBusy(true);
const key = pendingRenewalKey(vm.api_id);
try {
await api(`${vmPath(vm)}/renewals`, { method: 'POST', body: { idempotency_key: key } });
clearPendingRenewalKey(vm.api_id);
await loadVms();
toast('续费成功,下一账期将在当前已购期限末尾生效。已关机的虚拟机仍保持关机,需要时请手动开机。', 'success');
} catch (error) {
toast(`${error.message} 可再次点击续费安全核对同一请求。`, 'error');
} finally {
state.mutationBusy = false;
setDetailBusy(false);
}
}
async function powerAction(action, dangerous = false) { if (state.mutationBusy) return; if (dangerous && !confirm('此操作会强制断电,可能丢失未保存的数据。确认继续?')) return; state.mutationBusy = true; setDetailBusy(true); try { const result = await api(`${vmPath()}/actions`, { method: 'POST', body: { action } }); toast('电源任务已提交,正在执行…'); if (result.job_id) await pollJob('power', result.job_id); toast('电源操作已完成。', 'success'); await loadVms(); } catch (error) { toast(error.message, 'error'); } finally { state.mutationBusy = false; setDetailBusy(false); } }
async function deleteVm() { const vm = selectedVm(); if (!vm || state.mutationBusy) return; const confirmation = prompt(`永久删除“${vm.name}”及面板管理的虚拟磁盘,此操作不可恢复。请输入完整虚拟机名称确认:`); if (confirmation === null) return; if (confirmation !== vm.name) return toast('虚拟机名称不匹配,未执行删除。', 'error'); state.mutationBusy = true; setDetailBusy(true); try { const result = await api(vmPath(vm), { method: 'DELETE', body: { confirmation_name: vm.name, delete_disks: true } }); toast('删除任务已提交,正在执行…'); if (result.job_id) await pollJob('delete', result.job_id); toast('虚拟机删除已完成。', 'success'); state.selectedVmId = null; showView('list'); await loadVms(); } catch (error) { toast(error.message, 'error'); } finally { state.mutationBusy = false; setDetailBusy(false); } }
function setDetailBusy(busy) { if (!busy && selectedVm()) renderVmDetail(selectedVm()); else if (busy) document.querySelectorAll('#vm-detail-primary-actions button,#vm-detail-danger-actions button').forEach(button => { button.disabled = true; }); }
async function credential(copy = false) {
const vm = selectedVm(); if (!vm || !vm.password_available) return;
const vm = selectedVm(); if (!vm?.can_view_credential || !vm.password_available) return;
if (!copy && state.credentialVisible) return clearCredential();
const id = vm.api_id;
try { const result = await api(`${vmPath(vm)}/credential`, { method: 'POST' }); if (selectedVm()?.api_id !== id || state.view !== 'detail') return; if (!result?.password) throw new Error('没有可查看的初始密码。'); if (copy) { await copyText(result.password); toast('初始密码已复制。', 'success'); } else { state.credentialPassword = result.password; state.credentialVisible = true; text('vm-detail-root-password', result.password); byId('vm-detail-password-reveal').setAttribute('aria-pressed', 'true'); clearTimeout(state.credentialTimer); state.credentialTimer = setTimeout(clearCredential, 30000); } } catch (error) { toast(error.message, 'error'); }
@ -91,33 +291,91 @@ function renderKeyCards(kind) {
function sourceMode() { return byId('create-form').querySelector('[name="source"]:checked').value; }
function updateSource() { const cloud = sourceMode() === 'cloud'; byId('vm-iso-field').hidden = cloud; byId('vm-iso').required = !cloud; byId('vm-iso').disabled = cloud; byId('vm-template-field').hidden = !cloud; byId('vm-template').disabled = !cloud; byId('vm-cloud-fields').hidden = !cloud; byId('vm-root-password').disabled = !cloud; byId('create-iso-tip').hidden = cloud; byId('create-cloud-tip').hidden = !cloud; }
function strongPassword(value) { return !value || (value.length >= 12 && new TextEncoder().encode(value).length <= 256 && /[A-Z]/.test(value) && /[a-z]/.test(value) && /[0-9]/.test(value) && /[^A-Za-z0-9\s]/.test(value) && !/[\r\n\0]/.test(value)); }
function planValidityDays(plan) { return firstNumber(plan?.validity_days, plan?.service_days, plan?.period_days); }
function planTrafficBytes(plan) {
const gib = firstNumber(plan?.traffic_gib);
return firstNumber(plan?.traffic_quota_bytes, gib === null ? null : gib * 1024 ** 3);
}
function planBillingLabel(plan) {
const days = planValidityDays(plan);
const traffic = planTrafficBytes(plan);
return `${days === null ? '账期未提供' : `${Math.floor(days)} 天/期`} · ${traffic === null ? '每期流量未提供' : `每期流量 ${formatTrafficQuota(traffic)}`}`;
}
async function openCreate() {
state.selectedVmId = null; showView('create'); showError('create-error', '');
try { const [plans] = await Promise.all([api('/api/v1/plans'), loadKeys()]); state.plans = items(plans); const select = byId('vm-plan'); const previous = select.value; select.replaceChildren(makeElement('option', '', state.plans.length ? '请选择套餐' : '暂无可售套餐')); select.firstElementChild.value = ''; for (const plan of state.plans) { const option = makeElement('option', '', `${plan.name} · ¥${plan.price}`); option.value = String(plan.id); select.append(option); } select.value = previous; if (!select.value && state.plans.length === 1) select.value = String(state.plans[0].id); await loadPlanResources(); updateSource(); } catch (error) { showError('create-error', error.message); }
try { const [plans] = await Promise.all([api('/api/v1/plans'), loadKeys()]); state.plans = items(plans); const select = byId('vm-plan'); const previous = select.value; select.replaceChildren(makeElement('option', '', state.plans.length ? '请选择套餐' : '暂无可售套餐')); select.firstElementChild.value = ''; for (const plan of state.plans) { const days = planValidityDays(plan); const option = makeElement('option', '', `${plan.name} · ¥${plan.price}${days === null ? '' : ` / ${Math.floor(days)} 天`}`); option.value = String(plan.id); select.append(option); } select.value = previous; if (!select.value && state.plans.length === 1) select.value = String(state.plans[0].id); await loadPlanResources(); updateSource(); } catch (error) { showError('create-error', error.message); }
}
async function loadPlanResources() {
const planId = byId('vm-plan').value; const plan = state.plans.find(item => String(item.id) === planId); const epoch = ++state.formEpoch;
state.resourceController?.abort(); state.resourceController = new AbortController(); state.resources = null; state.templates = []; renderTemplateCatalog('vm-template'); byId('vm-iso').replaceChildren(makeElement('option', '', '请选择套餐')); byId('vm-iso').firstElementChild.value = '';
byId('create-submit').disabled = true;
if (!plan) { text('vm-plan-summary', '选择套餐后显示资源规格和一次性价格。'); return; }
text('vm-plan-summary', `${plan.cpu_cores} vCPU · ${plan.memory_mib} MiB · ${plan.disk_gib} GiB · ${plan.bandwidth_mbps} Mbps · ${plan.region || '地区未设置'} · 余额一次性支付 ¥${plan.price}`);
if (!plan) { text('vm-plan-summary', '选择套餐后显示资源规格、每期价格、账期和每期流量额度。'); return; }
text('vm-plan-summary', `${plan.cpu_cores} vCPU · ${plan.memory_mib} MiB · ${plan.disk_gib} GiB · ${plan.bandwidth_mbps} Mbps · ${plan.region || '地区未设置'} · ${planBillingLabel(plan)} · 每期 ¥${plan.price}`);
try { const resources = await api(`/api/v1/resources?plan_id=${encodeURIComponent(planId)}`, { signal: state.resourceController.signal }); if (epoch !== state.formEpoch) return; state.resources = resources; state.templates = items(resources.templates || resources.cloud_templates); renderTemplateCatalog('vm-template'); const isos = items(resources.isos || resources.iso_images); const select = byId('vm-iso'); select.replaceChildren(makeElement('option', '', isos.length ? '选择安装镜像' : '没有可用 ISO')); select.firstElementChild.value = ''; for (const iso of isos) { const path = typeof iso === 'string' ? iso : iso.path || iso.name; if (!path) continue; const option = makeElement('option', '', typeof iso === 'string' ? iso.split('/').pop() : iso.name || iso.path.split('/').pop()); option.value = path; select.append(option); } byId('create-submit').disabled = false; }
catch (error) { if (epoch === state.formEpoch && error.name !== 'AbortError') showError('create-error', error.message); }
}
let orderIdempotency = '';
function orderStorageKey() { return `${ORDER_STORAGE_PREFIX}${state.userId || 'unknown'}`; }
async function purchaseFingerprint(body) {
if (!globalThis.crypto?.subtle) throw new Error('当前浏览器不支持安全订单核对,请更新浏览器。');
const canonical = {}; for (const key of Object.keys(body).sort()) canonical[key] = body[key];
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(JSON.stringify(canonical)));
return Array.from(new Uint8Array(digest), value => value.toString(16).padStart(2, '0')).join('');
}
async function pendingOrderKey(fingerprint) {
if (state.orderIdempotency && state.orderFingerprint === fingerprint) return state.orderIdempotency;
let saved = null;
try { saved = JSON.parse(sessionStorage.getItem(orderStorageKey()) || 'null'); } catch {}
let key = saved && saved.fingerprint === fingerprint ? String(saved.key || '') : '';
if (!/^[A-Za-z0-9_.-]{16,128}$/.test(key)) key = crypto.randomUUID();
state.orderIdempotency = key;
state.orderFingerprint = fingerprint;
try { sessionStorage.setItem(orderStorageKey(), JSON.stringify({ key, fingerprint })); } catch {}
return key;
}
function clearPendingOrderKey() {
state.orderIdempotency = '';
state.orderFingerprint = '';
try { sessionStorage.removeItem(orderStorageKey()); } catch {}
}
async function existingPendingOrder(key) {
try { return await api(`/api/v1/orders/by-request/${encodeURIComponent(key)}`); }
catch (error) { if (error.status === 404) return null; throw error; }
}
async function buy(event) {
event.preventDefault(); if (state.mutationBusy) return;
const cloud = sourceMode() === 'cloud'; const password = byId('vm-root-password').value;
if (!strongPassword(password)) return showError('create-error', '密码需至少 12 个字符,包含大写、小写、数字和符号。');
if (cloud && !byId('vm-template').value) return showError('create-error', '请选择系统镜像版本。');
if (!byId('vm-plan').value || !state.resources) return showError('create-error', '请先选择可用套餐。');
if (!confirm('确认从账户余额一次性支付所选套餐价格并开通虚拟机?首版不包含自动续费。')) return;
const selectedPlan = state.plans.find(plan => String(plan.id) === byId('vm-plan').value);
const purchaseSummary = selectedPlan ? `每期 ¥${selectedPlan.price},${planBillingLabel(selectedPlan)}` : '所选套餐';
if (!confirm(`确认从账户余额支付并开通虚拟机?${purchaseSummary}。到期前可手动续费。`)) return;
state.mutationBusy = true; byId('create-submit').disabled = true; showError('create-error', '');
if (!orderIdempotency) orderIdempotency = crypto.randomUUID();
const body = { plan_id: Number(byId('vm-plan').value), name: byId('vm-name').value.trim(), source: sourceMode(), idempotency_key: orderIdempotency };
const body = { plan_id: Number(byId('vm-plan').value), name: byId('vm-name').value.trim(), source: sourceMode() };
if (cloud) { body.template_id = byId('vm-template').value; if (state.selectedKeys.create) body.guest_ssh_key_id = state.selectedKeys.create; if (password) body.root_password = password; } else body.iso_path = byId('vm-iso').value;
try { await api('/api/v1/orders', { method: 'POST', body }); orderIdempotency = ''; byId('vm-root-password').value = ''; toast('订单已提交,后台持续开通中。', 'success'); showView('orders'); await loadOrders(); }
catch (error) { showError('create-error', error.message); }
try {
const fingerprint = await purchaseFingerprint(body);
const orderIdempotency = await pendingOrderKey(fingerprint);
body.idempotency_key = orderIdempotency;
const recovered = await existingPendingOrder(orderIdempotency);
if (!recovered) await api('/api/v1/orders', { method: 'POST', body });
clearPendingOrderKey(); byId('vm-root-password').value = '';
toast(recovered ? '已找回之前提交的订单,没有重复扣款。' : '订单已提交,后台持续开通中。', 'success');
showView('orders'); await loadOrders();
}
catch (error) {
// A timeout or 409 can mean the first request committed but its response
// was lost. Reconcile by the same durable key before reporting failure.
try {
const orderIdempotency = state.orderIdempotency;
const recovered = orderIdempotency ? await existingPendingOrder(orderIdempotency) : null;
if (recovered) {
clearPendingOrderKey(); byId('vm-root-password').value = '';
toast('已核对到订单,没有重复扣款。', 'success'); showView('orders'); await loadOrders(); return;
}
} catch {}
showError('create-error', `${error.message} 可再次点击提交安全核对同一订单。`);
}
finally { delete body.root_password; state.mutationBusy = false; byId('create-submit').disabled = !state.resources; }
}
function formatOrderDate(value) { const date = new Date(typeof value === 'number' ? value * 1000 : value); return Number.isFinite(date.getTime()) ? date.toLocaleString('zh-CN', { hour12: false }) : '时间未记录'; }
@ -155,6 +413,7 @@ async function loadOrders() {
} catch (error) { showError('cloud-page-error', error.message); }
}
async function openReinstall(vm) {
if (!vm?.can_mutate) return toast('当前账户只有查看权限。', 'error');
showView('reinstall'); text('reinstall-subtitle', `${vm.name} · 保留原有公网网络参数,重新创建系统盘。`); text('reinstall-public-ip', vmIp(vm)); byId('reinstall-confirm-name').value = ''; byId('reinstall-root-password').value = ''; byId('reinstall-template').value = ''; showError('reinstall-error', ''); byId('reinstall-progress').hidden = true; byId('reinstall-result').hidden = true; byId('reinstall-fields').hidden = false;
try { const resources = await api(`/api/v1/resources?vm_id=${encodeURIComponent(vm.api_id)}`); state.templates = items(resources.templates || resources.cloud_templates); renderTemplateCatalog('reinstall-template'); await loadKeys(); } catch (error) { showError('reinstall-error', error.message); }
}
@ -174,7 +433,7 @@ async function reinstall(event) {
catch (error) { showError('reinstall-error', error.message); }
finally { delete body.root_password; state.mutationBusy = false; byId('reinstall-submit').disabled = false; byId('reinstall-progress').hidden = true; }
}
async function openRescue(vm) { showView('rescue'); text('rescue-subtitle', `${vm.name} · 挂载救援 ISO 并启动救援环境。`); byId('rescue-confirm-name').value = ''; byId('rescue-template-id').value = ''; showError('rescue-error', ''); try { const [resources, status] = await Promise.all([api(`/api/v1/resources?vm_id=${encodeURIComponent(vm.api_id)}`), api(`${vmPath(vm)}/rescue`)]); const container = byId('rescue-template-cards'); container.replaceChildren(); for (const template of items(resources.rescue_templates)) { const button = makeElement('button', 'ssh-key-option', template.name); button.type = 'button'; button.dataset.templateId = String(template.id); button.setAttribute('aria-pressed', 'false'); button.addEventListener('click', () => { byId('rescue-template-id').value = template.id; container.querySelectorAll('button').forEach(item => item.setAttribute('aria-pressed', String(item === button))); }); container.append(button); } if (!container.children.length) container.append(makeElement('p', 'muted', '暂无救援模板,请联系管理员。')); renderRescueStatus(status); } catch (error) { showError('rescue-error', error.message); } }
async function openRescue(vm) { if (!vm?.can_mutate) return toast('当前账户只有查看权限。', 'error'); showView('rescue'); text('rescue-subtitle', `${vm.name} · 挂载救援 ISO 并启动救援环境。`); byId('rescue-confirm-name').value = ''; byId('rescue-template-id').value = ''; showError('rescue-error', ''); try { const [resources, status] = await Promise.all([api(`/api/v1/resources?vm_id=${encodeURIComponent(vm.api_id)}`), api(`${vmPath(vm)}/rescue`)]); const container = byId('rescue-template-cards'); container.replaceChildren(); for (const template of items(resources.rescue_templates)) { const button = makeElement('button', 'ssh-key-option', template.name); button.type = 'button'; button.dataset.templateId = String(template.id); button.setAttribute('aria-pressed', 'false'); button.addEventListener('click', () => { byId('rescue-template-id').value = template.id; container.querySelectorAll('button').forEach(item => item.setAttribute('aria-pressed', String(item === button))); }); container.append(button); } if (!container.children.length) container.append(makeElement('p', 'muted', '暂无救援模板,请联系管理员。')); renderRescueStatus(status); } catch (error) { showError('rescue-error', error.message); } }
function renderRescueStatus(status) { state.rescueActive = status.active === true || status.rescue_mode === true; state.rescueStaged = status.staged === true || status.rescue_staged === true; text('rescue-status', status.message || (state.rescueActive ? '当前已进入救援模式。' : state.rescueStaged ? '救援镜像已预挂载,正常关机失败时可以强制冷启动进入救援。' : '尚未挂载救援镜像。')); byId('rescue-force-start').hidden = !state.rescueStaged; byId('rescue-exit').hidden = !state.rescueActive && !state.rescueStaged; }
async function rescueAction(action) {
const vm = selectedVm(); if (!vm || state.mutationBusy) return; if (byId('rescue-confirm-name').value !== vm.name) return showError('rescue-error', '请输入完整虚拟机名称确认。');
@ -187,6 +446,7 @@ async function rescueAction(action) {
}
function closeConsole() { state.consoleToken++; state.rfb?.disconnect(); state.rfb = null; byId('console-modal').hidden = true; byId('console-password-form').hidden = true; byId('console-password').value = ''; byId('console-cad').disabled = true; }
async function openConsole(vm) {
if (!vm?.can_mutate) return toast('当前账户只有查看权限。', 'error');
closeConsole(); const token = ++state.consoleToken; byId('console-modal').hidden = false; text('console-title', `${vm.name} · 控制台`); text('console-status', '正在申请短期控制台连接…'); const placeholder = byId('console-placeholder'); placeholder.hidden = false;
try { const [ticket, module] = await Promise.all([api(`${vmPath(vm)}/console-ticket`, { method: 'POST' }), import('/cloud/vendor/novnc/core/rfb.js')]); if (token !== state.consoleToken) return; const url = new URL(ticket.websocket_path, location.href); if (url.origin !== location.origin || url.pathname !== '/engine-console' || url.search || url.hash || typeof ticket.ticket !== 'string' || !/^[A-Za-z0-9_-]+$/.test(ticket.ticket)) throw new Error('控制台地址或票据无效。'); url.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'; const rfb = new module.default(byId('console-stage'), url.href, { shared: true, wsProtocols: ['cloud-ticket.' + ticket.ticket] }); state.rfb = rfb; rfb.scaleViewport = true; rfb.resizeSession = false; placeholder.hidden = true; rfb.addEventListener('connect', () => { if (state.rfb !== rfb) return; text('console-status', '已连接'); byId('console-cad').disabled = false; }); rfb.addEventListener('disconnect', event => { if (state.rfb !== rfb) return; text('console-status', event.detail?.clean ? '连接已关闭' : '连接已中断,请关闭后重试。'); byId('console-cad').disabled = true; }); rfb.addEventListener('credentialsrequired', () => { if (state.rfb !== rfb) return; byId('console-password-form').hidden = false; byId('console-password').focus(); text('console-status', '请输入 VNC 密码'); }); } catch (error) { if (token === state.consoleToken) { text('console-status', `连接失败:${error.message}`); placeholder.hidden = false; } }
}
@ -741,15 +1001,16 @@ document.querySelectorAll('[data-view]').forEach(button => button.addEventListen
for (const id of ['refresh-button', 'vm-detail-refresh-button']) byId(id).addEventListener('click', loadVms);
byId('create-button').addEventListener('click', openCreate); byId('create-back').addEventListener('click', () => showView('list')); byId('vm-detail-back-button').addEventListener('click', () => { state.selectedVmId = null; showView('list'); }); byId('ssh-keys-back-button').addEventListener('click', () => showView('list'));
for (const id of ['reinstall-back', 'reinstall-cancel', 'rescue-back']) byId(id).addEventListener('click', () => { if (!state.mutationBusy) { const vm = selectedVm(); if (vm) openVmDetail(vm.uuid); else showView('list'); } });
byId('search-input').addEventListener('input', () => renderVMs()); byId('state-filter').addEventListener('change', () => renderVMs()); byId('vm-plan').addEventListener('change', () => { orderIdempotency = ''; void loadPlanResources(); }); byId('create-form').querySelectorAll('[name="source"]').forEach(radio => radio.addEventListener('change', updateSource));
byId('search-input').addEventListener('input', () => renderVMs()); byId('state-filter').addEventListener('change', () => renderVMs()); byId('vm-plan').addEventListener('change', () => { void loadPlanResources(); }); byId('create-form').querySelectorAll('[name="source"]').forEach(radio => radio.addEventListener('change', updateSource));
byId('create-form').addEventListener('submit', buy); byId('reinstall-form').addEventListener('submit', reinstall); byId('rescue-form').addEventListener('submit', event => { event.preventDefault(); void rescueAction('load'); }); byId('rescue-force-start').addEventListener('click', () => rescueAction('force-start')); byId('rescue-exit').addEventListener('click', () => rescueAction('exit')); byId('orders-refresh').addEventListener('click', loadOrders);
for (const id of ['vm-open-ssh-keys', 'reinstall-open-ssh-keys']) byId(id).addEventListener('click', async () => { showView('keys'); try { await loadKeys(); } catch (error) { toast(error.message, 'error'); } });
byId('ssh-key-form').addEventListener('submit', async event => { event.preventDefault(); const button = byId('ssh-key-save-button'); button.disabled = true; showError('ssh-key-form-error', ''); try { await api('/api/v1/ssh-keys', { method: 'POST', body: { name: byId('ssh-key-name').value.trim(), public_key: byId('ssh-key-public-key').value.trim() } }); byId('ssh-key-form').reset(); await loadKeys(); toast('公钥已保存。', 'success'); } catch (error) { showError('ssh-key-form-error', error.message); } finally { button.disabled = false; } });
byId('ssh-key-file').addEventListener('change', async event => { const file = event.target.files?.[0]; if (!file) return; if (file.size > 16384) return showError('ssh-key-form-error', '公钥文件超过 16 KiB 限制。'); const content = await file.text(); if (/PRIVATE KEY/.test(content)) return showError('ssh-key-form-error', '不能导入私钥,只能导入 .pub 公钥。'); byId('ssh-key-public-key').value = content.trim(); });
byId('vm-detail-ip-copy').addEventListener('click', async () => { try { await copyText(vmIp(selectedVm())); toast('公网 IP 已复制。'); } catch (error) { toast(error.message, 'error'); } }); byId('vm-detail-password-reveal').addEventListener('click', () => credential()); byId('vm-detail-password-copy').addEventListener('click', () => credential(true));
byId('vm-detail-renew-button').addEventListener('click', renewVm);
byId('vm-metrics-range').addEventListener('change', () => { state.metricsRange = byId('vm-metrics-range').value; state.metricsRequest?.controller.abort(); state.metricsRequest = null; void loadVmMetrics(); }); window.addEventListener('resize', () => { clearTimeout(state.metricsResizeTimer); state.metricsResizeTimer = setTimeout(() => { if (state.metricsPayload && state.view === 'detail') renderVmMetrics(state.metricsPayload); }, 150); });
byId('console-modal').querySelector('[data-close-modal]').addEventListener('click', closeConsole); byId('console-cad').addEventListener('click', () => state.rfb?.sendCtrlAltDel()); byId('console-fullscreen').addEventListener('click', () => { byId('console-stage').requestFullscreen?.().catch(error => toast(error.message, 'error')); }); byId('console-password-form').addEventListener('submit', event => { event.preventDefault(); state.rfb?.sendCredentials({ password: byId('console-password').value }); byId('console-password').value = ''; byId('console-password-form').hidden = true; }); window.addEventListener('keydown', event => { if (event.key === 'Escape') closeConsole(); });
document.addEventListener('visibilitychange', () => { if (document.hidden) clearCredential(); else { void loadVms(); if (state.view === 'detail') startVmMetrics(); } }); window.addEventListener('pagehide', () => { clearCredential(); closeConsole(); state.resourceController?.abort(); }); byId('logout-button').addEventListener('click', () => { clearCredential(); closeConsole(); location.assign('/user/logout'); });
async function initialize() { const initialHash = location.hash; try { const session = await api('/api/v1/session'); if (session.authenticated === false) { location.assign('/auth/login'); return; } state.csrfToken = session.csrf_token || ''; if (!state.csrfToken) throw new Error('会话 CSRF 令牌缺失,请重新登录。'); state.username = session.username || session.user_name || session.email || session.user?.email || '用户'; state.admin = session.admin === true || session.is_admin === true || Number(session.is_admin) === 1; text('current-username', state.username); for (const id of ['cloud-admin-link', 'cloud-admin-menu-link']) { const link = byId(id); if (link) link.hidden = !state.admin; } showView('list'); updateSource(); await loadVms(); await routeHash(initialHash); setInterval(() => { if (!document.hidden) { void loadVms(); if (state.view === 'orders') void loadOrders(); } }, 15000); } catch (error) { showError('cloud-page-error', error.message); } }
async function initialize() { const initialHash = location.hash; try { const session = await api('/api/v1/session'); if (session.authenticated === false) { location.assign('/auth/login'); return; } state.csrfToken = session.csrf_token || ''; if (!state.csrfToken) throw new Error('会话 CSRF 令牌缺失,请重新登录。'); state.userId = String(session.user_id || ''); if (!/^\d+$/.test(state.userId)) throw new Error('会话用户标识缺失,请重新登录。'); state.username = session.username || session.user_name || session.email || session.user?.email || '用户'; state.admin = session.admin === true || session.is_admin === true || Number(session.is_admin) === 1; text('current-username', state.username); for (const id of ['cloud-admin-link', 'cloud-admin-menu-link']) { const link = byId(id); if (link) link.hidden = !state.admin; } showView('list'); updateSource(); await loadVms(); await routeHash(initialHash); setInterval(() => { if (!document.hidden) { void loadVms(); if (state.view === 'orders') void loadOrders(); } }, 15000); } catch (error) { showError('cloud-page-error', error.message); } }
window.addEventListener('hashchange', () => { if (state.csrfToken) void routeHash(); });
void initialize();

@ -49,6 +49,23 @@
.cloud-plan-summary { display: flex; flex-wrap: wrap; gap: 12px; }
.cloud-page-progress { margin: 16px 0; }
.vm-detail-secret { max-width: 100%; overflow-wrap: anywhere; }
.vm-service-panel { margin-top: 18px; }
.vm-service-panel .panel-heading { align-items: center; }
.vm-service-panel .panel-heading > div { min-width: 0; }
.vm-renew-button { flex: none; }
.vm-service-facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(155px, 1fr)); gap: 0; margin: 0; padding: 5px 24px 13px; }
.vm-service-facts > div { min-width: 0; padding: 15px 12px 15px 0; border-bottom: 1px solid #edf1f5; }
.vm-service-facts dt { margin: 0 0 8px; color: #91a0af; font-size: 11px; font-weight: 700; }
.vm-service-facts dd { margin: 0; color: #273c52; font-size: 13px; font-weight: 700; line-height: 1.5; overflow-wrap: anywhere; }
.service-pill { display: inline-flex; align-items: center; gap: 7px; padding: 6px 9px; border-radius: 6px; background: #f1f3f6; color: #758499; font-size: 11px; font-weight: 800; white-space: nowrap; }
.service-pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.service-pill.active { background: #e7f8ef; color: #20966a; }
.service-pill.warning { background: #fff4e5; color: #a87120; }
.service-pill.stopped { background: #fff0f1; color: #bd4654; }
.service-pill.unknown { background: #f1f3f6; color: #758499; }
.vm-traffic-progress { height: 8px; margin: 0 24px 22px; overflow: hidden; border-radius: 999px; background: #e7eef2; }
.vm-traffic-progress > span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #1bb6b0, #0f9eaa); transition: width .25s ease; }
.vm-traffic-progress.is-exhausted > span { background: #d94350; }
.ssh-key-choice-label { align-self: center; }
#vm-plan-summary { display: block; padding: 16px; }
#cloud-page-error { margin-bottom: 20px; }
@ -73,6 +90,8 @@
@media (max-width: 740px) {
.cloud-order { grid-template-columns: 1fr; gap: 13px; }
.cloud-order-actions { justify-content: flex-start; }
.vm-service-panel .panel-heading { align-items: flex-start; }
.vm-service-panel .vm-renew-button { width: 100%; }
}
@media (max-width: 520px) {
@ -84,4 +103,7 @@
.cloud-order { padding: 19px 17px; }
.cloud-order-actions { width: 100%; }
.cloud-order-actions .row-button { flex: 1 1 145px; }
.vm-service-facts { grid-template-columns: 1fr 1fr; padding: 4px 17px 12px; }
.vm-service-facts > div { padding: 12px 8px 12px 0; }
.vm-traffic-progress { margin: 0 17px 18px; }
}

@ -1,2 +1,2 @@
{assign var=admin_title value='开通订单'}{assign var=admin_active value='cloud_orders'}{include file='admin/static_header.tpl'}
<link rel="stylesheet" href="/cloud/admin.css"><section class="cloud-admin" data-cloud-admin="orders"><h4>余额开通订单</h4><p class="text-muted">首版采用余额一次性购买,不包含自动到期续费。失败或不确定的开通任务需核查实际宿主机状态,不要重复创建或扣款。</p><p id="cloud-admin-status" class="alert alert-info cloud-admin-notice" role="status">正在加载…</p><button id="admin-records-refresh" class="btn btn-outline-primary mb-3" type="button">刷新</button><div class="card"><div class="card-body table-responsive"><table class="table"><thead><tr><th>订单</th><th>用户</th><th>套餐</th><th>金额</th><th>状态</th><th>实例 / 任务</th><th>时间 / 错误</th></tr></thead><tbody id="admin-records"></tbody></table></div></div></section><script type="module" src="/cloud/admin.js?v={$venix_asset_version}"></script>{include file='admin/static_footer.tpl'}
<link rel="stylesheet" href="/cloud/admin.css"><section class="cloud-admin" data-cloud-admin="orders"><h4>余额开通订单</h4><p class="text-muted">开通套餐按设置的账期生效,用户可在实例详情手动续费,不自动扣款。失败或不确定的开通任务需核查实际宿主机状态,不要重复创建或扣款。</p><p id="cloud-admin-status" class="alert alert-info cloud-admin-notice" role="status">正在加载…</p><button id="admin-records-refresh" class="btn btn-outline-primary mb-3" type="button">刷新</button><div class="card"><div class="card-body table-responsive"><table class="table"><thead><tr><th>订单</th><th>用户</th><th>套餐</th><th>金额</th><th>状态</th><th>实例 / 任务</th><th>时间 / 错误</th></tr></thead><tbody id="admin-records"></tbody></table></div></div></section><script type="module" src="/cloud/admin.js?v={$venix_asset_version}"></script>{include file='admin/static_footer.tpl'}

@ -1,5 +1,5 @@
{assign var=admin_title value='新增商品'}{assign var=admin_active value='shop'}{include file='admin/static_header.tpl'}
<div class="py-3"><h4>新增云服务器套餐</h4><p class="text-muted">绑定宿主机后,用户可用余额购买开通。价格为本次开通价格,本版不含到期与续费。</p><a class="btn btn-light" href="/admin/shop">返回商品列表</a></div>
<div class="py-3"><h4>新增云服务器套餐</h4><p class="text-muted">绑定宿主机并设置账期、每期流量额度后,用户可用余额购买开通及手动续费。</p><a class="btn btn-light" href="/admin/shop">返回商品列表</a></div>
<form class="card" id="shopForm" data-method="POST" data-url="/admin/shop"><div class="card-body"><div class="row g-3"><div class="col-12"><label class="form-label" for="name">商品名称</label><input class="form-control" id="name" name="name" type="text" maxlength="128" value="" required></div>
<div class="col-md-6"><label class="form-label" for="cpu_cores">CPU 核数</label><input class="form-control" id="cpu_cores" name="cpu_cores" type="number" min="1" max="64" step="1" value="2" required></div>
<div class="col-md-6"><label class="form-label" for="host_id">开通宿主机</label><select class="form-select" id="host_id" name="host_id" data-selected=""><option value="">未绑定(禁止购买)</option></select></div>
@ -7,6 +7,8 @@
<div class="col-md-6"><label class="form-label" for="bandwidth_mbps">带宽(Mbps)</label><input class="form-control" id="bandwidth_mbps" name="bandwidth_mbps" type="number" min="1" max="100000" step="1" value="100" required></div>
<div class="col-md-6"><label class="form-label" for="disk_gib">硬盘(GiB)</label><input class="form-control" id="disk_gib" name="disk_gib" type="number" min="1" max="4096" step="1" value="20" required></div>
<div class="col-md-6"><label class="form-label" for="price">价格</label><input class="form-control" id="price" name="price" type="number" min="0" max="100000000" step="0.01" value="0" required></div>
<div class="col-md-6"><label class="form-label" for="validity_days">有效期(天)</label><input class="form-control" id="validity_days" name="validity_days" type="number" min="1" max="3650" step="1" value="30" required><div class="form-text">购买或续费一次增加的服务天数。</div></div>
<div class="col-md-6"><label class="form-label" for="traffic_gib">每期流量额度(GiB)</label><input class="form-control" id="traffic_gib" name="traffic_gib" type="number" min="0" max="1048576" step="1" value="1024" required><div class="form-text">每个账期独立计算下载与上传合计额度;填 0 表示不限流量。</div></div>
<div class="col-md-6"><label class="form-label" for="stock">库存</label><input class="form-control" id="stock" name="stock" type="number" min="-1" max="2147483647" step="1" value="100" required></div>
<div class="col-md-6"><label class="form-label" for="sort">排序</label><input class="form-control" id="sort" name="sort" type="number" min="-1000000" max="1000000" step="1" value="0" required></div>
<div class="col-md-6"><label class="form-label" for="status">状态</label><select class="form-select" id="status" name="status"><option value="1" selected>上架</option><option value="0" >下架</option></select></div></div><p id="shopFormStatus" class="admin-config-status">待保存</p><button class="btn btn-primary" id="shopFormSubmit" type="submit" {if !$admin_can_manage|default:false}disabled{/if}>保存商品</button></div></form><div class="admin-toast" id="shopFormToast"></div>

@ -7,6 +7,8 @@
<div class="col-md-6"><label class="form-label" for="bandwidth_mbps">带宽(Mbps)</label><input class="form-control" id="bandwidth_mbps" name="bandwidth_mbps" type="number" min="1" max="100000" step="1" value="{$shop->bandwidth_mbps|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="disk_gib">硬盘(GiB)</label><input class="form-control" id="disk_gib" name="disk_gib" type="number" min="1" max="4096" step="1" value="{$shop->disk_gib|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="price">价格</label><input class="form-control" id="price" name="price" type="number" min="0" max="100000000" step="0.01" value="{$shop->price|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="validity_days">有效期(天)</label><input class="form-control" id="validity_days" name="validity_days" type="number" min="1" max="3650" step="1" value="{$shop->validity_days|default:30|escape:'html'}" required><div class="form-text">购买或续费一次增加的服务天数;修改后不追溯已有服务周期。</div></div>
<div class="col-md-6"><label class="form-label" for="traffic_gib">每期流量额度(GiB)</label><input class="form-control" id="traffic_gib" name="traffic_gib" type="number" min="0" max="1048576" step="1" value="0" data-quota-bytes="{$shop->traffic_quota_bytes|default:0|escape:'html'}" required><div class="form-text">每个账期独立计算下载与上传合计额度;填 0 表示不限流量,修改后不追溯已有实例。</div></div>
<div class="col-md-6"><label class="form-label" for="stock">库存</label><input class="form-control" id="stock" name="stock" type="number" min="-1" max="2147483647" step="1" value="{$shop->stock|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="sort">排序</label><input class="form-control" id="sort" name="sort" type="number" min="-1000000" max="1000000" step="1" value="{$shop->sort|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="status">状态</label><select class="form-select" id="status" name="status"><option value="1" {if $shop->status == 1}selected{/if}>上架</option><option value="0" {if $shop->status == 0}selected{/if}>下架</option></select></div></div><p id="shopFormStatus" class="admin-config-status">待保存</p><button class="btn btn-primary" id="shopFormSubmit" type="submit" {if !$admin_can_manage|default:false}disabled{/if}>保存商品</button></div></form><div class="admin-toast" id="shopFormToast"></div>

@ -5,8 +5,15 @@
var submit = document.getElementById('shopFormSubmit');
var status = document.getElementById('shopFormStatus');
var toast = document.getElementById('shopFormToast');
var fields = ['name','host_id','price','cpu_cores','memory_mib','bandwidth_mbps','disk_gib','status','stock','sort'];
var fields = ['name','host_id','price','validity_days','traffic_gib','cpu_cores','memory_mib','bandwidth_mbps','disk_gib','status','stock','sort'];
var hostSelect = document.getElementById('host_id');
var trafficInput = document.getElementById('traffic_gib');
if (trafficInput && trafficInput.hasAttribute('data-quota-bytes')) {
var quotaBytes = Number(trafficInput.getAttribute('data-quota-bytes'));
if (Number.isFinite(quotaBytes) && quotaBytes >= 0) {
trafficInput.value = String(Math.floor(quotaBytes / 1073741824));
}
}
if (hostSelect) {
fetch('/api/v1/admin/hosts', {credentials: 'same-origin'})
.then(function (response) { if (!response.ok) throw new Error('宿主机列表读取失败'); return response.json(); })

@ -1,7 +1,7 @@
{assign var=admin_title value='商品管理'}
{assign var=admin_active value='shop'}
{assign var=page_title value='商品列表'}
{assign var=page_desc value='管理云服务器套餐资源规格、价格、库存和上下架状态。'}
{assign var=page_desc value='管理云服务器套餐资源规格、账期、每期流量额度、价格、库存和上下架状态。'}
{assign var=page_icon value='mdi-storefront-outline'}
{assign var=admin_table_storage_key value='venix-admin-shop-columns'}
{assign var=admin_table_ajax_url value='/admin/shop/ajax'}
@ -13,6 +13,6 @@
{assign var=admin_table_create_url value='/admin/shop/create'}
{assign var=admin_table_create_text value='新增商品'}
{assign var=admin_table_create_icon value='mdi-plus-circle-outline'}
{assign var=admin_table_columns_json value='[{"key":"op","label":"操作","width":"170px","sortable":false,"action":"shopActions"},{"key":"id","label":"ID","width":"120px"},{"key":"name","label":"商品名称","width":"120px"},{"key":"cpu_cores","label":"CPU","width":"120px"},{"key":"memory_mib","label":"内存 MiB","width":"120px"},{"key":"bandwidth_mbps","label":"带宽 Mbps","width":"120px"},{"key":"disk_gib","label":"硬盘 GiB","width":"120px"},{"key":"price","label":"价格","width":"120px"},{"key":"status","label":"状态","width":"120px"},{"key":"stock","label":"库存","width":"120px"},{"key":"sort","label":"排序","width":"120px"}]'}
{assign var=admin_table_default_columns_json value='["op","id","name","cpu_cores","memory_mib","bandwidth_mbps","disk_gib","price","status","stock","sort"]'}
{assign var=admin_table_columns_json value='[{"key":"op","label":"操作","width":"170px","sortable":false,"action":"shopActions"},{"key":"id","label":"ID","width":"120px"},{"key":"name","label":"商品名称","width":"120px"},{"key":"cpu_cores","label":"CPU","width":"120px"},{"key":"memory_mib","label":"内存 MiB","width":"120px"},{"key":"bandwidth_mbps","label":"带宽 Mbps","width":"120px"},{"key":"disk_gib","label":"硬盘 GiB","width":"120px"},{"key":"validity_days","label":"每期天数","width":"130px"},{"key":"traffic_gib","label":"每期流量 GiB(0=不限)","width":"190px"},{"key":"price","label":"每期价格","width":"120px"},{"key":"status","label":"状态","width":"120px"},{"key":"stock","label":"库存","width":"120px"},{"key":"sort","label":"排序","width":"120px"}]'}
{assign var=admin_table_default_columns_json value='["op","id","name","cpu_cores","memory_mib","bandwidth_mbps","disk_gib","validity_days","traffic_gib","price","status","stock","sort"]'}
{include file='admin/components/table_page.tpl'}

@ -38,6 +38,23 @@
<div class="vm-detail-operations"><section class="inventory-panel vm-detail-panel" aria-labelledby="vm-detail-actions-title"><div class="panel-heading"><div><h2 id="vm-detail-actions-title">日常操作</h2><p>在这里管理这台虚拟机的电源和控制台。</p></div></div><div id="vm-detail-primary-actions" class="vm-detail-actions"></div></section>
<section class="inventory-panel vm-detail-panel vm-danger-panel" aria-labelledby="vm-detail-danger-title"><div class="panel-heading"><div><h2 id="vm-detail-danger-title">维护与危险操作</h2><p>救援会先预挂载 ISO,再尝试正常关机并开机;强制断电可能丢失未保存的数据,重装或删除会清除数据。</p></div></div><div id="vm-detail-danger-actions" class="vm-detail-actions"></div></section></div>
</div>
<section class="inventory-panel vm-service-panel" aria-labelledby="vm-service-title">
<div class="panel-heading"><div><h2 id="vm-service-title">服务周期与流量</h2><p id="vm-detail-service-note">正在读取套餐服务信息…</p></div><button id="vm-detail-renew-button" class="button button-primary vm-renew-button" type="button" hidden>立即续费</button></div>
<dl class="vm-service-facts">
<div><dt>服务状态</dt><dd><span id="vm-detail-service-status" class="service-pill unknown">暂未提供</span></dd></div>
<div><dt>网络状态</dt><dd><span id="vm-detail-network-status" class="service-pill unknown">暂未提供</span></dd></div>
<div><dt>当前账期开始</dt><dd id="vm-detail-period-start">暂未提供</dd></div>
<div><dt>当前账期结束</dt><dd id="vm-detail-period-end">暂未提供</dd></div>
<div><dt>最终服务到期</dt><dd id="vm-detail-expires-at">暂未提供</dd></div>
<div><dt>已预付未来账期</dt><dd id="vm-detail-prepaid-periods">暂未提供</dd></div>
<div><dt>本期下载(RX)</dt><dd id="vm-detail-traffic-rx">暂未提供</dd></div>
<div><dt>本期上传(TX)</dt><dd id="vm-detail-traffic-tx">暂未提供</dd></div>
<div><dt>本期合计已用</dt><dd id="vm-detail-traffic-used">暂未提供</dd></div>
<div><dt>本期流量额度</dt><dd id="vm-detail-traffic-quota">暂未提供</dd></div>
<div><dt>本期剩余流量</dt><dd id="vm-detail-traffic-remaining">暂未提供</dd></div>
</dl>
<div id="vm-detail-traffic-progress" class="vm-traffic-progress" role="progressbar" aria-label="套餐流量使用率" aria-valuemin="0" aria-valuemax="100" hidden><span id="vm-detail-traffic-progress-bar"></span></div>
</section>
<section class="vm-metrics" aria-labelledby="vm-metrics-title">
<div class="vm-metrics-heading"><div><span class="eyebrow">PERFORMANCE HISTORY</span><h2 id="vm-metrics-title">性能监控</h2><p>每分钟采样并自动刷新,历史数据最长保留 30 天;长时间范围会聚合,较长的采样缺口留空。</p></div><label class="vm-metrics-range-control" for="vm-metrics-range">时间范围 <select id="vm-metrics-range"><option value="1h">最近 1 小时</option><option value="24h" selected>最近 24 小时</option><option value="7d">最近 7 天</option><option value="30d">最近 30 天</option></select></label></div>
<p id="vm-metrics-status" class="vm-metrics-status" role="status">选择虚拟机后加载监控数据。</p>
@ -58,7 +75,7 @@
<fieldset class="create-source-picker"><legend>系统来源</legend><label><input type="radio" name="source" value="iso" /><span><strong>ISO 安装</strong><small>通过 VNC 安装系统</small></span></label><label><input type="radio" name="source" value="cloud" checked /><span><strong>公网云镜像</strong><small>自动配置公网 IP 后直接开机</small></span></label></fieldset>
<div class="form-grid">
<div class="field full"><label for="vm-name">名称 <span>*</span></label><input id="vm-name" name="name" type="text" required minlength="1" maxlength="63" pattern="[a-zA-Z0-9][a-zA-Z0-9._-]*" autocomplete="off" placeholder="例如 web-prod-01" /><small>使用英文字母、数字、点、下划线或连字符。</small></div>
<div class="field full"><label for="vm-plan">云服务器套餐 <span>*</span></label><select id="vm-plan" required><option value="">请选择套餐</option></select><div id="vm-plan-summary" class="form-tip" aria-live="polite">选择套餐后显示资源规格和一次性价格。</div><small>CPU、内存、硬盘、带宽、宿主机及公网地址由所选套餐确定,不能自行修改。</small></div>
<div class="field full"><label for="vm-plan">云服务器套餐 <span>*</span></label><select id="vm-plan" required><option value="">请选择套餐</option></select><div id="vm-plan-summary" class="form-tip" aria-live="polite">选择套餐后显示资源规格、每期价格、账期和每期流量额度。</div><small>CPU、内存、硬盘、带宽、账期、每期流量额度、宿主机及公网地址由所选套餐确定,不能自行修改。</small></div>
<div class="field full" id="vm-iso-field"><label for="vm-iso">安装 ISO <span>*</span></label><select id="vm-iso" name="iso" required><option value="">正在加载镜像…</option></select><small>镜像来自宿主机允许使用的 ISO 目录。</small></div>
<div class="field full" id="vm-template-field" hidden><span id="vm-template-label" class="template-field-label">系统镜像 <span>*</span></span><input id="vm-template" name="template_id" type="hidden" value="" disabled /><div id="vm-template-catalog" class="template-catalog" role="group" aria-labelledby="vm-template-label" aria-describedby="vm-template-hint" aria-required="true"></div><small id="vm-template-hint">点击系统图标下方的箭头展开版本,再选择已导入的镜像;模板需支持 cloud-init。</small></div>
<div class="cloud-fields full" id="vm-cloud-fields" hidden>
@ -83,7 +100,7 @@
<div class="form-grid">
<div class="field full"><span id="reinstall-template-label" class="template-field-label">重装镜像 <span>*</span></span><input id="reinstall-template" name="template_id" type="hidden" value="" /><div id="reinstall-template-catalog" class="template-catalog" role="group" aria-labelledby="reinstall-template-label" aria-describedby="reinstall-template-hint" aria-required="true"></div><small id="reinstall-template-hint">点击系统图标下方的箭头展开版本,选择当前宿主机已导入的云镜像。</small></div>
<div class="field full"><span class="ssh-key-picker-title">SSH 公钥(可选)</span><div id="reinstall-ssh-key-cards" class="ssh-key-picker" role="group" aria-label="重装系统时选择 SSH 公钥"></div><small>直接选择“公钥管理”中保存的公钥;再次点击已选公钥可取消。填写密码时可与公钥同时使用。</small><button id="reinstall-open-ssh-keys" class="ssh-key-picker-manage" type="button">管理我的公钥</button></div>
<div class="field full" id="reinstall-root-password-field"><label for="reinstall-root-password">初始 root 密码(可选)</label><input id="reinstall-root-password" name="root_password" type="password" maxlength="256" autocomplete="new-password" spellcheck="false" aria-describedby="reinstall-root-password-hint" placeholder="留空时自动生成强密码" /><small id="reinstall-root-password-hint" aria-live="polite">留空时自动生成强密码,即使选择了公钥;自行设置需至少 12 个字符,包含大写字母、小写字母和符号。</small></div>
<div class="field full" id="reinstall-root-password-field"><label for="reinstall-root-password">初始 root 密码(可选)</label><input id="reinstall-root-password" name="root_password" type="password" maxlength="256" autocomplete="new-password" spellcheck="false" aria-describedby="reinstall-root-password-hint" placeholder="留空时自动生成强密码" /><small id="reinstall-root-password-hint" aria-live="polite">留空时自动生成强密码,即使选择了公钥;自行设置需至少 12 个字符,包含大写字母、小写字母、数字和符号。</small></div>
<div class="field full"><label for="reinstall-confirm-name">输入虚拟机名称以确认删除旧系统盘 <span>*</span></label><input id="reinstall-confirm-name" name="confirmation_name" type="text" required autocomplete="off" spellcheck="false" placeholder="请输入完整的虚拟机名称" /><small id="reinstall-confirm-hint"></small></div>
</div>
</div>

@ -14,15 +14,32 @@ CREATE TABLE IF NOT EXISTS cloud_instances (
host_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
order_id BIGINT UNSIGNED DEFAULT NULL,
plan_id BIGINT UNSIGNED DEFAULT NULL,
current_period_id BIGINT UNSIGNED DEFAULT NULL,
name VARCHAR(63) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'active',
service_status VARCHAR(32) NOT NULL DEFAULT 'exempt',
network_status VARCHAR(32) NOT NULL DEFAULT 'active',
expires_at BIGINT UNSIGNED DEFAULT NULL,
renewal_price DECIMAL(12,2) DEFAULT NULL,
renewal_days INT UNSIGNED NOT NULL DEFAULT 0,
renewal_traffic_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
traffic_quota_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
traffic_download_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
traffic_upload_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
lifetime_download_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
lifetime_upload_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
traffic_synced_at BIGINT UNSIGNED NOT NULL DEFAULT 0,
suspended_at BIGINT UNSIGNED DEFAULT NULL,
network_suspended_at BIGINT UNSIGNED DEFAULT NULL,
spec_json LONGTEXT NOT NULL,
credential_cipher LONGTEXT DEFAULT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE KEY uniq_cloud_host_vm (host_id, uuid),
UNIQUE KEY uniq_cloud_instance_order (order_id),
KEY cloud_instance_owner (user_id, status)
KEY cloud_instance_owner (user_id, status),
KEY cloud_instance_lifecycle (service_status, expires_at, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_orders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
@ -41,6 +58,46 @@ CREATE TABLE IF NOT EXISTS cloud_orders (
UNIQUE KEY uniq_cloud_order_request (user_id, idempotency_key),
KEY cloud_order_owner (user_id, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_service_periods (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
instance_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
plan_id BIGINT UNSIGNED DEFAULT NULL,
order_id BIGINT UNSIGNED DEFAULT NULL,
renewal_id BIGINT UNSIGNED DEFAULT NULL,
starts_at BIGINT UNSIGNED NOT NULL,
ends_at BIGINT UNSIGNED NOT NULL,
traffic_quota_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
download_base_bytes BIGINT UNSIGNED DEFAULT NULL,
upload_base_bytes BIGINT UNSIGNED DEFAULT NULL,
download_used_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
upload_used_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
status VARCHAR(16) NOT NULL DEFAULT 'scheduled',
activated_at BIGINT UNSIGNED DEFAULT NULL,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY uniq_cloud_period_order (order_id),
UNIQUE KEY uniq_cloud_period_renewal (renewal_id),
KEY cloud_period_timeline (instance_id, starts_at, ends_at),
KEY cloud_period_activation (status, starts_at, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_renewals (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
instance_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
plan_id BIGINT UNSIGNED DEFAULT NULL,
period_id BIGINT UNSIGNED DEFAULT NULL,
idempotency_key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
amount DECIMAL(12,2) NOT NULL,
validity_days INT UNSIGNED NOT NULL,
traffic_bytes_added BIGINT UNSIGNED NOT NULL DEFAULT 0,
previous_expires_at BIGINT UNSIGNED NOT NULL,
new_expires_at BIGINT UNSIGNED NOT NULL,
created_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY uniq_cloud_renewal_request (user_id, idempotency_key),
KEY cloud_renewal_instance (instance_id, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_jobs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
operation_id CHAR(32) NOT NULL,
@ -63,6 +120,7 @@ CREATE TABLE IF NOT EXISTS cloud_jobs (
UNIQUE KEY uniq_cloud_operation (operation_id),
UNIQUE KEY uniq_cloud_provision_order (order_id),
KEY cloud_job_poll (status, available_at, lease_until),
KEY cloud_job_instance_state (instance_id, status, id),
KEY cloud_job_owner (user_id, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_ssh_keys (

@ -1,8 +1,10 @@
-- Same schema as engine database._METRIC_SCHEMA, not a separate PHP metric model.
-- Same DDL as the engine-owned metric/traffic schemas and migrations.
-- Engine owns collection, retention and writes; PHP initializes the empty log database.
CREATE TABLE IF NOT EXISTS engine_vm_metric_samples (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
user_id BIGINT UNSIGNED NULL,
sampled_at BIGINT UNSIGNED NOT NULL,
cpu_percent DOUBLE NULL,
memory_used_mib DOUBLE NULL,
@ -11,6 +13,52 @@ CREATE TABLE IF NOT EXISTS engine_vm_metric_samples (
disk_write_bps DOUBLE NULL,
network_rx_bps DOUBLE NULL,
network_tx_bps DOUBLE NULL,
network_rx_bytes BIGINT UNSIGNED NULL,
network_tx_bytes BIGINT UNSIGNED NULL,
PRIMARY KEY (host_scope, vm_uuid, sampled_at),
KEY vm_metric_retention (sampled_at)
KEY vm_metric_retention (sampled_at),
KEY vm_metric_owner (user_id, host_id, sampled_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS engine_vm_traffic_totals (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
user_id BIGINT UNSIGNED NULL,
last_observed_at BIGINT UNSIGNED NULL,
network_signature CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
network_baseline_json JSON NOT NULL,
download_rx_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
upload_tx_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
updated_at BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (host_scope, vm_uuid),
KEY vm_traffic_owner (user_id, host_id),
KEY vm_traffic_host (host_id, vm_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Compact, durable billing ledger. Chart samples below are pruned after 30
-- days; the first cumulative counter observed in every hour is not.
CREATE TABLE IF NOT EXISTS engine_vm_traffic_checkpoints (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
checkpoint_hour BIGINT UNSIGNED NOT NULL,
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
user_id BIGINT UNSIGNED NULL,
first_observed_at BIGINT UNSIGNED NOT NULL,
download_rx_bytes BIGINT UNSIGNED NOT NULL,
upload_tx_bytes BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (host_scope, vm_uuid, checkpoint_hour),
KEY vm_traffic_checkpoint_owner (user_id, host_id, first_observed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Idempotent MariaDB migration for installations created before byte accounting.
ALTER TABLE engine_vm_metric_samples ADD COLUMN IF NOT EXISTS
host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL AFTER vm_uuid;
ALTER TABLE engine_vm_metric_samples ADD COLUMN IF NOT EXISTS
user_id BIGINT UNSIGNED NULL AFTER host_id;
ALTER TABLE engine_vm_metric_samples ADD COLUMN IF NOT EXISTS
network_rx_bytes BIGINT UNSIGNED NULL AFTER network_tx_bps;
ALTER TABLE engine_vm_metric_samples ADD COLUMN IF NOT EXISTS
network_tx_bytes BIGINT UNSIGNED NULL AFTER network_rx_bytes;
ALTER TABLE engine_vm_metric_samples ADD INDEX IF NOT EXISTS
vm_metric_owner (user_id, host_id, sampled_at);

@ -230,6 +230,8 @@ CREATE TABLE `shop` (
`memory_mib` int(11) NOT NULL,
`bandwidth_mbps` int(11) NOT NULL,
`disk_gib` int(11) NOT NULL,
`validity_days` int(10) unsigned NOT NULL DEFAULT 30,
`traffic_quota_bytes` bigint(20) unsigned NOT NULL DEFAULT 0,
`status` tinyint(1) NOT NULL DEFAULT 1,
`stock` int(11) NOT NULL DEFAULT -1,
`sort` int(11) NOT NULL DEFAULT 0

@ -0,0 +1,781 @@
<?php
// Billing lifecycle tests only. The in-memory keyring is not encryption and
// is never loaded by production.
namespace App\Services {
final class Config
{
public static array $values = [];
public static function get($name, $default = null)
{
return self::$values[$name] ?? $default;
}
}
}
namespace App\Services\Cloud {
final class CloudSecrets
{
private static array $memory = [];
public static function key(string $purpose): string
{
return str_repeat('k', 32);
}
public static function encrypt(array $payload): string
{
$id = bin2hex(random_bytes(16));
self::$memory[$id] = $payload;
return 'test-only:' . $id;
}
public static function decrypt(string $cipher): array
{
return self::$memory[substr($cipher, 10)];
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudJob;
use App\Models\CloudRenewal;
use App\Models\CloudServicePeriod;
use App\Models\User;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudLifecycleService;
use App\Services\Cloud\CloudRenewalService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'log');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('money');
$table->integer('enable')->default(1);
$table->integer('restrict_balance_purchase')->default(0);
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('user_id');
$table->integer('order_id')->nullable();
$table->integer('plan_id')->nullable();
$table->integer('current_period_id')->nullable();
$table->string('uuid');
$table->string('name');
$table->string('status');
$table->string('service_status');
$table->string('network_status');
$table->integer('expires_at')->nullable();
$table->string('renewal_price')->nullable();
$table->integer('renewal_days')->default(0);
$table->integer('renewal_traffic_bytes')->default(0);
$table->integer('traffic_quota_bytes')->default(0);
$table->integer('traffic_download_bytes')->default(0);
$table->integer('traffic_upload_bytes')->default(0);
$table->integer('lifetime_download_bytes')->default(0);
$table->integer('lifetime_upload_bytes')->default(0);
$table->integer('traffic_synced_at')->default(0);
$table->integer('suspended_at')->nullable();
$table->integer('network_suspended_at')->nullable();
$table->text('spec_json');
$table->text('credential_cipher')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique(['host_id', 'uuid']);
});
$schema->create('cloud_service_periods', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('plan_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('renewal_id')->nullable();
$table->integer('starts_at');
$table->integer('ends_at');
$table->integer('traffic_quota_bytes')->default(0);
$table->integer('download_base_bytes')->nullable();
$table->integer('upload_base_bytes')->nullable();
$table->integer('download_used_bytes')->default(0);
$table->integer('upload_used_bytes')->default(0);
$table->string('status');
$table->integer('activated_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique('order_id');
$table->unique('renewal_id');
});
$schema->create('cloud_renewals', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('plan_id')->nullable();
$table->integer('period_id')->nullable();
$table->string('idempotency_key');
$table->string('request_hash');
$table->string('amount');
$table->integer('validity_days');
$table->integer('traffic_bytes_added');
$table->integer('previous_expires_at');
$table->integer('new_expires_at');
$table->integer('created_at');
$table->unique(['user_id', 'idempotency_key']);
});
$schema->create('cloud_jobs', static function (Blueprint $table): void {
$table->increments('id');
$table->string('operation_id')->unique();
$table->integer('order_id')->nullable();
$table->integer('instance_id')->nullable();
$table->integer('user_id');
$table->integer('host_id');
$table->string('kind');
$table->string('status');
$table->text('payload_cipher');
$table->integer('attempts')->default(0);
$table->integer('dispatched_at')->nullable();
$table->string('lease_token')->nullable();
$table->integer('lease_until')->default(0);
$table->integer('available_at');
$table->string('error_code')->nullable();
$table->text('result_json')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$logSchema = $capsule->schema('log');
$logSchema->create('engine_vm_traffic_totals', static function (Blueprint $table): void {
$table->string('host_scope');
$table->string('vm_uuid');
$table->string('host_id')->nullable();
$table->integer('user_id')->nullable();
$table->integer('last_observed_at')->nullable();
$table->integer('download_rx_bytes')->default(0);
$table->integer('upload_tx_bytes')->default(0);
$table->integer('updated_at')->default(0);
$table->primary(['host_scope', 'vm_uuid']);
});
$logSchema->create('engine_vm_traffic_checkpoints', static function (Blueprint $table): void {
$table->string('host_scope');
$table->string('vm_uuid');
$table->integer('checkpoint_hour');
$table->string('host_id')->nullable();
$table->integer('user_id')->nullable();
$table->integer('first_observed_at');
$table->integer('download_rx_bytes');
$table->integer('upload_tx_bytes');
$table->primary(['host_scope', 'vm_uuid', 'checkpoint_hour']);
});
$user = new User();
$user->id = 1;
$user->money = '100.00';
$user->enable = 1;
$user->restrict_balance_purchase = 0;
$user->save();
$now = time();
$instance = new CloudInstance();
$instance->host_id = 1;
$instance->user_id = 1;
$instance->plan_id = 7;
$instance->uuid = '11111111-1111-4111-8111-111111111111';
$instance->name = 'billed-vm';
$instance->status = 'active';
$instance->service_status = 'active';
$instance->network_status = 'active';
$instance->expires_at = $now + 3600;
$instance->renewal_price = '10.00';
$instance->renewal_days = 30;
$instance->renewal_traffic_bytes = 1000;
$instance->traffic_quota_bytes = 1000;
$instance->spec_json = '{}';
$instance->created_at = $instance->updated_at = $now - 100;
$instance->save();
$period = new CloudServicePeriod();
$period->instance_id = $instance->id;
$period->user_id = 1;
$period->plan_id = 7;
$period->starts_at = $now - 100;
$period->ends_at = $now + 3600;
$period->traffic_quota_bytes = 1000;
$period->download_base_bytes = 0;
$period->upload_base_bytes = 0;
$period->download_used_bytes = 0;
$period->upload_used_bytes = 0;
$period->status = 'active';
$period->activated_at = $now - 100;
$period->created_at = $period->updated_at = $now - 100;
$period->save();
$instance->current_period_id = $period->id;
$instance->save();
$scope = hash('sha256', 'virt-cloud/host/1');
$capsule->connection('log')->table('engine_vm_traffic_totals')->insert([
'host_scope' => $scope,
'vm_uuid' => $instance->uuid,
'host_id' => '1',
'user_id' => 1,
'last_observed_at' => $now,
'download_rx_bytes' => 700,
'upload_tx_bytes' => 400,
'updated_at' => $now,
]);
$lifecycle = new CloudLifecycleService();
$lifecycle->tick(100);
$instance->refresh();
$period->refresh();
$networkSuspend = CloudJob::where('instance_id', $instance->id)
->where('kind', 'network_suspend')->first();
$assert($period->status === 'exhausted', 'Combined upload/download did not exhaust the period quota.');
$assert((int) $period->download_used_bytes === 700 && (int) $period->upload_used_bytes === 400,
'Current-period directional traffic was not synchronized exactly.');
$assert($instance->service_status === 'active' && $instance->network_status === 'blocking',
'Traffic exhaustion stopped service instead of requesting network-only suspension.');
$assert($networkSuspend !== null
&& !CloudJob::where('instance_id', $instance->id)->where('kind', 'service_suspend')->exists(),
'Traffic exhaustion queued the wrong enforcement operation.');
$actor = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false,
'can_manage' => false, 'session_id' => ''];
$periodEndsAt = (int) $period->ends_at;
$renewal = CloudRenewalService::renew($instance, [
'idempotency_key' => 'renew-current-period-0001',
], $actor);
$sameRenewal = CloudRenewalService::renew($instance->fresh(), [
'idempotency_key' => 'renew-current-period-0001',
], $actor);
$scheduled = CloudServicePeriod::find($renewal->period_id);
$instance->refresh();
$assert($renewal->id === $sameRenewal->id && CloudRenewal::count() === 1
&& User::find(1)->money === '90.00', 'Renewal idempotency did not debit exactly once.');
$assert($scheduled->status === 'scheduled' && (int) $scheduled->starts_at === $periodEndsAt,
'Early renewal did not create a future independent period.');
$assert((int) $instance->current_period_id === (int) $period->id
&& (int) $instance->traffic_download_bytes === 700
&& (int) $instance->traffic_upload_bytes === 400
&& $instance->network_status === 'blocking',
'Early renewal refreshed current traffic or restored networking before the paid period began.');
// Model a successfully applied link-down operation, then move the clock
// boundary into the past. The next period must reset only its own usage
// and request link restoration without starting another VM service.
$networkSuspend->status = 'succeeded';
$networkSuspend->save();
$instance->network_status = 'blocked_traffic';
$instance->save();
$period->ends_at = $now - 1;
$period->save();
$scheduled->starts_at = $now - 1;
$scheduled->save();
$lifecycle->tick(100);
$instance->refresh();
$period->refresh();
$scheduled->refresh();
$networkResume = CloudJob::where('instance_id', $instance->id)
->where('kind', 'network_resume')->first();
$assert($period->status === 'finished' && $scheduled->status === 'active'
&& (int) $instance->current_period_id === (int) $scheduled->id,
'The next prepaid period was not activated at the period boundary.');
$assert((int) $scheduled->download_base_bytes === 700
&& (int) $scheduled->upload_base_bytes === 400
&& (int) $scheduled->download_used_bytes === 0
&& (int) $scheduled->upload_used_bytes === 0,
'New-period traffic did not start from an independent baseline.');
$assert($instance->service_status === 'active' && $instance->network_status === 'restoring'
&& $networkResume !== null,
'The new period did not request automatic network restoration.');
$networkResume->status = 'succeeded';
$networkResume->save();
$instance->network_status = 'active';
$instance->save();
$capsule->connection('log')->table('engine_vm_traffic_totals')
->where('host_scope', $scope)->where('vm_uuid', $instance->uuid)->update([
'last_observed_at' => $now + 1,
'download_rx_bytes' => 800,
'upload_tx_bytes' => 450,
'updated_at' => $now + 1,
]);
$lifecycle->tick(100);
$scheduled->refresh();
$assert((int) $scheduled->download_used_bytes === 100
&& (int) $scheduled->upload_used_bytes === 50,
'New-period usage included traffic paid for in the previous period.');
$scheduled->ends_at = $now - 1;
$scheduled->save();
$instance->expires_at = $now - 1;
$instance->save();
$lifecycle->tick(100);
$instance->refresh();
$serviceSuspend = CloudJob::where('instance_id', $instance->id)
->where('kind', 'service_suspend')->first();
$assert($instance->service_status === 'suspending_expired' && $serviceSuspend !== null,
'Final paid expiry did not request full service suspension.');
// A missing sample at the billing boundary must fail open. Otherwise the
// first counter catch-up after an outage would charge previous-period
// bytes to the new period and could immediately disconnect the tenant.
$stale = new CloudInstance();
$stale->host_id = 1;
$stale->user_id = 1;
$stale->plan_id = 7;
$stale->uuid = '33333333-3333-4333-8333-333333333333';
$stale->name = 'stale-boundary-vm';
$stale->status = 'active';
$stale->service_status = 'active';
$stale->network_status = 'blocked_traffic';
$stale->expires_at = $now + 3600;
$stale->renewal_price = '10.00';
$stale->renewal_days = 30;
$stale->renewal_traffic_bytes = 100;
$stale->traffic_quota_bytes = 100;
$stale->lifetime_download_bytes = 5000;
$stale->lifetime_upload_bytes = 2000;
$stale->traffic_synced_at = $now - 300;
$stale->spec_json = '{}';
$stale->created_at = $stale->updated_at = $now - 7200;
$stale->save();
$staleOld = new CloudServicePeriod();
$staleOld->instance_id = $stale->id;
$staleOld->user_id = 1;
$staleOld->plan_id = 7;
$staleOld->starts_at = $now - 7200;
$staleOld->ends_at = $now - 1;
$staleOld->traffic_quota_bytes = 100;
$staleOld->download_base_bytes = 0;
$staleOld->upload_base_bytes = 0;
$staleOld->download_used_bytes = 5000;
$staleOld->upload_used_bytes = 2000;
$staleOld->status = 'exhausted';
$staleOld->activated_at = $now - 7200;
$staleOld->created_at = $staleOld->updated_at = $now - 7200;
$staleOld->save();
$staleNew = new CloudServicePeriod();
$staleNew->instance_id = $stale->id;
$staleNew->user_id = 1;
$staleNew->plan_id = 7;
$staleNew->starts_at = $now - 1;
$staleNew->ends_at = $now + 3600;
$staleNew->traffic_quota_bytes = 100;
$staleNew->download_used_bytes = 0;
$staleNew->upload_used_bytes = 0;
$staleNew->status = 'scheduled';
$staleNew->created_at = $staleNew->updated_at = $now - 60;
$staleNew->save();
$stale->current_period_id = $staleOld->id;
$stale->save();
$lifecycle->tick(100);
$stale->refresh();
$staleNew->refresh();
$staleResume = CloudJob::where('instance_id', $stale->id)
->where('kind', 'network_resume')->first();
$assert($staleNew->status === 'active'
&& $staleNew->download_base_bytes === null
&& $staleNew->upload_base_bytes === null
&& (int) $staleNew->download_used_bytes === 0
&& (int) $staleNew->upload_used_bytes === 0,
'A stale pre-boundary sample was used as the new period baseline.');
$assert($stale->network_status === 'restoring' && $staleResume !== null
&& !CloudJob::where('instance_id', $stale->id)->where('kind', 'network_suspend')->exists(),
'Unknown boundary traffic failed closed or prevented the paid-period network restore.');
$staleResume->status = 'succeeded';
$staleResume->save();
$stale->network_status = 'active';
$stale->save();
$capsule->connection('log')->table('engine_vm_traffic_totals')->insert([
'host_scope' => $scope,
'vm_uuid' => $stale->uuid,
'host_id' => '1',
'user_id' => 1,
'last_observed_at' => $now + 2,
'download_rx_bytes' => 8000,
'upload_tx_bytes' => 4000,
'updated_at' => $now + 2,
]);
$lifecycle->tick(100);
$staleNew->refresh();
$assert((int) $staleNew->download_base_bytes === 8000
&& (int) $staleNew->upload_base_bytes === 4000
&& (int) $staleNew->download_used_bytes === 0
&& (int) $staleNew->upload_used_bytes === 0,
'The first post-boundary observation did not establish a fail-open baseline.');
$capsule->connection('log')->table('engine_vm_traffic_totals')
->where('host_scope', $scope)->where('vm_uuid', $stale->uuid)->update([
'last_observed_at' => $now + 3,
'download_rx_bytes' => 8070,
'upload_tx_bytes' => 4040,
'updated_at' => $now + 3,
]);
$lifecycle->tick(100);
$stale->refresh();
$staleNew->refresh();
$assert((int) $staleNew->download_used_bytes === 70
&& (int) $staleNew->upload_used_bytes === 40
&& $stale->network_status === 'blocking'
&& CloudJob::where('instance_id', $stale->id)->where('kind', 'network_suspend')->exists(),
'Known post-baseline usage did not enforce the combined period quota.');
// A renewal submitted just after an expired period, before the lifecycle
// daemon has closed it, must not leave two periods marked active.
$late = new CloudInstance();
$late->host_id = 1;
$late->user_id = 1;
$late->plan_id = 7;
$late->uuid = '22222222-2222-4222-8222-222222222222';
$late->name = 'late-renewal-vm';
$late->status = 'active';
$late->service_status = 'active';
$late->network_status = 'blocked_traffic';
$late->expires_at = $now - 60;
$late->renewal_price = '10.00';
$late->renewal_days = 30;
$late->renewal_traffic_bytes = 1000;
$late->traffic_quota_bytes = 1000;
$late->lifetime_download_bytes = 2000;
$late->lifetime_upload_bytes = 1000;
$late->spec_json = '{}';
$late->created_at = $late->updated_at = $now - 7200;
$late->save();
$expiredPeriod = new CloudServicePeriod();
$expiredPeriod->instance_id = $late->id;
$expiredPeriod->user_id = 1;
$expiredPeriod->plan_id = 7;
$expiredPeriod->starts_at = $now - 7200;
$expiredPeriod->ends_at = $now - 60;
$expiredPeriod->traffic_quota_bytes = 1000;
$expiredPeriod->download_base_bytes = 0;
$expiredPeriod->upload_base_bytes = 0;
$expiredPeriod->download_used_bytes = 2000;
$expiredPeriod->upload_used_bytes = 1000;
$expiredPeriod->status = 'active';
$expiredPeriod->activated_at = $now - 7200;
$expiredPeriod->created_at = $expiredPeriod->updated_at = $now - 7200;
$expiredPeriod->save();
$late->current_period_id = $expiredPeriod->id;
$late->save();
$lateRenewal = CloudRenewalService::renew($late, [
'idempotency_key' => 'late-renewal-period-0001',
], $actor);
$late->refresh();
$expiredPeriod->refresh();
$latePeriod = CloudServicePeriod::find($lateRenewal->period_id);
$assert($expiredPeriod->status === 'finished' && $latePeriod->status === 'active'
&& (int) $late->current_period_id === (int) $latePeriod->id,
'Late renewal left an expired period active beside the new current period.');
// The tick argument is a database chunk size, not a permanent low-ID cap.
// Both instances must be reconciled even when one row is fetched per chunk.
$expiredIds = [];
foreach ([
'44444444-4444-4444-8444-444444444444',
'55555555-5555-4555-8555-555555555555',
] as $number => $uuid) {
$candidate = new CloudInstance();
$candidate->host_id = 1;
$candidate->user_id = 1;
$candidate->uuid = $uuid;
$candidate->name = 'expired-' . $number;
$candidate->status = 'active';
$candidate->service_status = 'active';
$candidate->network_status = 'active';
$candidate->expires_at = $now - 1;
$candidate->spec_json = '{}';
$candidate->created_at = $candidate->updated_at = $now - 100;
$candidate->save();
$expiredIds[] = $candidate->id;
}
$lifecycle->tick(1);
foreach ($expiredIds as $id) {
$assert(CloudInstance::find($id)->service_status === 'suspending_expired'
&& CloudJob::where('instance_id', $id)->where('kind', 'service_suspend')->exists(),
'Lifecycle chunking permanently starved a higher instance ID.');
}
$makeInstance = static function (string $uuid, string $name, int $expiresAt,
string $serviceStatus = 'active', string $networkStatus = 'active') use ($now): CloudInstance {
$value = new CloudInstance();
$value->host_id = 1;
$value->user_id = 1;
$value->plan_id = 7;
$value->uuid = $uuid;
$value->name = $name;
$value->status = 'active';
$value->service_status = $serviceStatus;
$value->network_status = $networkStatus;
$value->expires_at = $expiresAt;
$value->spec_json = '{}';
$value->created_at = $value->updated_at = $now - 7200;
$value->save();
return $value;
};
$makePeriod = static function (CloudInstance $value, int $startsAt, int $endsAt,
string $status = 'scheduled', int $quota = 1000) use ($now): CloudServicePeriod {
$periodValue = new CloudServicePeriod();
$periodValue->instance_id = $value->id;
$periodValue->user_id = $value->user_id;
$periodValue->plan_id = $value->plan_id;
$periodValue->starts_at = $startsAt;
$periodValue->ends_at = $endsAt;
$periodValue->traffic_quota_bytes = $quota;
$periodValue->download_used_bytes = 0;
$periodValue->upload_used_bytes = 0;
$periodValue->status = $status;
$periodValue->created_at = $periodValue->updated_at = $now - 7200;
$periodValue->save();
return $periodValue;
};
$makeJob = static function (CloudInstance $value, string $kind,
string $status = 'uncertain') use ($now): CloudJob {
$jobValue = new CloudJob();
$jobValue->operation_id = bin2hex(random_bytes(16));
$jobValue->instance_id = $value->id;
$jobValue->user_id = $value->user_id;
$jobValue->host_id = $value->host_id;
$jobValue->kind = $kind;
$jobValue->status = $status;
$jobValue->payload_cipher = \App\Services\Cloud\CloudSecrets::encrypt([
'method' => 'POST', 'resource' => 'test', 'body' => [],
]);
$jobValue->attempts = 1;
$jobValue->dispatched_at = $now - 60;
$jobValue->lease_until = 0;
$jobValue->available_at = $now;
$jobValue->created_at = $jobValue->updated_at = $now - 60;
$jobValue->save();
return $jobValue;
};
// If the lifecycle daemon misses multiple complete prepaid periods, it
// must finish those historical rows without ever activating them. Only
// the one period that contains "now" may restore networking.
$backlog = $makeInstance('66666666-6666-4666-8666-666666666666',
'period-backlog-vm', $now + 1800, 'active', 'blocked_traffic');
$backlog->lifetime_download_bytes = 10000;
$backlog->lifetime_upload_bytes = 5000;
$backlog->traffic_synced_at = $now;
$backlog->save();
$missedOne = $makePeriod($backlog, $now - 4000, $now - 3000);
$missedTwo = $makePeriod($backlog, $now - 3000, $now - 2000);
$currentBacklog = $makePeriod($backlog, $now - 100, $now + 900);
$futureBacklog = $makePeriod($backlog, $now + 900, $now + 1800);
$lifecycle->tick(100);
$backlog->refresh();
$missedOne->refresh();
$missedTwo->refresh();
$currentBacklog->refresh();
$futureBacklog->refresh();
$assert($missedOne->status === 'finished' && $missedOne->activated_at === null
&& $missedTwo->status === 'finished' && $missedTwo->activated_at === null,
'Historical scheduled periods were briefly activated after daemon downtime.');
$assert($currentBacklog->status === 'active'
&& (int) $backlog->current_period_id === (int) $currentBacklog->id
&& $futureBacklog->status === 'scheduled',
'Lifecycle did not select exactly the currently valid prepaid period.');
$assert($backlog->network_status === 'restoring'
&& CloudJob::where('instance_id', $backlog->id)->where('kind', 'network_resume')->count() === 1,
'Current-period activation did not produce exactly one network restore.');
// Lost-response action/delete jobs can remain uncertain forever because
// they are not safely replayable. They must not grant free service by
// blocking final-expiry enforcement. Replayable and target-state policy
// uncertainty must still serialize against a duplicate operation.
$nonReplayable = $makeInstance('77777777-7777-4777-8777-777777777777',
'non-replayable-uncertain', $now - 1);
$makeJob($nonReplayable, 'action');
$makeJob($nonReplayable, 'delete');
$replayable = $makeInstance('88888888-8888-4888-8888-888888888888',
'replayable-uncertain', $now - 1);
$makeJob($replayable, 'reinstall');
$policyPending = $makeInstance('99999999-9999-4999-8999-999999999999',
'policy-uncertain', $now - 1, 'suspending_expired');
$makeJob($policyPending, 'service_suspend');
$lifecycle->tick(100);
$nonReplayable->refresh();
$replayable->refresh();
$policyPending->refresh();
$assert($nonReplayable->service_status === 'suspending_expired'
&& CloudJob::where('instance_id', $nonReplayable->id)
->where('kind', 'service_suspend')->count() === 1,
'Permanent uncertain action/delete jobs blocked final service suspension.');
$assert($replayable->service_status === 'suspending_expired'
&& CloudJob::where('instance_id', $replayable->id)
->where('kind', 'service_suspend')->count() === 0,
'Replayable uncertain work did not serialize expiry policy.');
$assert(CloudJob::where('instance_id', $policyPending->id)
->where('kind', 'service_suspend')->count() === 1,
'An uncertain policy target-state operation was duplicated.');
// Renewal prepayment is intentionally bounded. Rejection must happen in
// the wallet transaction before either money or ledger rows are changed.
\App\Services\Config::$values['cloud_max_renewal_periods_ahead'] = 2;
$renewalLimited = $makeInstance('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
'renewal-limit-vm', $now + 3000);
$renewalLimited->renewal_price = '7.25';
$renewalLimited->renewal_days = 30;
$renewalLimited->renewal_traffic_bytes = 2048;
$renewalLimited->traffic_quota_bytes = 2048;
$renewalLimited->save();
$limitedCurrent = $makePeriod($renewalLimited, $now - 100, $now + 1000, 'active', 2048);
$limitedCurrent->download_base_bytes = 0;
$limitedCurrent->upload_base_bytes = 0;
$limitedCurrent->activated_at = $now - 100;
$limitedCurrent->save();
$makePeriod($renewalLimited, $now + 1000, $now + 2000, 'scheduled', 2048);
$makePeriod($renewalLimited, $now + 2000, $now + 3000, 'scheduled', 2048);
$renewalLimited->current_period_id = $limitedCurrent->id;
$renewalLimited->save();
$balanceBeforeLimit = (string) User::find(1)->money;
$periodCountBeforeLimit = CloudServicePeriod::where('instance_id', $renewalLimited->id)->count();
$renewalCountBeforeLimit = CloudRenewal::where('instance_id', $renewalLimited->id)->count();
$expiryBeforeLimit = (int) $renewalLimited->expires_at;
try {
CloudRenewalService::renew($renewalLimited, [
'idempotency_key' => 'renewal-ahead-limit-0001',
], $actor);
throw new RuntimeException('Renewal ahead limit was not enforced.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409, 'Renewal ahead limit returned the wrong status.');
}
$renewalLimited->refresh();
$assert((string) User::find(1)->money === $balanceBeforeLimit
&& CloudServicePeriod::where('instance_id', $renewalLimited->id)->count() === $periodCountBeforeLimit
&& CloudRenewal::where('instance_id', $renewalLimited->id)->count() === $renewalCountBeforeLimit
&& (int) $renewalLimited->expires_at === $expiryBeforeLimit,
'Rejected over-limit renewal changed money, expiry or ledger rows.');
unset(\App\Services\Config::$values['cloud_max_renewal_periods_ahead']);
// A newer log timestamp with lower cumulative counters indicates a log
// reset or corrupt observation. Billing totals and blocked networking are
// monotonic and must not roll back with it.
$rollback = $makeInstance('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
'counter-rollback-vm', $now + 3600, 'active', 'blocked_traffic');
$rollback->traffic_quota_bytes = 1000;
$rollback->lifetime_download_bytes = 1000;
$rollback->lifetime_upload_bytes = 500;
$rollback->traffic_download_bytes = 1000;
$rollback->traffic_upload_bytes = 500;
$rollback->traffic_synced_at = $now - 10;
$rollback->save();
$rollbackPeriod = $makePeriod($rollback, $now - 100, $now + 3600, 'exhausted', 1000);
$rollbackPeriod->download_base_bytes = 0;
$rollbackPeriod->upload_base_bytes = 0;
$rollbackPeriod->download_used_bytes = 1000;
$rollbackPeriod->upload_used_bytes = 500;
$rollbackPeriod->activated_at = $now - 100;
$rollbackPeriod->save();
$rollback->current_period_id = $rollbackPeriod->id;
$rollback->save();
$capsule->connection('log')->table('engine_vm_traffic_totals')->insert([
'host_scope' => $scope,
'vm_uuid' => $rollback->uuid,
'host_id' => '1',
'user_id' => 1,
'last_observed_at' => $now + 100,
'download_rx_bytes' => 100,
'upload_tx_bytes' => 50,
'updated_at' => $now + 100,
]);
$lifecycle->tick(100);
$rollback->refresh();
$rollbackPeriod->refresh();
$assert((int) $rollback->lifetime_download_bytes === 1000
&& (int) $rollback->lifetime_upload_bytes === 500
&& (int) $rollback->traffic_synced_at === $now - 10
&& (int) $rollbackPeriod->download_used_bytes === 1000
&& (int) $rollbackPeriod->upload_used_bytes === 500,
'A newer but lower log total rolled billing counters backward.');
$assert($rollback->network_status === 'blocked_traffic'
&& !CloudJob::where('instance_id', $rollback->id)->where('kind', 'network_resume')->exists(),
'Counter rollback incorrectly restored an exhausted VM network.');
// Hourly cumulative checkpoints survive the 30-day chart retention and
// keep an independent billing boundary reconstructable after a prolonged
// lifecycle-daemon outage.
$checkpointed = $makeInstance('cccccccc-cccc-4ccc-8ccc-cccccccccccc',
'checkpoint-boundary-vm', $now + 3600);
$checkpointed->lifetime_download_bytes = 1000;
$checkpointed->lifetime_upload_bytes = 500;
$checkpointed->traffic_synced_at = $now;
$checkpointed->save();
$checkpointPeriod = $makePeriod($checkpointed, $now - 3600, $now + 3600,
'scheduled', 10000);
$capsule->connection('log')->table('engine_vm_traffic_checkpoints')->insert([
'host_scope' => $scope,
'vm_uuid' => $checkpointed->uuid,
'checkpoint_hour' => intdiv($now - 3540, 3600),
'host_id' => '1',
'user_id' => 1,
'first_observed_at' => $now - 3540,
'download_rx_bytes' => 400,
'upload_tx_bytes' => 200,
]);
$lifecycle->tick(100);
$checkpointed->refresh();
$checkpointPeriod->refresh();
$assert($checkpointPeriod->status === 'active'
&& (int) $checkpointPeriod->download_base_bytes === 400
&& (int) $checkpointPeriod->upload_base_bytes === 200
&& (int) $checkpointPeriod->download_used_bytes === 600
&& (int) $checkpointPeriod->upload_used_bytes === 300,
'Durable checkpoint did not reconstruct the independent period boundary.');
// Restoring a paid period may auto-start only a VM that expiry policy
// actually stopped. A tenant-stopped VM merely has autostart re-enabled.
foreach ([true, false] as $policyStoppedRunningVm) {
$suffix = $policyStoppedRunningVm ? 'd' : 'e';
$resume = $makeInstance(str_repeat($suffix, 8) . '-' . str_repeat($suffix, 4)
. '-4' . str_repeat($suffix, 3) . '-8' . str_repeat($suffix, 3)
. '-' . str_repeat($suffix, 12), 'resume-marker-' . $suffix,
$now + 3600, 'resume_pending');
$resume->suspended_at = $policyStoppedRunningVm ? $now - 10 : null;
$resume->traffic_synced_at = $now;
$resume->save();
$resumePeriod = $makePeriod($resume, $now - 100, $now + 3600,
'active', 1000);
$resumePeriod->download_base_bytes = 0;
$resumePeriod->upload_base_bytes = 0;
$resumePeriod->activated_at = $now - 100;
$resumePeriod->save();
$resume->current_period_id = $resumePeriod->id;
$resume->save();
$lifecycle->tick(100);
$resumeJob = CloudJob::where('instance_id', $resume->id)
->where('kind', 'service_resume')->first();
$resumePayload = $resumeJob === null ? []
: \App\Services\Cloud\CloudSecrets::decrypt($resumeJob->payload_cipher);
$assert($resumeJob !== null
&& ($resumePayload['body']['start_if_suspended'] ?? null) === $policyStoppedRunningVm,
'Service resume did not preserve whether expiry policy stopped a running VM.');
}
echo "Cloud independent-period renewal, traffic quota and lifecycle checks passed.\n";
}

@ -45,16 +45,17 @@ namespace {
$capsule->setAsGlobal(); $capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('user', function (Blueprint $t) { $t->increments('id'); $t->string('money'); $t->integer('enable')->default(1); $t->integer('is_admin')->default(0); $t->integer('restrict_balance_purchase')->default(0); });
$schema->create('shop', function (Blueprint $t) { $t->increments('id'); $t->integer('host_id'); $t->string('name'); $t->string('price'); foreach (['cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib', 'status', 'stock', 'sort'] as $v) { $t->integer($v)->default(0); } });
$schema->create('shop', function (Blueprint $t) { $t->increments('id'); $t->integer('host_id'); $t->string('name'); $t->string('price'); foreach (['cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib', 'validity_days', 'traffic_quota_bytes', 'status', 'stock', 'sort'] as $v) { $t->integer($v)->default(0); } });
$schema->create('cloud_hosts', function (Blueprint $t) { $t->increments('id'); $t->string('name'); $t->string('region')->default(''); $t->integer('enabled'); $t->string('default_network'); });
$schema->create('cloud_orders', function (Blueprint $t) { $t->increments('id'); foreach (['user_id', 'plan_id', 'host_id'] as $v) { $t->integer($v); } $t->string('idempotency_key'); $t->string('request_hash'); $t->string('amount'); $t->string('status'); $t->text('spec_json'); $t->integer('instance_id')->nullable(); $t->integer('refunded_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['user_id', 'idempotency_key']); });
$schema->create('cloud_jobs', function (Blueprint $t) { $t->increments('id'); $t->string('operation_id')->unique(); foreach (['order_id', 'instance_id'] as $v) { $t->integer($v)->nullable(); } foreach (['user_id', 'host_id'] as $v) { $t->integer($v); } $t->string('kind'); $t->string('status'); $t->text('payload_cipher'); $t->integer('attempts')->default(0); $t->integer('dispatched_at')->nullable(); $t->string('lease_token')->nullable(); $t->integer('lease_until')->default(0); $t->integer('available_at'); $t->string('error_code')->nullable(); $t->text('result_json')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); });
$schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); foreach (['host_id', 'user_id'] as $v) { $t->integer($v); } $t->integer('order_id')->nullable(); $t->string('uuid'); $t->string('name'); $t->string('status'); $t->text('spec_json'); $t->text('credential_cipher')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); $t->unique(['host_id', 'uuid']); });
$schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); foreach (['host_id', 'user_id'] as $v) { $t->integer($v); } foreach (['order_id', 'plan_id', 'current_period_id', 'expires_at', 'suspended_at', 'network_suspended_at'] as $v) { $t->integer($v)->nullable(); } foreach (['renewal_days', 'renewal_traffic_bytes', 'traffic_quota_bytes', 'traffic_download_bytes', 'traffic_upload_bytes', 'lifetime_download_bytes', 'lifetime_upload_bytes', 'traffic_synced_at'] as $v) { $t->integer($v)->default(0); } $t->string('renewal_price')->nullable(); $t->string('uuid'); $t->string('name'); $t->string('status'); $t->string('service_status')->default('exempt'); $t->string('network_status')->default('active'); $t->text('spec_json'); $t->text('credential_cipher')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); $t->unique(['host_id', 'uuid']); });
$schema->create('cloud_service_periods', function (Blueprint $t) { $t->increments('id'); foreach (['instance_id', 'user_id'] as $v) { $t->integer($v); } foreach (['plan_id', 'order_id', 'renewal_id', 'download_base_bytes', 'upload_base_bytes', 'activated_at'] as $v) { $t->integer($v)->nullable(); } foreach (['starts_at', 'ends_at', 'traffic_quota_bytes', 'download_used_bytes', 'upload_used_bytes', 'created_at', 'updated_at'] as $v) { $t->integer($v)->default(0); } $t->string('status'); $t->unique('order_id'); $t->unique('renewal_id'); });
$schema->create('cloud_ssh_keys', function (Blueprint $t) { $t->increments('id'); $t->integer('user_id'); $t->string('name'); $t->text('public_key'); $t->string('fingerprint'); $t->integer('created_at'); });
$user = new User(); $user->id = 1; $user->money = '100.00'; $user->enable = 1; $user->is_admin = false; $user->save(); $user->isLogin = true;
\App\Services\Auth::$user = $user;
$host = new CloudHost(); $host->name = 'host'; $host->enabled = true; $host->default_network = 'bridge:br0'; $host->save();
$plan = new Shop(); $plan->host_id = $host->id; $plan->name = 'plan'; $plan->price = '9.99'; $plan->cpu_cores = 2; $plan->memory_mib = 2048; $plan->bandwidth_mbps = 100; $plan->disk_gib = 20; $plan->status = 1; $plan->stock = 10; $plan->save();
$plan = new Shop(); $plan->host_id = $host->id; $plan->name = 'plan'; $plan->price = '9.99'; $plan->cpu_cores = 2; $plan->memory_mib = 2048; $plan->bandwidth_mbps = 100; $plan->disk_gib = 20; $plan->validity_days = 30; $plan->traffic_quota_bytes = 1024 * 1024 * 1024; $plan->status = 1; $plan->stock = 10; $plan->save();
$actor = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false, 'session_id' => ''];
$wrongOperationClient = new CloudEngineClient(new Client(['handler' => HandlerStack::create(new MockHandler([
new Response(422, [], '{"operation_id":"another-operation","status":"failed","safe_to_refund":true}')
@ -76,7 +77,8 @@ namespace {
$stack = HandlerStack::create($mock); $stack->push(Middleware::history($history));
$worker = new CloudWorkerService(new CloudEngineClient(new Client(['handler' => $stack])));
$worker->tick();
$assert(CloudOrder::find($first->id)->status === 'succeeded' && CloudInstance::count() === 1, 'Successful provision not associated.');
$assert(CloudOrder::find($first->id)->status === 'succeeded' && CloudInstance::count() === 1
&& \App\Models\CloudServicePeriod::count() === 1, 'Successful provision did not create its first service period.');
$posted = json_decode((string) $history[0]['request']->getBody(), true);
$signedActor = json_decode(base64_decode(strtr($history[0]['request']->getHeaderLine('X-Cloud-Actor'), '-_', '+/')), true);
$assert($posted['vcpu'] === 2 && $posted['bandwidth_mbps'] === 100 && $posted['network'] === 'bridge:br0'
@ -122,6 +124,147 @@ namespace {
$mock->append(new Response(200, [], '{"status":"succeeded","uuid":"11111111-1111-4111-8111-111111111111","state":"running"}')); $worker->tick();
$last = end($history)['request']; $signedActor = json_decode(base64_decode(strtr($last->getHeaderLine('X-Cloud-Actor'), '-_', '+/')), true);
$assert(CloudJob::find($actionJob->id)->status === 'succeeded' && $signedActor['can_manage'] === true && !isset($signedActor['can_provision']), 'Power task delegated provision authority or did not complete.');
// A policy request whose first POST was lost before the engine operation
// record existed is safe to replay with the same operation ID. A stale
// suspend target must instead be skipped after the authoritative 404.
$instance->network_status = 'blocking';
$instance->save();
$policy = new CloudJob();
$policy->operation_id = bin2hex(random_bytes(16));
$policy->instance_id = $instance->id;
$policy->user_id = $instance->user_id;
$policy->host_id = $instance->host_id;
$policy->kind = 'network_suspend';
$policy->status = 'uncertain';
$policy->payload_cipher = \App\Services\Cloud\CloudSecrets::encrypt([
'method' => 'POST', 'resource' => 'vms/' . $instance->uuid . '/network-suspend',
'body' => [], 'owner_id' => (int) $instance->user_id, 'system_policy' => true,
]);
$policy->attempts = 1;
$policy->dispatched_at = time() - 30;
$policy->lease_until = 0;
$policy->available_at = 0;
$policy->created_at = $policy->updated_at = time() - 30;
$policy->save();
$beforePolicyRequests = count($history);
$mock->append(new Response(404, [], '{}'));
$mock->append(new Response(200, [], '{"status":"succeeded","result":{}}'));
$worker->tick();
$policyRequests = array_slice($history, $beforePolicyRequests);
$assert(count($policyRequests) === 2
&& $policyRequests[0]['request']->getMethod() === 'GET'
&& $policyRequests[0]['request']->getUri()->getQuery() === 'reconcile=false'
&& $policyRequests[1]['request']->getMethod() === 'POST'
&& CloudJob::find($policy->id)->status === 'succeeded'
&& $instance->fresh()->network_status === 'blocked_traffic',
'Missing target-state operation was not replayed safely with the same desired state.');
$instance->network_status = 'active';
$instance->save();
$stalePolicy = $policy->replicate();
$stalePolicy->operation_id = bin2hex(random_bytes(16));
$stalePolicy->status = 'uncertain';
$stalePolicy->result_json = null;
$stalePolicy->error_code = null;
$stalePolicy->lease_token = null;
$stalePolicy->lease_until = 0;
$stalePolicy->available_at = 0;
$stalePolicy->created_at = $stalePolicy->updated_at = time() - 30;
$stalePolicy->save();
$beforeStaleRequests = count($history);
$mock->append(new Response(404, [], '{}'));
$worker->tick();
$assert(count($history) === $beforeStaleRequests + 1
&& end($history)['request']->getUri()->getQuery() === 'reconcile=false'
&& CloudJob::find($stalePolicy->id)->status === 'succeeded'
&& $instance->fresh()->network_status === 'active',
'A stale policy suspend was replayed after the desired state changed.');
// Desired state may reverse while the old engine request is still inside
// libvirt. The PHP job must remain nonterminal until that executor exits;
// otherwise lifecycle could dispatch the opposite request concurrently.
$instance->network_status = 'restore_pending';
$instance->save();
$activePolicy = $policy->replicate();
$activePolicy->operation_id = bin2hex(random_bytes(16));
$activePolicy->status = 'uncertain';
$activePolicy->result_json = null;
$activePolicy->error_code = null;
$activePolicy->lease_token = null;
$activePolicy->lease_until = 0;
$activePolicy->available_at = 0;
$activePolicy->created_at = $activePolicy->updated_at = time() - 30;
$activePolicy->save();
$beforeActiveRequests = count($history);
$mock->append(new Response(200, [], json_encode([
'status' => 'uncertain', 'executor_active' => true,
])));
$worker->tick();
$activePolicy->refresh();
$assert(count($history) === $beforeActiveRequests + 1
&& end($history)['request']->getUri()->getQuery() === 'reconcile=false'
&& $activePolicy->status === 'uncertain'
&& $activePolicy->available_at > time()
&& $instance->fresh()->network_status === 'restore_pending',
'An active stale policy executor was closed before libvirt finished.');
$activePolicy->available_at = 0;
$activePolicy->save();
$mock->append(new Response(200, [], json_encode([
'status' => 'succeeded', 'executor_active' => false,
'result' => ['network_suspended' => true],
])));
$worker->tick();
$assert(CloudJob::find($activePolicy->id)->status === 'succeeded'
&& $instance->fresh()->network_status === 'restore_pending',
'A completed stale policy did not preserve the newer desired state.');
// After an engine restart executor_active is false, but an existing
// nonterminal operation may have only partly changed libvirt. Reconcile
// that old target before the pending reverse target is allowed to run.
$crashedPolicy = $policy->replicate();
$crashedPolicy->operation_id = bin2hex(random_bytes(16));
$crashedPolicy->status = 'uncertain';
$crashedPolicy->result_json = null;
$crashedPolicy->error_code = null;
$crashedPolicy->lease_token = null;
$crashedPolicy->lease_until = 0;
$crashedPolicy->available_at = 0;
$crashedPolicy->created_at = $crashedPolicy->updated_at = time() - 30;
$crashedPolicy->save();
$beforeCrashRequests = count($history);
$mock->append(new Response(200, [], json_encode([
'status' => 'uncertain', 'executor_active' => false,
])));
$mock->append(new Response(200, [], json_encode([
'status' => 'succeeded', 'executor_active' => false,
'result' => ['network_suspended' => true],
])));
$worker->tick();
$crashRequests = array_slice($history, $beforeCrashRequests);
$assert(count($crashRequests) === 2
&& $crashRequests[0]['request']->getMethod() === 'GET'
&& $crashRequests[0]['request']->getUri()->getQuery() === 'reconcile=false'
&& $crashRequests[1]['request']->getMethod() === 'POST'
&& CloudJob::find($crashedPolicy->id)->status === 'succeeded'
&& $instance->fresh()->network_status === 'restore_pending',
'A crash-interrupted stale target was skipped instead of converged before reversal.');
// A non-policy mutation whose authoritative engine operation has vanished
// cannot be replayed safely and must become terminal instead of blocking
// expiry enforcement forever.
$missingAction = CloudJobService::enqueue($instance, $actor, 'action', 'POST',
'/actions', ['action' => 'reboot']);
$missingAction->status = 'uncertain';
$missingAction->dispatched_at = time() - 30;
$missingAction->available_at = 0;
$missingAction->save();
$mock->append(new Response(404, [], '{}'));
$worker->tick();
$assert(CloudJob::find($missingAction->id)->status === 'failed',
'A vanished non-policy engine operation remained permanently uncertain.');
$reinstall = CloudJobService::enqueue($instance, $actor, 'reinstall', 'POST', '/reinstall', ['template_id' => $input['template_id'], 'confirmation_name' => $instance->name, 'root_password' => 'NewStrongPassword!123'], ['password' => 'NewStrongPassword!123', 'login_name' => 'root']);
$oldCredential = $instance->credential_cipher;
$mock->append(new Response(202, [], '{"status":"pending","job_id":"remote-reinstall-id"}')); $worker->tick();

@ -15,12 +15,17 @@ namespace {
}
};
$input = ['name' => 'Cloud starter', 'price' => '19.90', 'cpu_cores' => '2', 'memory_mib' => '2048',
'bandwidth_mbps' => '100', 'disk_gib' => '20', 'status' => '1', 'stock' => '-1', 'sort' => '0'];
'bandwidth_mbps' => '100', 'disk_gib' => '20', 'validity_days' => '30', 'traffic_gib' => '1024',
'status' => '1', 'stock' => '-1', 'sort' => '0'];
$data = Shop::validatePlan($input);
$assert($data['cpu_cores'] === 2 && $data['disk_gib'] === 20 && $data['price'] === '19.90', 'Plan values were not normalized.');
$assert(!array_key_exists('class', $data) && !array_key_exists('expire', $data), 'A cloud plan contains old entitlements.');
$assert($data['cpu_cores'] === 2 && $data['disk_gib'] === 20 && $data['price'] === '19.90'
&& $data['validity_days'] === 30 && $data['traffic_quota_bytes'] === 1024 * Shop::GIB,
'Plan values were not normalized.');
$assert(!array_key_exists('class', $data) && !array_key_exists('expire', $data)
&& !array_key_exists('traffic_gib', $data), 'A cloud plan contains legacy or noncanonical entitlements.');
foreach (['cpu_cores' => 0, 'memory_mib' => 511, 'disk_gib' => 4097, 'bandwidth_mbps' => 0,
'price' => '1.999', 'status' => 2, 'stock' => -2, 'name' => ''] as $key => $invalid) {
'validity_days' => 0, 'traffic_gib' => -1, 'price' => '1.999', 'status' => 2,
'stock' => -2, 'name' => ''] as $key => $invalid) {
try {
Shop::validatePlan(array_replace($input, [$key => $invalid]));
throw new \RuntimeException('Invalid plan value accepted: ' . $key);
@ -34,5 +39,15 @@ namespace {
$body = substr($source, $start, $end - $start);
$assert(strpos($body, '409') !== false && strpos($body, 'save(') === false && strpos($body, 'transaction(') === false,
'Purchase must not debit or create an order before virtualization integration.');
$controller = file_get_contents(dirname(__DIR__) . '/app/Controllers/CloudApiController.php');
$client = file_get_contents(dirname(__DIR__) . '/public/cloud/app.js');
$assert(str_contains($controller, '\'can_mutate\' => $owner || $global')
&& str_contains($controller, '\'can_view_credential\' => $owner || $global')
&& str_contains($client, '!vm.can_view_credential')
&& str_contains($client, 'danger.closest(\'section\').hidden = !vm.can_mutate'),
'Readonly administrators were offered another tenant\'s credentials or VM mutation controls.');
$assert(str_contains($controller, '$suffix === \'/rescue/exit\'')
&& str_contains($controller, "['blocking', 'blocked_traffic', 'block_failed']"),
'Rescue exit does not preserve the authoritative traffic suspension target.');
echo 'Cloud plan definition checks passed.' . PHP_EOL;
}

@ -46,9 +46,9 @@ namespace {
$schema = $capsule->schema('default');
$schema->create('user', function (Blueprint $t) { $t->increments('id'); $t->integer('enable')->default(1); $t->integer('is_admin')->default(0); $t->integer('login_session_version')->default(0); });
$schema->create('cloud_sessions', function (Blueprint $t) { $t->string('id')->primary(); $t->integer('user_id'); $t->integer('version'); $t->string('fingerprint')->unique(); $t->string('auth_driver'); $t->text('auth_reference')->nullable(); $t->integer('active'); $t->integer('expires_at'); $t->integer('created_at'); });
$schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); $t->integer('host_id'); $t->integer('user_id'); $t->string('uuid'); $t->string('status'); });
$capsule->getConnection('default')->table('cloud_instances')->insert(['host_id' => 9, 'user_id' => 1, 'uuid' => '11111111-1111-4111-8111-111111111111', 'status' => 'active']);
$capsule->getConnection('default')->table('cloud_instances')->insert(['host_id' => 9, 'user_id' => 2, 'uuid' => '22222222-2222-4222-8222-222222222222', 'status' => 'active']);
$schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); $t->integer('host_id'); $t->integer('user_id'); $t->string('uuid'); $t->string('status'); $t->string('service_status')->default('active'); $t->integer('expires_at')->nullable(); });
$capsule->getConnection('default')->table('cloud_instances')->insert(['host_id' => 9, 'user_id' => 1, 'uuid' => '11111111-1111-4111-8111-111111111111', 'status' => 'active', 'expires_at' => time() + 3600]);
$capsule->getConnection('default')->table('cloud_instances')->insert(['host_id' => 9, 'user_id' => 2, 'uuid' => '22222222-2222-4222-8222-222222222222', 'status' => 'active', 'expires_at' => time() + 3600]);
$user = new User(); $user->id = 1; $user->enable = 1; $user->is_admin = false; $user->login_session_version = 0; $user->save(); $user->isLogin = true; \App\Services\Auth::$user = $user;
$_COOKIE = ['uid' => '1', 'key' => 'authenticated-cookie-reference', 'ip' => 'bound-cookie', 'expire_in' => (string) (time() + 300), 'login_session_version' => '0'];
$id = CloudSessionService::current($user);
@ -56,6 +56,9 @@ namespace {
$assert(CloudSessionService::active($id, 9, '11111111-1111-4111-8111-111111111111'), 'Active owner console rejected.');
$assert(!CloudSessionService::active($id, 9, '22222222-2222-4222-8222-222222222222')
&& !CloudSessionService::active($id, 8, '11111111-1111-4111-8111-111111111111'), 'Console crossed tenant or host boundary.');
$capsule->getConnection('default')->table('cloud_instances')->where('user_id', 1)->update(['service_status' => 'suspended_expired']);
$assert(!CloudSessionService::active($id, 9, '11111111-1111-4111-8111-111111111111'), 'Expired service retained console access.');
$capsule->getConnection('default')->table('cloud_instances')->where('user_id', 1)->update(['service_status' => 'active']);
User::where('id', 1)->update(['enable' => 0]); $assert(!CloudSessionService::active($id), 'Disabled user retained console.'); User::where('id', 1)->update(['enable' => 1]);
CloudSession::where('id', $id)->update(['expires_at' => time() - 1]); $assert(!CloudSessionService::active($id), 'Expired session retained console.'); CloudSession::where('id', $id)->update(['expires_at' => time() + 300]);
LoginSessionRevocationService::revokeUser($user); $assert(!CloudSessionService::active($id), 'Password/version change retained console.');

@ -5,16 +5,37 @@ $python = file_get_contents(dirname($root) . '/engine/virsh_panel/database.py');
if (preg_match('/_METRIC_SCHEMA\s*=\s*"""(.*?)"""/s', $python, $match) !== 1) {
throw new RuntimeException('Engine metric schema missing');
}
if (preg_match('/_TRAFFIC_SCHEMA\s*=\s*"""(.*?)"""/s', $python, $trafficMatch) !== 1) {
throw new RuntimeException('Engine traffic schema missing');
}
if (preg_match('/_TRAFFIC_CHECKPOINT_SCHEMA\s*=\s*"""(.*?)"""/s', $python, $checkpointMatch) !== 1) {
throw new RuntimeException('Engine traffic checkpoint schema missing');
}
$sql = file_get_contents($root . '/sql/cloud_log.sql');
$normalize = static function ($value): string {
$value = preg_replace('/^\s*--[^\n]*$/m', '', $value);
return rtrim(trim(preg_replace('/\s+/', ' ', $value)), ';');
};
$expected = str_replace('vm_metric_samples', 'engine_vm_metric_samples', $match[1]);
if ($normalize($sql) !== $normalize($expected)) {
throw new RuntimeException('Fresh-install metric SQL differs from engine-owned schema');
$metric = $normalize(str_replace('vm_metric_samples', 'engine_vm_metric_samples', $match[1]));
$traffic = $normalize(str_replace('vm_traffic_totals', 'engine_vm_traffic_totals', $trafficMatch[1]));
$checkpoint = $normalize(str_replace('vm_traffic_checkpoints', 'engine_vm_traffic_checkpoints', $checkpointMatch[1]));
$actual = $normalize($sql);
if (!str_contains($actual, $metric) || !str_contains($actual, $traffic)
|| !str_contains($actual, $checkpoint)) {
throw new RuntimeException('Fresh-install metric/traffic/checkpoint SQL differs from engine-owned schemas');
}
foreach ([
'host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL AFTER vm_uuid',
'user_id BIGINT UNSIGNED NULL AFTER host_id',
'network_rx_bytes BIGINT UNSIGNED NULL AFTER network_tx_bps',
'network_tx_bytes BIGINT UNSIGNED NULL AFTER network_rx_bytes',
'vm_metric_owner (user_id, host_id, sampled_at)',
] as $migration) {
if (!str_contains($actual, $migration)) {
throw new RuntimeException('Log schema is missing an idempotent metric migration');
}
}
if (!str_contains($sql, 'CREATE TABLE IF NOT EXISTS engine_vm_metric_samples')) {
throw new RuntimeException('Metric initializer must execute DDL, not an empty/comment-only query');
}
echo "Fresh-install log metric DDL matches the Python engine schema.\n";
echo "Fresh-install metric and traffic DDL matches the Python engine schemas.\n";

Loading…
Cancel
Save