Skip to content

Commit 2e110c7

Browse files
authored
Merge pull request #9 from thin-edge/fix-delete-project-handling
fix(uninstall): support removing project by just removing the active-project package
2 parents 6140465 + 692ed42 commit 2e110c7

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

src/sm-plugin/nodered

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ activate_project() {
126126
if [ "$(echo "$response" | jq -r '.active')" = "$name" ]; then
127127
log "\nSuccessfully activated project. name=$name"
128128
else
129-
log "\nFailed however sometimes the service needs to be restated before the setting is active. response=$response"
129+
log "\nFailed however sometimes the service needs to be restarted before the setting is active. response=$response"
130130
fi
131131

132132
#
@@ -148,7 +148,7 @@ activate_project() {
148148
exit "$EXIT_FAILURE"
149149
fi
150150

151-
log "node-red project was activate successfully. name=$CURRENT_PROJECT"
151+
log "node-red project was activated successfully. name=$CURRENT_PROJECT"
152152
}
153153

154154
install_project_from_tarball() {
@@ -300,7 +300,6 @@ case "$COMMAND" in
300300
exit 0
301301
fi
302302

303-
check_project_mode
304303

305304
log "Installing ${PLUGIN_NAME} package: ${MODULE_NAME}:${MODULE_VERSION}"
306305

@@ -349,17 +348,37 @@ case "$COMMAND" in
349348

350349
remove)
351350
# don't fail if it does not exist
351+
352+
# Support removing the project when the user removes the
353+
# active project.
354+
# This might be changed to actually just disable the project in node-red
355+
# but the API is unclear how to do this. So for now just remove the project.
356+
# The user can always just disable the node-red service if they want to disable
357+
# the project temporarily
358+
if [ "$MODULE_NAME" = "$PROP_ACTIVE_PROJECT" ]; then
359+
log "Setting project name from active project property: name=$MODULE_VERSION"
360+
MODULE_NAME="$MODULE_VERSION"
361+
# we don't know really care about the module version in this case
362+
MODULE_VERSION=""
363+
fi
364+
352365
log "Removing ${PLUGIN_NAME} package: ${MODULE_NAME}:${MODULE_VERSION}"
353366

354367
PROJECT_DIR="$NODERED_DIR/projects/$MODULE_NAME"
355368
log "Removing node-red project. path=$PROJECT_DIR"
356369

357-
if grep -F "$MODULE_NAME" "$ACTIVE_PROJECT_FILE" -q; then
370+
requires_restart=0
371+
if grep -F "$MODULE_NAME" "$ACTIVE_PROJECT_FILE" -q >/dev/null 2>&1; then
358372
rm -f "$ACTIVE_PROJECT_FILE"
373+
requires_restart=1
359374
fi
360375

361-
# TODO: Should the project be official deregistered from nodered before it is removed?
362376
rm -Rf "$PROJECT_DIR"
377+
378+
if [ "$requires_restart" = "1" ]; then
379+
restart_node_red
380+
sleep 5
381+
fi
363382
;;
364383

365384
prepare)
@@ -377,6 +396,16 @@ case "$COMMAND" in
377396
restart_node_red
378397
sleep 5
379398
fi
399+
400+
# Check if the api is reachable as this is an indication if it is running or not
401+
if ! curl --silent -H "Content-Type: application/json" -XGET "$NODERED_API/projects" 2>/dev/null; then
402+
log "Starting node-red as the endpoint was not reachable"
403+
restart_node_red
404+
sleep 5
405+
fi
406+
407+
# Verify that the project feature is enabled
408+
check_project_mode
380409
;;
381410

382411
finalize)

tests/main/management.robot

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ Install node-red from github url
1212
${operation}= Cumulocity.Install Software nodered-demo,latest::nodered,${binary_url} active-project,nodered-demo::nodered
1313
Operation Should Be SUCCESSFUL ${operation}
1414
Cumulocity.Device Should Have Installed Software nodered-demo
15+
Cumulocity.Should Have Services name=nodered-temperature-flow status=up service_type=nodered
1516

1617
Install node-red from tarball
1718
${binary_url}= Cumulocity.Create Inventory Binary nodered-demo nodered-project file=${CURDIR}/../testdata/nodered-demo__main@c7c6b5d.tar.gz
1819
${operation}= Cumulocity.Install Software nodered-demo,latest::nodered,${binary_url} active-project,nodered-demo::nodered
1920
Operation Should Be SUCCESSFUL ${operation}
2021
Cumulocity.Device Should Have Installed Software nodered-demo,0.0.1
21-
22+
Cumulocity.Should Have Services name=nodered-temperature-flow status=up service_type=nodered
2223

2324
Uninstall node-red project via Cumulocity
2425
# Skip Missing Uninstall software keyword
@@ -27,3 +28,31 @@ Uninstall node-red project via Cumulocity
2728
${mo}= Cumulocity.Device Should Have Fragments c8y_SoftwareList
2829
Log ${mo}
2930
Should Not Contain ${mo} nodered-demo
31+
Cumulocity.Should Have Services name=nodered-temperature-flow status=down service_type=nodered
32+
33+
Uninstall node-red project via Cumulocity using the active project
34+
# install first
35+
${binary_url}= Cumulocity.Create Inventory Binary nodered-demo nodered-project file=${CURDIR}/../testdata/nodered-demo__main@c7c6b5d.tar.gz
36+
${operation}= Cumulocity.Install Software nodered-demo,latest::nodered,${binary_url} active-project,nodered-demo::nodered
37+
Operation Should Be SUCCESSFUL ${operation}
38+
Cumulocity.Device Should Have Installed Software nodered-demo,0.0.1
39+
Cumulocity.Should Have Services name=nodered-temperature-flow status=up service_type=nodered
40+
41+
# then remove
42+
${operation}= Cumulocity.Create Operation fragments={"c8y_SoftwareUpdate":[{"name":"active-project","version":"nodered-demo::nodered","url":"","action":"delete"}]} description=Remove nodered-demo package
43+
Operation Should Be SUCCESSFUL ${operation}
44+
${mo}= Cumulocity.Device Should Have Fragments c8y_SoftwareList
45+
Log ${mo}
46+
Should Not Contain ${mo} nodered-demo
47+
Should Not Contain ${mo} active-project
48+
Cumulocity.Should Have Services name=nodered-temperature-flow status=down service_type=nodered
49+
50+
Install new project when nodered is not running
51+
${operation}= Cumulocity.Execute Shell Command text=sudo systemctl stop nodered
52+
Operation Should Be SUCCESSFUL ${operation}
53+
54+
${binary_url}= Cumulocity.Create Inventory Binary nodered-demo nodered-project file=${CURDIR}/../testdata/nodered-demo__main@c7c6b5d.tar.gz
55+
${operation}= Cumulocity.Install Software nodered-demo,latest::nodered,${binary_url} active-project,nodered-demo::nodered
56+
Operation Should Be SUCCESSFUL ${operation}
57+
Cumulocity.Device Should Have Installed Software nodered-demo,0.0.1
58+
Cumulocity.Should Have Services name=nodered-temperature-flow status=up service_type=nodered

tests/main/telemetry.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ node-red status should publish to health endpoint
2727
Custom Setup
2828
Set Main Device
2929
${binary_url}= Cumulocity.Create Inventory Binary nodered-demo nodered-project file=${CURDIR}/../testdata/nodered-demo.cfg
30-
${operation}= Cumulocity.Install Software nodered-demo,latest::nodered,${binary_url}
30+
${operation}= Cumulocity.Install Software nodered-demo,latest::nodered,${binary_url} active-project,nodered-demo::nodered
3131
Operation Should Be SUCCESSFUL ${operation}

0 commit comments

Comments
 (0)