From 645d61a80759925e187b3a92dd9bb018ab471712 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 12 Nov 2017 10:41:22 -0200 Subject: Misc enhancements --- share/trashman/docker/unix/linux/debian/test | 5 +++ share/trashman/trashman/debian | 23 +++++++++-- share/trashman/trashman/functions | 62 ++++++++++++++++++++++++---- trashman | 26 +++--------- 4 files changed, 85 insertions(+), 31 deletions(-) diff --git a/share/trashman/docker/unix/linux/debian/test b/share/trashman/docker/unix/linux/debian/test index 901d29a..684453c 100755 --- a/share/trashman/docker/unix/linux/debian/test +++ b/share/trashman/docker/unix/linux/debian/test @@ -9,6 +9,11 @@ SHARE="$1" # Include basic functions . $SHARE/trashman/functions || exit 1 +# Basic check +if ! which docker > /dev/null 2>&1; then + exit 1 +fi + # Run hello-world test program __trashman_echo "Running docker helll-world image..." docker run hello-world diff --git a/share/trashman/trashman/debian b/share/trashman/trashman/debian index bcc0e6b..395512b 100644 --- a/share/trashman/trashman/debian +++ b/share/trashman/trashman/debian @@ -29,7 +29,9 @@ __trashman_apt_check() { return fi - dpkg -s $1 > /dev/null 2>&1 + #dpkg -s $1 > /dev/null 2>&1 + # See https://stackoverflow.com/questions/1298066/check-if-a-package-is-installed-and-then-install-it-if-its-not#1298103 + dpkg-query -W -f='${Status}' $1 | grep -q '^install ok' return $? } @@ -40,11 +42,24 @@ __trashman_apt_install_packages() { fi for package in $*; do - dpkg -s $package > /dev/null 2>&1 + __trashman_apt_check $package - if [ "$?" == "1" ]; then + if [ "$?" = "1" ]; then __trashman_echo "Installing package $package..." - DEBIAN_FRONTEND=noninteractive $SUDO apt-get install $package -y || exit 1 + DEBIAN_FRONTEND=noninteractive apt-get install $package -y || exit 1 fi done } + +# Install an apt repository key +__trashman_install_apt_key() { + local orig="$1" + local dest="$2" + + if [ ! -e "$orig" ]; then + exit 1 + fi + + cp $orig /etc/apt/trusted.gpg.d/$dest || exit 1 + chown root.root /etc/apt/trusted.gpg.d/$dest && chmod 644 /etc/apt/trusted.gpg.d/$dest || exit 1 +} diff --git a/share/trashman/trashman/functions b/share/trashman/trashman/functions index 5f655de..4208b45 100644 --- a/share/trashman/trashman/functions +++ b/share/trashman/trashman/functions @@ -48,6 +48,20 @@ __trashman_arch() { uname -m } +# Require a program available on $PATH +__trashman_require() { + if [ -z "$1" ]; then + return + fi + + for program in $*; do + if ! which $program > /dev/null 2>&1; then + __trashman_echo "Requirement absent: $program" + exit 1 + fi + done +} + # Return the folder where actions are available for a package __trashman_actions_folder() { local package="$1" @@ -152,14 +166,48 @@ __trashman_merge() { ) } -__trashman_install_apt_key() { - local orig="$1" - local dest="$2" - if [ ! -e "$orig" ]; then - exit 1 +__trashman_check_exit_status() { + local action="$1" + local status="$2" + + if [ -z "$status" ]; then + return + fi + + if [ "$action" = "check" ]; then + if [ "$status" = "0" ]; then + __trashman_echo "Package $package is installed system-wide" + elif [ "$status" = "1" ]; then + __trashman_echo "Package $package is not installed system-wide" + elif [ "$status" = "2" ]; then + __trashman_echo "Package $package is partially installed system-wide" + fi fi - cp $orig /etc/apt/trusted.gpg.d/$dest || exit 1 - chown root.root /etc/apt/trusted.gpg.d/$dest && chmod 644 /etc/apt/trusted.gpg.d/$dest || exit 1 + if [ "$action" = "test" ]; then + if [ "$status" = "0" ]; then + __trashman_echo "Package $package is running as expected" + elif [ "$status" = "1" ]; then + __trashman_echo "Package $package is not running as expected" + fi + fi +} + +# Check for superuser privileges +__trashman_check_root() { + local action="$1" + + if [ -z "$action" ]; then + return + fi + + if [ "$action" != "install" ] && [ "$action" != "remove" ] && [ "$action" != "upgrade" ]; then + return + fi + + if [ "`whoami`" != "root" ]; then + __trashman_echo "Action $action requires root privileges" + exit 1 + fi } diff --git a/trashman b/trashman index 89546b6..539e15d 100755 --- a/trashman +++ b/trashman @@ -78,31 +78,17 @@ else if [ ! -z "`__trashman_actions $package`" ]; then folder="`__trashman_actions_folder $package`" + # Checking for privileges + __trashman_check_root $ACTION + if [ -x "$SHARE/$package/$folder/$ACTION" ]; then if [ "$ACTION" != "check" ] && [ "$ACTION" != "test" ]; then - __trashman_echo "Dumpsterizing your system: action $ACTION for $package ($folder)..." + __trashman_echo "Issuing action $ACTION for $package ($folder)..." fi + # Run action for package $SHARE/$package/$folder/$ACTION $SHARE - status="$?" - - if [ "$ACTION" = "check" ]; then - if [ "$status" = "0" ]; then - __trashman_echo "Package $package is installed system-wide" - elif [ "$status" = "1" ]; then - __trashman_echo "Package $package is not installed system-wide" - elif [ "$status" = "2" ]; then - __trashman_echo "Package $package is partially installed system-wide" - fi - fi - - if [ "$ACTION" = "test" ]; then - if [ "$status" = "0" ]; then - __trashman_echo "Package $package is running as expected" - elif [ "$status" = "1" ]; then - __trashman_echo "Package $package is not running as expected" - fi - fi + __trashman_check_exit_status $ACTION $? else __trashman_echo "No such action $ACTION for package $package, skipping" fi -- cgit v1.2.3