-
Notifications
You must be signed in to change notification settings - Fork 2
/
ztsetup
executable file
·112 lines (95 loc) · 2.86 KB
/
ztsetup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#
# A QAD script to install zerotier and automatically join a list of networks
# We also optionally update the local /etc/hosts file
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
# shellcheck disable=SC1090
. "$SCRIPT_DIR/poshlib/poshlib.sh" || exit 1
use strict
use utils
use-from .
use apt-repo
if [[ -d /etc/apt ]]; then
APT=true
elif [[ -d /etc/yum ]]; then
YUM=true
else
echo "Distribution not supported!"
exit -1
fi
# NETWORKS is a comma-separated list of zerotier networks to join
# Comments are delimited by colons
NETWORKS="8056c2e21c000001:world"
# It can also be newline-separated, and/or with space-delimited comments
#NETWORKS=$'8056c2e21c000001 world\n0000000000000000 nowhere'
if [[ -f /etc/ztsetup ]]; then
# shellcheck disable=SC1091
. /etc/ztsetup
fi
if [[ -f ~/.ztsetup ]]; then
# shellcheck disable=SC1090
. ~/.ztsetup
fi
if [[ ${1:-} ]]; then
NETWORKS="$1"
fi
# canonicalise
NETWORKS=$(echo "$NETWORKS"|tr ' ,' ': ')
if [[ ${APT:-} ]]; then
# Support headless installation
export DEBIAN_FRONTEND=noninteractive
export NEEDRESTART_SUSPEND=y
# Attempt to autodetect our distribution. This should work for debian and ubuntu
# IFF the main distribution is configured sensibly
: "${DIST:=$(awk '/^deb.*main/ {print $3}' /etc/apt/sources.list | grep -v '-' | head -1)}"
if [[ $DIST == "focal" ]]; then ## there is no repo for ubuntu 20.04 atm, 18.04 works just fine
DIST="bionic"
fi
if zerotier-cli info 2>/dev/null; then
echo "Already installed"
else
apt-repo add zerotier "http://download.zerotier.com/debian/$DIST $DIST main" \
https://raw.githubusercontent.com/zerotier/ZeroTierOne/master/doc/contact%40zerotier.com.gpg
apt-get update && apt-get -y install zerotier-one
fi
elif [[ ${YUM:-} ]]; then
DIST=$(yum repolist | awk '/^!?base/ {print $2}')
if [[ $DIST == "CentOS-7" || $DIST == "RHEL-7" ]]; then
URL="https://download.zerotier.com/redhat/el/7"
else
echo "Distribution not supported!"
exit -1
fi
cat <<EOF >/etc/yum.repos.d/zerotier.repo
[zerotier]
name=Zerotier for ${DIST}
baseurl=${URL}
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZEROTIER
EOF
curl -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZEROTIER https://download.zerotier.com/contact%40zerotier.com.gpg
yum -y --setopt=skip_missing_names_on_install=False install zerotier-one
systemctl enable zerotier-one
systemctl start zerotier-one
fi
while ! zerotier-cli info; do
# Usually takes a moment for zerotier to become active
sleep 1
done
# auto join the standard networks
for network in $NETWORKS; do
network_id=${network%%:*}
zerotier-cli join "$network_id"
done
# and add some useful name resolutions
if [[ -f /etc/zthosts ]]; then
cat /etc/zthosts >> /etc/hosts
fi
if [[ -f ~/.zthosts ]]; then
cat ~/.zthosts >> /etc/hosts
fi
if [[ -n "${tempdir:-}" ]]; then
rm -rf "$tempdir"
fi