Skip to content

Commit

Permalink
develmaster: final commits for v2.2.2 (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olf0 authored Apr 2, 2023
2 parents 15e0286 + bcb9ad7 commit ec2ec96
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 61 deletions.
125 changes: 66 additions & 59 deletions bin/harbour-storeman-installer
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -ufC # "-u" (error when using uninitialised variables),
# "-f" (disable pathname expansion) and "-C" (no-clobber / -overwrite).
# Ultimately decided against "-e", because this script better runs through,
Expand Down Expand Up @@ -53,23 +53,30 @@ logfile="$2"
umask 113
[ "$PWD" = / ] || cd / # Set PWD to /, if not already

# Prefix first output with one plus two linebreaks, if logfile size > 0
linebreaks=""
# Prefix first output with a linebreak, dashes and another linebreak, if logfile size > 0
if [ -s "$logfile" ]
then linebreaks="\n-----------------------------\n\n"
then printf '\n%s\n\n' "-----------------------------"
fi

# Write first entry to logfile:
logentry="[Info] PID $mypid is logging to $logfile"
if printf "${linebreaks}%s\n" "$(date -Iseconds) $logentry"
then systemd-cat -t "$called" -p 6 printf "%s\n" "$logentry"
else systemd-cat -t "$called" -p 4 printf "%s\n" "[Warning] PID $mypid failed to write to $logfile"
logentry="[Info] PID $mypid is logging to $logfile on"
if printf '%s %s\n' "$logentry" "$(date -Iseconds)"
then systemd-cat -t "$called" -p 6 printf '%s\n' "$logentry"
else systemd-cat -t "$called" -p 4 printf '%s\n' "[Warning] PID $mypid failed to write to $logfile"
fi

# Log some status data:
printf "\n%s" 'From /etc/os-release: '; source /etc/os-release; printf "%s\n" "$ID $VERSION_ID $SAILFISH_FLAVOUR"
printf "\n%s\n" 'Output of `ssu status`, UID omitted:'; ssu s | fgrep -iv 'UID:'
printf "\n%s\n" '"storeman" entries from `ssu lr`:'; ssu lr | fgrep storeman | tr -s ' '
source /etc/os-release; logentry="[Debug] From /etc/os-release: $ID $VERSION_ID $SAILFISH_FLAVOUR"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf '%s\n' "$logentry"

ssus="$(ssu s | grep -iv 'UID:\? ')"; logentry='`ssu status`, UID omitted: '
printf '\n%s\n%s\n' "$(date -Iseconds) $logentry" "$ssus"
systemd-cat -t "$called" -p 7 printf '%s' "$logentry $(printf '%s' "$ssus" | sed 's/$/, /g' | tr -d '\n')"

ssulr="$(ssu lr | fgrep storeman | tr -s ' ')"; logentry='"storeman" entries from `ssu lr`:'
printf '\n%s\n%s\n' "$(date -Iseconds) $logentry" "$ssulr"
systemd-cat -t "$called" -p 7 printf '%s' "$logentry $(printf '%s' "$ssulr" | sed -e 's/^ - / /g' -e 's/ ... / /g' | tr -s '\n' ',')"

# Provide RPM with a little time to proceed finishing the RPM transaction,
# which called this script asynchronously, because pkcon might fail
Expand All @@ -80,7 +87,7 @@ printf "\n%s\n" '"storeman" entries from `ssu lr`:'; ssu lr | fgrep storeman | t
i=0
maxi=3
rpmXctn=running
while [ $i != $maxi ] && ps -eo pid | grep -Fq "$gppid"
while [ $i != $maxi ] && ps -eo pid | fgrep -q "$gppid"
do
sleep 1
i=$(($i+1))
Expand All @@ -93,15 +100,15 @@ sleep 1
# `pkcon search name` is also intended to serve as a synchronisation point,
# because most of pkcon's sub-commands wait until the package management
# backend (on SailfishOS: libzypp) is idle:
if pkgname="$(pkcon -p search name "$called" | grep "^Installed " | tr -s " " | cut -f 2 -d " " | rev | cut -f 2- -d "." | rev | grep -m 1 "^$called-[0-9]")"
if pkgname="$(pkcon -p search name "$called" | grep '^Installed ' | tr -s ' ' | cut -f 2 -d ' ' | rev | cut -f 2- -d '.' | rev | grep -m 1 "^$called-[0-9]")"
then pkgkitd=ok
else pkgkitd=KO
fi

# Write $pkgname to logfile:
logentry="[Debug] Installed, now running: $pkgname"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf '%s\n' "$logentry"

# Out of occasional bad experience when pkcon is being hammered with
# commands, also prefix `pkcon repo-set-data` with a second sleep:
Expand All @@ -110,60 +117,60 @@ maxi=4 # 4 is the minimal value, which makes sense here
maxihalf=$(($maxi/2))
for i in $(seq $maxi)
do
logentry="pkcon -pv repo-set-data harbour-storeman-obs refresh-now true"
printf "\n%s\n" "$(date -Iseconds) [Step 1 / 2] $logentry"
systemd-cat -t "$called" -p 6 printf "%s\n" "[Info] Executing: $logentry"
logentry='pkcon -pv repo-set-data harbour-storeman-obs refresh-now true'
printf '\n%s\n' "$(date -Iseconds) [Step 1 / 2] $logentry"
systemd-cat -t "$called" -p 6 printf '%s\n' "[Info] Executing: $logentry"
eval $logentry
retc=$?
[ $retc = 0 ] && break # Went fine
wait=$(($i*2+1))
if [ $i = 1 ]
then
logentry="[Notice] Failed to refresh harbour-storeman-obs repository, because error-code $retc was returned by: $logentry"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf '%s\n' "$logentry"
logentry="[Debug] Sleeping for $wait seconds."
printf "%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf "%s\n" "$logentry"
printf '%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf '%s\n' "$logentry"
sleep $wait
logentry="[Info] Retry #$i"
printf "\n%s" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 6 printf "%s\n" "$logentry"
printf '\n%s' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 6 printf '%s\n' "$logentry"
elif [ $i -le $maxihalf ]
then
logentry="[Warning] Failed to refresh harbour-storeman-obs repository, because error-code $retc was returned by: $logentry"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 4 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 4 printf '%s\n' "$logentry"
logentry="[Notice] Trying to terminate (i.e., sending SIGTERM to) all processes named (ID) `pkcon`, then sleeping for $wait seconds."
printf "%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf "%s\n" "$logentry"
printf '%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf '%s\n' "$logentry"
killall -q -TERM pkcon
sleep $wait
logentry="[Info] Retry #$i"
printf "\n%s" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 6 printf "%s\n" "$logentry"
printf '\n%s' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 6 printf '%s\n' "$logentry"
elif [ $i -lt $maxi ]
then
logentry="[Error] Failed to refresh harbour-storeman-obs repository, because error-code $retc was returned by: $logentry"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 3 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 3 printf '%s\n' "$logentry"
logentry="[Warning] Trying to interrupt (i.e., sending SIGINT to) all processes named (ID) `pkcon`, then sleeping for $wait seconds."
printf "%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 4 printf "%s\n" "$logentry"
printf '%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 4 printf '%s\n' "$logentry"
killall -q -INT pkcon
sleep $i
killall -q -TERM pkcon
sleep $i
logentry="[Info] Retry #$i"
printf "\n%s" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 6 printf "%s\n" "$logentry"
printf '\n%s' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 6 printf '%s\n' "$logentry"
else
logentry="[Error] Failed to refresh harbour-storeman-obs repository, because error-code $retc was returned by: $logentry"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 3 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 3 printf '%s\n' "$logentry"
logentry="[Warning] Trying to hang-up (i.e., sending SIGHUP to) all processes named (ID) `pkcon`, then sleeping for $i seconds and ultimately killing them."
printf "%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 4 printf "%s\n" "$logentry"
printf '%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 4 printf '%s\n' "$logentry"
killall -q -HUP pkcon
sleep $i
killall -qw -KILL pkcon
Expand All @@ -174,38 +181,38 @@ do
systemctl start packagekit.service
sleep 1
logentry="[Notice] Final retry #$i"
printf "\n%s" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf "%s\n" "$logentry"
logentry="pkcon -pv repo-set-data harbour-storeman-obs refresh-now true"
printf "\n%s\n" "$(date -Iseconds) [Step 1 / 2] $logentry"
systemd-cat -t "$called" -p 6 printf "%s\n" "[Info] Executing: $logentry"
printf '\n%s' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf '%s\n' "$logentry"
logentry='pkcon -pv repo-set-data harbour-storeman-obs refresh-now true'
printf '\n%s\n' "$(date -Iseconds) [Step 1 / 2] $logentry"
systemd-cat -t "$called" -p 6 printf '%s\n' "[Info] Executing: $logentry"
eval $logentry
retc=$?
[ $retc = 0 ] && break # Went fine
logentry="[Critical] Failed to refresh harbour-storeman-obs repository, because error-code $retc was returned by: $logentry"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 2 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 2 printf '%s\n' "$logentry"
killall -qw -KILL pkcon
sleep 1
pkcon -pv quit
sleep 1
systemctl stop packagekit.service
systemctl start packagekit.service
logentry="[Notice] Scheduling harbour-storeman-installer for removal in 20 seconds, trying to install harbour-storeman despite the failed repository refresh meanwhile."
printf "%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf "%s\n" "$logentry"
logentry='[Notice] Scheduling harbour-storeman-installer for removal in 20 seconds, trying to install harbour-storeman despite the failed repository refresh meanwhile.'
printf '%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 5 printf '%s\n' "$logentry"
sleep 1
logentry="pkcon -pvy remove $called"
setsid --fork sh -c '(i=0; while [ $i -le 9 ] && ps -eo pid | grep -Fq "$1"; do sleep 1; i=$(($i+1)); done;\
sleep 20; printf "\n%s\n" "$(date -Iseconds) [Self-removal] $2";\
systemd-cat -t "$3" -p 6 printf "%s\n" "[Info] Self-removal in 20 seconds: $2";\
setsid --fork sh -c '(i=0; while [ $i -le 9 ] && ps -eo pid | fgrep -q "$1"; do sleep 1; i=$(($i+1)); done;\
sleep 20; printf '\n%s\n' "$(date -Iseconds) [Self-removal] $2";\
systemd-cat -t "$3" -p 6 printf '%s\n' "[Info] Self-removal in 20 seconds: $2";\
eval $2)' sh_rm_inst-storeman "$mypid" "$logentry" "$called"
# The first 15 characters of the spawned process' name
# (to be used for, e.g., `ps` and `pgrep` / `pkill`) are:
# sh_rm_inst-stor
fi
done
logentry="pkcon -pvy install harbour-storeman"
logentry='pkcon -pvy install harbour-storeman'
# Sleeping again for a second right before the next pkcon call, because pkcon
# *rarely* fails installing a package ("not found") immediately after an
# individual repository refresh via `repo-set-data <repo> refresh-now true`.
Expand All @@ -216,9 +223,9 @@ logentry="pkcon -pvy install harbour-storeman"
# It also shall always be the last command issued (WRT control-flow),
# except for the final log message.
setsid --fork sh -c '(sleep 1;\
i=0; while [ $i -le 9 ] && ps -eo pid | grep -Fq "$1"; do sleep 1; i=$(($i+1)); done;\
printf "\n%s\n" "$(date -Iseconds) [Step 2 / 2] $2";\
systemd-cat -t "$3" -p 6 printf "%s\n" "[Info] Executing: $2";\
i=0; while [ $i -le 9 ] && ps -eo pid | fgrep -q "$1"; do sleep 1; i=$(($i+1)); done;\
printf '\n%s\n' "$(date -Iseconds) [Step 2 / 2] $2";\
systemd-cat -t "$3" -p 6 printf '%s\n' "[Info] Executing: $2";\
eval $2)' sh_do_inst-storeman "$mypid" "$logentry" "$called"
# The first 15 characters of the spawned process' name
# (to be used for, e.g., `ps` and `pgrep` / `pkill`) are:
Expand All @@ -229,7 +236,7 @@ setsid --fork sh -c '(sleep 1;\
# For a detailed description of double-forking in shell code, see
# https://github.com/storeman-developers/harbour-storeman-installer/blob/master/double-fork-in-shell.md
logentry="[Debug] ${called}'s main script (PID: ${mypid}) finishes"
printf "\n%s\n" "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf "%s\n" "$logentry"
printf '\n%s\n' "$(date -Iseconds) $logentry"
systemd-cat -t "$called" -p 7 printf '%s\n' "$logentry"
exit $retc

4 changes: 2 additions & 2 deletions rpm/harbour-storeman-installer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Name: harbour-storeman-installer
# natural number greater or equal to 1, which may be prefixed with one of
# {alpha,beta,rc,release} (e.g., "beta3"). For details and reasons, see
# https://github.com/storeman-developers/harbour-storeman-installer/wiki/Git-tag-format
Version: 2.2.1
Release: release2
Version: 2.2.2
Release: release3
Group: Applications/System
URL: https://github.com/storeman-developers/%{name}
# These "Source0:" line below requires that the value of %%{name} is also the
Expand Down

0 comments on commit ec2ec96

Please sign in to comment.