summaryrefslogtreecommitdiff
path: root/ext/installfiles/linux/install.tmpl.sh
blob: 3968b264393c62b20357424c30f954bf2b50281f (plain)
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
148
149
150
151
152
#!/bin/bash

export PATH=/bin:/usr/bin:/sbin:/usr/sbin
shopt -s expand_aliases

dryRun=0

echo "*** ZeroTier One install/update ***"

if [ "$UID" -ne 0 ]; then
	echo "Not running as root so doing dry run (no modifications to system)..."
	dryRun=1
fi

# Detect systemd vs. regular init
SYSTEMDUNITDIR=
if [ -e /bin/systemctl -o -e /usr/bin/systemctl -o -e /usr/local/bin/systemctl -o -e /sbin/systemctl -o -e /usr/sbin/systemctl ]; then
	if [ -e /usr/bin/pkg-config ]; then
		SYSTEMDUNITDIR=`/usr/bin/pkg-config systemd --variable=systemdsystemunitdir`
	fi
	if [ -z "$SYSTEMDUNITDIR" -o ! -d "$SYSTEMDUNITDIR" ]; then
		if [ -d /usr/lib/systemd/system ]; then
			SYSTEMDUNITDIR=/usr/lib/systemd/system
		fi
		if [ -d /etc/systemd/system ]; then
			SYSTEMDUNITDIR=/etc/systemd/system
		fi
	fi
fi

if [ $dryRun -gt 0 ]; then
	alias ln="echo '>> ln'"
	alias rm="echo '>> rm'"
	alias mv="echo '>> mv'"
	alias cp="echo '>> cp'"
	alias chown="echo '>> chown'"
	alias chgrp="echo '>> chgrp'"
	alias chmod="echo '>> chmod'"
	alias chkconfig="echo '>> chkconfig'"
	alias zerotier-cli="echo '>> zerotier-cli'"
	alias service="echo '>> service'"
	alias systemctl="echo '>> systemctl'"
fi

scriptPath="`dirname "$0"`/`basename "$0"`"
if [ ! -r "$scriptPath" ]; then
	scriptPath="$0"
	if [ ! -r "$scriptPath" ]; then
		echo "Installer cannot determine its own path; $scriptPath is not readable."
		exit 2
	fi
fi

endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1`
if [ "$endMarkerIndex" -le 100 ]; then
	echo 'Internal error: unable to find end of script / start of binary data marker.'
	exit 2
fi
blobStart=`expr $endMarkerIndex + 17`
if [ "$blobStart" -le "$endMarkerIndex" ]; then
	echo 'Internal error: unable to find end of script / start of binary data marker.'
	exit 2
fi

echo 'Extracting files...'
if [ $dryRun -gt 0 ]; then
	echo ">> tail -c +$blobStart \"$scriptPath\" | gunzip -c | tar -xvop -C / -f -"
	tail -c +$blobStart "$scriptPath" | gunzip -c | tar -t -f - | sed 's/^/>>   /'
else
	tail -c +$blobStart "$scriptPath" | gunzip -c | tar -xvop --no-overwrite-dir -C / -f -
fi

if [ $dryRun -eq 0 -a ! -d "/var/lib/zerotier-one" ]; then
	echo 'Archive extraction failed, cannot find zerotier-one binary in "/var/lib/zerotier-one".'
	exit 2
fi

echo 'Installing zerotier-cli command line utility...'

rm -f /usr/bin/zerotier-cli
ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-cli

echo 'Installing and (re-)starting zerotier-one daemon...'

# Note: ensure that service restarts are the last thing this script actually
# does, since these may kill the script itself. Also note the & to allow
# them to finish independently.
if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then
	# If this was updated or upgraded from an init.d based system, clean up the old
	# init.d stuff before installing directly via systemd.
	if [ -f /etc/init.d/zerotier-one ]; then
		if [ -e /sbin/chkconfig -o -e /usr/sbin/chkconfig -o -e /bin/chkconfig -o -e /usr/bin/chkconfig ]; then
			chkconfig zerotier-one off
		fi
		rm -f /etc/init.d/zerotier-one
	fi

	cp -f /tmp/systemd_zerotier-one.service "$SYSTEMDUNITDIR/zerotier-one.service"
	rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one

	systemctl enable zerotier-one
	systemctl restart zerotier-one &
else
	cp -f /tmp/init.d_zerotier-one /etc/init.d/zerotier-one
	chmod 0755 /etc/init.d/zerotier-one
	rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one

	if [ -f /sbin/chkconfig -o -f /usr/sbin/chkconfig -o -f /usr/bin/chkconfig -o -f /bin/chkconfig ]; then
		chkconfig zerotier-one on
	else
		if [ -d /etc/rc0.d ]; then
			rm -f /etc/rc0.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one
		fi
		if [ -d /etc/rc1.d ]; then
			rm -f /etc/rc1.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc1.d/K89zerotier-one
		fi
		if [ -d /etc/rc2.d ]; then
			rm -f /etc/rc2.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one
		fi
		if [ -d /etc/rc3.d ]; then
			rm -f /etc/rc3.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one
		fi
		if [ -d /etc/rc4.d ]; then
			rm -f /etc/rc4.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one
		fi
		if [ -d /etc/rc5.d ]; then
			rm -f /etc/rc5.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one
		fi
		if [ -d /etc/rc6.d ]; then
			rm -f /etc/rc6.d/???zerotier-one
			ln -sf /etc/init.d/zerotier-one /etc/rc6.d/K89zerotier-one
		fi
	fi

	service zerotier-one restart &
fi

sleep 1

exit 0

# Do not remove the last line or add a carriage return to it! The installer
# looks for an unterminated line beginning with 16 #'s in itself to find
# the binary blob data, which is appended after it.

################