@@ -30,22 +30,34 @@ ssm_cmd_install() {
3030 ssm_collect_env_entries_for_service " ${project_dir} " " ${SSM_RESOLVED_TARGET_NAME} "
3131 local env_block
3232 env_block=" $( ssm_render_environment_lines) "
33- local service_unit_file=" ${render_dir} /$( ssm_service_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
33+ local service_unit_name
34+ service_unit_name=" $( ssm_service_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
35+ local service_unit_file=" ${render_dir} /${service_unit_name} "
36+ local destination_unit_file
37+ destination_unit_file=" $( ssm_unit_dir_for_scope " ${scope} " ) /${service_unit_name} "
38+ local previous_active_state
39+ previous_active_state=" $( ssm_unit_activity_state " ${scope} " " ${service_unit_name} " ) "
3440 ssm_render_service_unit " ${source_file} " " ${env_block} " > " ${service_unit_file} "
3541 ssm_verify_unit_file " ${service_unit_file} " || ssm_die " systemd-analyze verify failed for ${service_unit_file} "
3642 if [[ " ${SSM_CLI_DRY_RUN} " == " 1" ]]; then
3743 printf ' %s\n' " $( basename " ${service_unit_file} " ) "
3844 return 0
3945 fi
40- mkdir -p " $( ssm_unit_dir_for_scope " ${scope} " ) "
41- cp " ${service_unit_file} " " $( ssm_unit_dir_for_scope " ${scope} " ) /"
46+ ssm_write_unit_file " ${service_unit_file} " " ${destination_unit_file} "
4247 ssm_daemon_reload " ${scope} "
43- printf ' installed =%s\n' " $( ssm_service_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
48+ printf ' %s =%s\n' " ${SSM_WRITE_STATUS} " " ${service_unit_name} "
4449 if [[ " ${SSM_CLI_START_AFTER_INSTALL} " == " 1" ]]; then
45- ssm_systemctl " ${scope} " start " $( ssm_service_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
46- printf ' started=%s\n' " $( ssm_service_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
50+ if ssm_is_running_like_state " ${previous_active_state} " ; then
51+ ssm_systemctl " ${scope} " restart " ${service_unit_name} "
52+ printf ' restarted=%s\n' " ${service_unit_name} "
53+ else
54+ ssm_systemctl " ${scope} " start " ${service_unit_name} "
55+ printf ' started=%s\n' " ${service_unit_name} "
56+ fi
57+ elif [[ " ${SSM_WRITE_STATUS} " == " updated" ]] && ssm_is_running_like_state " ${previous_active_state} " ; then
58+ printf ' note=配置已更新,运行中的 unit 需要 restart 才会生效\n'
4759 fi
48- ssm_print_unit_summary " ${SSM_RESOLVED_TARGET_NAME} " " ${scope} " " $( ssm_service_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
60+ ssm_print_unit_summary " ${SSM_RESOLVED_TARGET_NAME} " " ${scope} " " ${service_unit_name} "
4961 ;;
5062 timer)
5163 ssm_parse_timer_config " ${project_dir} " " ${SSM_RESOLVED_TARGET_NAME} "
@@ -60,7 +72,15 @@ ssm_cmd_install() {
6072 local task_unit_name
6173 task_unit_name=" $( ssm_timer_task_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
6274 local task_unit_file=" ${render_dir} /${task_unit_name} "
63- local timer_unit_file=" ${render_dir} /$( ssm_timer_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
75+ local timer_unit_name
76+ timer_unit_name=" $( ssm_timer_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
77+ local timer_unit_file=" ${render_dir} /${timer_unit_name} "
78+ local destination_task_unit_file
79+ destination_task_unit_file=" $( ssm_unit_dir_for_scope " ${scope} " ) /${task_unit_name} "
80+ local destination_timer_unit_file
81+ destination_timer_unit_file=" $( ssm_unit_dir_for_scope " ${scope} " ) /${timer_unit_name} "
82+ local previous_active_state
83+ previous_active_state=" $( ssm_unit_activity_state " ${scope} " " ${timer_unit_name} " ) "
6484 local task_exec_command=" "
6585
6686 if [[ " ${TARGET_TYPE} " == " service" ]]; then
@@ -86,16 +106,25 @@ ssm_cmd_install() {
86106 return 0
87107 fi
88108
89- mkdir -p " $( ssm_unit_dir_for_scope " ${scope} " ) "
90- cp " ${task_unit_file} " " $( ssm_unit_dir_for_scope " ${scope} " ) /"
91- cp " ${timer_unit_file} " " $( ssm_unit_dir_for_scope " ${scope} " ) /"
109+ ssm_write_unit_file " ${task_unit_file} " " ${destination_task_unit_file} "
110+ local task_write_status=" ${SSM_WRITE_STATUS} "
111+ ssm_write_unit_file " ${timer_unit_file} " " ${destination_timer_unit_file} "
112+ local timer_write_status=" ${SSM_WRITE_STATUS} "
92113 ssm_daemon_reload " ${scope} "
93- printf ' installed=%s\n' " $( ssm_timer_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
114+ printf ' %s=%s\n' " ${timer_write_status} " " ${timer_unit_name} "
115+ printf ' %s=%s\n' " ${task_write_status} " " ${task_unit_name} "
94116 if [[ " ${SSM_CLI_START_AFTER_INSTALL} " == " 1" ]]; then
95- ssm_systemctl " ${scope} " start " $( ssm_timer_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
96- printf ' started=%s\n' " $( ssm_timer_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
117+ if ssm_is_running_like_state " ${previous_active_state} " ; then
118+ ssm_systemctl " ${scope} " restart " ${timer_unit_name} "
119+ printf ' restarted=%s\n' " ${timer_unit_name} "
120+ else
121+ ssm_systemctl " ${scope} " start " ${timer_unit_name} "
122+ printf ' started=%s\n' " ${timer_unit_name} "
123+ fi
124+ elif [[ " ${timer_write_status} " == " updated" ]] && ssm_is_running_like_state " ${previous_active_state} " ; then
125+ printf ' note=配置已更新,运行中的 unit 需要 restart 才会生效\n'
97126 fi
98- ssm_print_unit_summary " ${SSM_RESOLVED_TARGET_NAME} " " ${scope} " " $( ssm_timer_unit_name " ${SSM_RESOLVED_TARGET_NAME} " ) "
127+ ssm_print_unit_summary " ${SSM_RESOLVED_TARGET_NAME} " " ${scope} " " ${timer_unit_name} "
99128 ;;
100129 * )
101130 ssm_die " Unknown install target kind: ${target_kind} "
0 commit comments