added vm name and owner check
This commit is contained in:
parent
0700999247
commit
65ef7afb4a
120
deploy.sh
120
deploy.sh
|
@ -1,5 +1,6 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
|
# shellcheck disable=SC2038
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
|
|
||||||
# CONF_FILE "_vms.conf" needs to have the following variables:
|
# CONF_FILE "_vms.conf" needs to have the following variables:
|
||||||
|
@ -64,19 +65,23 @@ main () {
|
||||||
|
|
||||||
list_files() {
|
list_files() {
|
||||||
# Find all the VM config files.
|
# Find all the VM config files.
|
||||||
# Takes the directory with vm*.txt files.
|
# Takes the directory with vm*.txt files
|
||||||
find "$1" -type f -name "vm*.txt" -maxdepth 1 | sort | xargs grep -l "message"
|
find "$1" -type f -name "vm*.txt" -maxdepth 1 | sort -V | xargs grep -l "message"
|
||||||
}
|
}
|
||||||
|
|
||||||
find_vm() {
|
find_vm() {
|
||||||
# Find the number of the VM (VM#).
|
# Find the number of the VM (VM#).
|
||||||
# Takes the directory with vm*.txt files and instance name as variable.
|
# Takes the directory with vm*.txt files and instance
|
||||||
find "$1" -type f -name "vm*.txt" -maxdepth 1 | xargs grep -l "$2" | sed 's/^\.\/vm//;s/\.txt$//'
|
# 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_mac() {
|
||||||
# Fetch the MAC address for the VM.
|
# Fetch the MAC address for the VM.
|
||||||
# Takes the MAC_PREFIX and VM#.
|
# Takes the MAC_PREFIX and VM#
|
||||||
# print the MAC address
|
# print the MAC address
|
||||||
echo "${1}:${2}"
|
echo "${1}:${2}"
|
||||||
}
|
}
|
||||||
|
@ -104,10 +109,45 @@ fetch_ipv6() {
|
||||||
|
|
||||||
generate_passwd() {
|
generate_passwd() {
|
||||||
# Generate a random password for the install-<MAC>.conf file.
|
# 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
|
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() {
|
render_vm_conf() {
|
||||||
# Generate vm.comf
|
# Generate vm.comf
|
||||||
|
@ -123,22 +163,21 @@ render_vm_conf() {
|
||||||
while read -r f
|
while read -r f
|
||||||
do
|
do
|
||||||
. "$f"
|
. "$f"
|
||||||
printf "vm \"%s\" {\\n" "$instance"
|
_instance=$(check_instance "$f" "$instance")
|
||||||
|
_owner=$(check_owner "$username" "$owner")
|
||||||
|
printf "vm \"%s\" {\\n" "$_instance"
|
||||||
printf "\\tdisable\\n"
|
printf "\\tdisable\\n"
|
||||||
if test -n "$owner"
|
printf "\\towner %s\\n" "$_owner"
|
||||||
then
|
if ! test -f "${IMAGES}/${_instance}.img"
|
||||||
printf "\\towner %s\\n" "$owner"
|
then printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES"
|
||||||
fi
|
fi
|
||||||
if ! test -f "${IMAGES}/${instance}.img"
|
printf "\\tdisk \"%s/%s.img\"\\n" "$IMAGES" "$_instance"
|
||||||
then
|
|
||||||
printf "\\tboot \"%s/bsd.rd\"\\n" "$IMAGES"
|
|
||||||
fi
|
|
||||||
printf "\\tdisk \"%s/%s.img\"\\n" "$IMAGES" "$instance"
|
|
||||||
printf "\\tinterface tap {\\n"
|
printf "\\tinterface tap {\\n"
|
||||||
printf "\\t\\tswitch \"uplink_vlan921\"\\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 "\\t}\\n"
|
||||||
printf "}\\n"
|
printf "}\\n"
|
||||||
|
clear_variables
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,17 +198,17 @@ render_dhcpd_conf() {
|
||||||
while read -r f
|
while read -r f
|
||||||
do
|
do
|
||||||
. "$f"
|
. "$f"
|
||||||
printf "\\thost %s {\\n" "$instance"
|
_instance=$(check_instance "$f" "$instance")
|
||||||
printf "\\t\\thardware ethernet %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$instance")")"
|
printf "\\thost %s {\\n" "$_instance"
|
||||||
printf "\\t\\tfixed-address %s\\n" "$(fetch_ip "$IP_PREFIX" "$IP_START" "$(find_vm "$VMS" "$instance")")"
|
printf "\\t\\thardware ethernet %s\\n" "$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")"
|
||||||
if ! test -f "${IMAGES}/${instance}.img"
|
printf "\\t\\tfixed-address %s\\n" "$(fetch_ip "$IP_PREFIX" "$IP_START" "$(find_vm "$VMS" "$_instance")")"
|
||||||
then
|
if ! test -f "${IMAGES}/${_instance}.img"
|
||||||
printf "\\t\\tfilename \"auto_install\"\\n"
|
then printf "\\t\\tfilename \"auto_install\"\\n"
|
||||||
else
|
else printf "\\t\\tfilename \"auto_upgrade\"\\n"
|
||||||
printf "\\t\\tfilename \"auto_upgrade\"\\n"
|
|
||||||
fi
|
fi
|
||||||
printf "\\t\\toption host-name \"%s\"\\n" "$hostname"
|
printf "\\t\\toption host-name \"%s\"\\n" "$hostname"
|
||||||
printf "\\t}\\n"
|
printf "\\t}\\n"
|
||||||
|
clear_variables
|
||||||
done
|
done
|
||||||
printf "}\\n"
|
printf "}\\n"
|
||||||
}
|
}
|
||||||
|
@ -184,11 +223,12 @@ render_install_conf() {
|
||||||
while read -r f
|
while read -r f
|
||||||
do
|
do
|
||||||
. "$f"
|
. "$f"
|
||||||
|
_instance=$(check_instance "$f" "$instance")
|
||||||
_pass="$(generate_passwd)"
|
_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')
|
_ipv6_gateway=$(echo "$_ipv6" | sed -e 's/::[0-9]*$/::1/g')
|
||||||
_mac=$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$instance")")
|
_mac=$(fetch_mac "$MAC_PREFIX" "$(find_vm "$VMS" "$_instance")")
|
||||||
if ! test -f "${IMAGES}/${instance}.img"
|
if ! test -f "${IMAGES}/${_instance}.img"
|
||||||
then
|
then
|
||||||
cat <<-EOF > "${HTDOCS}/install-${_mac}.conf"
|
cat <<-EOF > "${HTDOCS}/install-${_mac}.conf"
|
||||||
#
|
#
|
||||||
|
@ -216,6 +256,7 @@ render_install_conf() {
|
||||||
then rm -rf "${HTDOCS}/install-${_mac}.conf"
|
then rm -rf "${HTDOCS}/install-${_mac}.conf"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
clear_variables
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,10 +264,12 @@ create_images() {
|
||||||
while read -r f
|
while read -r f
|
||||||
do
|
do
|
||||||
. "$f"
|
. "$f"
|
||||||
if ! test -f "${IMAGES}/${instance}.img"
|
_instance=$(check_instance "$f" "$instance")
|
||||||
then vmctl create "${IMAGES}/${instance}.img" -s 50G > /dev/null
|
if ! test -f "${IMAGES}/${_instance}.img"
|
||||||
echo "Image file created: ${IMAGES}/${instance}.img"
|
then vmctl create "${IMAGES}/${_instance}.img" -s 50G > /dev/null
|
||||||
|
echo "Image file created: ${IMAGES}/${_instance}.img"
|
||||||
fi
|
fi
|
||||||
|
clear_variables
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,21 +277,18 @@ create_users() {
|
||||||
while read -r f
|
while read -r f
|
||||||
do
|
do
|
||||||
. "$f"
|
. "$f"
|
||||||
if test -n "$owner"
|
_owner=$(check_owner "$username" "$owner")
|
||||||
|
if test -n "$_owner"
|
||||||
then
|
then
|
||||||
if ! grep -e "^$owner" /etc/passwd > /dev/null
|
if ! grep -e "^$_owner" /etc/passwd > /dev/null
|
||||||
then
|
then
|
||||||
useradd -m -G "$VMDUSERS" "$owner"
|
useradd -m -G "$VMDUSERS" "$_owner"
|
||||||
echo "$message" > "/home/${owner}/.ssh/authorized_keys"
|
echo "$message" > "/home/${_owner}/.ssh/authorized_keys"
|
||||||
echo "User created: $owner"
|
echo "User created: $_owner"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
clear_variables
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
restart_service() {
|
|
||||||
rcctl restart dhcpd
|
|
||||||
vmctl reload
|
|
||||||
}
|
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|
2
vm1.txt
2
vm1.txt
|
@ -1,4 +1,4 @@
|
||||||
instance="vm1"
|
instance="vm1instance"
|
||||||
date="2018/09/18"
|
date="2018/09/18"
|
||||||
payment=
|
payment=
|
||||||
donated=
|
donated=
|
||||||
|
|
2
vm2.txt
2
vm2.txt
|
@ -1,4 +1,4 @@
|
||||||
instance="vm2"
|
instance="vm2instance"
|
||||||
date="2018/09/18"
|
date="2018/09/18"
|
||||||
payment=
|
payment=
|
||||||
donated=
|
donated=
|
||||||
|
|
9
vm3.txt
9
vm3.txt
|
@ -1,11 +1,10 @@
|
||||||
instance="vm3"
|
|
||||||
date="2018/09/18"
|
date="2018/09/18"
|
||||||
payment=
|
payment=
|
||||||
donated=
|
donated=
|
||||||
owner=
|
owner="rolf"
|
||||||
name="User Three"
|
name="User Three"
|
||||||
email="user.three@gmail.com"
|
email="user.three@gmail.com"
|
||||||
message="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZ5LtCgngY6ehDPRA4+hBWl1NtfNNy5++0NjHuVNQgFls4hjdDeouNz1zPL6HXh72PgsBUEoTUucNi8BjOL//qFOCfKiPSJfiGUty+xIjyPowigoDx76z+hOwXVeKJ9sGMmknfH0x1z9Da+ShnNM5r1WwTz5JBV4tlVnQlYX65PeskWJSreTKRoPGSfNU2xxIJePmp0sCTJXfgDooqT7gR8W07vEYfW3pYReJXz3ipD/YBbkAXOxJEa3B75As+K7QC0UgTazq9u7mg+BTuRI9dAybyGqVWG+4EsiVwr57+5yLQkHRsS3JoBZXgyHJQ92o65Tt9eWQZ4DedgTha0d"
|
message="ssh-rsa AAAAB4NzaC1yc2EAAAADAQABAAABAQDZ5LtCgngY6ehDPRA4+hBWl1NtfNNy5++0NjHuVNQgFls4hjdDeouNz1zPL6HXh72PgsBUEoTUucNi8BjOL//qFOCfKiPSJfiGUty+xIjyPowigoDx76z+hOwXVeKJ9sGMmknfH0x1z9Da+ShnNM5r1WwTz5JBV4tlVnQlYX65PeskWJSreTKRoPGSfNU2xxIJePmp0sCTJXfgDooqT7gR8W07vEYfW4pYReJXz4ipD/YBbkAXOxJEa4B75As+K7QC0UgTazq9u7mg+BTuRI9dAybyGqVWG+4EsiVwr57+5yLQkHRsS4JoBZXgyHJQ92o65Tt9eWQZ4DedgTha0d"
|
||||||
hostname="vm3.example.com"
|
hostname="vmthree.example.com"
|
||||||
username="user3"
|
username="userthree"
|
||||||
note=
|
note=
|
||||||
|
|
Loading…
Reference in New Issue