@@ -14,21 +14,70 @@ ssm_cmd_install() {
1414
1515 local project_dir
1616 project_dir=" $( ssm_find_project_dir " ${SSM_CLI_PROJECT_DIR:- } " ) "
17+ local render_dir
18+ render_dir=" $( mktemp -d) "
19+ trap ' rm -rf ' " '" ${render_dir} " '" ' ' RETURN
20+
21+ local source_file=" "
22+ local scope=" system"
1723
1824 case " ${target_kind} " in
1925 service)
2026 ssm_parse_service_config " ${project_dir} " " ${target_name} "
2127 ssm_require_safe_name " UNIT_PREFIX" " ${UNIT_PREFIX} "
22- printf ' %s\n' " $( ssm_service_unit_name " ${target_name} " ) "
28+ source_file=" $( ssm_service_config_path " ${project_dir} " " ${target_name} " ) "
29+ scope=" ${SSM_SERVICE_SCOPE} "
30+ local service_unit_file=" ${render_dir} /$( ssm_service_unit_name " ${target_name} " ) "
31+ ssm_render_service_unit " ${source_file} " > " ${service_unit_file} "
32+ ssm_verify_unit_file " ${service_unit_file} " || ssm_die " systemd-analyze verify failed for ${service_unit_file} "
33+ if [[ " ${SSM_CLI_DRY_RUN} " == " 1" ]]; then
34+ printf ' %s\n' " $( basename " ${service_unit_file} " ) "
35+ return 0
36+ fi
37+ mkdir -p " $( ssm_unit_dir_for_scope " ${scope} " ) "
38+ cp " ${service_unit_file} " " $( ssm_unit_dir_for_scope " ${scope} " ) /"
39+ ssm_daemon_reload " ${scope} "
2340 ;;
2441 timer)
2542 ssm_parse_timer_config " ${project_dir} " " ${target_name} "
2643 ssm_require_safe_name " UNIT_PREFIX" " ${UNIT_PREFIX} "
27- ssm_resolve_schedule " ${SCHEDULE} " > /dev/null
28- printf ' %s\n' " $( ssm_timer_unit_name " ${target_name} " ) "
29- if [[ " ${TARGET_TYPE} " == " task" || " ${TARGET_TYPE} " == " service" ]]; then
30- printf ' %s\n' " $( ssm_timer_task_unit_name " ${target_name} " ) "
44+ source_file=" $( ssm_timer_config_path " ${project_dir} " " ${target_name} " ) "
45+ scope=" ${SSM_TIMER_SCOPE} "
46+ local schedule_block
47+ schedule_block=" $( ssm_resolve_schedule " ${SCHEDULE} " ) "
48+ local task_unit_name
49+ task_unit_name=" $( ssm_timer_task_unit_name " ${target_name} " ) "
50+ local task_unit_file=" ${render_dir} /${task_unit_name} "
51+ local timer_unit_file=" ${render_dir} /$( ssm_timer_unit_name " ${target_name} " ) "
52+ local task_exec_command=" "
53+
54+ if [[ " ${TARGET_TYPE} " == " service" ]]; then
55+ local target_unit
56+ target_unit=" $( ssm_service_unit_name " ${TARGET_NAME} " ) "
57+ if [[ " ${scope} " == " user" ]]; then
58+ task_exec_command=" /usr/bin/env bash -lc 'systemctl --user ${ACTION:- restart} ${target_unit} '"
59+ else
60+ task_exec_command=" /usr/bin/env bash -lc 'systemctl ${ACTION:- restart} ${target_unit} '"
61+ fi
62+ else
63+ task_exec_command=" ${COMMAND} "
3164 fi
65+
66+ ssm_render_task_service_unit " ${source_file} " " ${task_exec_command} " > " ${task_unit_file} "
67+ ssm_render_timer_unit " ${source_file} " " ${task_unit_name} " " ${schedule_block} " > " ${timer_unit_file} "
68+ ssm_verify_unit_file " ${task_unit_file} " || ssm_die " systemd-analyze verify failed for ${task_unit_file} "
69+ ssm_verify_unit_file " ${timer_unit_file} " || ssm_die " systemd-analyze verify failed for ${timer_unit_file} "
70+
71+ if [[ " ${SSM_CLI_DRY_RUN} " == " 1" ]]; then
72+ printf ' %s\n' " $( basename " ${timer_unit_file} " ) "
73+ printf ' %s\n' " $( basename " ${task_unit_file} " ) "
74+ return 0
75+ fi
76+
77+ mkdir -p " $( ssm_unit_dir_for_scope " ${scope} " ) "
78+ cp " ${task_unit_file} " " $( ssm_unit_dir_for_scope " ${scope} " ) /"
79+ cp " ${timer_unit_file} " " $( ssm_unit_dir_for_scope " ${scope} " ) /"
80+ ssm_daemon_reload " ${scope} "
3281 ;;
3382 * )
3483 ssm_die " Unknown install target kind: ${target_kind} "
0 commit comments