From 855a980df3ac4a5a8be98528d3638bf7da4fff4f Mon Sep 17 00:00:00 2001 From: mischa Date: Mon, 24 Sep 2018 14:58:03 +0200 Subject: [PATCH] Tons of changes :) --- _vms.conf | 20 ++++++-- deploy.sh | 142 ++++++++++++++++++++++++++++++++++++++++-------------- vm1.txt | 4 -- vm2.txt | 10 ++-- vm3.txt | 4 -- vm4.txt | 4 -- 6 files changed, 124 insertions(+), 60 deletions(-) diff --git a/_vms.conf b/_vms.conf index bcfa6f3..1cd07da 100644 --- a/_vms.conf +++ b/_vms.conf @@ -1,14 +1,24 @@ -SERVER="server1 +# Server config for install-.conf +SERVER="server1" DOMAIN="example.com" +# IP / MAC config +IP_PREFIX="46.23.92" +IP_START=170 +IPV6_PREFIX="2a03:6000:9210" +IPV6_START=9200 +MAC_PREFIX="fe:e1:bb:d4:c3" +# .conf locations +VMS="." ETC="." -IMG="." +IMAGES="." HTDOCS="." -VMDUSERS="vmdusers" +# vm.conf +VMDUSERS="_vmdusers" UPLINK="uplink_vlan42" BRIDGE="bridge42" -ROUTER="192.168.1.1 +# dhcpd.conf +ROUTER="192.168.1.1" DNS="192.186.1.3" SUBNET="192.168.1.0" NETMASK="255.255.255.0" RANGE="192.168.1.32 192.168.1.127" - diff --git a/deploy.sh b/deploy.sh index 16193cb..6fdfcfd 100755 --- a/deploy.sh +++ b/deploy.sh @@ -3,21 +3,30 @@ # shellcheck disable=SC2154 # CONF_FILE "_vms.conf" needs to have the following variables: -#ROUTER="192.168.0.1" -#DNS="192.168.0.1" +## Server config for install-.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" -#IMG="/var/vmm" +#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.168.0.1" -#SUBNET="192.168.0.0" +#DNS="192.186.0.1" +#SUBNET="192.168.0.1" #NETMASK="255.255.255.0" -#RANGE="192.168.0.10 192.168.0.10" +#RANGE="192.168.1.10 192.168.1.10" set -e @@ -29,7 +38,7 @@ main () { echo "New config files created for $SERVER @ $date" fs=$( - list_files + list_files "$VMS" ) if test -n "$fs" @@ -37,10 +46,10 @@ main () { echo "$fs" echo "$fs" | - render_vm_conf >> "${ETC}/vm.conf" + render_vm_conf > "${ETC}/vm.conf" echo "$fs" | - render_dhcpd_conf >> "${ETC}/dhcpd.conf" + render_dhcpd_conf > "${ETC}/dhcpd.conf" echo "$fs" | render_install_conf @@ -51,16 +60,62 @@ main () { echo "$fs" | create_users fi - - restart_service } - + list_files() { - find . -type f -name "vm*.txt" -maxdepth 1 | sort | xargs awk '/message/ { print FILENAME }' + # 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-.conf file. + # Doesn't take variables. + tr -cd '[:print:]' < /dev/urandom | fold -w 20 | head -n 1 +} + + render_vm_conf() { - printf "#\\n# File generated on %s\\n#\\n" "$date" > "${IMG}/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" @@ -74,21 +129,27 @@ render_vm_conf() { then printf "\\towner %s\\n" "$owner" fi - if ! test -f "${IMG}/${instance}.img" + if ! test -f "${IMAGES}/${instance}.img" then - printf "\\tboot \"%s/bsd.rd\"\\n" "$IMG" + printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES" fi - printf "\\tdisk \"%s/%s.img\"\\n" "$IMG" "$instance" + printf "\\tdisk \"%s/%s.img\"\\n" "$IMAGES" "$instance" printf "\\tinterface tap {\\n" printf "\\t\\tswitch \"uplink_vlan921\"\\n" - printf "\\t\\tlladdr %s\\n" "$mac" + printf "\\t\\tlladdr %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$instance")")" printf "\\t}\\n" printf "}\\n" done } render_dhcpd_conf() { - printf "#\\n# File generated on %s\\n#\\n" "$date" > "${ETC}/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" @@ -99,9 +160,9 @@ render_dhcpd_conf() { do . "$f" printf "\\thost %s {\\n" "$instance" - printf "\\t\\thardware ethernet %s\\n" "$mac" - printf "\\t\\tfixed-address %s\\n" "$ip" - if ! test -f "${IMG}/${instance}.img" + 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 @@ -114,36 +175,45 @@ render_dhcpd_conf() { } render_install_conf() { + # Generate install-.comf + # Takes defaults from of _vms.conf and iterate over the vm*.txt files. + # When the VM image doesn't exist create the install-.conf file. + # When the VM image does exist remove the install-.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" - ipv6_gateway=$(echo "$ipv6" | sed -e 's/::[0-9]*$/::1/g') - if ! test -f "${IMG}/${instance}.img" + _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" + cat <<-EOF > "${HTDOCS}/install-${_mac}.conf" # # File generated on $date # System hostname = $hostname - Password for root = $pass + 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 + 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 + 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" + echo "Install file created: ${HTDOCS}/install-${_mac}.conf" else - if test -f "${HTDOCS}/install-${mac}.conf" - then rm -rf "${HTDOCS}/install-${mac}.conf" + if test -f "${HTDOCS}/install-${_mac}.conf" + then rm -rf "${HTDOCS}/install-${_mac}.conf" fi fi done @@ -153,9 +223,9 @@ create_images() { while read -r f do . "$f" - if ! test -f "${IMG}/${instance}.img" - then vmctl create "${IMG}/${instance}.img" -s 50G > /dev/null - echo "Image file created: ${IMG}/${instance}.img" + 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 } diff --git a/vm1.txt b/vm1.txt index 3b67669..67854bf 100644 --- a/vm1.txt +++ b/vm1.txt @@ -1,8 +1,4 @@ instance="vm1" -ip="192.168.0.10" -ipv6="2a03:6000:192::10" -mac="fe:e1:bb:d4:ce:a9" -pass="kOlyAvD6lqLDS4X" date="2018/09/18" payment= donated= diff --git a/vm2.txt b/vm2.txt index 0069096..2533344 100644 --- a/vm2.txt +++ b/vm2.txt @@ -1,15 +1,11 @@ instance="vm2" -ip="192.168.0.11 -ipv6="2a03:6000:192::11" -mac="fe:e1:bb:d4:f2:6a" -pass="IRiH5fXiez3iDvU" date="2018/09/18" payment= donated= owner= -name="user Two +name="User Two" email="user.two@gmail.com" message="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5SudIBvFRhkxldn1OEgnQdl7PxMQjm2DyYCqHjy" -hostname="user2" -username="vm2" +hostname="vm2" +username="user2" note="Second!" diff --git a/vm3.txt b/vm3.txt index 837496a..c7abf02 100644 --- a/vm3.txt +++ b/vm3.txt @@ -1,8 +1,4 @@ instance="vm3" -ip="192.168.0.12 -ipv6="2a03:6000:192::12" -mac="fe:e1:bb:d4:ad:fd" -pass="fJweGUoqglVKqU4" date="2018/09/18" payment= donated= diff --git a/vm4.txt b/vm4.txt index c128bef..13a23a8 100644 --- a/vm4.txt +++ b/vm4.txt @@ -1,8 +1,4 @@ instance= -ip="192.168.0.14 -ipv6="2a03:6000:192::14" -mac="fe:e1:bb:d4:f9:bd" -pass="c8sEqOueujdf3Z" date= payment= donated=