From e4684f5448cfea4e295c0a58e69791f7050b7174 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Mon, 22 Jul 2024 16:35:39 +0200 Subject: [PATCH 1/5] ns-plug: add script to update the packages Features: - all changes are written to syslog - accept the 'force' flag to gather updates from stable channel regardless of subscription status --- packages/ns-plug/Makefile | 1 + .../files/40_ns-plug_automatic_updates | 7 ++ packages/ns-plug/files/update-packages | 73 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 packages/ns-plug/files/40_ns-plug_automatic_updates create mode 100644 packages/ns-plug/files/update-packages diff --git a/packages/ns-plug/Makefile b/packages/ns-plug/Makefile index ac8aabb68..6d9fee73e 100644 --- a/packages/ns-plug/Makefile +++ b/packages/ns-plug/Makefile @@ -79,6 +79,7 @@ define Package/ns-plug/install $(INSTALL_BIN) ./files/register $(1)/usr/sbin $(INSTALL_BIN) ./files/unregister $(1)/usr/sbin $(INSTALL_BIN) ./files/subscription-info $(1)/usr/sbin + $(INSTALL_BIN) ./files/update-packages $(1)/usr/sbin $(INSTALL_BIN) ./files/inventory $(1)/usr/sbin $(INSTALL_BIN) ./files/ns-download $(1)/usr/bin $(INSTALL_BIN) ./files/20_ns-plug $(1)/etc/uci-defaults diff --git a/packages/ns-plug/files/40_ns-plug_automatic_updates b/packages/ns-plug/files/40_ns-plug_automatic_updates new file mode 100644 index 000000000..ce724d9d4 --- /dev/null +++ b/packages/ns-plug/files/40_ns-plug_automatic_updates @@ -0,0 +1,7 @@ +old_cmd="sleep \$(( RANDOM % 18000 )); /bin/opkg list-upgradable | /usr/bin/cut -f 1 -d ' ' | /usr/bin/xargs -r opkg upgrade" + +# Remove old command from crontab and add new one +if crontab -l | grep -q "$old_cmd"; then + crontab -l | grep -v "$old_cmd" | sort | uniq | crontab - + /usr/libexec/ns-api/schedule-automatic-updates add +fi diff --git a/packages/ns-plug/files/update-packages b/packages/ns-plug/files/update-packages new file mode 100644 index 000000000..f8b455a71 --- /dev/null +++ b/packages/ns-plug/files/update-packages @@ -0,0 +1,73 @@ +#!/bin/bash + +# +# Copyright (C) 2024 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-2.0-only +# + +# +# Update packages and log everything to syslog +# Usage: update-packages [--force-stable] +# --force-stable: force update from stable channel even if the system has a subscription +# + +error_exit() { + echo "$1" | logger -s -t update-packages + exit 1 +} + +cleanup() { + if [ -f /etc/opkg/distfeeds.conf.orig ]; then + echo "Restoring original distfeeds.conf" | logger -s -t update-packages + mv /etc/opkg/distfeeds.conf.orig /etc/opkg/distfeeds.conf || error_exit "Failed to restore distfeeds.conf" + fi +} + +# Check if the force flag is set, default to false +force=0 +if [ "$1" = "--force-stable" ]; then + force=1 + echo "Flag force-stable is set" | logger -s -t update-packages +fi + +channel='dev' +# channel is subscription if "$(uci -q get ns-plug.config.system_id)" is not empty +if [ -n "$(uci -q get ns-plug.config.system_id)" ]; then + channel='subscription' +fi + +# Set up trap to call cleanup function on script exit +trap cleanup EXIT + +# Create temporary opkg configuration +if [ "$force" -eq 1 ]; then + channel='stable' + # Preserve original distfeed.conf + cp /etc/opkg/distfeeds.conf /etc/opkg/distfeeds.conf.orig || error_exit "Failed to backup distfeeds.conf" + + echo "Creating temporary distfeed configuration" | logger -s -t update-packages + # make sure to replace dev with stable in case we are using a dev image + sed 's/dev/stable/g' /rom/etc/opkg/distfeeds.conf > /etc/opkg/distfeeds.conf || error_exit "Failed to create temporary opkg configuration" +fi + +echo "Updating packages from $channel channel" | logger -s -t update-packages + +# Update metadata, make sure to output even if in case of error +output=$(opkg update 2>&1) +status=$? +echo "$output" | logger -s -t update-packages +[ $status -ne 0 ] && error_exit "Failed to update metadata" + +error_count=0 +# Upgrade each package individually and capture output +opkg list-upgradable | cut -f 1 -d ' ' | while read -r package; do + output=$(opkg upgrade "$package" 2>&1) + status=$? + [ $status -ne 0 ] && error_count=$((error_count + 1)) + echo "$output" | logger -s -t update-packages +done + +# Check if there were any errors +[ $error_count -ne 0 ] && error_exit "Failed to upgrade $error_count packages" + +echo "Update successful" | logger -s -t update-packages From 9e12ca3df41950642dab43133a50f38599a195a1 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Tue, 23 Jul 2024 09:14:59 +0200 Subject: [PATCH 2/5] ns-api: use new update command --- packages/ns-api/files/ns.update | 2 +- packages/ns-api/files/schedule-automatic-updates | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ns-api/files/ns.update b/packages/ns-api/files/ns.update index 9bbae898e..9066fcc4b 100755 --- a/packages/ns-api/files/ns.update +++ b/packages/ns-api/files/ns.update @@ -80,7 +80,7 @@ def get_package_updates_lat_check(): def install_package_updates(): try: - out = subprocess.check_output("/usr/sbin/screen -dmS install_package_updates /bin/bash -c \"/bin/opkg list-upgradable | /usr/bin/cut -f 1 -d ' ' | /usr/bin/xargs -r opkg upgrade\"", shell=True) + out = subprocess.check_output("/usr/sbin/screen -dmS install_package_updates /usr/sbin/update-packages", shell=True) except Exception as e: print(e, file=sys.stderr) return utils.generic_error("opkg_ugrade_failed") diff --git a/packages/ns-api/files/schedule-automatic-updates b/packages/ns-api/files/schedule-automatic-updates index d98e8f4b3..ea56effe5 100755 --- a/packages/ns-api/files/schedule-automatic-updates +++ b/packages/ns-api/files/schedule-automatic-updates @@ -11,7 +11,7 @@ action=${1:-"check"} timestamp=$2 -cmd="sleep \$(( RANDOM % 18000 )); /bin/opkg list-upgradable | /usr/bin/cut -f 1 -d ' ' | /usr/bin/xargs -r opkg upgrade" +cmd="sleep \$(( RANDOM % 18000 )); /usr/sbin/update-packages" if [ "$action" == "add" ]; then crontab -l | grep -q "$cmd" || echo "5 2 * * * $cmd" >> /etc/crontabs/root From 25920e2b7d4b059e6d66d0086bd53286a9d187a5 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Tue, 23 Jul 2024 09:15:15 +0200 Subject: [PATCH 3/5] ns-plug: migrate from old to new update command --- packages/ns-plug/Makefile | 3 +++ packages/ns-plug/files/40_ns-plug_automatic_updates | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/ns-plug/Makefile b/packages/ns-plug/Makefile index 6d9fee73e..782f80f4a 100644 --- a/packages/ns-plug/Makefile +++ b/packages/ns-plug/Makefile @@ -42,6 +42,8 @@ define Package/ns-plug/postinst if [ -z "$${IPKG_INSTROOT}" ]; then (. /etc/uci-defaults/20_ns-plug) rm -f /etc/uci-defaults/20_ns-plug + (. /etc/uci-defaults/40_ns-plug_automatic_updates) + rm -f /etc/uci-defaults/40_ns-plug_automatic_updates /etc/init.d/cron restart fi exit 0 @@ -84,6 +86,7 @@ define Package/ns-plug/install $(INSTALL_BIN) ./files/ns-download $(1)/usr/bin $(INSTALL_BIN) ./files/20_ns-plug $(1)/etc/uci-defaults $(INSTALL_BIN) ./files/30_ns-plug_alerts $(1)/etc/uci-defaults + $(INSTALL_BIN) ./files/40_ns-plug_automatic_updates $(1)/etc/uci-defaults $(INSTALL_BIN) ./files/netadata_enable_alerts $(1)/usr/share/ns-plug/hooks/register/70netadata_enable_alerts $(INSTALL_BIN) ./files/netadata_disable_alerts $(1)/usr/share/ns-plug/hooks/unregister/70netadata_disable_alerts $(INSTALL_BIN) ./files/enable_automatic_updates $(1)/usr/share/ns-plug/hooks/register/60enable_automatic_updates diff --git a/packages/ns-plug/files/40_ns-plug_automatic_updates b/packages/ns-plug/files/40_ns-plug_automatic_updates index ce724d9d4..94d6a7d45 100644 --- a/packages/ns-plug/files/40_ns-plug_automatic_updates +++ b/packages/ns-plug/files/40_ns-plug_automatic_updates @@ -1,7 +1,9 @@ old_cmd="sleep \$(( RANDOM % 18000 )); /bin/opkg list-upgradable | /usr/bin/cut -f 1 -d ' ' | /usr/bin/xargs -r opkg upgrade" +cmd="sleep \$(( RANDOM % 18000 )); /usr/sbin/update-packages" # Remove old command from crontab and add new one if crontab -l | grep -q "$old_cmd"; then crontab -l | grep -v "$old_cmd" | sort | uniq | crontab - - /usr/libexec/ns-api/schedule-automatic-updates add + # can't use /usr/libexec/ns-api/schedule-automatic-updates because new version could not be installed yet + crontab -l | grep -q "$cmd" || echo "5 2 * * * $cmd" >> /etc/crontabs/root fi From 1676e4cbb7fe0f975d04c141cc7352e61f86f1b7 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Tue, 23 Jul 2024 09:18:58 +0200 Subject: [PATCH 4/5] docs: packages, add new force update command --- docs/design/distfeed.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/design/distfeed.md b/docs/design/distfeed.md index b2dbef5c0..a4c022392 100644 --- a/docs/design/distfeed.md +++ b/docs/design/distfeed.md @@ -79,11 +79,7 @@ Updates are pushed to the subscription channel after one week from the release d If you have a machine with a valid subscription and want to force an update, you can use the following commands: ```bash -cp /etc/opkg/distfeeds.conf /etc/opkg/distfeeds.conf.ori -cat /rom/etc/opkg/distfeeds.conf | sed 's/dev/stable/g' > /etc/opkg/distfeeds.conf -opkg update -/bin/opkg list-upgradable | /usr/bin/cut -f 1 -d ' ' | /usr/bin/xargs -r opkg upgrade && echo "Update successful!" -mv /etc/opkg/distfeeds.conf.ori /etc/opkg/distfeeds.conf +update-packages --force-stable ``` At the end, the original `distfeeds.conf` file is restored. From 681774c70d73db36321f3dcb7ebb39b04f412e7e Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Tue, 23 Jul 2024 10:52:51 +0200 Subject: [PATCH 5/5] ns-plug: fix postinst execution During package update, files are not copied inside /etc/uci-default. A typical error is: //usr/lib/opkg/info/ns-plug.postinst: /usr/lib/opkg/info/ns-plug.postinst-pkg: line 5: /etc/uci-defaults/40_ns-plug_automatic_updates: not found To avoid such error: - duplicate the script and put it under /usr/libexec - execute the script inside postinst --- packages/ns-plug/Makefile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/ns-plug/Makefile b/packages/ns-plug/Makefile index 782f80f4a..7e5dacb58 100644 --- a/packages/ns-plug/Makefile +++ b/packages/ns-plug/Makefile @@ -40,10 +40,7 @@ endef define Package/ns-plug/postinst #!/bin/sh if [ -z "$${IPKG_INSTROOT}" ]; then - (. /etc/uci-defaults/20_ns-plug) - rm -f /etc/uci-defaults/20_ns-plug - (. /etc/uci-defaults/40_ns-plug_automatic_updates) - rm -f /etc/uci-defaults/40_ns-plug_automatic_updates + /usr/libexec/ns-plug/40_ns-plug_automatic_updates /etc/init.d/cron restart fi exit 0 @@ -71,6 +68,7 @@ define Package/ns-plug/install $(INSTALL_DIR) $(1)/etc/netdata $(INSTALL_DIR) $(1)/lib/upgrade/keep.d $(INSTALL_DIR) $(1)/usr/lib/netdata/python.d/ + $(INSTALL_DIR) $(1)/usr/libexec/ns-plug $(INSTALL_BIN) ./files/ns-plug.init $(1)/etc/init.d/ns-plug $(INSTALL_BIN) ./files/ns-plug $(1)/usr/sbin/ns-plug $(INSTALL_BIN) ./files/distfeed-setup $(1)/usr/sbin/distfeed-setup @@ -87,6 +85,7 @@ define Package/ns-plug/install $(INSTALL_BIN) ./files/20_ns-plug $(1)/etc/uci-defaults $(INSTALL_BIN) ./files/30_ns-plug_alerts $(1)/etc/uci-defaults $(INSTALL_BIN) ./files/40_ns-plug_automatic_updates $(1)/etc/uci-defaults + $(INSTALL_BIN) ./files/40_ns-plug_automatic_updates $(1)/usr/libexec/ns-plug $(INSTALL_BIN) ./files/netadata_enable_alerts $(1)/usr/share/ns-plug/hooks/register/70netadata_enable_alerts $(INSTALL_BIN) ./files/netadata_disable_alerts $(1)/usr/share/ns-plug/hooks/unregister/70netadata_disable_alerts $(INSTALL_BIN) ./files/enable_automatic_updates $(1)/usr/share/ns-plug/hooks/register/60enable_automatic_updates