-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #667 from NethServer/force_update
New update-packages script #668
- Loading branch information
Showing
6 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 - | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |