Files

131 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2021-02-24 03:43:45 -08:00
#!/bin/sh
grepzt() {
[ -f /var/lib/zerotier-one/zerotier-one.pid -a -n "$(cat /var/lib/zerotier-one/zerotier-one.pid 2>/dev/null)" -a -d "/proc/$(cat /var/lib/zerotier-one/zerotier-one.pid 2>/dev/null)" ]
2021-02-24 03:43:45 -08:00
return $?
}
2021-04-13 13:18:21 -07:00
mkztfile() {
file=$1
mode=$2
content=$3
echo "creating $file"
2021-04-13 13:18:21 -07:00
mkdir -p /var/lib/zerotier-one
echo -n "$content" > "/var/lib/zerotier-one/$file"
2021-04-13 13:18:21 -07:00
chmod "$mode" "/var/lib/zerotier-one/$file"
}
if [ "x$ZEROTIER_API_SECRET" != "x" ]
then
mkztfile authtoken.secret 0600 "$ZEROTIER_API_SECRET"
mkztfile metricstoken.secret 0600 "$ZEROTIER_API_SECRET"
2021-04-13 13:18:21 -07:00
fi
if [ "x$ZEROTIER_IDENTITY_PUBLIC" != "x" ]
then
mkztfile identity.public 0644 "$ZEROTIER_IDENTITY_PUBLIC"
fi
if [ "x$ZEROTIER_IDENTITY_SECRET" != "x" ]
then
mkztfile identity.secret 0600 "$ZEROTIER_IDENTITY_SECRET"
fi
if [ "x$ZEROTIER_LOCAL_CONF" != "x" ]
then
mkztfile local.conf 0644 "$ZEROTIER_LOCAL_CONF"
fi
2021-06-01 09:27:05 -07:00
mkztfile zerotier-one.port 0600 "9993"
killzerotier() {
2022-04-13 00:09:46 -07:00
log "Killing zerotier"
kill $(cat /var/lib/zerotier-one/zerotier-one.pid 2>/dev/null)
2021-06-01 09:27:05 -07:00
exit 0
}
2022-04-13 00:09:46 -07:00
log_header() {
echo -n "\r=>"
}
log_detail_header() {
echo -n "\r===>"
}
log() {
echo "$(log_header)" "$@"
}
log_params() {
title=$1
shift
log "$title" "[$@]"
}
log_detail() {
echo "$(log_detail_header)" "$@"
}
log_detail_params() {
title=$1
shift
log_detail "$title" "[$@]"
}
2021-06-01 09:27:05 -07:00
trap killzerotier INT TERM
2022-04-13 00:09:46 -07:00
log "Configuring networks to join"
2022-03-23 17:56:09 -07:00
mkdir -p /var/lib/zerotier-one/networks.d
2023-03-07 16:50:34 -05:00
log_params "Joining networks from command line:" $@
2022-03-23 17:56:09 -07:00
for i in "$@"
do
2022-04-13 00:09:46 -07:00
log_detail_params "Configuring join:" "$i"
2022-03-23 17:56:09 -07:00
touch "/var/lib/zerotier-one/networks.d/${i}.conf"
done
2023-03-07 16:50:34 -05:00
if [ "x$ZEROTIER_JOIN_NETWORKS" != "x" ]
then
log_params "Joining networks from environment:" $ZEROTIER_JOIN_NETWORKS
for i in $ZEROTIER_JOIN_NETWORKS
2023-03-07 16:50:34 -05:00
do
log_detail_params "Configuring join:" "$i"
touch "/var/lib/zerotier-one/networks.d/${i}.conf"
done
fi
2022-04-13 00:09:46 -07:00
log "Starting ZeroTier"
2021-06-01 12:30:01 -07:00
nohup /usr/sbin/zerotier-one &
2021-02-24 03:43:45 -08:00
while ! grepzt
do
2022-04-13 00:09:46 -07:00
log_detail "ZeroTier hasn't started, waiting a second"
2022-03-23 17:56:09 -07:00
if [ -f nohup.out ]
then
tail -n 10 nohup.out
fi
2021-02-24 03:43:45 -08:00
sleep 1
done
2022-04-13 00:09:46 -07:00
log_params "Writing healthcheck for networks:" $@
2021-02-24 03:43:45 -08:00
2022-03-23 18:08:46 -07:00
cat >/healthcheck.sh <<EOF
#!/bin/bash
for i in $@ $ZEROTIER_JOIN_NETWORKS
2021-02-24 03:43:45 -08:00
do
2022-03-23 18:08:46 -07:00
[ "\$(zerotier-cli get \$i status)" = "OK" ] || exit 1
2021-02-24 03:43:45 -08:00
done
2022-03-23 18:08:46 -07:00
EOF
chmod +x /healthcheck.sh
2021-02-24 03:43:45 -08:00
2022-04-13 00:09:46 -07:00
log_params "zerotier-cli info:" "$(zerotier-cli info)"
2022-04-08 21:03:54 -07:00
2022-04-13 00:09:46 -07:00
log "Sleeping infinitely"
2022-03-23 18:09:04 -07:00
while true
do
sleep 1
done