-
Notifications
You must be signed in to change notification settings - Fork 62
/
install.sh
255 lines (220 loc) · 5.89 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/sh
set -e
GITHUB_URL="https://github.com/coroot/coroot-node-agent/releases"
DOWNLOADER=
SUDO=sudo
if [ $(id -u) -eq 0 ]; then
SUDO=
fi
BIN_DIR=/usr/bin
SYSTEMD_DIR=/etc/systemd/system
VERSION=
SYSTEM_NAME=coroot-node-agent
SYSTEMD_SERVICE=${SYSTEM_NAME}.service
UNINSTALL_SH=${BIN_DIR}/${SYSTEM_NAME}-uninstall.sh
FILE_SERVICE=${SYSTEMD_DIR}/${SYSTEMD_SERVICE}
FILE_ENV=${SYSTEMD_DIR}/${SYSTEMD_SERVICE}.env
ENV_VARS="^(LISTEN|CGROUPFS_ROOT|DISABLE_LOG_PARSING|DISABLE_PINGER|DISABLE_L7_TRACING|TRACK_PUBLIC_NETWORK|EPHEMERAL_PORT_RANGE|PROVIDER|REGION|AVAILABILITY_ZONE|INSTANCE_TYPE|INSTANCE_LIFE_CYCLE|LOG_PER_SECOND|LOG_BURST|COLLECTOR_ENDPOINT|API_KEY|METRICS_ENDPOINT|TRACES_ENDPOINT|LOGS_ENDPOINT|PROFILES_ENDPOINT|SCRAPE_INTERVAL|WAL_DIR)"
info()
{
echo '[INFO] ' "$@"
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
verify_system() {
if [ -x /bin/systemctl ] || type systemctl > /dev/null 2>&1; then
return
fi
fatal 'Cannot find systemd'
}
verify_executable() {
if [ ! -x ${BIN_DIR}/coroot-node-agent ]; then
fatal "Executable coroot-node-agent binary not found at ${BIN_DIR}/coroot-node-agent"
fi
}
verify_arch() {
if [ -z "$ARCH" ]; then
ARCH=$(uname -m)
fi
case $ARCH in
amd64)
ARCH=amd64
;;
x86_64)
ARCH=amd64
;;
arm64)
ARCH=arm64
;;
aarch64)
ARCH=arm64
;;
*)
fatal "Unsupported architecture $ARCH"
esac
}
verify_downloader() {
[ -x "$(command -v $1)" ] || return 1
DOWNLOADER=$1
return 0
}
setup_tmp() {
TMP_DIR=$(mktemp -d -t coroot-agent-install.XXXXXXXXXX)
TMP_BIN=${TMP_DIR}/coroot-node-agent
cleanup() {
code=$?
set +e
trap - EXIT
rm -rf ${TMP_DIR}
exit $code
}
trap cleanup INT EXIT
}
get_release_version() {
info "Finding the latest release"
latest_release_url=${GITHUB_URL}/latest
case $DOWNLOADER in
curl)
VERSION=$(curl -w '%{url_effective}' -L -s -S ${latest_release_url} -o /dev/null | sed -e 's|.*/||')
;;
wget)
VERSION=$(wget -SqO /dev/null ${latest_release_url} 2>&1 | grep -i Location | sed -e 's|.*/||')
;;
*)
fatal "Incorrect downloader executable '$DOWNLOADER'"
;;
esac
info "The latest release is ${VERSION}"
}
download_binary() {
info "Downloading binary"
URL="${GITHUB_URL}/download/${VERSION}/coroot-node-agent-${ARCH}"
set +e
case $DOWNLOADER in
curl)
curl -o ${TMP_BIN} -sfL ${URL}
;;
wget)
wget -qO ${TMP_BIN} ${URL}
;;
*)
fatal "Incorrect executable '$DOWNLOADER'"
;;
esac
[ $? -eq 0 ] || fatal 'Download failed'
set -e
}
setup_binary() {
chmod 755 ${TMP_BIN}
info "Installing coroot-node-agent to ${BIN_DIR}/coroot-node-agent"
$SUDO chown root:root ${TMP_BIN}
$SUDO mv -f ${TMP_BIN} ${BIN_DIR}/coroot-node-agent
}
download() {
verify_arch
verify_downloader curl || verify_downloader wget || fatal 'Can not find curl or wget for downloading files'
setup_tmp
get_release_version
download_binary
setup_binary
}
create_uninstall() {
info "Creating uninstall script ${UNINSTALL_SH}"
$SUDO tee ${UNINSTALL_SH} >/dev/null << EOF
#!/bin/sh
set -x
[ \$(id -u) -eq 0 ] || exec sudo \$0 \$@
systemctl stop ${SYSTEM_NAME}
systemctl disable ${SYSTEM_NAME}
systemctl reset-failed ${SYSTEM_NAME}
systemctl daemon-reload
rm -f ${FILE_SERVICE}
rm -f ${FILE_ENV}
remove_uninstall() {
rm -f ${UNINSTALL_SH}
}
trap remove_uninstall EXIT
rm -rf /var/lib/coroot-node-agent || true
rm -f ${BIN_DIR}/coroot-node-agent
EOF
$SUDO chmod 755 ${UNINSTALL_SH}
$SUDO chown root:root ${UNINSTALL_SH}
}
systemd_disable() {
$SUDO systemctl disable ${SYSTEM_NAME} >/dev/null 2>&1 || true
$SUDO rm -f ${FILE_SERVICE} || true
$SUDO rm -f ${FILE_ENV} || true
}
create_env_file() {
info "env: Creating environment file ${FILE_ENV}"
$SUDO touch ${FILE_ENV}
$SUDO chmod 0600 ${FILE_ENV}
sh -c export | while read x v; do echo $v; done | grep -E ${ENV_VARS} | $SUDO tee ${FILE_ENV} >/dev/null
}
create_systemd_service_file() {
info "systemd: Creating service file ${FILE_SERVICE}"
$SUDO tee ${FILE_SERVICE} >/dev/null << EOF
[Unit]
Description=Coroot node agent
Documentation=https://coroot.com
Wants=network-online.target
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=exec
EnvironmentFile=-/etc/default/%N
EnvironmentFile=-/etc/sysconfig/%N
EnvironmentFile=-${FILE_ENV}
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
ExecStart=${BIN_DIR}/coroot-node-agent
EOF
}
create_service_file() {
create_systemd_service_file
return 0
}
get_installed_hashes() {
$SUDO sha256sum ${BIN_DIR}/coroot-node-agent ${FILE_SERVICE} ${FILE_ENV} 2>&1 || true
}
systemd_enable() {
info "systemd: Enabling ${SYSTEM_NAME} unit"
$SUDO systemctl enable ${FILE_SERVICE} >/dev/null
$SUDO systemctl daemon-reload >/dev/null
}
systemd_start() {
info "systemd: Starting ${SYSTEM_NAME}"
$SUDO systemctl restart ${SYSTEM_NAME}
}
service_enable_and_start() {
systemd_enable
POST_INSTALL_HASHES=$(get_installed_hashes)
if [ "${PRE_INSTALL_HASHES}" = "${POST_INSTALL_HASHES}" ]; then
info 'No change detected so skipping service start'
return
fi
systemd_start
return 0
}
{
verify_system
download
create_uninstall
systemd_disable
create_env_file
create_service_file
service_enable_and_start
}