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
MACHINE_CONF_FILE="/root/machine.conf"
if [ ! -f "$MACHINE_CONF_FILE" ]; then
echo "Please check existence of $MACHINE_CONF_FILE"
if [[ ! -f "${MACHINE_CONF_FILE}" ]]; then
echo "Please check existence of ${MACHINE_CONF_FILE}"
exit 99
fi
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"
exit 99
else
SERVER_NAME=$1
SERVER_IPV4_ADDRESS=$(echo "$SERVER_NAME" | sed -r -n 's/(srv)([0-9]{3})/\2/p')
if [[ ! "$SERVER_IPV4_ADDRESS" =~ ^[0-9]{3}$ ]]; then
echo "Could not validate IPv4: $SERVER_IPV4_ADDRESS"
SERVER_IPV4_ADDRESS=$(echo "${SERVER_NAME}" | sed -r -n 's/(srv)([0-9]{3})/\2/p')
if [[ ! "${SERVER_IPV4_ADDRESS}" =~ ^[0-9]{3}$ ]]; then
echo "Could not validate IPv4: ${SERVER_IPV4_ADDRESS}"
exit 99
else
NET_IPV4_ADDRESS="$SERVER_IPV4_BLOCK.$SERVER_IPV4_ADDRESS"
NET_IPV4_ADDRESS="${SERVER_IPV4_BLOCK}.${SERVER_IPV4_ADDRESS}"
fi
fi
# 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_ADDRESS: $NET_IPV4_ADDRESS"
echo "NET_IPV4_GATEWAY: $NET_IPV4_GATEWAY"
#echo "NET_IPV4_MAC ${NET_IPV4_MAC}"
echo "NET_IPV4_ADDRESS: ${NET_IPV4_ADDRESS}"
echo "NET_IPV4_GATEWAY: ${NET_IPV4_GATEWAY}"
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
if [[ ! $REPLY =~ ^[Yy]$ ]]
read -p "Do you want to create a machine with this config (y/n)?\n" -n 1 -r
if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then
exit 1
fi
......@@ -52,15 +55,15 @@ fi
#NET_IPV6_NETMASK="64"
#NET_IPV6_GATEWAY="fe80::1"
HOST_NAME=$SERVER_NAME
HOST_NAME=${SERVER_NAME}
# create passwd
PASSWORD=$(makepasswd --minchars=10 --maxchars=12)
echo "Generated random password"
echo "ADD TO PASSWORD STORE/KEEP IN SAFE PLACE"
echo $PASSWORD
read -p "Are you ready to continue? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
echo "\nGenerated random password"
echo "ADD TO PASSWORD STORE / KEEP IN SAFE PLACE"
echo ${PASSWORD}
read -p "Are you ready to continue (y/n)?\n" -n 1 -r
if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then
exit 1
fi
......@@ -72,7 +75,7 @@ wait_for_shutdown()
echo "Waiting for shutdown of $1"
while true; do
virsh list | grep -c $1 > /dev/null
if [ $? -eq 1 ]; then
if [[ $? -eq 1 ]]; then
echo
echo "VM stopped: $1"
return 0
......@@ -83,19 +86,19 @@ wait_for_shutdown()
}
# note on preseeding
# file must be injected in the root/intrd filesysetm
# if file is not named preseed.cfg the file name must be passed to the kerne like preseed/file=/some/whatever.cfg
# file must be injected in the root/initrd filesysetm
# 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:
# https://www.debian.org/releases/stable/amd64/apbs02.html.en#preseed-loading
#VOLUME=$SERVER_NAME
#VOLUME=${SERVER_NAME}
#--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 \
--connect qemu:///system \
--name $SERVER_NAME \
--name ${SERVER_NAME} \
--ram 2048 \
--disk pool=vgpool,size=20,bus=ide \
--vcpus 2 \
......@@ -105,31 +108,31 @@ virt-install \
--graphics none \
--console pty,target_type=serial \
--location 'http://deb.debian.org/debian/dists/jessie/main/installer-amd64/' \
--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"
--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"
echo "Finished with virt-install."
read -p "Did the installation finish and the VM shut down? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
read -p "Did the installation finish and the VM shut down?\n" -n 1 -r
if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then
exit 1
fi
echo "Doing some aftermath..."
echo "\nDoing some aftermath..."
# 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
# 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/gatherkeys.sh > $SCRIPT_PATH/authorized_keys
virt-copy-in -d $SERVER_NAME $SCRIPT_PATH/authorized_keys /root/.ssh/
SCRIPT_PATH=$(dirname $([[ -L $0 ]] && readlink -f $0 || echo $0))
${SCRIPT_PATH}/gatherkeys.sh > ${SCRIPT_PATH}/authorized_keys
virt-copy-in -d ${SERVER_NAME} ${SCRIPT_PATH}/authorized_keys /root/.ssh/
# start again
virsh start $SERVER_NAME
virsh start ${SERVER_NAME}
# 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