-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathstart_firsttime_init.sh
executable file
·147 lines (135 loc) · 4.93 KB
/
start_firsttime_init.sh
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# debugging purpose
if [ ! -z $DEBUG_ENV ]; then
echo "Debugging: Print EnvVar:"
printenv
fi
# create dest folder
mkdir -p /etc/zt-mkworld
# make sure we've got it chowned
chown -R zerotier-one:zerotier-one /opt/key-networks/ztncui
chown -R zerotier-one:zerotier-one /var/lib/zerotier-one
# detect if identity folder exists
if [ ! -d /var/lib/zerotier-one ]; then
mkdir -p /var/lib/zerotier-one
fi
# detect if identity private key exists
if [ ! -f /var/lib/zerotier-one/identity.secret ]; then
cd /var/lib/zerotier-one
/usr/sbin/zerotier-idtool generate identity.secret identity.public
fi
# always make httpfs folder
mkdir -p /opt/key-networks/ztncui/etc/httpfs
# detect public ip
if [ -z $MYADDR ]; then
echo "Set Your IP Address to continue."
echo "If you don't do that, I will automatically detect."
MYEXTADDR=$(curl --connect-timeout 5 ip.sb)
if [ -z $MYEXTADDR ]; then
MYINTADDR=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
MYADDR=${MYINTADDR}
else
MYADDR=${MYEXTADDR}
fi
fi
MYDOMAIN=${MYDOMAIN:-ztncui.docker.test} # Used for planet comment
echo "YOUR IP: ${MYADDR}"
echo "YOUR DOMAIN: ${MYDOMAIN}"
cd /etc/zt-mkworld
# detect if ALREADY_INITED
if [ -f /etc/zt-mkworld/ALREADY_INITED ]; then
echo "ALREADY_INITED detected."
exit 0
# if not exist, goto planet file generate.
else
# if set to 0, won't do anything.
if [[ $AUTOGEN_PLANET -eq 0 ]]; then
# finally create ALREADY_INITED flag file
echo "AUTOGEN_PLANET is 0. Set to inited and exit."
touch /etc/zt-mkworld/ALREADY_INITED
exit 0
fi
# AUTOGEN_PLANET is not 0, backup now.
if [[ -f /var/lib/zerotier-one/planet ]]; then
cp /var/lib/zerotier-one/planet /var/lib/zerotier-one/planet.bak.$(date +%s)
fi
# if AUTOGEN_PLANET is set to 1, check if identity.public exists,
# then generate mkworld.config.json on the fly
if [[ $AUTOGEN_PLANET -eq 1 ]]; then
# check if identity.public exists
if [ -f /var/lib/zerotier-one/identity.public ]; then
# generate json config file
rm -f /etc/zt-mkworld/mkworld.config.json
# now heredoc
cat << EOF > /etc/zt-mkworld/mkworld.config.json
{
"rootNodes": [
{
"comments": "custom planet - ${MYDOMAIN} - ${MYADDR}",
"identity": "$(cat /var/lib/zerotier-one/identity.public)",
"endpoints": [
"${MYADDR}/9993"
]
}
],
"signing": ["previous.c25519", "current.c25519"],
"output": "planet.custom",
"plID": 0,
"plBirth": 0,
"plRecommend": true
}
EOF
# run program under corresponding workdir, check exit code is 0.
cd /etc/zt-mkworld
/usr/local/bin/ztmkworld -c /etc/zt-mkworld/mkworld.config.json
# copy custom planet to /var/lib/zerotier-one and httpfs
if [[ $? -eq 0 ]]; then
cp -f ./planet.custom /var/lib/zerotier-one/planet
cp -f ./planet.custom /opt/key-networks/ztncui/etc/httpfs
chown -R zerotier-one:zerotier-one /var/lib/zerotier-one
echo "planet successfully generated."
else
echo "planet generator failed. exit now."
/run/s6/basedir/bin/halt
exit 1
fi
# finally create ALREADY_INITED flag file
echo "mkworld successfully ran."
touch /etc/zt-mkworld/ALREADY_INITED
exit 0
else
echo "identity.public does NOT exit, cannot generate planet file."
/run/s6/basedir/bin/halt
exit 1
fi
fi
# if set to 2, only use mkworld.config.json provided
# check if mkworld.config.json exists
if [[ $AUTOGEN_PLANET -eq 2 ]]; then
cd /etc/zt-mkworld
if [ ! -f /etc/zt-mkworld/mkworld.config.json ]; then
echo "/etc/zt-mkworld/mkworld.config.json not exists. exit now."
/run/s6/basedir/bin/halt
exit 1
fi
/usr/local/bin/ztmkworld -c /etc/zt-mkworld/mkworld.config.json
# check if successfully exit
# copy custom planet to /var/lib/zerotier-one and httpfs
if [[ $? -eq 0 ]]; then
cp -f ./planet.custom /var/lib/zerotier-one/planet
cp -f ./planet.custom /opt/key-networks/ztncui/etc/httpfs
chown -R zerotier-one:zerotier-one /var/lib/zerotier-one
echo "planet successfully generated."
else
echo "planet generator failed. exit now."
/run/s6/basedir/bin/halt
exit 1
fi
# finally create ALREADY_INITED flag file
echo "mkworld successfully ran."
touch /etc/zt-mkworld/ALREADY_INITED
exit 0
fi
# after generate, copy to httpfs folder, do not directly expose the mkworld config folder.
# the config folder contains secret keys!
fi