Browse Source

idem__apt_install

master
pvincent 2 years ago
parent
commit
7fcd815abf
  1. 7
      install.sh
  2. 13
      lib/functions.sh
  3. 25
      tools/idem_apt_install

7
install.sh

@ -55,13 +55,10 @@ function install_host() {
echo "$i" already overriden echo "$i" already overriden
fi fi
done done
source /etc/bash.bashrc
# shellcheck disable=SC1091
source "$CURDIR/lib/functions.sh"
idem_apt_install "${REQUIRED_PKGS[@]}"
sudo /opt/debian_bash/tools/idem_apt_install "${REQUIRED_PKGS[@]}"
fi fi
} }
function install_containers() { function install_containers() {

13
lib/functions.sh

@ -1,18 +1,5 @@
#!/bin/bash #!/bin/bash
# idempotent apt install [package1 package2 ...]
function idem_apt_install() {
[ -z "${VERBOSE:-}" ] && VERBOSE=false
# || echo "VERBOSE is set to $VERBOSE"
for i in "$@"; do
if ! dpkg -l "$i" 2>/dev/null | grep -q ^ii; then
sudo apt install -y "$i"
elif $VERBOSE; then
echo "apt package <$i> already installed!"
fi
done
}
## ##
# return 0 (true) if array (passed by name) contains element # return 0 (true) if array (passed by name) contains element
# usage: # usage:

25
tools/idem_apt_install

@ -0,0 +1,25 @@
#!/bin/bash
function usage() {
local BASECMD
BASECMD=$(basename "$0")
echo "usage: $BASECMD <packages...>"
echo 'idempotent debian package installation : update if necessary, install only if not yet done'
exit 1
}
[ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 2
[[ $# -lt 1 ]] && usage
if [ "$(date --date='-12 hours' +%s)" -gt "$(date -d "$(stat -c %y /var/lib/apt/lists/partial)" +%s)" ]; then
echo "updating repositoring..."
apt update
fi
for i in "$@"; do
if ! dpkg -l "$i" 2>/dev/null | grep -q ^ii; then
sudo apt install -y "$i"
elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then
echo "apt package <$i> already installed!"
fi
done
Loading…
Cancel
Save