2021-09-22 07:06:10 +00:00
#!/bin/bash
set -aeou pipefail
function printHelp( ) {
cat << EOF
Usage ${ 0 ##*/ } [ options..]
-h,-?, --help Show help and exit
2021-09-22 09:48:11 +00:00
-N, --network configure network settings
2021-09-22 07:06:10 +00:00
-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" " $* "
}
2021-09-22 14:33:00 +00:00
function parse_yaml( ) {
# ripped from https://gist.github.com/pkuczynski/8665367#gistcomment-2174214
local yaml_file = $1
local prefix = $2
local s
local w
local fs
s = '[[:space:]]*'
w = '[a-zA-Z0-9_]*'
fs = " $( echo @| tr @ '\034' ) "
(
sed -ne 's/--//g; s/\"/\\\"/g; s/\#.*//g; s/\s*$//g;' \
-e " s|^\( $s \)\( $w \) $s : $s \"\(.*\)\" $s \$|\1 $fs \2 $fs \3|p " \
-e " s|^\( $s \)\( $w \) $s [:-] $s \(.*\) $s \$|\1 $fs \2 $fs \3|p " |
awk -F" $fs " ' {
indent = length( $1 ) /2;
if ( length( $2 ) = = 0) { conj[ indent] = "+" ; } else { conj[ indent] = "" ; }
vname[ indent] = $2 ;
for ( i in vname) { if ( i > indent) { delete vname[ i] } }
if ( length( $3 ) > 0) {
vn = "" ; for ( i = 0; i<indent; i++) { vn = ( vn) ( vname[ i] ) ( "_" ) }
printf( "%s%s%s%s=(\"%s\")\n" , "'" $prefix "'" ,vn, $2 , conj[ indent-1] ,$3 ) ;
}
} ' |
sed 's/_=/+=/g'
) < " $yaml_file "
2021-09-22 10:30:23 +00:00
}
2021-09-22 07:06:10 +00:00
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 } "
}
2021-09-22 09:48:11 +00:00
function configure_network( ) {
new_log "Configure network"
if asktobreak; then
return
fi
2021-09-22 10:30:23 +00:00
eval $( parse_yaml salt/pillars/network.sls "SALT_" )
if [ [ -f salt/pillars/local.sls ] ] ; then
eval $( parse_yaml salt/pillars/local.sls "SALT_" )
fi
2021-09-22 14:33:00 +00:00
#configure internal interface
sudo nmcli connection modify " $SALT_network_interface_internal " ipv4.addresses " ${ SALT_network_ip } / ${ SALT_network_netmask } "
2021-09-22 10:30:23 +00:00
sudo nmcli connection modify " $SALT_network_interface_internal " ipv4.dns "127.0.0.1, 1.1.1.1, 1.1.1.2"
sudo nmcli connection modify " $SALT_network_interface_internal " ipv4.method manual
sudo nmcli connection down " $SALT_network_interface_internal "
sudo nmcli connection up " $SALT_network_interface_internal "
2021-09-22 14:33:00 +00:00
#configure external interface
2021-09-22 10:30:23 +00:00
if [ [ $( nmcli connection show | grep -i $SALT_network_interface_external | wc -l) = = 0 ] ] ; then
sudo nmcli connection add type vlan con-name " $SALT_network_interface_external " ifname " $SALT_network_interface_external " dev " $SALT_network_interface_internal " id " ${ SALT_network_interface_external #*. } "
fi
sudo nmcli connection modify " $SALT_network_interface_external " ipv4.method auto
sudo nmcli connection up " $SALT_network_interface_external "
2021-09-22 14:33:00 +00:00
#configure vlan interfaces
local len = ${# SALT_network_vlan__id [@] }
for ( ( i = 0; i<$len ; i++ ) ) ; do
ifname = " vlan. ${ SALT_network_vlan__id [ $i ] } "
if [ [ $( nmcli connection show | grep -i " $ifname " | wc -l) = = 0 ] ] ; then
sudo nmcli connection add type vlan con-name " $ifname " ifname " $ifname " dev " $SALT_network_interface_internal " id " ${ SALT_network_vlan__id [ $i ] } "
fi
sudo nmcli connection modify " $ifname " ipv4.addresses " ${ SALT_network_vlan__address [ $i ] } / ${ SALT_network_vlan__netmask [ $i ] } "
sudo nmcli connection modify " $ifname " ipv4.method manual
sudo nmcli connection up " $ifname "
done
2021-09-22 09:48:11 +00:00
}
2021-09-22 07:06:10 +00:00
#########################
#
# Main Script
#
########################
#initialize all options
ALL = true
SALT = false
2021-09-22 09:48:11 +00:00
NETWORK = false
2021-09-22 07:06:10 +00:00
ANSWER_YES = false
while :; do
case ${ 1 -noop } in
-h| -\? | --help)
printHelp
exit
; ;
-s| --salt)
SALT = true
ALL = false
; ;
2021-09-22 09:48:11 +00:00
-N| --network)
NETWORK = true
ALL = false
; ;
2021-09-22 07:06:10 +00:00
-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
2021-09-22 09:48:11 +00:00
[ [ $ALL = = true ] ] || [ [ $NETWORK = = true ] ] && configure_network
2021-09-22 07:06:10 +00:00
[ [ $ALL = = true ] ] || [ [ $SALT = = true ] ] && do_salt_call
printf "\n DONE!!!!!!!\n"