lixonet-ee/build.sh

97 lines
3.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
process_template ( ) {
processed_filename=$(dirname $1)/$(basename $1 .tmpl)
echo "Processing template $1 -> $processed_filename with args $2"
sh -c "sigil -f $1 -p $2 > $processed_filename"
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" " ")
# 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"
# Clone and enter the repository
2020-05-21 21:36:48 +00:00
rm -rf "$(basename "$git" .git)"
git clone $git || { echo "clone $git failed, quitting" ; exit 1; }
cd "$(basename "$git" .git)"
# Copy default files
cp -r ../tinc/* tinc/
cp -r ../bird/* bird/
2020-06-13 04:22:42 +00:00
cp -r ../bind/* bind/
2020-06-21 05:12:14 +00:00
# Copy system-local custom files (if they even exist)
2020-06-21 05:57:01 +00:00
cp -r $dir/tinc/* tinc/
cp -r $dir/bird/* bird/
cp -r $dir/bind/* bind/
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/{}
# Fill out templates and remove them after
process_templates "/etc/tinc/$netname/" "$sigil_vars"
# Copy private key
cp /etc/lixonet/$netname/tinc.key /etc/tinc/$netname/rsa_key.priv
# Set permissions for tinc scripts
chmod +x /etc/tinc/$netname/tinc-up
chmod +x /etc/tinc/$netname/tinc-down
chmod +x /etc/tinc/$netname/subnet-up
chmod +x /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"
echo "COMPOSE_PROJECT_NAME=$netname" > .env
docker-compose -p $netname up -d --build
rm .env
2020-05-21 21:28:31 +00:00
# Pop directory
cd $topdir
done