From 65ef7afb4a8e73e8150e2d1688ddd5850b9b5b02 Mon Sep 17 00:00:00 2001 From: mischa Date: Mon, 24 Sep 2018 22:25:26 +0200 Subject: [PATCH] added vm name and owner check --- deploy.sh | 120 ++++++++++++++++++++++++++++++++++++------------------ vm1.txt | 2 +- vm2.txt | 2 +- vm3.txt | 9 ++-- vm4.txt | 5 --- 5 files changed, 86 insertions(+), 52 deletions(-) diff --git a/deploy.sh b/deploy.sh index 6fdfcfd..be7da4f 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,5 +1,6 @@ #!/bin/sh # shellcheck disable=SC1090 +# shellcheck disable=SC2038 # shellcheck disable=SC2154 # CONF_FILE "_vms.conf" needs to have the following variables: @@ -64,19 +65,23 @@ main () { 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}" } @@ -104,10 +109,45 @@ fetch_ipv6() { generate_passwd() { # Generate a random password for the install-.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 @@ -123,22 +163,21 @@ render_vm_conf() { 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" + printf "\\towner %s\\n" "$_owner" + if ! test -f "${IMAGES}/${_instance}.img" + then printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES" 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 "\\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 } @@ -159,17 +198,17 @@ render_dhcpd_conf() { 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" } @@ -184,11 +223,12 @@ render_install_conf() { 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" # @@ -216,6 +256,7 @@ render_install_conf() { then rm -rf "${HTDOCS}/install-${_mac}.conf" fi fi + clear_variables done } @@ -223,10 +264,12 @@ 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" + _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 } @@ -234,21 +277,18 @@ create_users() { 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 "$@" diff --git a/vm1.txt b/vm1.txt index 67854bf..0dac299 100644 --- a/vm1.txt +++ b/vm1.txt @@ -1,4 +1,4 @@ -instance="vm1" +instance="vm1instance" date="2018/09/18" payment= donated= diff --git a/vm2.txt b/vm2.txt index 2533344..19f19d2 100644 --- a/vm2.txt +++ b/vm2.txt @@ -1,4 +1,4 @@ -instance="vm2" +instance="vm2instance" date="2018/09/18" payment= donated= diff --git a/vm3.txt b/vm3.txt index c7abf02..25c225c 100644 --- a/vm3.txt +++ b/vm3.txt @@ -1,11 +1,10 @@ -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= diff --git a/vm4.txt b/vm4.txt index 13a23a8..e69de29 100644 --- a/vm4.txt +++ b/vm4.txt @@ -1,5 +0,0 @@ -instance= -date= -payment= -donated= -owner=