pkg-manager.sh: Support running without sudo

To support minimal environments, notably minimal Docker containers
which don't have /usr/bin/sudo by the time pkg-manager.sh is invoked
(possibly to install sudo), support running all commands without
invoking sudo first. Of course this only works if invoked as root.

Note that this is still somewhat hacky, command-line parsing needs to
be cleaned up.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-01-14 19:16:18 +01:00
commit f6c3f49fca

View file

@ -45,13 +45,15 @@ cmd_dup()
done done
shift shift
[ "$sudo" ] && sudo="$sudo $env -S"
case $ID in case $ID in
opensuse|suse) opensuse|suse)
[ "$non_interactive" = 1 ] && { [ "$non_interactive" = 1 ] && {
opts="--force-resolution --auto-agree-with-licenses" opts="--force-resolution --auto-agree-with-licenses"
global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks" global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks"
} }
run sudo $env -S zypper $global_opts dup $opts "$@" run $sudo zypper $global_opts dup $opts "$@"
;; ;;
debian|ubuntu|raspbian) debian|ubuntu|raspbian)
fatal "Tried to run distribution upgrade on unsupported platform \"$ID\"" fatal "Tried to run distribution upgrade on unsupported platform \"$ID\""
@ -84,30 +86,32 @@ cmd_install()
done done
shift shift
[ "$sudo" ] && sudo="$sudo $env -S"
case $ID in case $ID in
opensuse|suse) opensuse|suse)
[ "$non_interactive" = 1 ] && { [ "$non_interactive" = 1 ] && {
opts="--force-resolution --auto-agree-with-licenses" opts="--force-resolution --auto-agree-with-licenses"
global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks" global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks"
} }
run sudo $env -S zypper $global_opts install $opts "$@" run $sudo zypper $global_opts install $opts "$@"
;; ;;
debian|ubuntu|raspbian) debian|ubuntu|raspbian)
[ "$non_interactive" = 1 ] && { [ "$non_interactive" = 1 ] && {
global_opts="$global_opts -yq" global_opts="$global_opts -yq"
env="$env DEBIAN_FRONTEND=noninteractive" env="$env DEBIAN_FRONTEND=noninteractive"
} }
run sudo $env -S apt-get $global_opts install "$@" run $sudo apt-get $global_opts install "$@"
;; ;;
arch) arch)
[ "$non_interactive" = 1 ] && { [ "$non_interactive" = 1 ] && {
global_opts="$global_opts --noconfirm" global_opts="$global_opts --noconfirm"
env="$env DEBIAN_FRONTEND=noninteractive" env="$env DEBIAN_FRONTEND=noninteractive"
} }
run sudo $env -S pacman $global_opts -S --needed "$@" run $sudo pacman $global_opts -S --needed "$@"
;; ;;
centos) centos)
run sudo $env -S yum $global_opts install -y "$@" run $sudo yum $global_opts install -y "$@"
;; ;;
*) *)
fatal "Tried to install on unsupported platform \"$ID\"" fatal "Tried to install on unsupported platform \"$ID\""
@ -131,19 +135,21 @@ cmd_refresh()
done done
shift shift
[ "$sudo" ] && sudo="$sudo $env -S"
case $ID in case $ID in
opensuse|suse) opensuse|suse)
[ "$non_interactive" = 1 ] && { [ "$non_interactive" = 1 ] && {
global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks" global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks"
} }
run sudo $env -S zypper $global_opts refresh $opts "$@" run $sudo zypper $global_opts refresh $opts "$@"
;; ;;
debian|ubuntu|raspbian) debian|ubuntu|raspbian)
run sudo $env -S apt-get $global_opts update "$@" run $sudo apt-get $global_opts update "$@"
;; ;;
centos) centos)
run sudo $env -S yum $global_opts clean expire-cache "$@" run $sudo yum $global_opts clean expire-cache "$@"
run sudo $env -S yum $global_opts makecache "$@" run $sudo yum $global_opts makecache "$@"
;; ;;
*) *)
fatal "Tried to update unsupported platform \"$ID\"" fatal "Tried to update unsupported platform \"$ID\""
@ -155,6 +161,8 @@ cmd_refresh()
myname=`basename $0` myname=`basename $0`
. /etc/os-release . /etc/os-release
ID=${ID%%-*} ID=${ID%%-*}
[ "$EUID" ] || EUID=$(id -un)
[ "$EUID" = 0 ] || sudo=/usr/bin/sudo
cmd="$1" cmd="$1"
shift shift
cmd_$cmd "$@" cmd_$cmd "$@"