commit - 0700999247a69ae82f8d118fcbfbad94db1aca04
commit + 65ef7afb4a8e73e8150e2d1688ddd5850b9b5b02
blob - 6fdfcfd2fe69832094a6c60547dea54fc5150814
blob + be7da4fa9b91b7a8cca73dcc5bc7777aba2fe46f
--- deploy.sh
+++ deploy.sh
#!/bin/sh
# shellcheck disable=SC1090
+# shellcheck disable=SC2038
# shellcheck disable=SC2154
# CONF_FILE "_vms.conf" needs to have the following variables:
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"
+ # Takes the directory with vm*.txt files
+ find "$1" -type f -name "vm*.txt" -maxdepth 1 | sort -V | 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$//'
+ # Takes the directory with vm*.txt files and instance
+ # Checks if instance exists otherwise returns the vm filename
+ if _vm=$(find "$1" -type f -name "vm*.txt" -maxdepth 1 | xargs grep -l "$2")
+ then echo "$_vm" | sed 's/^\.\/vm//;s/\.txt$//'
+ else echo "$2" | sed 's/^vm//'
+ fi
}
fetch_mac() {
# Fetch the MAC address for the VM.
- # Takes the MAC_PREFIX and VM#.
+ # Takes the MAC_PREFIX and VM#
# print the MAC address
echo "${1}:${2}"
}
generate_passwd() {
# Generate a random password for the install-<MAC>.conf file.
- # Doesn't take variables.
+ # Doesn't take variables
tr -cd '[:print:]' < /dev/urandom | fold -w 20 | head -n 1
}
+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"
+ then echo "$1" | sed 's/^\.\///;s/\.txt$//'
+ 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
+ unset username
+ unset note
+}
+
render_vm_conf() {
# Generate vm.comf
# Takes defaults from of _vms.conf and iterate over the vm*.txt files.
while read -r f
do
. "$f"
- printf "vm \"%s\" {\\n" "$instance"
+ _instance=$(check_instance "$f" "$instance")
+ _owner=$(check_owner "$username" "$owner")
+ 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"
+ printf "\\towner %s\\n" "$_owner"
+ if ! test -f "${IMAGES}/${_instance}.img"
+ then printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES"
fi
- printf "\\tdisk \"%s/%s.img\"\\n" "$IMAGES" "$instance"
+ 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\\tlladdr %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")"
printf "\\t}\\n"
printf "}\\n"
+ clear_variables
done
}
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"
+ _instance=$(check_instance "$f" "$instance")
+ 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"
+ clear_variables
done
printf "}\\n"
}
while read -r f
do
. "$f"
+ _instance=$(check_instance "$f" "$instance")
_pass="$(generate_passwd)"
- _ipv6=$(fetch_ipv6 "$IPV6_PREFIX" "$IPV6_START" "$IP_START" "$(find_vm "$VMS" "$instance")")
+ _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"
+ _mac=$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")
+ if ! test -f "${IMAGES}/${_instance}.img"
then
cat <<-EOF > "${HTDOCS}/install-${_mac}.conf"
#
then rm -rf "${HTDOCS}/install-${_mac}.conf"
fi
fi
+ clear_variables
done
}
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"
+ _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"
fi
+ clear_variables
done
}
while read -r f
do
. "$f"
- if test -n "$owner"
+ _owner=$(check_owner "$username" "$owner")
+ if test -n "$_owner"
then
- if ! grep -e "^$owner" /etc/passwd > /dev/null
+ 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"
+ useradd -m -G "$VMDUSERS" "$_owner"
+ echo "$message" > "/home/${_owner}/.ssh/authorized_keys"
+ echo "User created: $_owner"
fi
fi
+ clear_variables
done
}
-restart_service() {
- rcctl restart dhcpd
- vmctl reload
-}
-
main "$@"
blob - 67854bfccc30b95648b8ddbdf389dfc02c244733
blob + 0dac2995656224eff575dd90c8bd2986c54d5343
--- vm1.txt
+++ vm1.txt
-instance="vm1"
+instance="vm1instance"
date="2018/09/18"
payment=
donated=
blob - 25333445be35b6b6c7439818d28ee5c65726e402
blob + 19f19d282b678c437f2abffb38aa9afebb8f8b74
--- vm2.txt
+++ vm2.txt
-instance="vm2"
+instance="vm2instance"
date="2018/09/18"
payment=
donated=
blob - c7abf02e827b47f54279563795603accb87aae60
blob + 25c225cf1dd7e45d82d39842476737d1cfb0eacd
--- vm3.txt
+++ vm3.txt
-instance="vm3"
date="2018/09/18"
payment=
donated=
-owner=
+owner="rolf"
name="User Three"
email="user.three@gmail.com"
-message="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZ5LtCgngY6ehDPRA4+hBWl1NtfNNy5++0NjHuVNQgFls4hjdDeouNz1zPL6HXh72PgsBUEoTUucNi8BjOL//qFOCfKiPSJfiGUty+xIjyPowigoDx76z+hOwXVeKJ9sGMmknfH0x1z9Da+ShnNM5r1WwTz5JBV4tlVnQlYX65PeskWJSreTKRoPGSfNU2xxIJePmp0sCTJXfgDooqT7gR8W07vEYfW3pYReJXz3ipD/YBbkAXOxJEa3B75As+K7QC0UgTazq9u7mg+BTuRI9dAybyGqVWG+4EsiVwr57+5yLQkHRsS3JoBZXgyHJQ92o65Tt9eWQZ4DedgTha0d"
-hostname="vm3.example.com"
-username="user3"
+message="ssh-rsa AAAAB4NzaC1yc2EAAAADAQABAAABAQDZ5LtCgngY6ehDPRA4+hBWl1NtfNNy5++0NjHuVNQgFls4hjdDeouNz1zPL6HXh72PgsBUEoTUucNi8BjOL//qFOCfKiPSJfiGUty+xIjyPowigoDx76z+hOwXVeKJ9sGMmknfH0x1z9Da+ShnNM5r1WwTz5JBV4tlVnQlYX65PeskWJSreTKRoPGSfNU2xxIJePmp0sCTJXfgDooqT7gR8W07vEYfW4pYReJXz4ipD/YBbkAXOxJEa4B75As+K7QC0UgTazq9u7mg+BTuRI9dAybyGqVWG+4EsiVwr57+5yLQkHRsS4JoBZXgyHJQ92o65Tt9eWQZ4DedgTha0d"
+hostname="vmthree.example.com"
+username="userthree"
note=
blob - 13a23a8dc8f78999011e241e3c56399e572e62dd
blob + e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
--- vm4.txt
+++ vm4.txt
-instance=
-date=
-payment=
-donated=
-owner=