tlu/update.sh
2021-09-22 11:48:11 +02:00

140 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
set -aeou pipefail
function printHelp(){
cat << EOF
Usage ${0##*/} [options..]
-h,-?, --help Show help and exit
-N, --network configure network settings
-s, --salt run a masterless salt-call
-y, --yes answer 'yes' on all questions
EOF
}
function asktobreak(){
if [[ "$ANSWER_YES" == true ]];then
printf "\n"
return 1
fi
printf "Do you want to run this step (y/n)"
while read -r -n 1 -s answer; do
if [[ $answer == [YyNn] ]];then
[[ $answer == [Yy] ]] && return_value=1
[[ $answer == [Nn] ]] && return_value=0
printf "\n"
break;
fi
done
return $return_value
}
function new_log(){
# script output
printf "\n**** [%s] ***\n" "$*"
}
function log(){
printf "%s\n" "$*"
}
function check_prerequisites(){
set +e
local answer
type "$1" > /dev/null 2>&1
RESULT=$?
if [[ $RESULT != 0 ]];then
printf "$1 is not installed, do your want to install it [y/N]?"
read -r -n 1 -s answer;
printf "\n"
if [[ $answer == [yY] ]];then
sudo zypper --non-interactive install "$1"
fi
fi
set -e
}
function do_salt_call(){
local salt="sudo salt-call --local --file-root $HOME/tle/salt/states --pillar-root $HOME/tle/salt/pillars"
new_log "Running salt high state"
if asktobreak;then
return
fi
$salt state.apply pillar="{username: $USER}"
}
function configure_network(){
new_log "Configure network"
if asktobreak; then
return
fi
source network.env
sudo nmcli connection show
sudo nmcli connection modify "$INTERNAL_IF" ipv4.addresses "$INTERNAL_ADDRESS"
sudo nmcli connection modify "$INTERNAL_IF" ipv4.dns "127.0.0.1, 1.1.1.1, 1.1.1.2"
sudo nmcli connection modify "$INTERNAL_IF" ipv4.method manual
sudo nmcli connection down "$INTERNAL_IF"
sudo nmcli connection up "$INTERNAL_IF"
sudo nmcli connection add type vlan con-name "$EXTERNAL_IF" ifname "$EXTERNAL_IF" dev "$INTERNAL_IF" id "${EXTERNAL_IF#*.}"
sudo nmcli connection modify "$EXTERNAL_IF" ipv4.method auto
sudo nmcli connection up "$EXTERNAL_IF"
}
#########################
#
# Main Script
#
########################
#initialize all options
ALL=true
SALT=false
NETWORK=false
ANSWER_YES=false
while :; do
case ${1-noop} in
-h|-\?|--help)
printHelp
exit
;;
-s|--salt)
SALT=true
ALL=false
;;
-N|--network)
NETWORK=true
ALL=false
;;
-y|--yes)
ANSWER_YES=true
;;
--) #End of all options
shift
break
;;
-?*)
printf "'%s' is not a valid option\n" "$1" >&2
exit 1
;;
*) #Break out of case, no more options
break
esac
shift
done
for cmd in git salt-minion curl;do
check_prerequisites "$cmd"
done
[[ $ALL == true ]] || [[ $NETWORK == true ]] && configure_network
[[ $ALL == true ]] || [[ $SALT == true ]] && do_salt_call
printf "\n DONE!!!!!!!\n"