143 lines
5.7 KiB
Bash
Raw Normal View History

2020-04-27 21:01:19 +00:00
#!/bin/ash
2020-04-27 20:08:55 +00:00
# https://stackoverflow.com/questions/3474526/stop-on-first-error
# http://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
set +e
process_template ( ) {
processed_filename=$(dirname $1)/$(basename $1 .tmpl)
2020-06-28 13:53:47 -06:00
echo "Processing template $1 -> $processed_filename"
set -e
sh -c "sigil -f $1 -p $2 > $processed_filename"
set +e
rm $1
}
function join_by { local IFS="$1"; shift; echo "$*"; }
process_templates ( ) {
for template_filename in `find $1 -type f -name '*.tmpl'`
do
process_template $template_filename "$2"
done
}
topdir=`pwd`
2020-05-21 21:27:20 +00:00
for dir in `find /etc/lixonet/* -type d -maxdepth 0`
2020-05-21 21:27:39 +00:00
do
dir=${dir%*/} # remove the trailing "/"
netname=${dir##*/} # print everything after the final "/"
# Load relevant environment variables from lixonet.conf
unset git
source $dir/lixonet.conf
2020-05-21 21:29:59 +00:00
if [ -z "$git" ]; then echo "Missing 'git' variable in $dir/lixonet.conf"; exit 1; fi
2020-05-21 21:40:40 +00:00
sigil_vars=$(cat /etc/lixonet/${netname}/lixonet.conf | tr "\\n" " ")
sigil_vars=$(echo "$sigil_vars netname=$netname")
# Parse subnets into a collection of IP reverse zones
root_reverse_zones=$(echo '${network_address}/${global_prefix:-16}' | sigil -p $sigil_vars | xargs -I '{}' netcalc split {} 24 | sed 's/.0\/24$//' | awk 'BEGIN{FS="."}{print $3"."$2"."$1".in-addr.arpa"}' | sed -e ':a;N;$!ba;s/\n/,/g')
sigil_vars=$(echo "$sigil_vars root_reverse_zones=$root_reverse_zones")
local_reverse_zones=$(echo "$bgp_routes" | tr "," "\n" | xargs -I '{}' netcalc split {} 24 | sed 's/.0\/24$//' | awk 'BEGIN{FS="."}{print $3"."$2"."$1".in-addr.arpa"}' | sed -e ':a;N;$!ba;s/\n/,/g')
sigil_vars=$(echo "$sigil_vars local_reverse_zones=$local_reverse_zones")
echo "Configuring Lixonet3 network $netname from $git..."
echo "Variables: $sigil_vars"
2020-06-28 13:58:02 -06:00
# Clone and copy the repository
rm -rfv "$(basename "$git" .git)"
GIT_SSH_COMMAND="ssh -i /etc/lixonet/${netname}/id_rsa -o IdentitiesOnly=yes" git clone $git || { echo "clone $git failed, quitting" ; exit 1; }
2020-06-28 14:00:45 -06:00
echo "Creating work directory..."
rm -rfv work
2020-06-28 14:00:45 -06:00
mkdir --verbose work
cp -rv "$(basename "$git" .git)"/* work/ && cd work
# Copy default files
2021-09-21 19:26:12 -06:00
mkdir --verbose tinc; cp -rv ../tinc/* tinc/
mkdir --verbose bird; cp -rv ../bird/* bird/
mkdir --verbose bind; cp -rv ../bind/* bind/
2021-09-21 19:20:12 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
2021-09-21 19:26:12 -06:00
mkdir --verbose wireguard; cp -rv ../wireguard/* wireguard/
2021-09-21 19:20:12 -06:00
fi
2020-06-21 05:12:14 +00:00
# Copy system-local custom files (if they even exist)
cp -rv $dir/tinc/* tinc/
cp -rv $dir/bird/* bird/
cp -rv $dir/bind/* bind/
2021-09-21 19:20:12 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
cp -rv $dir/wireguard/* wireguard/
fi
2020-06-21 05:12:14 +00:00
# Tinc
# Remove existing configuration
rm -v -rf /etc/tinc/$netname/*
# Copy all tinc default files to /etc/(tinc)
find tinc -type d | sed -e "s@tinc@tinc/${netname}@g" | xargs -I '{}' mkdir --verbose -p /etc/{}
2020-05-21 21:36:28 +00:00
find tinc -type f | sed -e "s@tinc/@@g" | xargs -I '{}' cp --verbose tinc/{} /etc/tinc/$netname/{}
2020-06-22 00:18:11 +00:00
# Build list of all peer IP addresses
tinc_peers=$(cat /etc/tinc/$netname/hosts/* | grep 'Subnet' | grep '/32' | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' | sed -e ':a;N;$!ba;s/\n/,/g')
sigil_vars=$(echo "$sigil_vars tinc_peers=$tinc_peers")
# Fill out templates and remove them after
process_templates "/etc/tinc/$netname/" "$sigil_vars"
# Copy private key
cp -v /etc/lixonet/$netname/tinc.key /etc/tinc/$netname/rsa_key.priv
# Set permissions for tinc scripts
chmod +x /etc/tinc/$netname/tinc-up /etc/tinc/$netname/tinc-down /etc/tinc/$netname/subnet-up /etc/tinc/$netname/subnet-down
# Bird
# Remove existing configuration
rm -v -rf /etc/bird/$netname/*
2020-06-13 04:22:42 +00:00
# Copy all bird default files to /etc/(bird)
find bird -type d | sed -e "s@bird@bird/${netname}@g" | xargs -I '{}' mkdir --verbose -p /etc/{}
2020-05-21 21:41:46 +00:00
find bird -type f | sed -e "s@bird/@@g" | xargs -I '{}' cp --verbose bird/{} /etc/bird/$netname/{}
# Fill out templates and remove them after
process_templates "/etc/bird/$netname/" "$sigil_vars"
2020-06-13 04:22:42 +00:00
# BIND
# Remove existing configuration
rm -v -rf /etc/bind/$netname/*
# Copy all bind default files to /etc/(bind)
find bind -type d | sed -e "s@bind@bind/${netname}@g" | xargs -I '{}' mkdir --verbose -p /etc/{}
find bind -type f | sed -e "s@bind/@@g" | xargs -I '{}' cp --verbose bind/{} /etc/bind/$netname/{}
# Fill out templates and remove them after
process_templates "/etc/bind/$netname/" "$sigil_vars"
2021-09-21 14:01:51 -06:00
# Wireguard
2021-09-21 15:50:44 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
2021-09-21 15:17:59 -06:00
# Remove existing configuration
rm -v -rf /etc/wireguard/$netname/*
2021-09-21 15:50:44 -06:00
# Copy all wireguard default files to /etc/(wireguard)
2021-09-21 15:17:59 -06:00
find wireguard -type d | sed -e "s@wireguard@wireguard/${netname}@g" | xargs -I '{}' mkdir --verbose -p /etc/{}
find wireguard -type f | sed -e "s@wireguard/@@g" | xargs -I '{}' cp --verbose wireguard/{} /etc/wireguard/$netname/{}
# Fill out templates and remove them after
process_templates "/etc/wireguard/$netname/" "$sigil_vars"
fi
2020-06-28 13:50:55 -06:00
# Docker
2020-06-28 14:10:24 -06:00
# Copy templates and dependencies to directory
2020-06-28 14:02:30 -06:00
mkdir docker/
cp -rv ../docker/* docker/
2021-09-21 15:54:46 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
cp docker/services.opt/wireguard docker/services/wireguard
fi
cp -rv $dir/docker/* docker/
cp -v ./../docker-compose.yml.tmpl .
cp -v ../Dockerfile.* .
2020-06-28 13:50:55 -06:00
process_templates "." "$sigil_vars"
2020-06-13 04:22:42 +00:00
echo $sigil_vars | tr ' ' '\n' > .env
2020-06-28 14:02:56 -06:00
cat docker-compose.yml
set -e
2020-06-25 15:33:39 -06:00
docker-compose -p $netname down
2020-06-22 00:18:11 +00:00
docker-compose -p $netname up -d --build --remove-orphans
set +e
rm -v .env
2020-05-21 21:28:31 +00:00
# Pop directory
cd $topdir
done