lixonet-ee/build.sh

178 lines
6.9 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
2023-04-23 18:28:03 -06:00
export dir=${dir%*/} # remove the trailing "/"
export netname=${dir##*/} # print everything after the final "/"
export config_out="/etc/lixonet/${netname}/.config"
# 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)"
2024-02-23 17:04:57 -07:00
rsa_filename="/etc/lixonet/${netname}/id_rsa"
if [ ! -f $rsa_filename ]; then rsa_filename="/etc/lixonet/id_rsa"; fi
2024-02-23 17:00:45 -07:00
echo "Using SSH key: $rsa_filename"
2024-02-23 16:53:07 -07:00
GIT_SSH_COMMAND="ssh -i $rsa_filename -o IdentitiesOnly=yes" git clone $git || { echo "clone $git failed, quitting" ; exit 1; }
2023-04-19 20:08:07 -06:00
# Clone any add-ons
for addon_git in `echo "$addons" | tr "," "\n"`
do
2024-02-23 16:53:07 -07:00
rm -rfv "$(basename "$addon_git" .git)"
2024-02-23 17:03:49 -07:00
rsa_filename="/etc/lixonet/${netname}/$(basename "$addon_git" .git).key"
2024-02-23 16:53:07 -07:00
if [ ! -f $rsa_filename ]; then rsa_filename="/etc/lixonet/${netname}/id_rsa"; fi
2024-02-23 17:00:45 -07:00
echo "Using SSH key: $rsa_filename"
2024-02-23 16:53:07 -07:00
GIT_SSH_COMMAND="ssh -i $rsa_filename -o IdentitiesOnly=yes" git clone $addon_git || { echo "clone addon $addon_git failed, quitting" ; exit 1; }
2023-04-19 20:08:07 -06:00
done
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
2023-04-19 20:08:07 -06:00
cp -rv "$(basename "$git" .git)"/* work/
for addon_git in `echo "$addons" | tr "," "\n"`
do
cp -rv "$(basename "$addon_git" .git)"/* work/
done
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/
2023-03-21 13:18:19 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
mkdir --verbose wireguard; cp -rv ../wireguard/* wireguard/
fi
if [ "${ddns_enabled:-0}" -eq "1" ]; then
mkdir --verbose ddns; cp -rv ../ddns/* ddns/
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/
2023-03-21 13:18:19 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
cp -rv $dir/wireguard/* wireguard/
fi
if [ "${ddns:-0}" -eq "1" ]; then
cp -rv $dir/ddns/* ddns/
fi
# Tinc
# Remove existing configuration
2023-04-23 17:57:58 -06:00
rm -v -rf $config_out/tinc/*
# Copy all tinc default files to /etc/(tinc)
2023-04-23 17:57:58 -06:00
find tinc -type d | xargs -I '{}' mkdir --verbose -p $config_out/{}
2023-04-23 18:04:28 -06:00
find tinc -type f | sed -e "s@tinc/@@g" | xargs -I '{}' cp --verbose tinc/{} $config_out/tinc/{}
2020-06-22 00:18:11 +00:00
# Build list of all peer IP addresses
2023-04-23 17:57:58 -06:00
tinc_peers=$(cat $config_out/tinc/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')
2020-06-22 00:18:11 +00:00
sigil_vars=$(echo "$sigil_vars tinc_peers=$tinc_peers")
# Copy private key
2023-04-23 17:57:58 -06:00
cp -v /etc/lixonet/$netname/tinc.key $config_out/tinc/rsa_key.priv
2023-04-25 17:51:44 -06:00
# Bird
# Remove existing configuration
2023-04-23 17:57:58 -06:00
rm -v -rf $config_out/bird/*
2020-06-13 04:22:42 +00:00
# Copy all bird default files to /etc/(bird)
2023-04-23 17:57:58 -06:00
find bird -type d | xargs -I '{}' mkdir --verbose -p $config_out/{}
find bird -type f | sed -e "s@bird/@@g" | xargs -I '{}' cp --verbose bird/{} $config_out/bird/{}
2020-06-13 04:22:42 +00:00
# BIND
# Remove existing configuration
2023-04-23 17:57:58 -06:00
rm -v -rf $config_out/bind/*
2020-06-13 04:22:42 +00:00
# Copy all bind default files to /etc/(bind)
2023-04-23 18:06:53 -06:00
find bind -type d | xargs -I '{}' mkdir --verbose -p $config_out/{}
2023-04-23 17:57:58 -06:00
find bind -type f | sed -e "s@bind/@@g" | xargs -I '{}' cp --verbose bind/{} $config_out/bind/{}
2021-09-21 14:01:51 -06:00
2023-03-21 13:05:32 -06:00
# Wireguard
if [ "${wg_enabled:-0}" -eq "1" ]; then
# Remove existing configuration
2023-04-23 17:57:58 -06:00
rm -v -rf $config_out/wireguard/*
2023-03-21 13:05:32 -06:00
# Copy all wireguard default files to /etc/(wireguard)
2023-04-23 18:07:28 -06:00
find wireguard -type d | xargs -I '{}' mkdir --verbose -p $config_out/{}
2023-04-23 17:57:58 -06:00
find wireguard -type f | sed -e "s@wireguard/@@g" | xargs -I '{}' cp --verbose wireguard/{} $config_out/wireguard/{}
2023-03-21 13:05:32 -06:00
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/
2023-03-21 13:05:32 -06:00
if [ "${wg_enabled:-0}" -eq "1" ]; then
2023-04-23 18:25:32 -06:00
cp -v docker/services.opt/wireguard docker/services/wireguard
cp -v docker/services.opt/wireguard_router docker/services/wireguard_router
2023-03-21 13:05:32 -06:00
fi
if [ "${ddns_enabled:-0}" -eq "1" ]; then
2023-04-23 18:25:32 -06:00
cp -v docker/services.opt/ddns docker/services/ddns
2023-03-21 13:05:32 -06:00
fi
2023-04-23 17:40:44 -06:00
for addon_git in `echo "$addons" | tr "," "\n"`
do
2023-04-23 18:25:32 -06:00
echo "Running addon script for $(basename "$addon_git" .git)..."
chmod +x "$(basename "$addon_git" .git).sh" && "./$(basename "$addon_git" .git).sh"
2023-04-23 17:40:44 -06:00
done
2023-04-24 16:09:51 -06:00
# Process templates
2023-04-24 16:20:43 -06:00
echo "Processing configuration templates..."
2023-04-24 16:09:51 -06:00
process_templates "$config_out/" "$sigil_vars"
2023-04-24 16:20:43 -06:00
echo "Setting any processed shell scripts as executable..."
2023-04-25 17:38:47 -06:00
find $config_out -type f -name "*.sh" | xargs -I '{}' chmod -v +x {}
2023-04-25 17:51:44 -06:00
chmod -v +x $config_out/tinc/tinc-up $config_out/tinc/tinc-down $config_out/tinc/subnet-up $config_out/tinc/subnet-down $config_out/tinc/host-up $config_out/tinc/host-down $config_out/tinc/check-node
2021-09-21 15:54:46 -06:00
cp -rv $dir/docker/* docker/
cp -v ./../docker-compose.yml.tmpl .
cp -v ../Dockerfile.* .
2023-04-24 16:20:43 -06:00
echo "Processing Docker templates..."
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
2023-04-23 17:57:58 -06:00
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
2023-04-23 17:57:58 -06:00
set +e
rm -v .env
2020-05-21 21:28:31 +00:00
# Pop directory
cd $topdir
done