Initial commit

This commit is contained in:
mischa 2018-09-18 19:20:43 +02:00
commit 3423cb18cb
6 changed files with 252 additions and 0 deletions

14
_vms.conf Normal file
View File

@ -0,0 +1,14 @@
SERVER="server1
DOMAIN="example.com"
ETC="."
IMG="."
HTDOCS="."
VMDUSERS="vmdusers"
UPLINK="uplink_vlan42"
BRIDGE="bridge42"
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"

184
deploy.sh Executable file
View File

@ -0,0 +1,184 @@
#!/bin/sh
# shellcheck disable=SC1090
# shellcheck disable=SC2154
# CONF_FILE "_vms.conf" needs to have the following variables:
#ROUTER="192.168.0.1"
#DNS="192.168.0.1"
#SERVER="server1"
#DOMAIN="example.com"
#ETC="/etc"
#IMG="/var/vmm"
#HTDOCS="/var/www/htdocs"
#VMDUSERS="vmdusers"
#UPLINK="uplink_vlan42"
#BRIDGE="bridge42"
#ROUTER="192.168.0.1"
#DNS="192.168.0.1"
#SUBNET="192.168.0.0"
#NETMASK="255.255.255.0"
#RANGE="192.168.0.10 192.168.0.10"
set -e
main () {
CONF_FILE="$PWD/_vms.conf"
[ -f "$CONF_FILE" ] && . "$CONF_FILE"
date=$(date "+%Y-%m-%d %H:%M:%S")
echo "New config files created for $SERVER @ $date"
fs=$(
list_files
)
if test -n "$fs"
then
echo "$fs"
echo "$fs" |
render_vm_conf >> "${ETC}/vm.conf"
echo "$fs" |
render_dhcpd_conf >> "${ETC}/dhcpd.conf"
echo "$fs" |
render_install_conf
echo "$fs" |
create_images
echo "$fs" |
create_users
fi
restart_service
}
list_files() {
find . -type f -name "vm*.txt" -maxdepth 1 | sort | xargs awk '/message/ { print FILENAME }'
}
render_vm_conf() {
printf "#\\n# File generated on %s\\n#\\n" "$date" > "${IMG}/vm.conf"
printf "socket owner :%s\\n\\n" "$VMDUSERS"
printf "switch \"%s\" {\\n" "$UPLINK"
printf "\\tinterface %s\\n" "$BRIDGE"
printf "}\\n\\n"
while read -r f
do
. "$f"
printf "vm \"%s\" {\\n" "$instance"
printf "\\tdisable\\n"
if test -n "$owner"
then
printf "\\towner %s\\n" "$owner"
fi
if ! test -f "${IMG}/${instance}.img"
then
printf "\\tboot \"%s/bsd.rd\"\\n" "$IMG"
fi
printf "\\tdisk \"%s/%s.img\"\\n" "$IMG" "$instance"
printf "\\tinterface tap {\\n"
printf "\\t\\tswitch \"uplink_vlan921\"\\n"
printf "\\t\\tlladdr %s\\n" "$mac"
printf "\\t}\\n"
printf "}\\n"
done
}
render_dhcpd_conf() {
printf "#\\n# File generated on %s\\n#\\n" "$date" > "${ETC}/dhcpd.conf"
printf "option domain-name \"%s\";\\n" "$DOMAIN"
printf "option domain-name-servers \"%s\";\\n\\n" "$DNS"
printf "subnet %s netmask %s {\\n" "$SUBNET" "$NETMASK"
printf "\\toption routers %s;\\n" "$ROUTER"
printf "\\tserver-name \"%s.%s\";\\n" "$SERVER" "$DOMAIN"
printf "\\trange %s;\\n\\n" "$RANGE"
while read -r f
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"
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"
done
printf "}\\n"
}
render_install_conf() {
while read -r f
do
. "$f"
ipv6_gateway=$(echo "$ipv6" | sed -e 's/::[0-9]*$/::1/g')
if ! test -f "${IMG}/${instance}.img"
then
cat <<-EOF > "${HTDOCS}/install-${mac}.conf"
#
# File generated on $date
#
System hostname = $hostname
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
Setup a user = $username
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"
else
if test -f "${HTDOCS}/install-${mac}.conf"
then rm -rf "${HTDOCS}/install-${mac}.conf"
fi
fi
done
}
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"
fi
done
}
create_users() {
while read -r f
do
. "$f"
if test -n "$owner"
then
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"
fi
fi
done
}
restart_service() {
rcctl restart dhcpd
vmctl reload
}
main "$@"

15
vm1.txt Normal file
View File

@ -0,0 +1,15 @@
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=
owner="user1"
name="User One"
email="user.one@gmail.com"
message="ssh-ed25519 AAAAC3NzaC1lAILmSludIBvFRhkxldn1OEgnQdrNd7PxMQjm2DyYCqHjy user1"
hostname="vm1"
username="user1"
note="First!"

15
vm2.txt Normal file
View File

@ -0,0 +1,15 @@
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
email="user.two@gmail.com"
message="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5SudIBvFRhkxldn1OEgnQdl7PxMQjm2DyYCqHjy"
hostname="user2"
username="vm2"
note="Second!"

15
vm3.txt Normal file
View File

@ -0,0 +1,15 @@
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=
owner=
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"
note=

9
vm4.txt Normal file
View File

@ -0,0 +1,9 @@
instance=
ip="192.168.0.14
ipv6="2a03:6000:192::14"
mac="fe:e1:bb:d4:f9:bd"
pass="c8sEqOueujdf3Z"
date=
payment=
donated=
owner=