Skip to content

Commit d270a98

Browse files
committed
poc: prepare and finalize states
1 parent 9daac0e commit d270a98

File tree

4 files changed

+138
-11
lines changed

4 files changed

+138
-11
lines changed

tests/RobotFramework/tests/cumulocity/configuration/composite_config_update.toml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Custom config_update operation
22
# - use the type as the path for the target file
33
# - download the source url to the target
4+
#
5+
# The `workdir` can be used to exchange any transient metadata between the states
6+
#
47
operation = "config_update"
58

69
[init]
@@ -12,24 +15,38 @@ action = "proceed"
1215
on_success = "executing"
1316

1417
[executing]
15-
action = "proceed"
18+
script = "/etc/tedge/operations/workdir.sh"
1619
on_success = "download"
1720

1821
[download]
19-
script = "curl --fail --show-error --output /tmp/config_update_${.topic.cmd_id} ${.payload.tedgeUrl}"
22+
script = "curl --fail --show-error --output ${.payload.workdir}/config_update_${.topic.cmd_id} ${.payload.tedgeUrl}"
2023
on_success = "update"
24+
on_error = "failed"
2125

2226
[update]
23-
script = "sudo /usr/share/tedge/config-plugins/file set ${.payload.type} /tmp/config_update_${.topic.cmd_id}"
27+
script = "sudo /usr/share/tedge/config-plugins/file set ${.payload.type} ${.payload.workdir}/config_update_${.topic.cmd_id}"
2428
on_success = "apply"
29+
on_error = "rollback"
2530

2631
[apply]
27-
background_script = "sudo /usr/share/tedge/config-plugins/file apply ${.payload.type}"
32+
background_script = "sudo /usr/share/tedge/config-plugins/file apply ${.payload.type} ${.payload.workdir}"
2833
on_exec = "verify"
34+
on_error = "rollback"
2935

3036
[verify]
31-
script = "sudo /usr/share/tedge/config-plugins/file verify ${.payload.type}"
37+
script = "sudo /usr/share/tedge/config-plugins/file verify ${.payload.type} ${.payload.workdir}"
38+
on_success = "commit"
39+
on_error = "rollback"
40+
41+
[finalize]
42+
script = "sudo /usr/share/tedge/config-plugins/file finalize ${.payload.type} ${.payload.workdir}"
3243
on_success = "successful"
44+
on_error = "successful"
45+
46+
[rollback]
47+
script = "sudo /usr/share/tedge/config-plugins/file rollback ${.payload.type} ${.payload.workdir}"
48+
on_success = "failed"
49+
on_error = "failed"
3350

3451
[successful]
3552
action = "cleanup"

tests/RobotFramework/tests/cumulocity/configuration/config_operation_plugins.robot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Demo Test
9292
... /usr/share/tedge/config-plugins/file
9393
Execute Command chmod +x /usr/share/tedge/config-plugins/file
9494

95+
ThinEdgeIO.Transfer To Device ${CURDIR}/scripts/workdir.sh /etc/tedge/operations/workdir.sh
96+
Execute Command chmod +x /etc/tedge/operations/workdir.sh
9597
ThinEdgeIO.Transfer To Device ${CURDIR}/composite_config_update.toml /etc/tedge/operations/
9698

9799
Execute Command touch /etc/tedge/test.conf

tests/RobotFramework/tests/cumulocity/configuration/plugins/file

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2442
set_config() {
2543
target_config="$2"
2644
mv "$target_config" "$CONFIG_PATH"
45+
46+
echo "Config updated" >&2
2747
}
2848

2949
apply_config() {
50+
type="$1"
51+
workdir="$2"
52+
3053
systemctl restart tedge-agent
3154
}
3255

3356
verify_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

37117
main() {
@@ -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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
echo ":::begin-tedge:::"
5+
tmpdir=$(mktemp -d)
6+
echo "{\"workdir\": \"$tmpdir\"}"
7+
echo ":::end-tedge:::"

0 commit comments

Comments
 (0)