-
Notifications
You must be signed in to change notification settings - Fork 96
/
install.sh
188 lines (157 loc) · 3.63 KB
/
install.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/sh
set -e
set -o noglob
github_version() {
set +e
curl -s https://api.github.com/repos/mudler/edgevpn/releases/latest | \
grep tag_name | \
awk '{ print $2 }' | \
sed -e 's/\"//g' -e 's/,//g' || echo "v0.8.5"
set -e
}
DOWNLOADER=${DOWNLOADER:-curl}
VERSION=${VERSION:-$(github_version)}
info()
{
echo '[INFO] ' "$@"
}
warn()
{
echo '[WARN] ' "$@" >&2
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
detect_arch() {
if [ -z "$ARCH" ]; then
ARCH=$(uname -m)
fi
case $ARCH in
i386)
ARCH=i386
;;
amd64|x86_64)
ARCH=x86_64
;;
arm64|aarch64)
ARCH=arm64
;;
arm*)
ARCH=armv6
;;
*)
fatal "Unsupported architecture $ARCH"
esac
}
detect_platform() {
if [ -z "$OS" ]; then
OS=$(uname -o)
fi
case $OS in
*Linux)
OS=Linux
;;
*)
fatal "Unsupported platform $OS"
esac
}
verify_env() {
detect_arch
detect_platform
if [ -x /sbin/openrc-run ]; then
HAS_OPENRC=true
fi
if [ -x /bin/systemctl ] || type systemctl > /dev/null 2>&1; then
HAS_SYSTEMD=true
fi
SUDO=sudo
if [ $(id -u) -eq 0 ]; then
SUDO=
fi
if [ -n "${INSTALL_BIN_DIR}" ]; then
BIN_DIR=${INSTALL_BIN_DIR}
else
BIN_DIR=/usr/local/bin
if ! $SUDO sh -c "touch ${BIN_DIR}/ro-test && rm -rf ${BIN_DIR}/ro-test"; then
if [ -d /opt/bin ]; then
BIN_DIR=/opt/bin
fi
fi
fi
}
setup_service() {
if [ -n "${INSTALL_SYSTEMD_DIR}" ]; then
SYSTEMD_DIR="${INSTALL_SYSTEMD_DIR}"
else
SYSTEMD_DIR=/etc/systemd/system
fi
if [ "${HAS_SYSTEMD}" = true ]; then
FILE_SERVICE=${SYSTEMD_DIR}/[email protected]
$SUDO tee $FILE_SERVICE >/dev/null << EOF
[Unit]
Description=EdgeVPN Daemon
After=network.target
[Service]
EnvironmentFile=/etc/systemd/system.conf.d/edgevpn-%i.env
LimitNOFILE=49152
ExecStartPre=-/bin/sh -c "sysctl -w net.core.rmem_max=2500000"
ExecStart=$BIN_DIR/edgevpn
Restart=always
[Install]
WantedBy=multi-user.target
EOF
elif [ "${HAS_OPENRC}" = true ]; then
$SUDO tee /etc/init.d/edgevpn >/dev/null << EOF
#!/sbin/openrc-run
depend() {
after network-online
}
supervisor=supervise-daemon
name=edgevpn
command="${BIN_DIR}/edgevpn"
command_args="$(escape_dq "edgevpn")
>>${LOG_FILE} 2>&1"
output_log=${LOG_FILE}
error_log=${LOG_FILE}
pidfile="/var/run/edgevpn.pid"
respawn_delay=5
respawn_max=0
set -o allexport
if [ -f /etc/environment ]; then source /etc/environment; fi
if [ -f /etc/edgevpn.env ]; then source /etc/edgevpn.env; fi
set +o allexport
EOF
fi
}
download() {
[ $# -eq 2 ] || fatal 'download needs exactly 2 arguments'
case $DOWNLOADER in
curl)
curl -o $1 -sfL $2
;;
wget)
wget -qO $1 $2
;;
*)
fatal "Incorrect executable '$DOWNLOADER'"
;;
esac
# Abort if download command failed
[ $? -eq 0 ] || fatal 'Download failed'
}
install() {
info "Arch: $ARCH. OS: $OS Version: $VERSION (\$VERSION)"
TMP_DIR=$(mktemp -d -t edgevpn-install.XXXXXXXXXX)
download $TMP_DIR/out.tar.gz https://github.com/mudler/edgevpn/releases/download/$VERSION/edgevpn-$VERSION-$OS-$ARCH.tar.gz
# TODO verify w/ checksum
tar xvf $TMP_DIR/out.tar.gz -C $TMP_DIR
$SUDO cp -rf $TMP_DIR/edgevpn $BIN_DIR/
# TODO trap
rm -rf $TMP_DIR
# TODO setup env files for a network connection
}
verify_env
setup_service
install