@@ -9,7 +9,12 @@ usage() {
99 echo " Commands:"
1010 echo " list List all config types supported by this plugin"
1111 echo " get <type> Print the config for the specified type to stdout"
12+ echo " prepare <type> <workdir> Backup current config and prepare for update"
1213 echo " set <type> <new-config-path> Update the config for the specified type from the new config path"
14+ echo " apply <type> Apply the new configuration (restart service)"
15+ echo " verify <type> Verify the new configuration is working"
16+ echo " rollback <type> <workdir> Restore config from backup"
17+ echo " commit <type> <workdir> Finalize the update (cleanup backup)"
1318 exit 1
1419}
1520
@@ -21,17 +26,92 @@ get_config() {
2126 cat " $CONFIG_PATH "
2227}
2328
29+ prepare_config () {
30+ type=" $1 "
31+ workdir=" $2 "
32+
33+ # Backup current config
34+ cp " $CONFIG_PATH " " ${workdir} /config.backup"
35+
36+ # Capture current PID to verify restart later
37+ systemctl show tedge-agent --property=MainPID --value > " ${workdir} /pre_restart_pid.txt"
38+
39+ echo " Config backed up to ${workdir} /config.backup" >&2
40+ }
41+
2442set_config () {
2543 target_config=" $2 "
2644 mv " $target_config " " $CONFIG_PATH "
45+
46+ echo " Config updated" >&2
2747}
2848
2949apply_config () {
50+ type=" $1 "
51+ workdir=" $2 "
52+
3053 systemctl restart tedge-agent
3154}
3255
3356verify_config () {
34- systemctl is-active tedge-agent
57+ type=" $1 "
58+ workdir=" $2 "
59+
60+ # Check if service is active
61+ if ! systemctl is-active tedge-agent > /dev/null; then
62+ echo " Service tedge-agent is not active" >&2
63+ exit 1
64+ fi
65+
66+ # Verify restart by comparing PID
67+ if [ ! -f " ${workdir} /pre_restart_pid.txt" ]; then
68+ echo " Error: Missing pre-restart PID file" >&2
69+ exit 1
70+ fi
71+
72+ pre_pid=$( cat " ${workdir} /pre_restart_pid.txt" )
73+ current_pid=$( systemctl show tedge-agent --property=MainPID --value)
74+
75+ if [ " $pre_pid " = " $current_pid " ]; then
76+ echo " Error: Service PID unchanged ($pre_pid ), restart may not have occurred" >&2
77+ exit 1
78+ fi
79+
80+ echo " Restart verified: PID changed from $pre_pid to $current_pid " >&2
81+ }
82+
83+ finalize_config () {
84+ echo " Configuration update finalized successfully"
85+
86+ # No need to cleanup the backup/metadata files in the workdir,
87+ # as they are cleaned up automatically by the agent on operation completion.
88+ }
89+
90+ rollback_config () {
91+ type=" $1 "
92+ workdir=" $2 "
93+
94+ backup_file=" ${workdir} /config.backup"
95+
96+ if [ ! -f " $backup_file " ]; then
97+ echo " Error: Backup file not found at $backup_file " >&2
98+ exit 1
99+ fi
100+
101+ echo " Rolling back to previous configuration..." >&2
102+
103+ # Restore backup
104+ cp " $backup_file " " $CONFIG_PATH "
105+
106+ # Restart service with old config
107+ systemctl restart tedge-agent
108+
109+ # Verify rollback worked
110+ if systemctl is-active tedge-agent > /dev/null; then
111+ echo " Rollback successful, service is running" >&2
112+ else
113+ echo " Warning: Service is not active after rollback" >&2
114+ fi
35115}
36116
37117main () {
@@ -53,27 +133,48 @@ main() {
53133 fi
54134 get_config " $@ "
55135 ;;
136+ prepare)
137+ if [ $# -lt 2 ]; then
138+ echo " Error: 'prepare' command requires <type> and <workdir> arguments" >&2
139+ exit 1
140+ fi
141+ prepare_config " $@ "
142+ ;;
56143 set)
57- if [ $# -lt 1 ]; then
144+ if [ $# -lt 2 ]; then
58145 echo " Error: 'set' command requires <type> and <new-config-path> arguments" >&2
59146 exit 1
60147 fi
61148 set_config " $@ "
62149 ;;
63150 apply)
64- if [ $# -lt 1 ]; then
65- echo " Error: 'apply' command requires a <type> argument " >&2
151+ if [ $# -lt 2 ]; then
152+ echo " Error: 'apply' command requires <type> and <workdir> arguments " >&2
66153 exit 1
67154 fi
68155 apply_config " $@ "
69156 ;;
70157 verify)
71- if [ $# -lt 1 ]; then
72- echo " Error: 'verify' command requires a <type> argument " >&2
158+ if [ $# -lt 2 ]; then
159+ echo " Error: 'verify' command requires <type> and <workdir> arguments " >&2
73160 exit 1
74161 fi
75162 verify_config " $@ "
76163 ;;
164+ finalize)
165+ if [ $# -lt 2 ]; then
166+ echo " Error: 'commit' command requires <type> and <workdir> arguments" >&2
167+ exit 1
168+ fi
169+ finalize_config " $@ "
170+ ;;
171+ rollback)
172+ if [ $# -lt 2 ]; then
173+ echo " Error: 'rollback' command requires <type> and <workdir> arguments" >&2
174+ exit 1
175+ fi
176+ rollback_config " $@ "
177+ ;;
77178 * )
78179 echo " Error: Unknown command '$command '" >&2
79180 usage
0 commit comments