commit 14baa1fce787a8804816869c4543675009e274b6
parent 6951b13d8ccd9fbc04c9a079ce60285cec2846d9
Author: ccx <root@dorje.wpr.cz>
Date: Sun, 20 Feb 2022 14:58:51 +0100
Add postinstall function and /previous symlink for rollback.
Diffstat:
M | install | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/install b/install
@@ -3,6 +3,7 @@ setopt no_unset warn_create_global extended_glob
zmodload -F zsh/stat b:zstat || exit $?
zmodload -m -F zsh/files b:zf_\* || exit $?
zmodload zsh/zutil || exit $?
+zmodload zsh/datetime || exit $?
cd $0:h || exit $?
@@ -201,6 +202,66 @@ install_bzr_repos(){
done
}
+link_changed() {
+ [[ "${preinstall_links[$1]:-}" == "${postinstall_links[$1]:-}" ]]
+}
+preinstall() {
+ typeset -gA preinstall_links
+ typeset -g preinstall_current
+ local l
+ for l in /current/**/*(@N); do
+ preinstall_links[${l#/current/}]=$l:A
+ done
+ preinstall_current=/current
+ preinstall_current=$preinstall_current:A
+ cp -a /current /previous || exit $?
+}
+typeset -ft preinstall
+postinstall() {
+ typeset -gA postinstall_links
+ local l ret
+ for l in /current/**/*(@N); do
+ postinstall_links[${l#/current/}]=$l:A
+ done
+
+ if [[ -d /run/s6-rc/ ]]; then
+ if link_changed package; then
+ if link_changed s6-rc-source; then
+ local tmp_db=/run/old-s6-rc-db-migration.$EPOCHREALTIME
+
+ # s6-rc-format-upgrade to tmpdir, then s6-rc-update
+ : s6-rc-compile $tmp_db "$preinstall_links[s6-rc-source]" || return $?
+ : s6-rc-format-upgrade -v2 $tmp_db || return $?
+ : s6-rc-update -v2 /current/s6-rc-db/ || return $?
+ : rm -r $tmp_db || return $?
+ else
+ # s6-rc-format-upgrade to new source directly
+ : s6-rc-format-upgrade -v2 /current/s6-rc-db/ || return $?
+ fi
+ elif link_changed s6-rc-source; then
+ # s6-rc-update to new source directly
+ s6-rc-update -v2 /current/s6-rc-db/ || return $?
+ fi
+ if link_changed bzr/containers || link_changed bzr/confz; then
+ zsh -lc "quiet=1 confz site_containers" </dev/null 2>&1 | cat -v
+ ret=$((${(j.|.)pipestatus}))
+ if (($ret)); then
+ echo >&2 "Error: command failed ($ret): confz site_containers"
+ return $ret
+ fi
+ s6-svscanctl -a /run/service || return $?
+ fi
+ fi
+ return 0
+}
+typeset -ft postinstall
+postinstall_or_rollback() {
+ if ! postinstall; then
+ echo >&2 "Fatal: postinstall script failed, rolling back to $preinstall_current"
+ cp -a /previous /current
+ exit 1
+ fi
+}
# End of functions, now install things in right order
typeset -g submodule
@@ -220,4 +281,6 @@ require installed
submodule -c .
require installed
+preinstall
require current
+postinstall_or_rollback