diff --git a/install.sh b/install.sh index 8122a01..83f56ca 100755 --- a/install.sh +++ b/install.sh @@ -1,50 +1,43 @@ #!/bin/bash - BASEDIR=$PWD -TAG=$(cat /opt/debian-bash/.semver_git_tag) +REQUIRED_PKGS=(jq rsync file git) -function usage { - echo 'usage: --host | --containers | --full | --one-container ' - exit -1 +function usage() { + echo 'usage: --host | --containers | --full | --one-container ' + exit -1 } -function install_host { +function install_host() { echo "install on: $HOSTNAME" echo ---------------------- - [ `id -u` -ne 0 ] && echo 'root privilege required' && exit 1 - - # required packages jq - /usr/bin/dpkg-query --status jq >/dev/null 2>&1 - [ $? -ne 0 ] && /usr/bin/apt install -y jq - - # required packages rsync - /usr/bin/dpkg-query --status rsync >/dev/null 2>&1 - [ $? -ne 0 ] && /usr/bin/apt install -y rsync + [ $(id -u) -ne 0 ] && echo 'root privilege required' && exit 1 + # shellcheck disable=SC1091 + source "$BASEDIR/lib/functions.sh" + idem_apt_install "${REQUIRED_PKGS[@]}" if [[ ! $BASEDIR == '/opt/debian-bash' ]]; then # download and filfull /opt/debian-bash, then run it from folder - if [[ -L /opt/debian-bash ]];then - cd /opt/debian-bash - ./install.sh $PARAM1 - exit 0 - else - - rm -rf /opt/debian-bash - TEMP=`mktemp -d` - cd $TEMP - echo $TEMP - wget https://git.artcode.re/pvincent/debian-bash/archive/master.tar.gz - tar -xzf master.tar.gz - mv debian-bash /opt/ - cd /opt/debian-bash - ./install.sh $PARAM1 - rm -rf $TEMP - exit 0 - fi + if [[ -L /opt/debian-bash ]]; then + cd /opt/debian-bash && ./install.sh "$PARAM1" + exit 0 + else + + rm -rf /opt/debian-bash + TEMP=$(mktemp -d) + cd $TEMP + echo $TEMP + wget https://git.artcode.re/pvincent/debian-bash/archive/master.tar.gz + tar -xzf master.tar.gz + mv debian-bash /opt/ + cd /opt/debian-bash + ./install.sh $PARAM1 + rm -rf $TEMP + exit 0 + fi else @@ -56,8 +49,7 @@ function install_host { ORIGINAL="ORIGINAL" declare -a arr=(/etc/bash.bashrc /etc/inputrc /etc/vim/vimrc) - for i in "${arr[@]}" - do + for i in "${arr[@]}"; do if [ ! -f "$i.$ORIGINAL" ]; then echo "$i needs installation $(basename $i)" @@ -71,13 +63,13 @@ function install_host { fi } -function install_containers { +function install_containers() { echo "install containers" echo -------------------- # install inside active LXC containers if [[ -f '/snap/bin/lxc' ]]; then - for container in `/snap/bin/lxc list --format=json | /bin/jq -r '.[] | select(.state.status == "Running") | .name'`; do + for container in $(/snap/bin/lxc list --format=json | /bin/jq -r '.[] | select(.state.status == "Running") | .name'); do install_one_container $container echo done @@ -85,7 +77,7 @@ function install_containers { } -function install_one_container { +function install_one_container() { CT=$1 echo "install container <$CT>" echo ------------------------- @@ -100,27 +92,25 @@ function install_one_container { PARAM1=$1 case $PARAM1 in - "--host") - install_host - ;; - "--containers") - install_containers - ;; - "--one-container") - CT=$2 - if [ ! -z $CT ]; then - install_one_container $CT - else - usage - fi - ;; - "--full") - install_host - install_containers - ;; - *) +"--host") + install_host + ;; +"--containers") + install_containers + ;; +"--one-container") + CT=$2 + if [ ! -z $CT ]; then + install_one_container $CT + else usage - ;; + fi + ;; +"--full") + install_host + install_containers + ;; +*) + usage + ;; esac - - diff --git a/lib/functions.sh b/lib/functions.sh index e3c3dd3..af7abe4 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -1,45 +1,53 @@ +# idempotent apt install [package1 package2 ...] +function idem_apt_install() { + for i in $@; do + if ! (/usr/bin/dpkg-query --status "$i" >/dev/null 2>&1); then + /usr/bin/apt install -y "$i" + fi + done +} ## -# return 0 (true) if array (passed by name) contains element -# usage: +# return 0 (true) if array (passed by name) contains element +# usage: # ARRAY = ( a b c ) # containsElement ARRAY 'a' => 0 -containsElement () { - local -a 'arraykeys=("${!'"$1"'[@]}")' +containsElement() { + local -a 'arraykeys=("${!'"$1"'[@]}")' if $(isArray $1); then - for index in ${arraykeys[*]}; do - current=$1"[$index]" - [[ "${!current}" == "$2" ]] && return 0; # found - done - return 1; # not found + for index in ${arraykeys[*]}; do + current=$1"[$index]" + [[ "${!current}" == "$2" ]] && return 0 # found + done + return 1 # not found else - >&2 echo "ERROR: $1 not an array!" - return 2; # not an array + echo >&2 "ERROR: $1 not an array!" + return 2 # not an array fi } -isArray(){ - [[ "$(declare -p $1 2> /dev/null)" =~ "declare -a" ]] && return 0; # is an array - return 1; # not an array +isArray() { + [[ "$(declare -p $1 2>/dev/null)" =~ "declare -a" ]] && return 0 # is an array + return 1 # not an array } ## -# -askConfirmation () { +# +askConfirmation() { case "$1" in - y|Y|yes|YES ) - QUESTION="(Y/n)?" - DEFAULT=0 - ;; - * ) - QUESTION="(y/N)?" - DEFAULT=1 - ;; + y | Y | yes | YES) + QUESTION="(Y/n)?" + DEFAULT=0 + ;; + *) + QUESTION="(y/N)?" + DEFAULT=1 + ;; esac read -p "$QUESTION : " choice - case "$choice" in - y|Y|yes|YES ) return 0;; #true - n|no|N|NO ) return 1;; #false - * ) return $DEFAULT;; - esac -} \ No newline at end of file + case "$choice" in + y | Y | yes | YES) return 0 ;; #true + n | no | N | NO) return 1 ;; #false + *) return $DEFAULT ;; + esac +}