deploy.sh/deploy.sh

331 lines
8.2 KiB
Bash
Raw Permalink Normal View History

2018-09-18 19:20:43 +02:00
#!/bin/sh
# shellcheck disable=SC1090
2018-09-24 22:25:26 +02:00
# shellcheck disable=SC2038
2018-09-18 19:20:43 +02:00
# shellcheck disable=SC2154
2018-10-07 22:14:02 +02:00
# Deploy script for OpenBSD Amsterdam
2018-09-18 19:20:43 +02:00
# CONF_FILE "_vms.conf" needs to have the following variables:
## Server config for <MAC>-install.conf
2018-09-18 19:20:43 +02:00
#SERVER="server1"
#DOMAIN="example.com"
2018-09-24 14:58:03 +02:00
## 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"
2018-09-18 19:20:43 +02:00
#ETC="/etc"
2018-09-24 14:58:03 +02:00
#IMAGES="/var/vmm"
2018-09-18 19:20:43 +02:00
#HTDOCS="/var/www/htdocs"
2018-09-24 14:58:03 +02:00
## vm.conf
2018-09-18 19:20:43 +02:00
#VMDUSERS="vmdusers"
#UPLINK="uplink_vlan42"
#BRIDGE="bridge42"
2018-11-25 16:59:14 +01:00
#Optional
#UPLINK2="uplink_vlan421"
#BRIDGE2="bridge421"
2018-09-24 14:58:03 +02:00
## dhcpd.conf
2018-09-18 19:20:43 +02:00
#ROUTER="192.168.0.1"
2018-09-24 14:58:03 +02:00
#DNS="192.186.0.1"
#SUBNET="192.168.0.1"
2018-09-18 19:20:43 +02:00
#NETMASK="255.255.255.0"
2018-09-24 14:58:03 +02:00
#RANGE="192.168.1.10 192.168.1.10"
2018-09-18 19:20:43 +02:00
set -e
2018-10-10 21:55:43 +02:00
umask 022
2018-09-18 19:20:43 +02:00
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"
2018-09-24 23:32:46 +02:00
echo "VM definition files in: $VMS"
2018-09-18 19:20:43 +02:00
fs=$(
2018-09-24 14:58:03 +02:00
list_files "$VMS"
2018-09-18 19:20:43 +02:00
)
if test -n "$fs"
then
echo "$fs"
echo "$fs" |
2018-09-24 14:58:03 +02:00
render_vm_conf > "${ETC}/vm.conf"
2018-09-18 19:20:43 +02:00
echo "$fs" |
2018-09-24 14:58:03 +02:00
render_dhcpd_conf > "${ETC}/dhcpd.conf"
2018-09-18 19:20:43 +02:00
echo "$fs" |
render_install_conf
echo "$fs" |
create_images
echo "$fs" |
create_users
fi
2018-09-24 14:58:03 +02:00
}
list_files() {
# Find all the VM config files.
2018-09-24 22:25:26 +02:00
# Takes the directory with vm*.txt files
find "$1" -type f -name "vm*.txt" -maxdepth 1 | sort -V | xargs grep -l "message"
2018-09-24 14:58:03 +02:00
}
2018-09-18 19:20:43 +02:00
2018-09-24 14:58:03 +02:00
find_vm() {
# Find the number of the VM (VM#).
2018-09-24 22:25:26 +02:00
# Takes the directory with vm*.txt files and instance
# Checks if instance exists otherwise returns the vm filename
2018-09-24 23:32:46 +02:00
if _vm=$(cd "$1" && find . -type f -name "vm*.txt" -maxdepth 1 | xargs grep -l "$2")
2018-09-24 22:25:26 +02:00
then echo "$_vm" | sed 's/^\.\/vm//;s/\.txt$//'
else echo "$2" | sed 's/^vm//'
fi
2018-09-18 19:20:43 +02:00
}
2018-09-24 14:58:03 +02:00
fetch_mac() {
# Fetch the MAC address for the VM.
2018-09-24 22:25:26 +02:00
# Takes the MAC_PREFIX and VM#
2018-09-24 14:58:03 +02:00
# print the MAC address
echo "${1}:${2}"
}
2018-11-25 16:55:37 +01:00
fetch_switch() {
# Fetch the uplink for the VM.
# Takes the UPLINK and switch.
# print the switch
if test -z "$2"
then echo $1
else echo $2
fi
}
2018-09-24 14:58:03 +02:00
fetch_ip() {
# Fetch the IP address for the VM.
2018-11-25 16:55:37 +01:00
# Takes the IP_PREFIX, IP_START, VM# and ip.
2018-09-24 14:58:03 +02:00
# add IP_START and VM# and print the IP address
# print the IP address
2018-11-25 16:55:37 +01:00
if test -z "$4"
then
_prefix=$1
_host=$(($2 + $3))
echo "${_prefix}.${_host}"
else echo "$4"
fi
2018-09-24 14:58:03 +02:00
}
fetch_ipv6() {
# Fetch the IPv6 address of the VM.
2018-11-25 16:55:37 +01:00
# Takes the IPV6_PREFIX, IPV6_START, IP_START, VM# and ipv6.
2018-09-24 14:58:03 +02:00
# add IPV6_START and VM#, IP_START and VM# and print the IPv6 address
# print the IPv6 address
2018-11-25 16:55:37 +01:00
if test -z "$5"
then
_prefix=$1
_subnet=$(($2 + $4))
_host=$(($3 + $4))
echo "${_prefix}:${_subnet}::${_host}"
else echo "$5"
fi
2018-09-18 19:20:43 +02:00
}
2018-09-24 14:58:03 +02:00
generate_passwd() {
# Generate a random password for the <MAC>-install.conf file.
2018-09-24 22:25:26 +02:00
# Doesn't take variables
#tr -cd '[:alnum:]' < /dev/urandom | fold -w 20 | head -n 1
jot -rcs '' 20 33 126
2018-09-24 14:58:03 +02:00
}
2018-09-24 22:25:26 +02:00
check_instance() {
# Check if the instance name exists, otherwise return filename as VM.
# Takes vm*.txt and instance
# prints either filename or instance variable
if test -z "$2"
2018-09-24 23:32:46 +02:00
then echo "$1" | sed "s@^$VMS@@;s@^/@@;s/\\.txt$//"
2018-09-24 22:25:26 +02:00
else echo "$2"
fi
}
check_owner() {
# Check if the owner name exists, otherwise returns username.
# Takes username and owner
# prints either owner or username
if test -z "$2"
then echo "$1"
else echo "$2"
fi
}
clear_variables() {
# Clears all variables in vm*.txt.
# Doesn't take variables
unset instance
unset date
unset payment
unset donated
unset owner
unset name
unset email
unset message
unset hostname
2018-11-25 16:55:37 +01:00
unset ip
unset ipv6
2018-09-24 22:25:26 +02:00
unset username
unset note
}
2018-09-24 14:58:03 +02:00
2018-09-18 19:20:43 +02:00
render_vm_conf() {
2018-09-24 14:58:03 +02:00
# 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"
2018-09-18 19:20:43 +02:00
printf "socket owner :%s\\n\\n" "$VMDUSERS"
printf "switch \"%s\" {\\n" "$UPLINK"
printf "\\tinterface %s\\n" "$BRIDGE"
printf "}\\n\\n"
2018-11-25 16:55:37 +01:00
if ! test -z "$UPLINK2"
then
printf "switch \"%s\" {\\n" "$UPLINK2"
printf "\\tinterface %s\\n" "$BRIDGE2"
printf "}\\n\\n"
fi
2018-09-18 19:20:43 +02:00
while read -r f
do
. "$f"
2018-09-24 22:25:26 +02:00
_instance=$(check_instance "$f" "$instance")
_owner=$(check_owner "$username" "$owner")
printf "vm \"%s\" {\\n" "$_instance"
2018-09-18 19:20:43 +02:00
printf "\\tdisable\\n"
2018-09-24 22:25:26 +02:00
printf "\\towner %s\\n" "$_owner"
if ! test -f "${IMAGES}/${_instance}.img"
then printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES"
2018-09-18 19:20:43 +02:00
fi
2018-09-24 22:25:26 +02:00
printf "\\tdisk \"%s/%s.img\"\\n" "$IMAGES" "$_instance"
2018-09-18 19:20:43 +02:00
printf "\\tinterface tap {\\n"
2018-11-25 16:55:37 +01:00
printf "\\t\\tswitch \"$(fetch_switch "$UPLINK" "$switch")\"\\n"
2018-09-24 22:25:26 +02:00
printf "\\t\\tlladdr %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")"
2018-09-18 19:20:43 +02:00
printf "\\t}\\n"
printf "}\\n"
2018-09-24 22:25:26 +02:00
clear_variables
2018-09-18 19:20:43 +02:00
done
}
render_dhcpd_conf() {
2018-09-24 14:58:03 +02:00
# 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"
2018-09-18 19:20:43 +02:00
printf "option domain-name \"%s\";\\n" "$DOMAIN"
2018-10-06 09:57:58 +02:00
printf "option domain-name-servers %s;\\n\\n" "$DNS"
2018-09-18 19:20:43 +02:00
printf "subnet %s netmask %s {\\n" "$SUBNET" "$NETMASK"
printf "\\toption routers %s;\\n" "$ROUTER"
printf "\\tserver-name \"%s.%s\";\\n" "$SERVER" "$DOMAIN"
2018-10-20 16:45:40 +02:00
#printf "\\trange %s;\\n\\n" "$RANGE"
2018-09-18 19:20:43 +02:00
while read -r f
do
. "$f"
2018-09-24 22:25:26 +02:00
_instance=$(check_instance "$f" "$instance")
printf "\\thost %s {\\n" "$_instance"
2018-10-06 09:55:14 +02:00
printf "\\t\\thardware ethernet %s;\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")"
2018-11-25 16:55:37 +01:00
printf "\\t\\tfixed-address %s;\\n" "$(fetch_ip "$IP_PREFIX" "$IP_START" "$(find_vm "$VMS" "$_instance")" "$ip")"
2018-09-24 22:25:26 +02:00
if ! test -f "${IMAGES}/${_instance}.img"
2018-10-06 09:55:14 +02:00
then printf "\\t\\tfilename \"auto_install\";\\n"
else printf "\\t\\tfilename \"auto_upgrade\";\\n"
2018-09-18 19:20:43 +02:00
fi
2018-10-06 09:55:14 +02:00
printf "\\t\\toption host-name \"%s\";\\n" "$hostname"
2018-09-18 19:20:43 +02:00
printf "\\t}\\n"
2018-09-24 22:25:26 +02:00
clear_variables
2018-09-18 19:20:43 +02:00
done
printf "}\\n"
}
render_install_conf() {
# Generate <MAC>-install.comf
2018-09-24 14:58:03 +02:00
# Takes defaults from of _vms.conf and iterate over the vm*.txt files.
# When the VM image doesn't exist create the <MAC>-install.conf file.
# When the VM image does exist remove the <MAC>-install.conf file.
2018-09-24 14:58:03 +02:00
# fetch_ipv6() to get the correct IPv6 address of the VM.
# fetch_mac() to get the correct MAC address of the VM.
2018-09-18 19:20:43 +02:00
while read -r f
do
. "$f"
2018-09-24 22:25:26 +02:00
_instance=$(check_instance "$f" "$instance")
2018-09-24 14:58:03 +02:00
_pass="$(generate_passwd)"
2018-11-25 16:55:37 +01:00
_ipv6=$(fetch_ipv6 "$IPV6_PREFIX" "$IPV6_START" "$IP_START" "$(find_vm "$VMS" "$_instance")" "$ipv6")
2018-09-24 14:58:03 +02:00
_ipv6_gateway=$(echo "$_ipv6" | sed -e 's/::[0-9]*$/::1/g')
2018-09-24 22:25:26 +02:00
_mac=$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")
if ! test -f "${IMAGES}/${_instance}.img"
2018-09-18 19:20:43 +02:00
then
cat <<-EOF > "${HTDOCS}/${_mac}-install.conf"
2018-09-18 19:20:43 +02:00
#
# File generated on $date
#
System hostname = $hostname
2018-09-24 14:58:03 +02:00
Password for root = $_pass
2018-09-18 19:20:43 +02:00
Which speed should com0 = 115200
Network interfaces = vio0
IPv4 address for vio0 = dhcp
2018-09-24 14:58:03 +02:00
IPv6 address for vio0 = $_ipv6
IPv6 default router = $_ipv6_gateway
2018-09-18 19:20:43 +02:00
Setup a user = $username
2018-09-24 14:58:03 +02:00
Password for user = $_pass
Public ssh key for user = $message $_pass
2018-09-18 19:20:43 +02:00
Which disk is the root disk = sd0
What timezone are you in = Europe/Amsterdam
Location of sets = http
2018-09-30 10:38:01 +02:00
Server = openbsd.amsterdam
Set name(s) = -x* +xb* +xf* +site*
Continue anyway = yes
Continue without verification = yes
2018-09-18 19:20:43 +02:00
EOF
echo "Install file created: ${HTDOCS}/${_mac}-install.conf"
2018-09-18 19:20:43 +02:00
else
if test -f "${HTDOCS}/${_mac}-install.conf"
then rm -rf "${HTDOCS}/${_mac}-install.conf"
2018-09-18 19:20:43 +02:00
fi
fi
2018-09-24 22:25:26 +02:00
clear_variables
2018-09-18 19:20:43 +02:00
done
}
create_images() {
while read -r f
do
. "$f"
2018-09-24 22:25:26 +02:00
_instance=$(check_instance "$f" "$instance")
if ! test -f "${IMAGES}/${_instance}.img"
then vmctl create "${IMAGES}/${_instance}.img" -s 50G > /dev/null
echo "Image file created: ${IMAGES}/${_instance}.img"
2018-09-18 19:20:43 +02:00
fi
2018-09-24 22:25:26 +02:00
clear_variables
2018-09-18 19:20:43 +02:00
done
}
create_users() {
while read -r f
do
. "$f"
2018-09-24 22:25:26 +02:00
_owner=$(check_owner "$username" "$owner")
if test -n "$_owner"
2018-09-18 19:20:43 +02:00
then
2018-11-23 12:58:00 +01:00
if ! grep -e "^${_owner}:" /etc/passwd > /dev/null
2018-09-18 19:20:43 +02:00
then
2018-09-24 22:25:26 +02:00
useradd -m -G "$VMDUSERS" "$_owner"
echo "$message" > "/home/${_owner}/.ssh/authorized_keys"
echo "User created: $_owner"
2018-09-18 19:20:43 +02:00
fi
fi
2018-09-24 22:25:26 +02:00
clear_variables
2018-09-18 19:20:43 +02:00
done
}
main "$@"