deploy.sh/deploy.sh

255 lines
6.4 KiB
Bash
Executable File

#!/bin/sh
# shellcheck disable=SC1090
# shellcheck disable=SC2154
# CONF_FILE "_vms.conf" needs to have the following variables:
## Server config for install-<mac>.conf
#SERVER="server1"
#DOMAIN="example.com"
## IP / MAC config
#IP_PREFIX="192.168.0"
#IP_START=100
#IPV6_PREFIX="fe1:dead:beef"
#IPV6_START=1000
#MAC_PREFIX="fe:1e:bb:4d:3c"
## .conf locations
#VMS="/root/vms"
#ETC="/etc"
#IMAGES="/var/vmm"
#HTDOCS="/var/www/htdocs"
## vm.conf
#VMDUSERS="vmdusers"
#UPLINK="uplink_vlan42"
#BRIDGE="bridge42"
## dhcpd.conf
#ROUTER="192.168.0.1"
#DNS="192.186.0.1"
#SUBNET="192.168.0.1"
#NETMASK="255.255.255.0"
#RANGE="192.168.1.10 192.168.1.10"
set -e
main () {
CONF_FILE="$PWD/_vms.conf"
[ -f "$CONF_FILE" ] && . "$CONF_FILE"
date=$(date "+%Y-%m-%d %H:%M:%S")
echo "New config files created for $SERVER @ $date"
fs=$(
list_files "$VMS"
)
if test -n "$fs"
then
echo "$fs"
echo "$fs" |
render_vm_conf > "${ETC}/vm.conf"
echo "$fs" |
render_dhcpd_conf > "${ETC}/dhcpd.conf"
echo "$fs" |
render_install_conf
echo "$fs" |
create_images
echo "$fs" |
create_users
fi
}
list_files() {
# Find all the VM config files.
# Takes the directory with vm*.txt files.
find "$1" -type f -name "vm*.txt" -maxdepth 1 | sort | xargs grep -l "message"
}
find_vm() {
# Find the number of the VM (VM#).
# Takes the directory with vm*.txt files and instance name as variable.
find "$1" -type f -name "vm*.txt" -maxdepth 1 | xargs grep -l "$2" | sed 's/^\.\/vm//;s/\.txt$//'
}
fetch_mac() {
# Fetch the MAC address for the VM.
# Takes the MAC_PREFIX and VM#.
# print the MAC address
echo "${1}:${2}"
}
fetch_ip() {
# Fetch the IP address for the VM.
# Takes the IP_PREFIX, IP_START and VM#.
# add IP_START and VM# and print the IP address
# print the IP address
_prefix=$1
_host=$(($2 + $3))
echo "${_prefix}.${_host}"
}
fetch_ipv6() {
# Fetch the IPv6 address of the VM.
# Takes the IPV6_PREFIX, IPV6_START, IP_START and VM#
# add IPV6_START and VM#, IP_START and VM# and print the IPv6 address
# print the IPv6 address
_prefix=$1
_subnet=$(($2 + $4))
_host=$(($3 + $4))
echo "${_prefix}:${_subnet}::${_host}"
}
generate_passwd() {
# Generate a random password for the install-<MAC>.conf file.
# Doesn't take variables.
tr -cd '[:print:]' < /dev/urandom | fold -w 20 | head -n 1
}
render_vm_conf() {
# Generate vm.comf
# Takes defaults from of _vms.conf and iterate over the vm*.txt files.
# When the owner exists add "owner".
# When the VM image doesn't exist add "boot".
# fetch_mac() to get the correct MAC address of the VM.
printf "#\\n# File generated on %s\\n#\\n" "$date"
printf "socket owner :%s\\n\\n" "$VMDUSERS"
printf "switch \"%s\" {\\n" "$UPLINK"
printf "\\tinterface %s\\n" "$BRIDGE"
printf "}\\n\\n"
while read -r f
do
. "$f"
printf "vm \"%s\" {\\n" "$instance"
printf "\\tdisable\\n"
if test -n "$owner"
then
printf "\\towner %s\\n" "$owner"
fi
if ! test -f "${IMAGES}/${instance}.img"
then
printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES"
fi
printf "\\tdisk \"%s/%s.img\"\\n" "$IMAGES" "$instance"
printf "\\tinterface tap {\\n"
printf "\\t\\tswitch \"uplink_vlan921\"\\n"
printf "\\t\\tlladdr %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$instance")")"
printf "\\t}\\n"
printf "}\\n"
done
}
render_dhcpd_conf() {
# Generate dhcpd.comf
# Takes defaults from of _vms.conf and iterate over the vm*.txt files.
# When the VM image doesn't exist add "auto_install".
# When the VM image does exist add "auto_upgrade".
# fetch_mac() to get the correct MAC address of the VM.
# fetch_ip() to get the correct IP address of the VM.
printf "#\\n# File generated on %s\\n#\\n" "$date"
printf "option domain-name \"%s\";\\n" "$DOMAIN"
printf "option domain-name-servers \"%s\";\\n\\n" "$DNS"
printf "subnet %s netmask %s {\\n" "$SUBNET" "$NETMASK"
printf "\\toption routers %s;\\n" "$ROUTER"
printf "\\tserver-name \"%s.%s\";\\n" "$SERVER" "$DOMAIN"
printf "\\trange %s;\\n\\n" "$RANGE"
while read -r f
do
. "$f"
printf "\\thost %s {\\n" "$instance"
printf "\\t\\thardware ethernet %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$instance")")"
printf "\\t\\tfixed-address %s\\n" "$(fetch_ip "$IP_PREFIX" "$IP_START" "$(find_vm "$VMS" "$instance")")"
if ! test -f "${IMAGES}/${instance}.img"
then
printf "\\t\\tfilename \"auto_install\"\\n"
else
printf "\\t\\tfilename \"auto_upgrade\"\\n"
fi
printf "\\t\\toption host-name \"%s\"\\n" "$hostname"
printf "\\t}\\n"
done
printf "}\\n"
}
render_install_conf() {
# Generate install-<mac>.comf
# Takes defaults from of _vms.conf and iterate over the vm*.txt files.
# When the VM image doesn't exist create the install-<mac>.conf file.
# When the VM image does exist remove the install-<mac>.conf file.
# fetch_ipv6() to get the correct IPv6 address of the VM.
# fetch_mac() to get the correct MAC address of the VM.
while read -r f
do
. "$f"
_pass="$(generate_passwd)"
_ipv6=$(fetch_ipv6 "$IPV6_PREFIX" "$IPV6_START" "$IP_START" "$(find_vm "$VMS" "$instance")")
_ipv6_gateway=$(echo "$_ipv6" | sed -e 's/::[0-9]*$/::1/g')
_mac=$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$instance")")
if ! test -f "${IMAGES}/${instance}.img"
then
cat <<-EOF > "${HTDOCS}/install-${_mac}.conf"
#
# File generated on $date
#
System hostname = $hostname
Password for root = $_pass
Which speed should com0 = 115200
Network interfaces = vio0
IPv4 address for vio0 = dhcp
IPv6 address for vio0 = $_ipv6
IPv6 default router = $_ipv6_gateway
Setup a user = $username
Password for user = $_pass
Public ssh key for user = $message $_pass
Which disk is the root disk = sd0
What timezone are you in = Europe/Amsterdam
Location of sets = http
Server = ftp.nluug.nl
Set name(s) = -x* +xb* +xf*
EOF
echo "Install file created: ${HTDOCS}/install-${_mac}.conf"
else
if test -f "${HTDOCS}/install-${_mac}.conf"
then rm -rf "${HTDOCS}/install-${_mac}.conf"
fi
fi
done
}
create_images() {
while read -r f
do
. "$f"
if ! test -f "${IMAGES}/${instance}.img"
then vmctl create "${IMAGES}/${instance}.img" -s 50G > /dev/null
echo "Image file created: ${IMAGES}/${instance}.img"
fi
done
}
create_users() {
while read -r f
do
. "$f"
if test -n "$owner"
then
if ! grep -e "^$owner" /etc/passwd > /dev/null
then
useradd -m -G "$VMDUSERS" "$owner"
echo "$message" > "/home/${owner}/.ssh/authorized_keys"
echo "User created: $owner"
fi
fi
done
}
restart_service() {
rcctl restart dhcpd
vmctl reload
}
main "$@"