@kne/fastify-app-manager 0.1.28 → 0.1.29
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/libs/controllers/app.js +377 -137
- package/libs/controllers/dbops.js +130 -47
- package/libs/controllers/migration.js +50 -13
- package/libs/schema/response.js +234 -0
- package/package.json +1 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// 响应 schema 会参与 fast-json-stringify 序列化:对象一律 additionalProperties: true 以免裁掉未声明字段;
|
|
2
|
+
// 用 nullable 而非类型数组(类型数组遇到不匹配的值会直接 500),类型不确定的字段(如 id、日期、JSON 列)只写说明不声明类型
|
|
3
|
+
const str = description => ({ type: 'string', nullable: true, description });
|
|
4
|
+
const num = description => ({ type: 'number', nullable: true, description });
|
|
5
|
+
const bool = description => ({ type: 'boolean', nullable: true, description });
|
|
6
|
+
const any = description => ({ description });
|
|
7
|
+
const strArray = description => ({ type: 'array', nullable: true, description, items: { type: 'string' } });
|
|
8
|
+
const list = (description, items) => ({ type: 'array', nullable: true, description, items: items || {} });
|
|
9
|
+
const obj = (description, properties = {}) => ({ type: 'object', nullable: true, description, additionalProperties: true, properties });
|
|
10
|
+
|
|
11
|
+
const success = (description, schema) => ({
|
|
12
|
+
200: Object.assign({ type: 'object', additionalProperties: true }, schema, { description })
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const empty = (description = '操作成功,返回空对象') => success(description);
|
|
16
|
+
|
|
17
|
+
// 文件流、SSE 等非 JSON 响应:只写说明不声明结构,不参与序列化
|
|
18
|
+
const raw = description => ({ 200: { description } });
|
|
19
|
+
|
|
20
|
+
const pageResult = (description, item) => ({
|
|
21
|
+
properties: {
|
|
22
|
+
pageData: list(`${description}列表`, item),
|
|
23
|
+
totalCount: num('总条数')
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const APP_STATUS = '应用状态:idle 未部署,deploying 部署/启动中,running 运行中,stopped 已停止,error 异常';
|
|
28
|
+
|
|
29
|
+
const entry = obj('应用入口', {
|
|
30
|
+
label: str('入口名称'),
|
|
31
|
+
path: str('相对应用根的路径,以 / 开头'),
|
|
32
|
+
showInCenter: bool('是否在应用中心前台展示'),
|
|
33
|
+
url: str('入口完整访问地址(挂载前缀 + path)')
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const deployment = obj('进行中的部署,没有为 null', {
|
|
37
|
+
progressId: str('部署进度标识'),
|
|
38
|
+
versionId: any('部署的版本 ID'),
|
|
39
|
+
version: str('部署的版本号'),
|
|
40
|
+
source: str('来源:deploy 手动部署,restore 启动时补回部署目录'),
|
|
41
|
+
startedAt: any('开始时间'),
|
|
42
|
+
step: str('当前阶段:package / env / migrate / start / ready'),
|
|
43
|
+
detail: any('当前阶段的附加说明')
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const deployEvent = obj('部署进度事件', {
|
|
47
|
+
progressId: str('部署进度标识'),
|
|
48
|
+
step: str('阶段:package 准备部署包,env 校验环境变量,migrate SQL 迁移,start 启动进程,ready 等待就绪'),
|
|
49
|
+
status: str('阶段状态:running / done / skipped / error'),
|
|
50
|
+
detail: any('附加说明'),
|
|
51
|
+
message: str('失败原因'),
|
|
52
|
+
at: any('事件时间')
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const deployRecordProps = {
|
|
56
|
+
name: str('应用 name'),
|
|
57
|
+
progressId: str('部署进度标识'),
|
|
58
|
+
versionId: any('部署的版本 ID'),
|
|
59
|
+
version: str('部署的版本号'),
|
|
60
|
+
source: str('来源:deploy 手动部署,restore 启动时补回部署目录'),
|
|
61
|
+
status: str('部署结果:running 进行中,success 成功,failed 失败'),
|
|
62
|
+
step: str('最后到达的阶段'),
|
|
63
|
+
message: str('失败原因'),
|
|
64
|
+
startedAt: any('开始时间'),
|
|
65
|
+
finishedAt: any('结束时间,进行中为 null')
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const deployRecord = obj('部署记录(不含日志)', deployRecordProps);
|
|
69
|
+
|
|
70
|
+
const appProps = {
|
|
71
|
+
id: any('应用 ID'),
|
|
72
|
+
name: str('应用 name(小写 slug,同时作为访问路径 /app/{name})'),
|
|
73
|
+
label: str('应用显示名称'),
|
|
74
|
+
domain: str('绑定的域名,未绑定为 null'),
|
|
75
|
+
icon: str('应用图标(文件 ID 或地址)'),
|
|
76
|
+
description: str('应用描述'),
|
|
77
|
+
env: obj('应用环境变量;密钥类变量值被掩码为 ********,宿主维护的系统变量不返回'),
|
|
78
|
+
pm2Config: obj('PM2 覆盖配置'),
|
|
79
|
+
options: obj('扩展配置(分类、是否公开、入口、密钥变量名、所属表等)'),
|
|
80
|
+
port: num('分配给子应用进程的端口'),
|
|
81
|
+
status: str(APP_STATUS),
|
|
82
|
+
currentVersionId: any('当前部署的版本 ID,未部署为 null'),
|
|
83
|
+
rootPath: str('应用在服务器上的根目录'),
|
|
84
|
+
pm2Name: str('PM2 进程名'),
|
|
85
|
+
message: str('最近一次状态说明(如失败原因)'),
|
|
86
|
+
passthroughEnvKeys: strArray('宿主透传给子应用的环境变量名'),
|
|
87
|
+
secretEnvKeys: strArray('按密钥处理(值被掩码)的环境变量名'),
|
|
88
|
+
pathUrl: str('按路径访问的地址,如 /app/{name}/'),
|
|
89
|
+
category: any('应用分类:{ code, name } 或分类编码字符串,未设置为 null'),
|
|
90
|
+
isPublic: bool('是否公开(未登录可在应用中心看到),未设置视为公开'),
|
|
91
|
+
entries: list('应用入口', entry),
|
|
92
|
+
createdAt: any('创建时间'),
|
|
93
|
+
updatedAt: any('更新时间'),
|
|
94
|
+
deletedAt: any('删除时间')
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const app = description => obj(description || '应用', appProps);
|
|
98
|
+
|
|
99
|
+
const appDetail = obj(
|
|
100
|
+
'应用详情',
|
|
101
|
+
Object.assign({}, appProps, {
|
|
102
|
+
pm2Defaults: obj('插件级 PM2 默认配置'),
|
|
103
|
+
deployment,
|
|
104
|
+
lastDeployment: obj('最近一次部署记录(不含日志),没有为 null', deployRecordProps)
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const appListItem = obj('应用', Object.assign({}, appProps, { deployment }));
|
|
109
|
+
|
|
110
|
+
const centerApp = obj('应用中心中的应用', {
|
|
111
|
+
name: str('应用 name'),
|
|
112
|
+
label: str('应用显示名称'),
|
|
113
|
+
icon: str('应用图标'),
|
|
114
|
+
description: str('应用描述'),
|
|
115
|
+
category: any('应用分类:{ code, name } 或分类编码字符串,未设置为 null'),
|
|
116
|
+
isPublic: bool('是否公开'),
|
|
117
|
+
pathUrl: str('访问地址,如 /app/{name}/'),
|
|
118
|
+
entries: list('开放给前台的应用入口', entry),
|
|
119
|
+
deployment: obj('正在部署时为 { source },否则为 null', { source: str('部署来源') })
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const version = obj('应用版本', {
|
|
123
|
+
id: any('版本 ID'),
|
|
124
|
+
appName: str('所属应用 name'),
|
|
125
|
+
version: str('版本号'),
|
|
126
|
+
label: str('版本说明'),
|
|
127
|
+
artifactPath: str('版本解压目录'),
|
|
128
|
+
hasMigration: bool('是否包含 SQL 迁移脚本'),
|
|
129
|
+
migrationPath: str('相对 server 的 SQL 目录'),
|
|
130
|
+
fileId: str('宿主文件 ID;版本目录缺失时据此重建'),
|
|
131
|
+
sqlFiles: strArray('SQL 迁移文件名,没有迁移脚本时不返回'),
|
|
132
|
+
hasEnvSchema: bool('是否包含 server/env.schema.json'),
|
|
133
|
+
createdAt: any('上传时间'),
|
|
134
|
+
updatedAt: any('更新时间')
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const logLine = obj('日志行', {
|
|
138
|
+
line: num('行号(从 1 开始)'),
|
|
139
|
+
content: str('日志内容,行首时间已转换到配置的时区')
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const logFile = obj('日志文件', {
|
|
143
|
+
fileName: str('文件名,如 out.log、out-20260101-000000.log.gz'),
|
|
144
|
+
stream: str('输出流:out 标准输出,err 错误输出'),
|
|
145
|
+
size: num('文件大小(字节)'),
|
|
146
|
+
compressed: bool('是否为 gzip 压缩的归档'),
|
|
147
|
+
current: bool('是否为正在写入的当前日志'),
|
|
148
|
+
mtime: any('修改时间')
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const loadSample = obj('负载采样', {
|
|
152
|
+
name: str('应用 name'),
|
|
153
|
+
ts: num('采样时间戳(毫秒)'),
|
|
154
|
+
sampledAt: str('采样时间'),
|
|
155
|
+
status: str('进程状态,offline 表示不在运行'),
|
|
156
|
+
cpu: num('CPU 使用率(%)'),
|
|
157
|
+
memory: num('内存占用(字节)'),
|
|
158
|
+
instances: num('进程实例数'),
|
|
159
|
+
pids: list('进程 ID'),
|
|
160
|
+
uptime: num('运行时长(毫秒)'),
|
|
161
|
+
restarts: num('重启次数'),
|
|
162
|
+
requests: obj('网关请求指标(QPS、耗时、错误率等)')
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const column = obj('列信息', {
|
|
166
|
+
name: str('列名'),
|
|
167
|
+
type: any('列类型'),
|
|
168
|
+
primaryKey: bool('是否主键'),
|
|
169
|
+
allowNull: bool('是否允许为空'),
|
|
170
|
+
defaultValue: any('默认值')
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const tableInfo = obj('数据表', {
|
|
174
|
+
table: str('表名'),
|
|
175
|
+
columns: list('列信息', column),
|
|
176
|
+
primaryKey: strArray('主键列名'),
|
|
177
|
+
error: str('读取表结构失败时的错误信息')
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const DB_SCOPE = '数据库归属:shared 共享托管库(按表前缀区分应用),dedicated 应用独享库';
|
|
181
|
+
|
|
182
|
+
const cleanupResult = obj('清理结果', {
|
|
183
|
+
dropped: strArray('已删除的表(删除 sqlite 文件时为 file:{路径})'),
|
|
184
|
+
skipped: list('跳过的项', obj('跳过原因', { reason: str('原因') })),
|
|
185
|
+
failed: list('删除失败的表', obj('失败项', { table: str('表名'), error: str('错误信息') })),
|
|
186
|
+
sql: strArray('需要手工执行的 SQL(dryRun 时为将要执行的 SQL)'),
|
|
187
|
+
manualRequired: bool('是否需要手工处理'),
|
|
188
|
+
dbScope: str(DB_SCOPE)
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const exportResult = obj('导出结果', {
|
|
192
|
+
mode: str('导出方式:json 按表导出为 zip,file 复制 sqlite 文件'),
|
|
193
|
+
path: str('导出文件在服务器上的路径'),
|
|
194
|
+
tables: strArray('导出的表'),
|
|
195
|
+
rowCounts: obj('每张表导出的行数 { [表名]: 行数 }')
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const migrationResult = obj('迁移脚本操作结果', {
|
|
199
|
+
name: str('脚本文件名'),
|
|
200
|
+
executed: bool('操作后是否为已执行')
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
module.exports = {
|
|
204
|
+
str,
|
|
205
|
+
num,
|
|
206
|
+
bool,
|
|
207
|
+
any,
|
|
208
|
+
strArray,
|
|
209
|
+
list,
|
|
210
|
+
obj,
|
|
211
|
+
success,
|
|
212
|
+
empty,
|
|
213
|
+
raw,
|
|
214
|
+
pageResult,
|
|
215
|
+
APP_STATUS,
|
|
216
|
+
DB_SCOPE,
|
|
217
|
+
entry,
|
|
218
|
+
deployEvent,
|
|
219
|
+
deployRecordProps,
|
|
220
|
+
deployRecord,
|
|
221
|
+
app,
|
|
222
|
+
appDetail,
|
|
223
|
+
appListItem,
|
|
224
|
+
centerApp,
|
|
225
|
+
version,
|
|
226
|
+
logLine,
|
|
227
|
+
logFile,
|
|
228
|
+
loadSample,
|
|
229
|
+
column,
|
|
230
|
+
tableInfo,
|
|
231
|
+
cleanupResult,
|
|
232
|
+
exportResult,
|
|
233
|
+
migrationResult
|
|
234
|
+
};
|