|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ############################################ |
| 4 | +# This is the main deploy script |
| 5 | +# that creates all the containers for the |
| 6 | +# example.com domain |
| 7 | +# ############################################ |
| 8 | + |
| 9 | +# include the config file |
| 10 | +. config |
| 11 | + |
| 12 | +# The script needs to be run as root! |
| 13 | +# if we are not root, we will exit |
| 14 | +if [ "$EUID" -ne 0 ] |
| 15 | + then echo "Please run as root" |
| 16 | + exit |
| 17 | +fi |
| 18 | + |
| 19 | +# ask for consent and abort or start the installation |
| 20 | +(cat) <<EOF |
| 21 | +This will install a virtual network |
| 22 | +on your Proxmox VE host. |
| 23 | +the parameters that you have chosen are: |
| 24 | +
|
| 25 | +Domain name: $DOMAIN |
| 26 | +Virtual LAN interface: $LAN |
| 27 | +Virtual WAN interface: $WAN |
| 28 | +Container Storage: $STORAGE |
| 29 | +
|
| 30 | +Please type Enter to continue or CTRL-C to abort |
| 31 | +EOF |
| 32 | +read |
| 33 | + |
| 34 | +echo "Please specify a root password for the containers (input not shown)" |
| 35 | +read -s ROOTPASSWD |
| 36 | +echo "Please specify a non-root user name for the client container" |
| 37 | +read NONROOTUSER |
| 38 | +echo "Please specify a password for the non-root user (input not shown)" |
| 39 | +read -s NONROOTPASSWD |
| 40 | + |
| 41 | + |
| 42 | +# ######################################### |
| 43 | +echo "##### finding the template" |
| 44 | +# ######################################### |
| 45 | + |
| 46 | +pveam download $TEMPLATESTORAGE $TEMPLATENAME |
| 47 | +CTTEMPLATE=$(pveam list $TEMPLATESTORAGE |grep $TEMPLATENAME | cut -d " " -f 1 -) |
| 48 | +if [ "X" == "X${CTTEMPLATE}" ]; then |
| 49 | + echo "Template not available - exiting" |
| 50 | + exit |
| 51 | +fi |
| 52 | + |
| 53 | +# ######################################### |
| 54 | +echo "##### deploying the router" |
| 55 | +# ######################################### |
| 56 | + |
| 57 | +OPENWRTID=$(echo $CTID | cut -d "," -f 3 -) |
| 58 | +qm create $OPENWRTID --cores 1 --name "exc-OpenWrt" --net0 model=virtio,bridge=$LAN --net1 model=virtio,bridge=$WAN --storage $STORAGE --memory 512 |
| 59 | +wget -q -O - $OPENWRTURL | gunzip -c >/tmp/openwrt.img |
| 60 | +qm importdisk $OPENWRTID /tmp/openwrt.img $STORAGE --format qcow2 |
| 61 | +qm set $OPENWRTID --ide0 $STORAGE:vm-$OPENWRTID-disk-0 |
| 62 | +qm set $OPENWRTID --boot order=ide0 |
| 63 | +rm /tmp/openwrt.img |
| 64 | +qm start $OPENWRTID |
| 65 | + |
| 66 | +echo -e "\n ######### Please make sure the router has internet access" |
| 67 | +echo -e " ######### (open a shell on the VM and ping www.google.com or the like)" |
| 68 | +echo -e "\n ######### press ENTER to continue\n" |
| 69 | +read |
| 70 | + |
| 71 | +# ######################################### |
| 72 | +echo "##### deploying the client" |
| 73 | +# ######################################### |
| 74 | + |
| 75 | +CLIENTID=$(echo $CTID | cut -d "," -f 1 -) |
| 76 | +pct create $CLIENTID $CTTEMPLATE \ |
| 77 | + --cores 1 \ |
| 78 | + --description "RDP Server for the ${DOMAIN} domain" \ |
| 79 | + --hostname "exc-Client" \ |
| 80 | + --memory 2048 \ |
| 81 | + --password "$ROOTPASSWD" \ |
| 82 | + --storage $STORAGE \ |
| 83 | + --net0 name=eth0,bridge=$WAN,ip=dhcp \ |
| 84 | + --net1 name=eth1,bridge=$LAN,ip=dhcp \ |
| 85 | + --features nesting=1 \ |
| 86 | + --unprivileged 1 |
| 87 | + |
| 88 | +# ######################################### |
| 89 | +echo "##### deploying the docker-host" |
| 90 | +# ######################################### |
| 91 | + |
| 92 | +DOCKERID=$(echo $CTID | cut -d "," -f 2 -) |
| 93 | +pct create $DOCKERID $CTTEMPLATE \ |
| 94 | + --cores 1 \ |
| 95 | + --description "Docker host for the ${DOMAIN} domain" \ |
| 96 | + --hostname "exc-Docker" \ |
| 97 | + --memory 2048 \ |
| 98 | + --password "$ROOTPASSWD" \ |
| 99 | + --storage $STORAGE \ |
| 100 | + --net0 name=eth1,bridge=$LAN,ip=dhcp \ |
| 101 | + --features keyctl=1,nesting=1 \ |
| 102 | + --unprivileged 1 |
| 103 | + |
| 104 | +echo -e "\n ######### Please check the settings of the containers in the Proxmox GUI \n ######### press ENTER to continue\n" |
| 105 | +read |
| 106 | + |
| 107 | + |
| 108 | +# ######################################### |
| 109 | +echo "##### starting the containers" |
| 110 | +# ######################################### |
| 111 | + |
| 112 | +# start the containers |
| 113 | +pct start $CLIENTID |
| 114 | +pct start $DOCKERID |
| 115 | + |
| 116 | +# ######################################### |
| 117 | +echo -e "\n ##### creating self-signed certs \n" |
| 118 | +# ######################################### |
| 119 | + |
| 120 | +# in case the domain is not called example.com, let's bluntly replace it in the config file |
| 121 | +sed -i s/example.com/${DOMAIN}/ imap.cnf |
| 122 | +sed -i s/example.com/${DOMAIN}/ wildcard.cnf |
| 123 | + |
| 124 | +# create the Certificates - first the CA |
| 125 | +openssl req -newkey rsa:2048 -keyout rootCA.key -x509 -days 3650 -nodes -out rootCA.crt -subj "/CN=AAA_TestCA/C=DE/O=AAA_onemarcfifty/emailAddress=admin@${DOMAIN}" |
| 126 | + |
| 127 | +# now the CSR and cert for the *.example.com wildcard |
| 128 | +openssl req -newkey rsa:2048 -nodes -keyout wildcard.key -out wildcard.csr -subj "/CN=*.${DOMAIN}/C=DE/O=AAA_onemarcfifty/emailAddress=admin@${DOMAIN}" |
| 129 | +openssl x509 -req -in wildcard.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out wildcard.crt -days 3650 -sha256 -extfile wildcard.cnf -extensions req_ext |
| 130 | +cat wildcard.crt rootCA.crt >wildcard_fullchain.crt |
| 131 | + |
| 132 | +# now the CSR and cert for the imap server |
| 133 | +openssl req -newkey rsa:2048 -nodes -keyout imap.key -out imap.csr -subj "/CN=imap.${DOMAIN}/C=DE/O=AAA_onemarcfifty/emailAddress=admin@${DOMAIN}" |
| 134 | +openssl x509 -req -in imap.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out imap.crt -days 3650 -sha256 -extfile imap.cnf -extensions req_ext |
| 135 | +cat imap.crt rootCA.crt >imap_fullchain.crt |
| 136 | + |
| 137 | +# copy the certificates over to the containers |
| 138 | +for THECONTAINER in $CLIENTID $DOCKERID ; do |
| 139 | + pct exec $THECONTAINER -- mkdir -p /etc/certificates/${DOMAIN} |
| 140 | + for i in *.crt *.key ; do |
| 141 | + pct push $THECONTAINER $i /etc/certificates/${DOMAIN}/${i} |
| 142 | + done |
| 143 | +done |
| 144 | + |
| 145 | +# ######################################### |
| 146 | +echo -e "\n ##### configuring the containers \n" |
| 147 | +# ######################################### |
| 148 | + |
| 149 | +# create a non-root user |
| 150 | +pct exec $CLIENTID -- useradd -m -s /bin/bash -G sudo $NONROOTUSER |
| 151 | +pct exec $CLIENTID -- bash -c "echo -e '$NONROOTPASSWD\n$NONROOTPASSWD\n' | passwd $NONROOTUSER" |
| 152 | + |
| 153 | +# push dhcp settings to avoid routing over the ingress interface |
| 154 | +pct push $CLIENTID exc-client/dhclient.conf /etc/dhcp/dhclient.conf |
| 155 | +pct exec $CLIENTID -- systemctl restart networking |
| 156 | + |
| 157 | +# push and execute the init script to the client |
| 158 | +pct push $CLIENTID exc-client/init-router-script.sh /root/init-router-script.sh |
| 159 | +pct push $CLIENTID exc-client/init-script.sh /root/init-script.sh |
| 160 | +pct exec $CLIENTID -- bash /root/init-script.sh "$NONROOTUSER" "$DOMAIN" "$ROUTERIP" |
| 161 | + |
| 162 | +# push and execute the init script to the docker host |
| 163 | +pct push $DOCKERID exc-docker/init-script.sh /root/init-script.sh |
| 164 | +pct exec $DOCKERID -- bash /root/init-script.sh "$NONROOTUSER" "$DOMAIN" |
| 165 | + |
| 166 | +echo -e "\n ############# DONE - Please reboot all VMs and CTs \n" |
| 167 | +echo -e "\n ############# and then connect with RDP to the following IP address : \n" |
| 168 | +pct exec $CLIENTID -- ip -br addr | grep eth0 |
0 commit comments