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