Commit ff09922a authored by Stephan Großberndt's avatar Stephan Großberndt
Browse files

[TASK] Updated variable and condition syntax, added line breaks

parent 216a85c9
#!/bin/bash #!/bin/bash
MACHINE_CONF_FILE="/root/machine.conf" MACHINE_CONF_FILE="/root/machine.conf"
if [ ! -f "$MACHINE_CONF_FILE" ]; then if [[ ! -f "${MACHINE_CONF_FILE}" ]]; then
echo "Please check existence of $MACHINE_CONF_FILE" echo "Please check existence of ${MACHINE_CONF_FILE}"
exit 99 exit 99
fi fi
source $MACHINE_CONF_FILE source $MACHINE_CONF_FILE
if [ -z "$1" ]; then # Overwrite variables from ${MACHINE_CONF_FILE}
source ${MACHINE_CONF_FILE}
if [[ -z "$1" ]]; then
echo "You must supply a server name like srvXXX as argument" echo "You must supply a server name like srvXXX as argument"
exit 99 exit 99
else else
SERVER_NAME=$1 SERVER_NAME=$1
SERVER_IPV4_ADDRESS=$(echo "$SERVER_NAME" | sed -r -n 's/(srv)([0-9]{3})/\2/p') SERVER_IPV4_ADDRESS=$(echo "${SERVER_NAME}" | sed -r -n 's/(srv)([0-9]{3})/\2/p')
if [[ ! "$SERVER_IPV4_ADDRESS" =~ ^[0-9]{3}$ ]]; then if [[ ! "${SERVER_IPV4_ADDRESS}" =~ ^[0-9]{3}$ ]]; then
echo "Could not validate IPv4: $SERVER_IPV4_ADDRESS" echo "Could not validate IPv4: ${SERVER_IPV4_ADDRESS}"
exit 99 exit 99
else else
NET_IPV4_ADDRESS="$SERVER_IPV4_BLOCK.$SERVER_IPV4_ADDRESS" NET_IPV4_ADDRESS="${SERVER_IPV4_BLOCK}.${SERVER_IPV4_ADDRESS}"
fi fi
fi fi
# we should now have the following variables # we should now have the following variables
echo "SERVER_NAME: $SERVER_NAME" echo "SERVER_NAME: ${SERVER_NAME}"
#echo "NET_IPV4_MAC $NET_IPV4_MAC" #echo "NET_IPV4_MAC ${NET_IPV4_MAC}"
echo "NET_IPV4_ADDRESS: $NET_IPV4_ADDRESS" echo "NET_IPV4_ADDRESS: ${NET_IPV4_ADDRESS}"
echo "NET_IPV4_GATEWAY: $NET_IPV4_GATEWAY" echo "NET_IPV4_GATEWAY: ${NET_IPV4_GATEWAY}"
echo "NET_IPV4_BROADCAST: $NET_IPV4_BROADCAST" echo "NET_IPV4_BROADCAST: $NET_IPV4_BROADCAST"
echo "NET_IPV4_NETMASK: $NET_IPV4_NETMASK" echo "NET_IPV4_NETMASK: ${NET_IPV4_NETMASK}"
read -p "Do you want to create a machine with this config? " -n 1 -r read -p "Do you want to create a machine with this config (y/n)?\n" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]] if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then then
exit 1 exit 1
fi fi
...@@ -52,15 +55,15 @@ fi ...@@ -52,15 +55,15 @@ fi
#NET_IPV6_NETMASK="64" #NET_IPV6_NETMASK="64"
#NET_IPV6_GATEWAY="fe80::1" #NET_IPV6_GATEWAY="fe80::1"
HOST_NAME=$SERVER_NAME HOST_NAME=${SERVER_NAME}
# create passwd # create passwd
PASSWORD=$(makepasswd --minchars=10 --maxchars=12) PASSWORD=$(makepasswd --minchars=10 --maxchars=12)
echo "Generated random password" echo "\nGenerated random password"
echo "ADD TO PASSWORD STORE/KEEP IN SAFE PLACE" echo "ADD TO PASSWORD STORE / KEEP IN SAFE PLACE"
echo $PASSWORD echo ${PASSWORD}
read -p "Are you ready to continue? " -n 1 -r read -p "Are you ready to continue (y/n)?\n" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]] if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then then
exit 1 exit 1
fi fi
...@@ -72,7 +75,7 @@ wait_for_shutdown() ...@@ -72,7 +75,7 @@ wait_for_shutdown()
echo "Waiting for shutdown of $1" echo "Waiting for shutdown of $1"
while true; do while true; do
virsh list | grep -c $1 > /dev/null virsh list | grep -c $1 > /dev/null
if [ $? -eq 1 ]; then if [[ $? -eq 1 ]]; then
echo echo
echo "VM stopped: $1" echo "VM stopped: $1"
return 0 return 0
...@@ -83,19 +86,19 @@ wait_for_shutdown() ...@@ -83,19 +86,19 @@ wait_for_shutdown()
} }
# note on preseeding # note on preseeding
# file must be injected in the root/intrd filesysetm # file must be injected in the root/initrd filesysetm
# if file is not named preseed.cfg the file name must be passed to the kerne like preseed/file=/some/whatever.cfg # if file is not named preseed.cfg the file name must be passed to the kernel like preseed/file=/some/whatever.cfg
# see here for the full story: # see here for the full story:
# https://www.debian.org/releases/stable/amd64/apbs02.html.en#preseed-loading # https://www.debian.org/releases/stable/amd64/apbs02.html.en#preseed-loading
#VOLUME=$SERVER_NAME #VOLUME=${SERVER_NAME}
#--network bridge=br0 \ #--network bridge=br0 \
#--network bridge=br-ext,mac=$NET_IPV4_MAC,model=virtio \ #--network bridge=br-ext,mac=${NET_IPV4_MAC},model=virtio \
virt-install \ virt-install \
--connect qemu:///system \ --connect qemu:///system \
--name $SERVER_NAME \ --name ${SERVER_NAME} \
--ram 2048 \ --ram 2048 \
--disk pool=vgpool,size=20,bus=ide \ --disk pool=vgpool,size=20,bus=ide \
--vcpus 2 \ --vcpus 2 \
...@@ -105,31 +108,31 @@ virt-install \ ...@@ -105,31 +108,31 @@ virt-install \
--graphics none \ --graphics none \
--console pty,target_type=serial \ --console pty,target_type=serial \
--location 'http://deb.debian.org/debian/dists/jessie/main/installer-amd64/' \ --location 'http://deb.debian.org/debian/dists/jessie/main/installer-amd64/' \
--initrd-inject '/usr/local/virtinstaller/preseed.cfg' \ --initrd-inject "/usr/local/virtinstaller/preseed.cfg" \
--extra-args "auto=true netcfg/get_hostname=$HOST_NAME netcfg/hostname=$HOST_NAME passwd/root-password=$PASSWORD passwd/root-password-again=$PASSWORD netcfg/get_ipaddress=$NET_IPV4_ADDRESS netcfg/get_netmask=$NET_IPV4_NETMASK netcfg/get_gateway=$NET_IPV4_GATEWAY console=ttyS0,115200n8 serial" --extra-args "auto=true netcfg/get_hostname=${HOST_NAME} netcfg/hostname=${HOST_NAME} passwd/root-password=${PASSWORD} passwd/root-password-again=${PASSWORD} netcfg/get_ipaddress=${NET_IPV4_ADDRESS} netcfg/get_netmask=${NET_IPV4_NETMASK} netcfg/get_gateway=${NET_IPV4_GATEWAY} console=ttyS0,115200n8 serial"
echo "Finished with virt-install." echo "Finished with virt-install."
read -p "Did the installation finish and the VM shut down? " -n 1 -r read -p "Did the installation finish and the VM shut down?\n" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]] if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then then
exit 1 exit 1
fi fi
echo "Doing some aftermath..." echo "\nDoing some aftermath..."
# shutdown # shutdown
virsh destroy $SERVER_NAME virsh destroy ${SERVER_NAME}
wait_for_shutdown $SERVER_NAME wait_for_shutdown ${SERVER_NAME}
# copy ssh-keys of admins into machine # copy ssh-keys of admins into machine
# generate ssh-authorized keys so they can be uploaded into the root-account # generate ssh-authorized keys so they can be uploaded into the root-account
SCRIPT_PATH=$(dirname $([ -L $0 ] && readlink -f $0 || echo $0)) SCRIPT_PATH=$(dirname $([[ -L $0 ]] && readlink -f $0 || echo $0))
$SCRIPT_PATH/gatherkeys.sh > $SCRIPT_PATH/authorized_keys ${SCRIPT_PATH}/gatherkeys.sh > ${SCRIPT_PATH}/authorized_keys
virt-copy-in -d $SERVER_NAME $SCRIPT_PATH/authorized_keys /root/.ssh/ virt-copy-in -d ${SERVER_NAME} ${SCRIPT_PATH}/authorized_keys /root/.ssh/
# start again # start again
virsh start $SERVER_NAME virsh start ${SERVER_NAME}
# set domain to autostart # set domain to autostart
virsh autostart $SERVER_NAME virsh autostart ${SERVER_NAME}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment