@wdyy/skills 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.well-known/skills/index.json +2 -2
- package/.well-known/skills/wdyy-deployment-standard/SKILL.md +30 -46
- package/.well-known/skills/wdyy-deployment-standard/agents/openai.yaml +2 -2
- package/.well-known/skills/wdyy-deployment-standard/reference/docker-delivery-rules.md +103 -0
- package/.well-known/skills/wdyy-deployment-standard/scripts/generate-deployment-files.mjs +138 -155
- package/.well-known/skills/wdyy-deployment-standard/scripts/generate-deployment-files.test.mjs +97 -97
- package/.well-known/skills/wdyy-deployment-standard/scripts/validate-deployment-package.mjs +93 -116
- package/.well-known/skills/wdyy-deployment-standard/scripts/validate-deployment-package.test.mjs +185 -311
- package/.well-known/skills/wdyy-deployment-standard/templates/{Dockerfile.template → backend.Dockerfile.template} +5 -9
- package/.well-known/skills/wdyy-deployment-standard/templates/deploy.sh.template +210 -256
- package/.well-known/skills/wdyy-deployment-standard/templates/docker-compose.yml +11 -13
- package/.well-known/skills/wdyy-deployment-standard/templates/dockerignore.template +1 -1
- package/.well-known/skills/wdyy-deployment-standard/templates/env.example.template +0 -19
- package/.well-known/skills/wdyy-deployment-standard/templates/frontend-container.conf.template +0 -6
- package/.well-known/skills/wdyy-deployment-standard/templates/frontend.Dockerfile.template +9 -4
- package/README.md +2 -2
- package/lib/wdyy-cli.js +2 -1
- package/package.json +1 -1
- package/.well-known/skills/wdyy-deployment-standard/reference/linux-deployment-rules.md +0 -99
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
},
|
|
8
8
|
{
|
|
9
9
|
"name": "wdyy-deployment-standard",
|
|
10
|
-
"description": "
|
|
11
|
-
"files": ["SKILL.md", "agents/openai.yaml", "reference/
|
|
10
|
+
"description": "为无 CI/CD 的前后端项目生成并验证统一 Docker 交付:使用本地已有基础镜像构建前后端两个独立镜像,分别导出后封装为一个压缩包,数据库直连远程实例,服务器无参数一键部署整个项目栈。Use when 规划、实现、审查或执行人工 Docker 生产交付时。",
|
|
11
|
+
"files": ["SKILL.md", "agents/openai.yaml", "reference/docker-delivery-rules.md", "scripts/generate-deployment-files.mjs", "scripts/generate-deployment-files.test.mjs", "scripts/validate-deployment-package.mjs", "scripts/validate-deployment-package.test.mjs", "templates/backend.Dockerfile.template", "templates/frontend.Dockerfile.template", "templates/frontend-container.conf.template", "templates/env.example.template", "templates/deploy.sh.template", "templates/docker-compose.yml", "templates/dockerignore.template"]
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
14
|
"name": "wdyy-api-standard",
|
|
@@ -1,63 +1,47 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: wdyy-deployment-standard
|
|
3
|
-
description:
|
|
3
|
+
description: 为无 CI/CD 的前后端项目生成并验证统一 Docker 交付:使用本地已有基础镜像构建前后端两个独立镜像,分别导出后封装为一个压缩包,数据库直连远程实例,服务器无参数一键部署整个项目栈。Use when 规划、实现、审查或执行人工 Docker 生产交付时。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
#
|
|
6
|
+
# 统一 Docker 交付规范
|
|
7
7
|
|
|
8
8
|
## Overview
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
把一个项目作为一个整体交付:本地执行 `./deploy.sh build`,生成前端、后端两个独立镜像并分别导出,最终只产出一个压缩包;工程师人工上传并解压;服务器无参数执行 `./deploy.sh`,一次性加载并部署整个前后端项目栈。
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Workflow
|
|
13
13
|
|
|
14
|
-
1.
|
|
15
|
-
2. 完整读取 [
|
|
16
|
-
3.
|
|
17
|
-
4.
|
|
18
|
-
5.
|
|
14
|
+
1. 读取目标项目真实的 pnpm scripts、前后端构建输出、Dockerfile、健康检查、根 `.env` 和远程数据库连接方式。
|
|
15
|
+
2. 完整读取 [统一 Docker 交付规则](reference/docker-delivery-rules.md)。
|
|
16
|
+
3. 运行 `node scripts/generate-deployment-files.mjs --target <目标项目根目录> --write` 生成受版本控制文件并扩充 `.env`、`.env.example` 的部署段。既有生成文件不一致时先展示差异并等待工程师确认,确认后才使用 `--force`。
|
|
17
|
+
4. 在目标项目根执行生成器 `--check`,再运行 `scripts/validate-deployment-package.mjs` 和行为测试。
|
|
18
|
+
5. 本地执行 `./deploy.sh build`;只在全部校验成功后原子替换 `deploy/<PROJECT_NAME>-docker.tar.gz`。
|
|
19
|
+
6. 人工上传并解压压缩包,在服务器解压目录无参数执行 `./deploy.sh`。
|
|
19
20
|
|
|
20
|
-
##
|
|
21
|
+
## Fixed Contract
|
|
21
22
|
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
- `.env.example` 内置 `FRONTEND_BASE_IMAGE=nginx:stable` 与 `BACKEND_BASE_IMAGE=node:24-alpine3.24`;`.env` 可显式覆盖。
|
|
30
|
-
- 环境文件固定按“前端、后端、数据库、API、部署”排列;部署段只包含 `PROJECT_NAME`、`DOCKER_BIND_IP`、`FRONTEND_BASE_IMAGE`、`BACKEND_BASE_IMAGE`。
|
|
31
|
-
- 发布链路不接受数据库迁移参数,也不打包、校验或执行迁移文件。
|
|
23
|
+
- 构建 `<PROJECT_NAME>_frontend:latest` 与 `<PROJECT_NAME>_backend:latest` 两个独立镜像。
|
|
24
|
+
- 分别导出 `frontend-image.tar`、`backend-image.tar`,但只交付一个 `<PROJECT_NAME>-docker.tar.gz`。
|
|
25
|
+
- 构建前检查 `FRONTEND_BASE_IMAGE`、`BACKEND_BASE_IMAGE` 已存在于本地;缺失即失败,不执行 `docker pull`。
|
|
26
|
+
- Compose 只定义 frontend、backend;数据库通过 `.env` 直连远程实例,不生成数据库容器、数据卷或迁移路径。
|
|
27
|
+
- 服务器无参数命令整体部署;只额外允许 `stop` 和 `status`,不得独立操作单一服务。
|
|
28
|
+
- 前后端直接暴露 `.env` 指定的宿主机端口;两个容器共同挂载项目根 `./logs` 到 `/app/logs`。
|
|
29
|
+
- `.env` 可以进入人工上传的压缩包,但必须被 `.dockerignore` 排除,不得进入镜像。
|
|
32
30
|
|
|
33
|
-
##
|
|
31
|
+
## Prohibited
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
5. 在服务器项目根目录无参数执行 `./deploy.sh`,选择:运行或替换前端、运行或替换后端、运行或替换前后端、停止。
|
|
40
|
-
6. 运行或替换会载入所选服务的目标版本镜像、强制重建并健康检查;停止会同时停止 frontend 和 backend。
|
|
41
|
-
|
|
42
|
-
## 禁止事项
|
|
43
|
-
|
|
44
|
-
- 不得使用蓝绿、滚动切流、活动/历史颜色、宿主机 Nginx、自动回滚或旧版 start/replace/restart/remove/status/rollback 命令。
|
|
45
|
-
- 不得接受人工 build 版本参数,不得扫描版本目录替代发布账本或 `release.env`。
|
|
46
|
-
- 不得使用 Bash `source`/`eval` 解析 `.env` 或版本指针。
|
|
47
|
-
- 不得使用镜像 fallback、固定 `/srv`、固定 URL/IP/端口、PM2、systemd 或自动安装。
|
|
48
|
-
- 不得重新引入 `PROJECT_HTTP_PORT`、`FRONTEND_BLUE_PORT`、`FRONTEND_GREEN_PORT`、`BACKEND_BLUE_PORT`、`BACKEND_GREEN_PORT`、`FRONTEND_CONTAINER_PORT`、`BACKEND_CONTAINER_PORT`、`BACKEND_LISTEN_HOST` 或任何 `*_HEALTH_URL`、`*_VERSION_URL`。
|
|
49
|
-
- 不得自动执行 psql、迁移脚本或数据库回滚。
|
|
50
|
-
- 不得以字符串关键字存在代替命令行为、失败传播和资源边界测试。
|
|
33
|
+
- 不得使用镜像仓库 pull、镜像 fallback、数据库容器、数据库迁移、宿主机 Nginx、蓝绿、滚动切流或自动回滚。
|
|
34
|
+
- 不得保留发布账本、版本目录、`release.env`、交互菜单或前后端独立替换命令。
|
|
35
|
+
- 不得上传文件、拉取代码、安装 Docker、固定服务器目录或使用 `source`、`eval` 执行 `.env`。
|
|
36
|
+
- 不得跳过清单、镜像标签、Compose 服务范围、远程数据库配置或健康状态验证。
|
|
51
37
|
|
|
52
38
|
## Verification
|
|
53
39
|
|
|
54
|
-
- [ ]
|
|
55
|
-
- [ ]
|
|
56
|
-
- [ ]
|
|
57
|
-
- [ ]
|
|
58
|
-
- [ ]
|
|
59
|
-
- [ ]
|
|
60
|
-
- [ ] 连续两次构建的 deploy.sh 和 Compose 一致,失败不写发布记录、不暴露半成品。
|
|
61
|
-
- [ ] `deploy/` 含根 `.env`,但版本目录、Docker 上下文和镜像不含秘密文件。
|
|
40
|
+
- [ ] deploy 目录只有一个 `<PROJECT_NAME>-docker.tar.gz`。
|
|
41
|
+
- [ ] 压缩包解压后只含 `.env`、deploy.sh、docker-compose.yml、两个镜像 tar 和 manifest.sha256。
|
|
42
|
+
- [ ] 两个镜像归档各自只含预期的 `latest` 标签。
|
|
43
|
+
- [ ] 构建与服务器 Compose 均禁止 pull,服务器禁止 build。
|
|
44
|
+
- [ ] Compose 只含 frontend、backend,并使用远程数据库环境变量和根 logs。
|
|
45
|
+
- [ ] 无参数部署、stop、status 的实际行为及失败传播通过测试。
|
|
62
46
|
|
|
63
|
-
|
|
47
|
+
任一步失败必须指出具体阶段并返回非零;不得静默跳过、伪造成功或删除上一份成功交付包。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
interface:
|
|
2
2
|
display_name: "wdyy-deployment-standard"
|
|
3
|
-
short_description: "
|
|
4
|
-
default_prompt: "Use $wdyy-deployment-standard to
|
|
3
|
+
short_description: "统一前后端 Docker 双镜像打包与一键整栈部署"
|
|
4
|
+
default_prompt: "Use $wdyy-deployment-standard to build two local-only Docker images, package them together, and deploy the full stack in one command."
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# 统一 Docker 交付规则
|
|
2
|
+
|
|
3
|
+
## 1. 生成文件
|
|
4
|
+
|
|
5
|
+
目标项目维护以下受版本控制文件:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
deploy.sh
|
|
9
|
+
.dockerignore
|
|
10
|
+
src/frontend/Dockerfile
|
|
11
|
+
src/backend/Dockerfile
|
|
12
|
+
scripts/deployment/docker-compose.yml
|
|
13
|
+
scripts/deployment/frontend-container.conf.template
|
|
14
|
+
.env.example
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
使用 `scripts/generate-deployment-files.mjs --target <project-root> --write` 从 Skill templates 原子生成。现有文件与模板不一致时默认失败;工程师确认差异后才使用 `--force`。生成后必须执行 `--check`。
|
|
18
|
+
|
|
19
|
+
## 2. 环境配置
|
|
20
|
+
|
|
21
|
+
根 `.env` 是构建、压缩包和服务器 Compose 的唯一配置源,固定按“前端、后端、数据库、API、部署”排列。部署必须具有非空值:
|
|
22
|
+
|
|
23
|
+
```dotenv
|
|
24
|
+
FRONTEND_URL=0.0.0.0
|
|
25
|
+
FRONTEND_PORT=5173
|
|
26
|
+
BACKEND_URL=0.0.0.0
|
|
27
|
+
BACKEND_PORT=3000
|
|
28
|
+
DB_URL=database.example.internal
|
|
29
|
+
DB_PORT=5432
|
|
30
|
+
DB_USER=app_user
|
|
31
|
+
DB_PASSWORD=change_me
|
|
32
|
+
DB_NAME=app
|
|
33
|
+
DB_SCHEMA=public
|
|
34
|
+
PROJECT_NAME=example_project
|
|
35
|
+
DOCKER_BIND_IP=0.0.0.0
|
|
36
|
+
FRONTEND_BASE_IMAGE=nginx:stable
|
|
37
|
+
BACKEND_BASE_IMAGE=node:24-alpine3.24
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
- `PROJECT_NAME` 只允许小写字母开头,后续使用小写字母、数字或下划线。
|
|
41
|
+
- 前后端端口必须不同且位于 1–65535;`DB_PORT` 必须位于 1–65535。
|
|
42
|
+
- `.env` 权限必须为 `0600` 或只读 `0400`。
|
|
43
|
+
- 按数据解析 `.env`;不得 `source`、`eval` 或执行其中内容。
|
|
44
|
+
- `DB_URL` 指向远程数据库主机或地址;部署资产不得把它解释为 Docker 服务名并创建数据库容器。
|
|
45
|
+
- `.env.example` 可保留空数据库凭据,但真实 `.env` 构建时必须完整。
|
|
46
|
+
|
|
47
|
+
## 3. 本地镜像与构建
|
|
48
|
+
|
|
49
|
+
基础镜像引用允许使用标签,但必须在构建机本地预先存在。相同标签被人工替换后可能对应不同内容;构建过程不得访问镜像仓库。
|
|
50
|
+
|
|
51
|
+
`./deploy.sh build` 必须按顺序:
|
|
52
|
+
|
|
53
|
+
1. 验证根 `.env`、Dockerfile、Compose 和 `.dockerignore`。
|
|
54
|
+
2. 使用 `docker image inspect` 确认两个基础镜像存在。
|
|
55
|
+
3. 使用 `docker build --pull=false` 构建 `<PROJECT_NAME>_frontend:latest` 与 `<PROJECT_NAME>_backend:latest`。
|
|
56
|
+
4. 分别执行 Docker save,生成 `frontend-image.tar`、`backend-image.tar`。
|
|
57
|
+
5. 复制根 `.env`、deploy.sh、docker-compose.yml,生成覆盖全部包内文件的 `manifest.sha256`。
|
|
58
|
+
6. 验证镜像归档各自只含预期标签,验证完整包结构。
|
|
59
|
+
7. 生成 `deploy/<PROJECT_NAME>-docker.tar.gz`,成功后原子替换同名旧包。
|
|
60
|
+
|
|
61
|
+
构建失败必须保留上一份成功压缩包,并清理本次临时目录。不得维护发布账本、版本号、版本目录、`release.env` 或半成品目录。
|
|
62
|
+
|
|
63
|
+
## 4. 压缩包契约
|
|
64
|
+
|
|
65
|
+
压缩包解压根固定为:
|
|
66
|
+
|
|
67
|
+
```text
|
|
68
|
+
.env
|
|
69
|
+
deploy.sh
|
|
70
|
+
docker-compose.yml
|
|
71
|
+
frontend-image.tar
|
|
72
|
+
backend-image.tar
|
|
73
|
+
manifest.sha256
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
不得包含源代码、Git、依赖缓存、数据库目录、迁移脚本、秘密副本、符号链接或额外嵌套目录。`.env` 必须保持 owner-only 权限并纳入清单;`.dockerignore` 必须确保 `.env`、秘密、deploy、logs、Git、依赖缓存和虚拟环境不进入镜像构建上下文。
|
|
77
|
+
|
|
78
|
+
## 5. 服务器一键操作
|
|
79
|
+
|
|
80
|
+
工程师人工上传并解压单一压缩包。服务器部署根由解压后的 deploy.sh 物理路径确定。
|
|
81
|
+
|
|
82
|
+
- `./deploy.sh`:校验 `.env` 权限和内容、清单、两个镜像 tar 及预期标签;加载两个镜像;使用一次 `docker compose up -d --force-recreate --no-build --pull never --wait frontend backend` 整体部署并等待健康。
|
|
83
|
+
- `./deploy.sh stop`:同时停止 frontend、backend,保留容器、镜像、交付文件和日志。
|
|
84
|
+
- `./deploy.sh status`:输出当前 Compose 项目的服务状态,不改变 Docker 状态。
|
|
85
|
+
|
|
86
|
+
不得显示交互菜单,不得接收服务名,不得提供单独运行或替换 frontend/backend 的入口。非法参数必须在 Docker 状态变更前失败。
|
|
87
|
+
|
|
88
|
+
## 6. Compose、数据库和日志
|
|
89
|
+
|
|
90
|
+
- Compose 只允许 frontend、backend 两个服务,不设置 `container_name`。
|
|
91
|
+
- 镜像固定为 `${PROJECT_NAME}_frontend:latest`、`${PROJECT_NAME}_backend:latest`,不得提供 fallback。
|
|
92
|
+
- frontend 映射 `${DOCKER_BIND_IP}:${FRONTEND_PORT}:${FRONTEND_PORT}`;backend 同理。
|
|
93
|
+
- 两个服务均使用根 `.env`,并挂载 `./logs:/app/logs`。
|
|
94
|
+
- backend 直接读取 DB_URL、DB_PORT、DB_USER、DB_PASSWORD、DB_NAME、DB_SCHEMA。
|
|
95
|
+
- Compose 不得定义数据库服务、数据库镜像、数据库 volume、数据库端口映射或迁移任务。
|
|
96
|
+
- 前端 Nginx 日志写入 `/app/logs/frontend-access.log`、`/app/logs/frontend-error.log`;后端遵循 wdyy-logging-standard;部署操作写入 `./logs/deploy.log`。
|
|
97
|
+
|
|
98
|
+
## 7. 失败边界
|
|
99
|
+
|
|
100
|
+
- 基础镜像缺失、清单错误、镜像标签错误、远程数据库配置不完整或任一服务不健康时明确失败。
|
|
101
|
+
- 禁止 `docker pull`、服务器 `docker build`、镜像 fallback、自动回滚、自动安装和静默跳过。
|
|
102
|
+
- 脚本只操作当前 `PROJECT_NAME` 的 Compose 项目和 frontend、backend 服务,不清理其他项目资源。
|
|
103
|
+
- 旧部署资产由工程师根据实际状态单独迁移;新脚本不得猜测并删除。
|
|
@@ -9,215 +9,198 @@ const mappings = [
|
|
|
9
9
|
['deploy.sh.template', 'deploy.sh', 0o755],
|
|
10
10
|
['dockerignore.template', '.dockerignore', 0o644],
|
|
11
11
|
['frontend.Dockerfile.template', 'src/frontend/Dockerfile', 0o644],
|
|
12
|
-
['Dockerfile.template', 'src/backend/Dockerfile', 0o644],
|
|
12
|
+
['backend.Dockerfile.template', 'src/backend/Dockerfile', 0o644],
|
|
13
13
|
['docker-compose.yml', 'scripts/deployment/docker-compose.yml', 0o644],
|
|
14
14
|
['frontend-container.conf.template', 'scripts/deployment/frontend-container.conf.template', 0o644],
|
|
15
15
|
];
|
|
16
|
-
|
|
17
16
|
const sectionNames = ['前端', '后端', '数据库', 'API', '部署'];
|
|
18
|
-
const
|
|
17
|
+
const fixedSectionKeys = new Map([
|
|
19
18
|
['前端', ['FRONTEND_URL', 'FRONTEND_PORT']],
|
|
20
19
|
['后端', ['BACKEND_URL', 'BACKEND_PORT']],
|
|
21
20
|
['数据库', ['DB_URL', 'DB_PORT', 'DB_USER', 'DB_PASSWORD', 'DB_NAME', 'DB_SCHEMA']],
|
|
22
21
|
]);
|
|
23
|
-
const
|
|
24
|
-
'
|
|
25
|
-
'
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
'
|
|
31
|
-
'NGINX_SOURCE', 'LOG_DIR', 'HOST_LOG_DIR',
|
|
22
|
+
const exampleDefaults = new Map([
|
|
23
|
+
['FRONTEND_URL', '0.0.0.0'], ['FRONTEND_PORT', '5173'],
|
|
24
|
+
['BACKEND_URL', '0.0.0.0'], ['BACKEND_PORT', '3000'],
|
|
25
|
+
['DB_URL', ''], ['DB_PORT', '5432'], ['DB_USER', ''], ['DB_PASSWORD', ''], ['DB_NAME', ''], ['DB_SCHEMA', ''],
|
|
26
|
+
]);
|
|
27
|
+
const deploymentKeys = ['PROJECT_NAME', 'DOCKER_BIND_IP', 'FRONTEND_BASE_IMAGE', 'BACKEND_BASE_IMAGE'];
|
|
28
|
+
const removedKeys = new Set([
|
|
29
|
+
'PROJECT_HTTP_PORT', 'FRONTEND_BLUE_PORT', 'FRONTEND_GREEN_PORT', 'BACKEND_BLUE_PORT', 'BACKEND_GREEN_PORT',
|
|
30
|
+
'SERVER_PROJECTS_ROOT', 'NGINX_SOURCE', 'LOG_DIR', 'HOST_LOG_DIR', 'FRONTEND_CONTAINER_PORT',
|
|
31
|
+
'BACKEND_CONTAINER_PORT', 'BACKEND_LISTEN_HOST', 'DATABASE_MIGRATION_MODE', 'MIGRATIONS_SOURCE',
|
|
32
|
+
'MIGRATION_RUNNER_SOURCE', 'RELEASE_VERSION',
|
|
32
33
|
]);
|
|
33
34
|
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
function parseArgs(argv) {
|
|
36
|
+
let mode;
|
|
37
|
+
let force = false;
|
|
38
|
+
let target;
|
|
39
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
40
|
+
const argument = argv[index];
|
|
41
|
+
if (argument === '--target') {
|
|
42
|
+
if (target !== undefined || !argv[index + 1] || argv[index + 1].startsWith('--')) throw new Error('Duplicate or missing --target argument');
|
|
43
|
+
target = argv[++index];
|
|
44
|
+
} else if (argument === '--write' || argument === '--check') {
|
|
45
|
+
if (mode !== undefined) throw new Error('Exactly one of --write or --check is required');
|
|
46
|
+
mode = argument.slice(2);
|
|
47
|
+
} else if (argument === '--force') {
|
|
48
|
+
if (force) throw new Error('Duplicate --force argument');
|
|
49
|
+
force = true;
|
|
50
|
+
} else {
|
|
51
|
+
throw new Error(`Unknown argument: ${argument}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!mode || target === undefined || (force && mode !== 'write')) {
|
|
55
|
+
throw new Error('Usage: generate-deployment-files.mjs --target <project-root> (--write [--force] | --check)');
|
|
56
|
+
}
|
|
57
|
+
return { mode, force, target: resolve(target) };
|
|
58
|
+
}
|
|
40
59
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
60
|
+
function parseEnvironment(text, relativePath, requireExampleDefaults, deploymentDefaults) {
|
|
61
|
+
const sections = new Map(sectionNames.map((name) => [name, []]));
|
|
62
|
+
const headings = [];
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
let current;
|
|
65
|
+
for (const [index, raw] of text.replaceAll('\r\n', '\n').split('\n').entries()) {
|
|
66
|
+
if (!raw) continue;
|
|
67
|
+
const heading = raw.match(/^# (前端|后端|数据库|API|部署)$/);
|
|
44
68
|
if (heading) {
|
|
45
|
-
|
|
46
|
-
|
|
69
|
+
current = heading[1];
|
|
70
|
+
headings.push(current);
|
|
47
71
|
continue;
|
|
48
72
|
}
|
|
49
|
-
if (
|
|
50
|
-
if (!
|
|
51
|
-
const
|
|
52
|
-
if (!
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
sections.get(currentSection).push({ key: entry[1], value: entry[2] });
|
|
73
|
+
if (raw.startsWith('#')) continue;
|
|
74
|
+
if (!current) throw new Error(`${relativePath}:${index + 1} must be inside a standard section`);
|
|
75
|
+
const match = raw.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
|
|
76
|
+
if (!match) throw new Error(`${relativePath}:${index + 1} must use KEY=VALUE syntax`);
|
|
77
|
+
const [, key, value] = match;
|
|
78
|
+
if (seen.has(key)) throw new Error(`${relativePath} contains duplicate key: ${key}`);
|
|
79
|
+
if (removedKeys.has(key) || /_(?:HEALTH|VERSION)_URL$/.test(key)) throw new Error(`${relativePath} contains removed deployment configuration: ${key}`);
|
|
80
|
+
seen.add(key);
|
|
81
|
+
sections.get(current).push({ key, value });
|
|
59
82
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
throw new Error(`${relativeTarget} section order must be: ${sectionNames.map((name) => `# ${name}`).join(', ')}`);
|
|
83
|
+
if (headings.join('|') !== sectionNames.join('|')) {
|
|
84
|
+
throw new Error(`${relativePath} section order must be: ${sectionNames.map((name) => `# ${name}`).join(', ')}`);
|
|
63
85
|
}
|
|
64
|
-
for (const [section, expectedKeys] of
|
|
86
|
+
for (const [section, expectedKeys] of fixedSectionKeys) {
|
|
65
87
|
const entries = sections.get(section);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
throw new Error(`${relativeTarget} # ${section} keys must be: ${expectedKeys.join(', ')}`);
|
|
88
|
+
if (entries.map(({ key }) => key).join('|') !== expectedKeys.join('|')) {
|
|
89
|
+
throw new Error(`${relativePath} # ${section} keys must be: ${expectedKeys.join(', ')}`);
|
|
69
90
|
}
|
|
70
91
|
if (requireExampleDefaults) {
|
|
71
92
|
for (const { key, value } of entries) {
|
|
72
|
-
if (value !==
|
|
93
|
+
if (value !== exampleDefaults.get(key)) throw new Error(`${relativePath} must keep the standard default for ${key}`);
|
|
73
94
|
}
|
|
74
95
|
}
|
|
75
96
|
}
|
|
76
|
-
const
|
|
97
|
+
const deploymentEntries = sections.get('部署');
|
|
98
|
+
const actualDeploymentKeys = deploymentEntries.map(({ key }) => key);
|
|
77
99
|
if (actualDeploymentKeys.length !== 0 && actualDeploymentKeys.join('|') !== deploymentKeys.join('|')) {
|
|
78
|
-
throw new Error(`${
|
|
79
|
-
}
|
|
80
|
-
if (requireExampleDefaults && sections.get('部署').some(({ key, value }) => !['FRONTEND_BASE_IMAGE', 'BACKEND_BASE_IMAGE'].includes(key) && value !== '')) {
|
|
81
|
-
throw new Error(`${relativeTarget} deployment example values must be empty except base-image defaults`);
|
|
100
|
+
throw new Error(`${relativePath} # 部署 keys must be empty or exactly: ${deploymentKeys.join(', ')}`);
|
|
82
101
|
}
|
|
83
102
|
if (requireExampleDefaults) {
|
|
84
|
-
for (const key of
|
|
85
|
-
|
|
86
|
-
if (entry && entry.value !== exampleValues.get(key)) throw new Error(`${relativeTarget} must keep the standard default for ${key}`);
|
|
103
|
+
for (const { key, value } of deploymentEntries) {
|
|
104
|
+
if (value !== deploymentDefaults.get(key)) throw new Error(`${relativePath} must keep the standard deployment default for ${key}`);
|
|
87
105
|
}
|
|
88
106
|
}
|
|
89
107
|
return {
|
|
90
|
-
extended:
|
|
91
|
-
nonDeploymentKeys: [...
|
|
108
|
+
extended: deploymentEntries.length === deploymentKeys.length,
|
|
109
|
+
nonDeploymentKeys: [...seen].filter((key) => !deploymentKeys.includes(key)),
|
|
92
110
|
};
|
|
93
111
|
}
|
|
94
112
|
|
|
95
|
-
function extendDeploymentSection(text) {
|
|
96
|
-
|
|
113
|
+
function extendDeploymentSection(text, deploymentDefaults) {
|
|
114
|
+
const additions = deploymentKeys.map((key) => `${key}=${deploymentDefaults.get(key)}`).join('\n');
|
|
115
|
+
return `${text.trimEnd()}\n${additions}\n`;
|
|
97
116
|
}
|
|
98
117
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
throw new Error('Duplicate or missing --target argument');
|
|
108
|
-
}
|
|
109
|
-
targetArgument = args[index + 1];
|
|
110
|
-
index += 1;
|
|
111
|
-
} else if (argument === '--write' || argument === '--check') {
|
|
112
|
-
if (mode !== undefined) throw new Error('Exactly one of --write or --check is required');
|
|
113
|
-
mode = argument.slice(2);
|
|
114
|
-
} else if (argument === '--force') {
|
|
115
|
-
if (force) throw new Error('Duplicate --force argument');
|
|
116
|
-
force = true;
|
|
117
|
-
} else {
|
|
118
|
-
throw new Error(`Unknown argument: ${argument}`);
|
|
118
|
+
async function readRegularFile(path, label, allowMissing = false) {
|
|
119
|
+
try {
|
|
120
|
+
const info = await lstat(path);
|
|
121
|
+
if (info.isSymbolicLink() || !info.isFile()) throw new Error(`${label} must be a regular file`);
|
|
122
|
+
return await readFile(path);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (allowMissing && error.code === 'ENOENT') return undefined;
|
|
125
|
+
throw error;
|
|
119
126
|
}
|
|
120
127
|
}
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
|
|
129
|
+
async function atomicWrite(path, content, mode) {
|
|
130
|
+
await mkdir(dirname(path), { recursive: true });
|
|
131
|
+
const temporary = `${path}.wdyy-${process.pid}.tmp`;
|
|
132
|
+
try {
|
|
133
|
+
await writeFile(temporary, content, { flag: 'wx', mode });
|
|
134
|
+
await chmod(temporary, mode);
|
|
135
|
+
await rename(temporary, path);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
await unlink(temporary).catch(() => {});
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
123
140
|
}
|
|
124
141
|
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
if (!
|
|
142
|
+
const { mode, force, target } = parseArgs(process.argv.slice(2));
|
|
143
|
+
const targetInfo = await stat(target);
|
|
144
|
+
if (!targetInfo.isDirectory()) throw new Error(`Target project root is not a directory: ${target}`);
|
|
145
|
+
|
|
146
|
+
const deploymentTemplate = await readFile(join(templatesRoot, 'env.example.template'), 'utf8');
|
|
147
|
+
const deploymentDefaults = new Map([...deploymentTemplate.matchAll(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/gm)].map((match) => [match[1], match[2]]));
|
|
148
|
+
if (deploymentKeys.some((key) => !deploymentDefaults.has(key))) throw new Error('Deployment environment template is incomplete');
|
|
128
149
|
|
|
129
|
-
const environmentTemplate = await readFile(join(templatesRoot, 'env.example.template'), 'utf8');
|
|
130
|
-
const exampleValues = new Map(
|
|
131
|
-
[...environmentTemplate.matchAll(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/gm)].map((match) => [match[1], match[2]]),
|
|
132
|
-
);
|
|
133
150
|
const environmentPlans = [];
|
|
134
|
-
for (const [
|
|
135
|
-
const
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
const structure = parseEnvironmentStructure(existing, relativeTarget, requireExampleDefaults, exampleValues);
|
|
151
|
+
for (const [relativePath, expectedMode, requireDefaults] of [['.env', 0o600, false], ['.env.example', 0o644, true]]) {
|
|
152
|
+
const path = join(target, relativePath);
|
|
153
|
+
const existingBuffer = await readRegularFile(path, relativePath);
|
|
154
|
+
const existing = existingBuffer.toString('utf8');
|
|
155
|
+
const parsed = parseEnvironment(existing, relativePath, requireDefaults, deploymentDefaults);
|
|
140
156
|
environmentPlans.push({
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
nonDeploymentKeys: structure.nonDeploymentKeys,
|
|
146
|
-
expected: structure.extended ? existing : extendDeploymentSection(existing),
|
|
157
|
+
path, relativePath, expectedMode,
|
|
158
|
+
existing: existingBuffer,
|
|
159
|
+
expected: Buffer.from(parsed.extended ? existing : extendDeploymentSection(existing, deploymentDefaults)),
|
|
160
|
+
nonDeploymentKeys: parsed.nonDeploymentKeys,
|
|
147
161
|
});
|
|
148
162
|
}
|
|
149
163
|
if (environmentPlans[0].nonDeploymentKeys.join('|') !== environmentPlans[1].nonDeploymentKeys.join('|')) {
|
|
150
164
|
throw new Error('.env and .env.example variable names must remain consistent');
|
|
151
165
|
}
|
|
152
166
|
|
|
153
|
-
const
|
|
154
|
-
for (const [templateName,
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const targetInfo = await lstat(target);
|
|
161
|
-
if (targetInfo.isSymbolicLink() || !targetInfo.isFile()) throw new Error(`Generated target must be a regular file: ${relativeTarget}`);
|
|
162
|
-
existing = await readFile(target);
|
|
163
|
-
} catch (error) {
|
|
164
|
-
if (error.code !== 'ENOENT') throw error;
|
|
165
|
-
}
|
|
167
|
+
const generatedPlans = [];
|
|
168
|
+
for (const [templateName, relativePath, expectedMode] of mappings) {
|
|
169
|
+
const expected = await readFile(join(templatesRoot, templateName));
|
|
170
|
+
const path = join(target, relativePath);
|
|
171
|
+
const existing = await readRegularFile(path, relativePath, true);
|
|
172
|
+
generatedPlans.push({ path, relativePath, expectedMode, expected, existing });
|
|
173
|
+
}
|
|
166
174
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (!differs && relativeTarget === 'deploy.sh') {
|
|
171
|
-
const info = await stat(target);
|
|
172
|
-
if ((info.mode & 0o111) === 0) differences.push(`${relativeTarget} (not executable)`);
|
|
173
|
-
}
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
if (differs && existing && !force) {
|
|
177
|
-
differences.push(relativeTarget);
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
if (differs) {
|
|
181
|
-
await mkdir(dirname(target), { recursive: true });
|
|
182
|
-
const temporary = `${target}.wdyy-${process.pid}.tmp`;
|
|
183
|
-
try {
|
|
184
|
-
await writeFile(temporary, expected, { flag: 'wx', mode: expectedMode });
|
|
185
|
-
await chmod(temporary, expectedMode);
|
|
186
|
-
await rename(temporary, target);
|
|
187
|
-
} catch (error) {
|
|
188
|
-
await unlink(temporary).catch(() => {});
|
|
189
|
-
throw error;
|
|
190
|
-
}
|
|
191
|
-
} else {
|
|
192
|
-
await chmod(target, expectedMode);
|
|
193
|
-
}
|
|
175
|
+
const differingGenerated = generatedPlans.filter(({ existing, expected }) => existing && !existing.equals(expected)).map(({ relativePath }) => relativePath);
|
|
176
|
+
if (mode === 'write' && differingGenerated.length && !force) {
|
|
177
|
+
throw new Error(`Refusing to overwrite differing deployment files without --force: ${differingGenerated.join(', ')}`);
|
|
194
178
|
}
|
|
195
179
|
|
|
196
|
-
|
|
197
|
-
|
|
180
|
+
const differences = [];
|
|
181
|
+
for (const plan of generatedPlans) {
|
|
182
|
+
const differs = !plan.existing || !plan.existing.equals(plan.expected);
|
|
183
|
+
if (mode === 'check') {
|
|
184
|
+
if (differs) differences.push(plan.relativePath);
|
|
185
|
+
else {
|
|
186
|
+
const info = await stat(plan.path);
|
|
187
|
+
if ((info.mode & 0o777) !== plan.expectedMode) differences.push(`${plan.relativePath} (mode)`);
|
|
188
|
+
}
|
|
189
|
+
} else if (differs) await atomicWrite(plan.path, plan.expected, plan.expectedMode);
|
|
190
|
+
else await chmod(plan.path, plan.expectedMode);
|
|
198
191
|
}
|
|
199
192
|
|
|
200
193
|
for (const plan of environmentPlans) {
|
|
201
|
-
|
|
202
|
-
|
|
194
|
+
const differs = !plan.existing.equals(plan.expected);
|
|
195
|
+
if (mode === 'check') {
|
|
196
|
+
if (differs) differences.push(plan.relativePath);
|
|
203
197
|
else {
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
await writeFile(temporary, plan.expected, { flag: 'wx', mode: plan.expectedMode });
|
|
207
|
-
await chmod(temporary, plan.expectedMode);
|
|
208
|
-
await rename(temporary, plan.target);
|
|
209
|
-
} catch (error) {
|
|
210
|
-
await unlink(temporary).catch(() => {});
|
|
211
|
-
throw error;
|
|
212
|
-
}
|
|
198
|
+
const info = await stat(plan.path);
|
|
199
|
+
if ((info.mode & 0o777) !== plan.expectedMode) differences.push(`${plan.relativePath} (mode)`);
|
|
213
200
|
}
|
|
214
|
-
} else if (
|
|
215
|
-
|
|
216
|
-
}
|
|
201
|
+
} else if (differs) await atomicWrite(plan.path, plan.expected, plan.expectedMode);
|
|
202
|
+
else await chmod(plan.path, plan.expectedMode);
|
|
217
203
|
}
|
|
218
204
|
|
|
219
|
-
if (differences.length) {
|
|
220
|
-
|
|
221
|
-
throw new Error(`${action}: ${differences.join(', ')}`);
|
|
222
|
-
}
|
|
223
|
-
process.stdout.write(`${mode === 'write' ? 'generated' : 'verified'} deterministic deployment files in ${targetRoot}\n`);
|
|
205
|
+
if (differences.length) throw new Error(`Generated deployment files differ: ${differences.join(', ')}`);
|
|
206
|
+
process.stdout.write(`${mode === 'write' ? 'generated' : 'verified'} unified Docker delivery files in ${target}\n`);
|