Commit Diff


commit - 3423cb18cb6118b8762f2496a0a949c8d89b6f29
commit + 855a980df3ac4a5a8be98528d3638bf7da4fff4f
blob - bcfa6f318e924b8564648280bff10978588b1f23
blob + 1cd07da90131d2bee406196ce9366d6c024392f6
--- _vms.conf
+++ _vms.conf
@@ -1,14 +1,24 @@
-SERVER="server1
+# Server config for install-<mac>.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"
-
blob - 16193cb7d85a60bc9ddf6771c8d019b820cbd8d5
blob + 6fdfcfd2fe69832094a6c60547dea54fc5150814
--- deploy.sh
+++ 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-<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"
-#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
+}
+	
+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"
+}
 
-	restart_service
+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$//'
 }
 
-list_files() {
-	find . -type f -name "vm*.txt" -maxdepth 1 | sort | xargs awk '/message/ { print FILENAME }'
+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() {
-	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-<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"
-	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
 }
blob - 3b67669d76517ca9f01b9f43fef95438421a1f3f
blob + 67854bfccc30b95648b8ddbdf389dfc02c244733
--- vm1.txt
+++ 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=
blob - 00690964abde412fc0d7c84dadb6f9ee6893de46
blob + 25333445be35b6b6c7439818d28ee5c65726e402
--- vm2.txt
+++ 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!"
blob - 837496a6dfed336a8dbcea05a528670824ac37c5
blob + c7abf02e827b47f54279563795603accb87aae60
--- vm3.txt
+++ 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=
blob - c128bef1f03dbb24697cd3e6188d0da5b3a16782
blob + 13a23a8dc8f78999011e241e3c56399e572e62dd
--- vm4.txt
+++ 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=