@infly/libs 2.1.5 → 2.1.7
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.
|
@@ -15,6 +15,7 @@ trap cleanup EXIT
|
|
|
15
15
|
current_images=()
|
|
16
16
|
next_images=()
|
|
17
17
|
selected_indexes=()
|
|
18
|
+
selected_components=()
|
|
18
19
|
selected_services=()
|
|
19
20
|
for i in "${!component_names[@]}"; do
|
|
20
21
|
key=${image_keys[$i]}
|
|
@@ -37,6 +38,7 @@ for pair in "$@"; do
|
|
|
37
38
|
[[ "$suffix" != "$image" && "$suffix" =~ ^[0-9a-f]{12}$ ]] || { echo "只接受本组件正式 commit 镜像: $name" >&2; exit 1; }
|
|
38
39
|
next_images[$i]=$image
|
|
39
40
|
selected_indexes+=("$i")
|
|
41
|
+
selected_components+=("$name")
|
|
40
42
|
read -r -a group <<< "${service_groups[$i]}"
|
|
41
43
|
selected_services+=("${group[@]}")
|
|
42
44
|
done
|
|
@@ -65,10 +67,13 @@ rollback() {
|
|
|
65
67
|
echo "发布失败,恢复基线: $previous_env" >&2
|
|
66
68
|
cp -- "$previous_env" "$release_env_file"
|
|
67
69
|
chmod 600 "$release_env_file"
|
|
68
|
-
if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}"
|
|
70
|
+
if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}" \
|
|
71
|
+
|| ! run_post_deploy "${selected_components[@]}" \
|
|
72
|
+
|| ! bash "$bundle_dir/health-check.sh"; then
|
|
69
73
|
echo "自动回滚未通过验证,需人工恢复: $project_dir/$previous_env" >&2
|
|
70
74
|
fi
|
|
71
75
|
}
|
|
72
76
|
if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}"; then rollback; exit 1; fi
|
|
77
|
+
if ! run_post_deploy "${selected_components[@]}"; then rollback; exit 1; fi
|
|
73
78
|
if ! bash "$bundle_dir/health-check.sh"; then rollback; exit 1; fi
|
|
74
79
|
echo "发布成功: $target; 上一版本记录: $project_dir/$previous_env"
|
|
@@ -14,7 +14,7 @@ function renderDeploymentConfig(config, target) {
|
|
|
14
14
|
if (!deploy || deploy.ready !== true) throw new Error(`平台 ${target} 尚未完成人工服务器核验,禁止生产发布`);
|
|
15
15
|
const components = Object.entries(profile.components || {});
|
|
16
16
|
if (!components.length) throw new Error("部署必须配置 components");
|
|
17
|
-
const repositories = [], keys = [], serviceGroups = [];
|
|
17
|
+
const repositories = [], keys = [], serviceGroups = [], postDeployByComponent = [];
|
|
18
18
|
for (const [name, component] of components) {
|
|
19
19
|
required(name, "component", /^[a-z][a-z0-9_-]*$/);
|
|
20
20
|
const repository = required(component.repository, `${name}.repository`, /^[a-z0-9][a-z0-9.:/_-]+$/);
|
|
@@ -23,10 +23,20 @@ function renderDeploymentConfig(config, target) {
|
|
|
23
23
|
keys.push(required(component.imageEnv, `${name}.imageEnv`, /^[A-Z][A-Z0-9_]*$/));
|
|
24
24
|
if (!Array.isArray(component.services) || !component.services.length) throw new Error(`${name} 缺少 services`);
|
|
25
25
|
serviceGroups.push(component.services.map((service) => required(service, "service", /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/)).join(" "));
|
|
26
|
+
if (component.postDeploy != null && !Array.isArray(component.postDeploy)) throw new Error(`${name}.postDeploy 必须是数组`);
|
|
27
|
+
postDeployByComponent.push(component.postDeploy || []);
|
|
26
28
|
}
|
|
27
29
|
if (new Set(keys).size !== keys.length) throw new Error("imageEnv 不得重复");
|
|
28
30
|
const allServices = serviceGroups.flatMap((group) => group.split(" "));
|
|
29
31
|
if (new Set(allServices).size !== allServices.length) throw new Error("组件不能重复拥有同一个服务");
|
|
32
|
+
for (let i = 0; i < postDeployByComponent.length; i++) {
|
|
33
|
+
for (const action of postDeployByComponent[i]) {
|
|
34
|
+
if (!action || !allServices.includes(action.service) || !Array.isArray(action.command) || !action.command.length
|
|
35
|
+
|| !action.command.every((arg) => typeof arg === "string" && arg && !arg.includes("\0"))) {
|
|
36
|
+
throw new Error(`${components[i][0]}.postDeploy 必须绑定已配置服务和非空命令数组`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
30
40
|
const lines = ["# 由 @infly/libs 根据项目配置生成;只含非敏感参数。",
|
|
31
41
|
`target=${quote(required(target, "target", /^[a-zA-Z0-9_-]+$/))}`,
|
|
32
42
|
`registry=${quote(required(config.registry, "registry", /^[a-zA-Z0-9.:-]+$/))}`,
|
|
@@ -42,6 +52,15 @@ function renderDeploymentConfig(config, target) {
|
|
|
42
52
|
array("component_names", components.map(([name]) => name)), array("repositories", repositories),
|
|
43
53
|
array("image_keys", keys), array("service_groups", serviceGroups), array("health_services", allServices),
|
|
44
54
|
];
|
|
55
|
+
lines.push("run_post_deploy() {", " local component", " for component in \"$@\"; do", " case \"$component\" in");
|
|
56
|
+
for (let i = 0; i < components.length; i++) {
|
|
57
|
+
lines.push(` ${quote(components[i][0])})`);
|
|
58
|
+
for (const action of postDeployByComponent[i]) {
|
|
59
|
+
lines.push(` \"\${compose[@]}\" exec -T ${quote(action.service)} ${action.command.map(quote).join(" ")} || return $?`);
|
|
60
|
+
}
|
|
61
|
+
lines.push(" ;;");
|
|
62
|
+
}
|
|
63
|
+
lines.push(" *) echo \"未知发布组件: $component\" >&2; return 2 ;;", " esac", " done", "}");
|
|
45
64
|
const health = deploy.health || {};
|
|
46
65
|
if (!Array.isArray(health.urls) || !health.urls.length) throw new Error("必须配置 health.urls");
|
|
47
66
|
for (const url of health.urls) if (!/^https?:\/\//.test(url)) throw new Error("健康检查 URL 必须是 HTTP(S)");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infly/libs",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.7",
|
|
4
4
|
"description": "不受前端框架限制的独立工具库",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"author": "Kahal",
|
|
60
60
|
"license": "ISC",
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@infly/ts-libs": "^0.
|
|
62
|
+
"@infly/ts-libs": "^1.0.0",
|
|
63
63
|
"connect": "^3.7.0",
|
|
64
64
|
"dotenv": "16.6.1",
|
|
65
65
|
"enquirer": "^2.4.1",
|
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
const path = require("node:path");
|
|
2
|
-
const { spawnSync } = require("node:child_process");
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 在指定目录执行 git 命令,自动处理 safe.directory
|
|
6
|
-
*/
|
|
7
|
-
function runGit(cwd, args, { allowFailure = false } = {}) {
|
|
8
|
-
const absoluteCwd = path.resolve(cwd);
|
|
9
|
-
const safeDirectory = absoluteCwd.replaceAll("\\", "/");
|
|
10
|
-
const result = spawnSync(
|
|
11
|
-
"git",
|
|
12
|
-
["-c", `safe.directory=${safeDirectory}`, "-C", absoluteCwd, ...args],
|
|
13
|
-
{ encoding: "utf8", windowsHide: true },
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
if (result.status !== 0 && !allowFailure) {
|
|
17
|
-
const detail = (result.stderr || result.stdout || "Git command failed").trim();
|
|
18
|
-
throw new Error(detail);
|
|
19
|
-
}
|
|
20
|
-
if (result.status !== 0 && allowFailure) {
|
|
21
|
-
const detail = String(result.stderr || result.stdout || "").trim();
|
|
22
|
-
if (detail) {
|
|
1
|
+
const path = require("node:path");
|
|
2
|
+
const { spawnSync } = require("node:child_process");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 在指定目录执行 git 命令,自动处理 safe.directory
|
|
6
|
+
*/
|
|
7
|
+
function runGit(cwd, args, { allowFailure = false } = {}) {
|
|
8
|
+
const absoluteCwd = path.resolve(cwd);
|
|
9
|
+
const safeDirectory = absoluteCwd.replaceAll("\\", "/");
|
|
10
|
+
const result = spawnSync(
|
|
11
|
+
"git",
|
|
12
|
+
["-c", `safe.directory=${safeDirectory}`, "-C", absoluteCwd, ...args],
|
|
13
|
+
{ encoding: "utf8", windowsHide: true },
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
if (result.status !== 0 && !allowFailure) {
|
|
17
|
+
const detail = (result.stderr || result.stdout || "Git command failed").trim();
|
|
18
|
+
throw new Error(detail);
|
|
19
|
+
}
|
|
20
|
+
if (result.status !== 0 && allowFailure) {
|
|
21
|
+
const detail = String(result.stderr || result.stdout || "").trim();
|
|
22
|
+
if (detail) {
|
|
23
23
|
process.stderr.write(`[status:submodule] git ${args.join(" ")}: ${detail}\n`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return result.status === 0 ? result.stdout.trim() : "";
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 从 .gitmodules 中读取所有已配置的子模块(名称、路径、配置分支)
|
|
32
|
-
*/
|
|
33
|
-
function getConfiguredSubmodules(rootDir) {
|
|
34
|
-
const output = runGit(rootDir, [
|
|
35
|
-
"config",
|
|
36
|
-
"--file",
|
|
37
|
-
".gitmodules",
|
|
38
|
-
"--get-regexp",
|
|
39
|
-
"^submodule\\..*\\.path$",
|
|
40
|
-
]);
|
|
41
|
-
|
|
42
|
-
return output.split(/\r?\n/).filter(Boolean).map((line) => {
|
|
43
|
-
const separator = line.search(/\s/);
|
|
44
|
-
const key = line.slice(0, separator);
|
|
45
|
-
const submodulePath = line.slice(separator).trim();
|
|
46
|
-
const name = key.slice("submodule.".length, -".path".length);
|
|
47
|
-
const configuredBranch = runGit(
|
|
48
|
-
rootDir,
|
|
49
|
-
["config", "--file", ".gitmodules", "--get", `submodule.${name}.branch`],
|
|
50
|
-
{ allowFailure: true },
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
return { name, path: submodulePath, configuredBranch };
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 获取主仓 HEAD 中记录的某个子模块指针 commit
|
|
59
|
-
*/
|
|
60
|
-
function getParentPointer(rootDir, submodulePath) {
|
|
61
|
-
const treeEntry = runGit(rootDir, ["ls-tree", "HEAD", "--", submodulePath]);
|
|
62
|
-
return treeEntry ? treeEntry.split(/\s+/)[2] : "";
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 检查单个子模块的状态:当前分支、HEAD、指针漂移、远程漂移、是否 dirty
|
|
67
|
-
*/
|
|
68
|
-
function inspectSubmodule(rootDir, submodule) {
|
|
69
|
-
const worktree = path.join(rootDir, submodule.path);
|
|
70
|
-
const pointer = getParentPointer(rootDir, submodule.path);
|
|
71
|
-
const head = runGit(worktree, ["rev-parse", "HEAD"], { allowFailure: true });
|
|
72
|
-
|
|
73
|
-
if (!head) {
|
|
74
|
-
return {
|
|
75
|
-
path: submodule.path,
|
|
76
|
-
branch: "-",
|
|
77
|
-
head: "-",
|
|
78
|
-
pointer: pointer ? pointer.slice(0, 8) : "-",
|
|
79
|
-
originMaster: "-",
|
|
80
|
-
state: "not-initialized",
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const branch = runGit(worktree, ["branch", "--show-current"], { allowFailure: true });
|
|
85
|
-
const remoteBranch = submodule.configuredBranch || branch || "master";
|
|
86
|
-
const originHead = runGit(
|
|
87
|
-
worktree,
|
|
88
|
-
["rev-parse", "--verify", `origin/${remoteBranch}`],
|
|
89
|
-
{ allowFailure: true },
|
|
90
|
-
);
|
|
91
|
-
const dirty = Boolean(runGit(worktree, ["status", "--porcelain"], { allowFailure: true }));
|
|
92
|
-
const states = [];
|
|
93
|
-
|
|
94
|
-
if (dirty) states.push("dirty");
|
|
95
|
-
if (pointer && head !== pointer) states.push("pointer-drift");
|
|
96
|
-
if (originHead && head !== originHead) states.push("remote-drift");
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
path: submodule.path,
|
|
100
|
-
branch: branch || "DETACHED",
|
|
101
|
-
head: head.slice(0, 8),
|
|
102
|
-
pointer: pointer ? pointer.slice(0, 8) : "-",
|
|
103
|
-
originMaster: originHead ? originHead.slice(0, 8) : "-",
|
|
104
|
-
state: states.length > 0 ? states.join(",") : "ok",
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = {
|
|
109
|
-
getConfiguredSubmodules,
|
|
110
|
-
getParentPointer,
|
|
111
|
-
inspectSubmodule,
|
|
112
|
-
runGit,
|
|
113
|
-
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return result.status === 0 ? result.stdout.trim() : "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 从 .gitmodules 中读取所有已配置的子模块(名称、路径、配置分支)
|
|
32
|
+
*/
|
|
33
|
+
function getConfiguredSubmodules(rootDir) {
|
|
34
|
+
const output = runGit(rootDir, [
|
|
35
|
+
"config",
|
|
36
|
+
"--file",
|
|
37
|
+
".gitmodules",
|
|
38
|
+
"--get-regexp",
|
|
39
|
+
"^submodule\\..*\\.path$",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
return output.split(/\r?\n/).filter(Boolean).map((line) => {
|
|
43
|
+
const separator = line.search(/\s/);
|
|
44
|
+
const key = line.slice(0, separator);
|
|
45
|
+
const submodulePath = line.slice(separator).trim();
|
|
46
|
+
const name = key.slice("submodule.".length, -".path".length);
|
|
47
|
+
const configuredBranch = runGit(
|
|
48
|
+
rootDir,
|
|
49
|
+
["config", "--file", ".gitmodules", "--get", `submodule.${name}.branch`],
|
|
50
|
+
{ allowFailure: true },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return { name, path: submodulePath, configuredBranch };
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 获取主仓 HEAD 中记录的某个子模块指针 commit
|
|
59
|
+
*/
|
|
60
|
+
function getParentPointer(rootDir, submodulePath) {
|
|
61
|
+
const treeEntry = runGit(rootDir, ["ls-tree", "HEAD", "--", submodulePath]);
|
|
62
|
+
return treeEntry ? treeEntry.split(/\s+/)[2] : "";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 检查单个子模块的状态:当前分支、HEAD、指针漂移、远程漂移、是否 dirty
|
|
67
|
+
*/
|
|
68
|
+
function inspectSubmodule(rootDir, submodule) {
|
|
69
|
+
const worktree = path.join(rootDir, submodule.path);
|
|
70
|
+
const pointer = getParentPointer(rootDir, submodule.path);
|
|
71
|
+
const head = runGit(worktree, ["rev-parse", "HEAD"], { allowFailure: true });
|
|
72
|
+
|
|
73
|
+
if (!head) {
|
|
74
|
+
return {
|
|
75
|
+
path: submodule.path,
|
|
76
|
+
branch: "-",
|
|
77
|
+
head: "-",
|
|
78
|
+
pointer: pointer ? pointer.slice(0, 8) : "-",
|
|
79
|
+
originMaster: "-",
|
|
80
|
+
state: "not-initialized",
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const branch = runGit(worktree, ["branch", "--show-current"], { allowFailure: true });
|
|
85
|
+
const remoteBranch = submodule.configuredBranch || branch || "master";
|
|
86
|
+
const originHead = runGit(
|
|
87
|
+
worktree,
|
|
88
|
+
["rev-parse", "--verify", `origin/${remoteBranch}`],
|
|
89
|
+
{ allowFailure: true },
|
|
90
|
+
);
|
|
91
|
+
const dirty = Boolean(runGit(worktree, ["status", "--porcelain"], { allowFailure: true }));
|
|
92
|
+
const states = [];
|
|
93
|
+
|
|
94
|
+
if (dirty) states.push("dirty");
|
|
95
|
+
if (pointer && head !== pointer) states.push("pointer-drift");
|
|
96
|
+
if (originHead && head !== originHead) states.push("remote-drift");
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
path: submodule.path,
|
|
100
|
+
branch: branch || "DETACHED",
|
|
101
|
+
head: head.slice(0, 8),
|
|
102
|
+
pointer: pointer ? pointer.slice(0, 8) : "-",
|
|
103
|
+
originMaster: originHead ? originHead.slice(0, 8) : "-",
|
|
104
|
+
state: states.length > 0 ? states.join(",") : "ok",
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = {
|
|
109
|
+
getConfiguredSubmodules,
|
|
110
|
+
getParentPointer,
|
|
111
|
+
inspectSubmodule,
|
|
112
|
+
runGit,
|
|
113
|
+
};
|