aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSlack Coder <slackcoder@server.ky>2024-06-10 14:28:02 -0500
committerSlack Coder <slackcoder@server.ky>2024-06-10 14:33:13 -0500
commite5ad4e9c76a40013346ff7aef982450c0e00deef (patch)
treee5ff5205ed731221900c1f3166ec3196585a8bc1
parentb4538dfc0a1ec88b42399dd79f28aafe26d1ed90 (diff)
downloadslack-autoupdate-e5ad4e9c76a40013346ff7aef982450c0e00deef.tar.xz
Simplify slack-autoupdateHEADmaster
Reduce the lines of code by using bash functions to remove repetition. The functions define the different packages sources to check with the expected format given as comment in the file. It will be now to add and remove package sources.
-rw-r--r--src/recipes/use-current-kernel.sh156
-rw-r--r--src/slack-autoupdate127
2 files changed, 152 insertions, 131 deletions
diff --git a/src/recipes/use-current-kernel.sh b/src/recipes/use-current-kernel.sh
index 50b08e7..6673bfb 100644
--- a/src/recipes/use-current-kernel.sh
+++ b/src/recipes/use-current-kernel.sh
@@ -1,92 +1,92 @@
-# Download kernel updates from Slackware Current for a stable release.
-#
-# For this to work, make sure the kernel packages are blacklisted in
-# /etc/slackpkg/blacklist.
-#
-# kernel-generic.*
-# kernel-huge.*
-# kernel-modules.*
-# kernel-source.*
-#
-# This script uses an unusual pattern for running commands on exit, similar to
-# Golang's defer. These will not be run when the script is stopped with
-# Control+c.
-#
-# D="[command]; $D"; trap "$D" EXIT
-#
+slackware_current_kernel_source() {
+ # Download kernel updates from Slackware Current for a stable release.
+ #
+ # For this to work, make sure the kernel packages are blacklisted in
+ # /etc/slackpkg/blacklist.
+ #
+ # kernel-generic.*
+ # kernel-huge.*
+ # kernel-modules.*
+ # kernel-source.*
+ #
+ # This script uses an unusual pattern for running commands on exit, similar to
+ # Golang's defer. These will not be run when the script is stopped with
+ # Control+c.
+ #
+ # D="[command]; $D"; trap "$D" EXIT
+ #
-echo "Checking kernel updates from Slackware Current (slackpkg)..."
+ local staging_dir="$1"
+ local update_info="$2"
-# Get Slackware version.
-source /etc/os-release
+ # Get the system's Slackware version.
+ source /etc/os-release
-SOURCE=$(sed -n '
- # Remove leading and trailing blanks
- s/^[[:blank:]]*//
- s/[[:blank:]]*$//
- # Only one token is allowed per line
- /[[:blank:]]/d
- # A single solidus should end the URI
- s,[/]*$,/,
- # Print the lines beginning with one of the URI schemes we look for
- \@^file://@p
- \@^cdrom://@p
- \@^local://@p
- \@^https\{0,1\}://@p
- \@^ftps\{0,1\}://@p' /etc/slackpkg/mirrors)
-SOURCE="$(echo $SOURCE | sed "s/-$VERSION/-current/")"
+ local mirror=$(sed -n '
+ # Remove leading and trailing blanks
+ s/^[[:blank:]]*//
+ s/[[:blank:]]*$//
+ # Only one token is allowed per line
+ /[[:blank:]]/d
+ # A single solidus should end the URI
+ s,[/]*$,/,
+ # Print the lines beginning with one of the URI schemes we look for
+ \@^file://@p
+ \@^cdrom://@p
+ \@^local://@p
+ \@^https\{0,1\}://@p
+ \@^ftps\{0,1\}://@p' /etc/slackpkg/mirrors)
+ mirror="$(echo $mirror | sed "s/-$VERSION/-current/")"
-yes y | slackpkg -mirror="$SOURCE" -batch=on -default_answer=yes update
-D="yes | slackpkg -batch=on -default_answer=yes update"; trap "$D" EXIT
+ yes y | slackpkg -mirror="$mirror" -batch=on -default_answer=yes update
+ D="yes | slackpkg -batch=on -default_answer=yes update"; trap "$D" EXIT
-PACKAGE_UPDATES="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
-D="rm -f ${PACKAGE_UPDATES}; $D"; trap "$D" EXIT
+ local package_updates="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
+ D="rm -f ${package_updates}; $D"; trap "$D" EXIT
-mv /etc/slackpkg/blacklist /etc/slackpkg/blacklist.orig
-touch /etc/slackpkg/blacklist
-D="mv -f /etc/slackpkg/blacklist.orig /etc/slackpkg/blacklist; $D"; trap "$D" EXIT
+ mv /etc/slackpkg/blacklist /etc/slackpkg/blacklist.orig
+ touch /etc/slackpkg/blacklist
+ D="mv -f /etc/slackpkg/blacklist.orig /etc/slackpkg/blacklist; $D"; trap "$D" EXIT
-OUTPUT="$(slackpkg -batch=on -default_answer=n upgrade kernel)" || exit_code="$?"
-if [ $exit_code -ne 0 ] && [ $exit_code -ne 20 ] && [ $exit_code -ne 100 ]; then
+ local output="$(slackpkg -batch=on -default_answer=n upgrade kernel)" || exit_code="$?"
# slackpkg has several safe exit codes.
- exit exit_code
-fi
+ if [ $exit_code -ne 0 ] && [ $exit_code -ne 20 ] && [ $exit_code -ne 100 ]; then
+ exit exit_code
+ fi
-echo "$OUTPUT" \
- | grep "\.t.z$" \
- | tee "$PACKAGE_UPDATES"
-if [ ! -s "$PACKAGE_UPDATES" ]; then
- # No updates
- exit 0
-fi
+ echo "$output" \
+ | grep "\.t.z$" \
+ | tee "$package_updates"
+ if [ ! -s "$package_updates" ]; then
+ # No updates
+ exit 0
+ fi
-while read PKG; do
- PKG_URL="$(
- grep "${PKG}$" /var/lib/slackpkg/CHECKSUMS.md5 \
- | tr -s ' ' \
- | cut -f 2 -d ' '
- )" || exit "$?"
+ while read PKG; do
+ PKG_URL="$(
+ grep "${PKG}$" /var/lib/slackpkg/CHECKSUMS.md5 \
+ | tr -s ' ' \
+ | cut -f 2 -d ' '
+ )" || exit "$?"
- wget \
- --directory-prefix="$STAGING_DIR" \
- "${SOURCE}${PKG_URL}" \
- "${SOURCE}${PKG_URL}.asc" \
- || exit 1
-done <"$PACKAGE_UPDATES"
+ wget \
+ --directory-prefix="$STAGING_DIR" \
+ "${mirror}${PKG_URL}" \
+ "${mirror}${PKG_URL}.asc" \
+ || exit 1
+ done <"$package_updates"
-if ! gpg2 --verify-files "$STAGING_DIR"/kernel-*.asc; then
- echo "gpg verification failed"
- exit 1
-fi
+ if ! gpg2 --verify-files "$STAGING_DIR"/kernel-*.asc; then
+ echo "gpg verification failed"
+ exit 1
+ fi
-(
- # Redirect this subshell's standard output to info file.
- exec 1>>"$UPDATE_INFO"
+ (
+ # Redirect this subshell's standard output to info file.
+ exec 1>>"$update_info"
- echo "## Slackware current kernel updates"
- echo
- while read PKG; do
- echo " - $PKG"
- done <"$PACKAGE_UPDATES"
- echo
-)
+ while read PKG; do
+ echo " - $PKG"
+ done <"$package_updates"
+ )
+}
diff --git a/src/slack-autoupdate b/src/slack-autoupdate
index 70028a3..4784c23 100644
--- a/src/slack-autoupdate
+++ b/src/slack-autoupdate
@@ -58,28 +58,36 @@ fi
mkdir --parents "$STAGING_DIR"
find "$STAGING_DIR" -mindepth 1 | xargs rm -fr
-if ! OUTPUT="$(
- # Capture this subshell's error output to standard output.
- exec 2>&1
+# Following are bash functions to check one or more package sources for
+# updates.
+#
+# The functions must use the naming format "[source_name]_source". They
+# take two arguments.
+#
+# - First argument is the file path to save packages.
+# - Second argument is the file path to append update information.
+#
- echo "Checking updates for slackpkg..."
+slackpkg_source() {
+ local staging_dir="$1"
+ local update_info="$2"
yes | slackpkg -batch=on -default_answer=yes update || exit "$?"
- CHANGELOG_TXT="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
- PACKAGE_UPDATES="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
- trap "rm -f ${CHANGELOG_TXT} ${PACKAGE_UPDATES}" EXIT
+ local changelog_txt="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
+ local package_updates="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
+ trap "rm -f ${changelog_txt} ${package_updates}" EXIT
- OUTPUT="$(slackpkg -batch=on -default_answer=n upgrade-all)" || exit_code="$?"
- if [ $exit_code -ne 0 ] && [ $exit_code -ne 20 ] && [ $exit_code -ne 100 ]; then
+ local output="$(slackpkg -batch=on -default_answer=n upgrade-all)" || exit_code="$?"
+ if [[ $exit_code -ne 0 ]] && [[ $exit_code -ne 20 ]] && [[ $exit_code -ne 100 ]]; then
# Slackpkg has several safe exit codes.
exit exit_code
fi
- echo "$OUTPUT" \
+ echo "$output" \
| grep "\.t.z$" \
- | tee "$PACKAGE_UPDATES"
- if [ ! -s "$PACKAGE_UPDATES" ]; then
+ | tee "$package_updates"
+ if [ ! -s "$package_updates" ]; then
# No updates
exit 0
fi
@@ -90,7 +98,7 @@ if ! OUTPUT="$(
# around.
#
- SOURCE=$(sed -n '
+ local mirror=$(sed -n '
# Remove leading and trailing blanks
s/^[[:blank:]]*//
s/[[:blank:]]*$//
@@ -105,72 +113,69 @@ if ! OUTPUT="$(
\@^https\{0,1\}://@p
\@^ftps\{0,1\}://@p' /etc/slackpkg/mirrors)
- while read PKG; do
- PKG_URL="$(
- grep "${PKG}$" /var/lib/slackpkg/CHECKSUMS.md5 \
+ while read pkg; do
+ local pkg_url="$(
+ grep "${pkg}$" /var/lib/slackpkg/CHECKSUMS.md5 \
| tr -s ' ' \
| cut -f 2 -d ' '
)" || exit "$?"
wget \
--quiet \
- --directory-prefix="$STAGING_DIR" \
- "${SOURCE}${PKG_URL}" \
- "${SOURCE}${PKG_URL}.asc" \
+ --directory-prefix="$staging_dir" \
+ "${mirror}${pkg_url}" \
+ "${mirror}${pkg_url}.asc" \
|| exit "$?"
- done <"$PACKAGE_UPDATES"
+ done <"$package_updates"
- gpg2 --verify-files "$STAGING_DIR"/*.asc || exit "$?"
+ echo arg $1
+ echo staging_dir $staging_dir
+ gpg2 --verify-files "$staging_dir"/*.asc || exit "$?"
(
# Provide update information to the user.
# Redirect this subshell's standard output to info file.
- exec 1>>"$UPDATE_INFO"
+ exec 1>>"$update_info"
- LAST_INSTALLED_PACKAGE="$(
+ local last_installed_package="$(
grep \
"$(ls /var/log/packages | sed "s/$/\\\|/g" | tr --delete '\n')v4EcFvjXKlWB" \
/var/lib/slackpkg/ChangeLog.txt \
| head -n 1
)"
- echo "## Slackware updates"
- echo
- while read PKG; do
- echo " - $PKG"
- done <"$PACKAGE_UPDATES"
+ while read pkg; do
+ echo " - $pkg"
+ done <"$package_updates"
echo
echo "### Recent Changelog Entries"
- grep --before-context 10000 "$LAST_INSTALLED_PACKAGE" /var/lib/slackpkg/ChangeLog.txt
- echo
+ echo ""
+ grep --before-context 10000 "$last_installed_package" /var/lib/slackpkg/ChangeLog.txt
)
-)"; then
- >>"$UPDATE_ERROR" echo -e "slackpkg:\n\n$OUTPUT"
-fi
-
-if ! OUTPUT="$(
- exec 2>&1
+}
- echo "Checking updates for sbotools..."
+sbotools_source() {
+ local staging_dir="$1"
+ local update_info="$2"
if [ ! -f /etc/sbotools/sbotools.conf ] \
- || [ "$(sed -n 's/PKG_DIR=//p' /etc/sbotools/sbotools.conf)" != "$STAGING_DIR" ]; then
+ || [ "$(sed -n 's/PKG_DIR=//p' /etc/sbotools/sbotools.conf)" != "$staging_dir" ]; then
# Assume sbotools is disabled.
exit 0
fi
source /etc/profile.d/*.sh
- PACKAGE_UPDATES=$(mktemp /tmp/slack-autoupdate.XXXXXX)
- trap "rm -f ${PACKAGE_UPDATES}" EXIT
+ local package_updates=$(mktemp /tmp/slack-autoupdate.XXXXXX)
+ trap "rm -f ${package_updates}" EXIT
- (sbocheck | tee $PACKAGE_UPDATES) || exit 1
+ (sbocheck | tee $package_updates) || exit 1
# Avoid checking for 'no updates available'. It will not
# work if you host 'overrides' purposely different than what is
# on SBO.
- if ! grep "needs updating" "$PACKAGE_UPDATES"; then
+ if ! grep "needs updating" "$package_updates"; then
exit 0
fi
@@ -181,21 +186,37 @@ if ! OUTPUT="$(
|| exit "$?"
(
- exec 1>>"$UPDATE_INFO"
+ exec 1>>"$update_info"
- echo "## Slackbuild updates"
- echo
- cat "$PACKAGE_UPDATES"
- echo
+ cat "$package_updates"
)
-)"; then
- if [ -f "$UPDATE_ERROR" ]; then
- >>"$UPDATE_ERROR" echo ""
- >>"$UPDATE_ERROR" echo ""
+}
+
+update_sources=(slackpkg_source sbotools_source)
+for update_source in ${update_sources[@]}; do
+ source_name="$(echo "${update_source}" | sed "s/_source$//" | sed "s/_/ /")"
+
+ if [ -f "$UPDATE_INFO" ]; then
+ >>"$UPDATE_INFO" echo ""
fi
+ >>"$UPDATE_INFO" echo "## ${source_name} updates"
+ >>"$UPDATE_INFO" echo ""
- >>"$UPDATE_ERROR" echo -e "sbotools:\n\n$OUTPUT"
-fi
+ if ! OUTPUT="$(
+ exec 2>&1
+
+ echo "Checking updates for ${source_name}..."
+
+ $update_source "$STAGING_DIR" "$UPDATE_INFO"
+ )"; then
+ if [ -f "$UPDATE_ERROR" ]; then
+ >>"$UPDATE_ERROR" echo ""
+ >>"$UPDATE_ERROR" echo ""
+ fi
+
+ >>"$UPDATE_ERROR" echo -e "${source_name}:\n\n$OUTPUT"
+ fi
+done
if [ -z "$(find "$STAGING_DIR" -name "*.t*z" -or -name "info.txt" -or -name "error.txt")" ]; then
# No updates