Skip to content

Commit 6d006b2

Browse files
committed
feat(systemd-service-manager): 添加单元状态摘要输出功能
- 在 enable、disable、start、stop、restart 命令后添加状态摘要输出 - 在 install 命令中添加已安装和已启动的状态输出 - 移除 status 命令中的重复状态检查逻辑,统一使用新的摘要函数 - 新增 ssm_collect_unit_summary 和 ssm_print_unit_summary 函数提供统 一的状态摘要功能 - 添加对激活中但未启用的单元的友好提示信息 - 更新相关测试用例验证新的输出格式
1 parent 6dd7c0b commit 6d006b2

10 files changed

Lines changed: 96 additions & 12 deletions

File tree

scripts/bash/systemd-service-manager/commands/disable.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ ssm_cmd_disable() {
1111
local target_name="${2:-}"
1212
ssm_load_target_context "${target_kind}" "${target_name}"
1313
ssm_systemctl "${SSM_ACTIVE_SCOPE}" disable "${SSM_ACTIVE_UNIT}"
14+
printf 'disabled=%s\n' "${SSM_ACTIVE_UNIT}"
15+
ssm_print_unit_summary "${SSM_ACTIVE_NAME}" "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}"
1416
}

scripts/bash/systemd-service-manager/commands/enable.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ ssm_cmd_enable() {
1111
local target_name="${2:-}"
1212
ssm_load_target_context "${target_kind}" "${target_name}"
1313
ssm_systemctl "${SSM_ACTIVE_SCOPE}" enable "${SSM_ACTIVE_UNIT}"
14+
printf 'enabled=%s\n' "${SSM_ACTIVE_UNIT}"
15+
ssm_print_unit_summary "${SSM_ACTIVE_NAME}" "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}"
1416
}

scripts/bash/systemd-service-manager/commands/install.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ ssm_cmd_install() {
3737
mkdir -p "$(ssm_unit_dir_for_scope "${scope}")"
3838
cp "${service_unit_file}" "$(ssm_unit_dir_for_scope "${scope}")/"
3939
ssm_daemon_reload "${scope}"
40+
printf 'installed=%s\n' "$(ssm_service_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
4041
if [[ "${SSM_CLI_START_AFTER_INSTALL}" == "1" ]]; then
4142
ssm_systemctl "${scope}" start "$(ssm_service_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
43+
printf 'started=%s\n' "$(ssm_service_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
4244
fi
45+
ssm_print_unit_summary "${SSM_RESOLVED_TARGET_NAME}" "${scope}" "$(ssm_service_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
4346
;;
4447
timer)
4548
ssm_parse_timer_config "${project_dir}" "${SSM_RESOLVED_TARGET_NAME}"
@@ -81,9 +84,12 @@ ssm_cmd_install() {
8184
cp "${task_unit_file}" "$(ssm_unit_dir_for_scope "${scope}")/"
8285
cp "${timer_unit_file}" "$(ssm_unit_dir_for_scope "${scope}")/"
8386
ssm_daemon_reload "${scope}"
87+
printf 'installed=%s\n' "$(ssm_timer_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
8488
if [[ "${SSM_CLI_START_AFTER_INSTALL}" == "1" ]]; then
8589
ssm_systemctl "${scope}" start "$(ssm_timer_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
90+
printf 'started=%s\n' "$(ssm_timer_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
8691
fi
92+
ssm_print_unit_summary "${SSM_RESOLVED_TARGET_NAME}" "${scope}" "$(ssm_timer_unit_name "${SSM_RESOLVED_TARGET_NAME}")"
8793
;;
8894
*)
8995
ssm_die "Unknown install target kind: ${target_kind}"

scripts/bash/systemd-service-manager/commands/restart.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ ssm_cmd_restart() {
1111
local target_name="${2:-}"
1212
ssm_load_target_context "${target_kind}" "${target_name}"
1313
ssm_systemctl "${SSM_ACTIVE_SCOPE}" restart "${SSM_ACTIVE_UNIT}"
14+
printf 'restarted=%s\n' "${SSM_ACTIVE_UNIT}"
15+
ssm_print_unit_summary "${SSM_ACTIVE_NAME}" "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}"
1416
}

scripts/bash/systemd-service-manager/commands/start.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ ssm_cmd_start() {
1111
local target_name="${2:-}"
1212
ssm_load_target_context "${target_kind}" "${target_name}"
1313
ssm_systemctl "${SSM_ACTIVE_SCOPE}" start "${SSM_ACTIVE_UNIT}"
14+
printf 'started=%s\n' "${SSM_ACTIVE_UNIT}"
15+
ssm_print_unit_summary "${SSM_ACTIVE_NAME}" "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}"
1416
}

scripts/bash/systemd-service-manager/commands/status.sh

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,5 @@ ssm_cmd_status() {
1010
local target_kind="${1:-}"
1111
local target_name="${2:-}"
1212
ssm_load_target_context "${target_kind}" "${target_name}"
13-
14-
local enabled_state=""
15-
local active_state=""
16-
enabled_state="$(ssm_systemctl "${SSM_ACTIVE_SCOPE}" is-enabled "${SSM_ACTIVE_UNIT}" 2>/dev/null || true)"
17-
active_state="$(ssm_systemctl "${SSM_ACTIVE_SCOPE}" is-active "${SSM_ACTIVE_UNIT}" 2>/dev/null || true)"
18-
19-
printf 'name=%s\n' "${SSM_ACTIVE_NAME}"
20-
printf 'unit=%s\n' "${SSM_ACTIVE_UNIT}"
21-
printf 'scope=%s\n' "${SSM_ACTIVE_SCOPE}"
22-
printf 'installed=%s\n' "$(ssm_is_unit_installed "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}")"
23-
printf 'enabled=%s\n' "${enabled_state:-unknown}"
24-
printf 'active=%s\n' "${active_state:-unknown}"
13+
ssm_print_unit_summary "${SSM_ACTIVE_NAME}" "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}"
2514
}

scripts/bash/systemd-service-manager/commands/stop.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ ssm_cmd_stop() {
1111
local target_name="${2:-}"
1212
ssm_load_target_context "${target_kind}" "${target_name}"
1313
ssm_systemctl "${SSM_ACTIVE_SCOPE}" stop "${SSM_ACTIVE_UNIT}"
14+
printf 'stopped=%s\n' "${SSM_ACTIVE_UNIT}"
15+
ssm_print_unit_summary "${SSM_ACTIVE_NAME}" "${SSM_ACTIVE_SCOPE}" "${SSM_ACTIVE_UNIT}"
1416
}

scripts/bash/systemd-service-manager/lib/systemd.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,43 @@ ssm_load_target_context() {
142142
esac
143143
}
144144

145+
# 读取当前 unit 的 installed/enabled/active 摘要,供状态展示和成功提示复用。
146+
ssm_collect_unit_summary() {
147+
local scope="$1"
148+
local unit_name="$2"
149+
150+
SSM_SUMMARY_INSTALLED="$(ssm_is_unit_installed "${scope}" "${unit_name}")"
151+
SSM_SUMMARY_ENABLED="$(ssm_systemctl "${scope}" is-enabled "${unit_name}" 2>/dev/null || true)"
152+
SSM_SUMMARY_ACTIVE="$(ssm_systemctl "${scope}" is-active "${unit_name}" 2>/dev/null || true)"
153+
154+
[[ -n "${SSM_SUMMARY_ENABLED}" ]] || SSM_SUMMARY_ENABLED="unknown"
155+
[[ -n "${SSM_SUMMARY_ACTIVE}" ]] || SSM_SUMMARY_ACTIVE="unknown"
156+
}
157+
158+
# 输出统一状态摘要,并附加人类可读提示。
159+
ssm_print_unit_summary() {
160+
local name="$1"
161+
local scope="$2"
162+
local unit_name="$3"
163+
164+
ssm_collect_unit_summary "${scope}" "${unit_name}"
165+
166+
printf 'name=%s\n' "${name}"
167+
printf 'unit=%s\n' "${unit_name}"
168+
printf 'scope=%s\n' "${scope}"
169+
printf 'installed=%s\n' "${SSM_SUMMARY_INSTALLED}"
170+
printf 'enabled=%s\n' "${SSM_SUMMARY_ENABLED}"
171+
printf 'active=%s\n' "${SSM_SUMMARY_ACTIVE}"
172+
173+
if [[ "${SSM_SUMMARY_ENABLED}" == "disabled" && "${SSM_SUMMARY_ACTIVE}" != "inactive" ]]; then
174+
printf 'note=unit 已启动但未启用开机自启\n'
175+
fi
176+
177+
if [[ "${SSM_SUMMARY_ACTIVE}" == "activating" ]]; then
178+
printf 'note=unit 正在启动中\n'
179+
fi
180+
}
181+
145182
# 判断当前命令是否需要在非 root 下自动提权。
146183
ssm_should_auto_elevate() {
147184
local command="$1"

scripts/bash/systemd-service-manager/tests/install.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ describe('install command', () => {
162162
)
163163

164164
expect(result.exitCode).toBe(0)
165+
expect(result.stdout).toContain('installed=myapp-api.service')
166+
expect(result.stdout).toContain('started=myapp-api.service')
165167
expect(fs.readFileSync(systemctlLog, 'utf8')).toContain('daemon-reload')
166168
expect(fs.readFileSync(systemctlLog, 'utf8')).toContain('start myapp-api.service')
167169
})

scripts/bash/systemd-service-manager/tests/lifecycle.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,44 @@ describe('lifecycle commands', () => {
181181
expect(fs.readFileSync(sudoLog, 'utf8')).toContain(workspace.sourceEntry)
182182
expect(fs.readFileSync(systemctlLog, 'utf8')).toContain('start myapp-api.service')
183183
})
184+
185+
it('status explains activating but not enabled units', async () => {
186+
const workspace = createWorkspace()
187+
workspaces.push(workspace)
188+
189+
const systemctlLog = path.join(workspace.root, 'systemctl.log')
190+
installMockCommand(
191+
workspace,
192+
'systemctl',
193+
[
194+
'#!/usr/bin/env bash',
195+
'printf "%s\\n" "$*" >>"${SSM_SYSTEMCTL_LOG}"',
196+
'if [[ "$1" == "is-enabled" ]]; then printf "disabled\\n"; fi',
197+
'if [[ "$1" == "is-active" ]]; then printf "activating\\n"; fi',
198+
'exit 0',
199+
].join('\n'),
200+
)
201+
202+
const projectRoot = path.join(
203+
workspace.managerHome,
204+
'tests',
205+
'fixtures',
206+
'project-basic',
207+
)
208+
209+
const result = await runSource(
210+
workspace,
211+
['status', 'api', '--project', projectRoot],
212+
{
213+
SSM_TEST_EUID: '0',
214+
SSM_SYSTEMCTL_LOG: systemctlLog,
215+
},
216+
)
217+
218+
expect(result.exitCode).toBe(0)
219+
expect(result.stdout).toContain('enabled=disabled')
220+
expect(result.stdout).toContain('active=activating')
221+
expect(result.stdout).toContain('note=unit 已启动但未启用开机自启')
222+
expect(result.stdout).toContain('note=unit 正在启动中')
223+
})
184224
})

0 commit comments

Comments
 (0)