-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·377 lines (349 loc) · 12.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/bash
#
# Copyright 2024 Eduard Wolf
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -eu
set -o pipefail
if [[ -n $(command -v systemctl || echo '') ]]; then
echo 'use systemctl default values' >&2
service_binary='systemctl'
execution_directory='/usr/bin/'
service_directory='/etc/systemd/system/'
configuration_directory='/usr/share/kss/'
log_directory=''
service_file='kss.service'
service_user='www-data'
elif [[ -n $(command -v launchctl || echo '') ]]; then
echo 'use launchctl default values' >&2
service_binary='launchctl'
execution_directory='/usr/local/bin/'
service_directory='/Library/LaunchDaemons/'
configuration_directory='/Library/Application Support/kss/'
log_directory='/Library/Logs/'
service_file='kss.plist'
service_user='_www'
else
echo 'use empty default values' >&2
service_binary=''
execution_directory='/usr/bin/'
service_directory=''
configuration_directory=''
log_directory=''
service_file='kss.service'
service_user=''
fi
if [[ -n $(command -v gh || echo '') ]]; then
release_fetch_mode="gh"
else
release_fetch_mode="unknown"
fi
PROGRAM_NAME=$(basename "$0")
function usageText {
echo 'Usage: '"$PROGRAM_NAME"' [OPTIONS]'
echo 'install latest kss release'
echo ''
echo 'Options:'
echo '-h/--help prints this usage'
echo '-t/--token github token to use to download executables'
echo "-d/--directory[$execution_directory]"
echo ' directory for binary files'
echo '-r/--release-fetch-mode Options:'
echo ' gh - use authenticated Github commandline tool'
echo ' curl-authenticated - use curl authenticated with the token from the --token option'
echo ' curl - use curl without token - needs jq to parse rest response'
echo ' archive=<archive> - use tar archive as release'
echo '-v/--release-version Specify explicit release version (uses latest version by default)'
echo "-s/--service-directory[$service_directory]"
echo ' directory for service files'
echo "-c/--configuration-directory[$configuration_directory]"
echo ' directory for configuration files'
echo "-l/--log-directory[$log_directory]"
echo ' directory for log files (only used in MacOS)'
echo "-u/--user[$service_user] user which runs the service process"
}
function usage {
if [ -z "${1+''}" ]
then
usageText
exit 0
else
echo "$1" >&2
usageText >&2
exit 1
fi
}
authorization_token=''
release_version=''
release_fetch_mode_set='false'
while [[ $# -gt 0 ]]; do
key="$1"
if [ -z "${2+''}" ]; then
value=''
else
value="$2"
fi
case $key in
-h|--help)
usage
;;
-t|--token)
if [ "$value" ]; then
authorization_token="$value"
if [ "${release_fetch_mode_set}" == 'false' ] && [ "${release_fetch_mode}" != 'gh' ]; then
release_fetch_mode='curl-authenticated'
fi
shift
else
usage 'the --token option needs an argument'
fi
;;
-d|--directory)
if [ "$value" ]; then
execution_directory="${value%/}/"
shift
else
usage 'the --directory option needs an argument'
fi
;;
-v|--release-version)
if [ "$value" ]; then
release_version="$value"
shift
else
usage 'the --release-version option needs an argument'
fi
;;
-r|--release-fetch-mode)
release_fetch_mode_set='true'
case $value in
gh)
if [[ -n $(command -v gh || echo '') ]]; then
release_fetch_mode="gh"
else
usage '--release-fetch-mode set to gh but can'\''t find gh command'
fi
;;
curl-authenticated)
release_fetch_mode="curl-authenticated"
;;
curl)
if [[ -n $(command -v jq || echo '') ]]; then
release_fetch_mode="curl"
else
usage '--release-fetch-mode set to curl but can'\''t find jq command, which is needed to parse the REST API'
fi
;;
archive=*)
release_fetch_mode="archive"
archive_file="${value:8}"
if [ ! -f "$archive_file" ]; then
usage "couldn't find archive file '$archive_file'"
fi
;;
'')
usage 'the --release-fetch-mode option needs an argument'
;;
*)
usage "unknown option ${value} for option --release-fetch-mode - please specify gh, curl-authenticated or curl"
;;
esac
shift
;;
-s|--service-directory)
if [ "$value" ]; then
service_directory="${value%/}/"
shift
else
usage 'the --service-directory option needs an argument'
fi
;;
-c|--configuration-directory)
if [ "$value" ]; then
configuration_directory="${value%/}/"
shift
else
usage 'the --configuration-directory option needs an argument'
fi
;;
-l|--log-directory)
if [ "$value" ]; then
log_directory="${value%/}/"
shift
else
usage 'the --log-directory option needs an argument'
fi
;;
-u|--user)
if [ "$value" ]; then
service_user="${value}"
shift
else
usage 'the --user option needs an argument'
fi
;;
*)
usage "unknown option ${key}"
;;
esac
shift
done
if [ "${release_fetch_mode}" == 'unknown' ] && [[ -n $(command -v jq || echo '') ]]; then
release_fetch_mode='curl'
fi
if [ "${release_fetch_mode}" == 'curl-authenticated' ] && [ "${authorization_token}" == '' ]; then
usage "'--release-fetch-mode' with option 'curl-authenticated' needs also '--token' to be specified"
fi
if [ "${release_fetch_mode}" == 'archive' ] && [ "${release_version}" != '' ]; then
usage "'--release-fetch-mode' with option 'archive' doesn't support a specific release version (--release-version option)"
fi
if [ "${release_fetch_mode}" == 'unknown' ]; then
usage "please specify '--release-fetch-mode' as no default option was found"
fi
if [ "${release_fetch_mode}" == 'gh' ]; then
if ! gh auth status >/dev/null && [ -z "${GH_TOKEN:-}" ]; then
usage "gh commandline tool is not setup. Please login via 'gh auth login', specify 'GH_TOKEN' or use '--release-fetch-mode curl' or '--release-fetch-mode curl-authenticated --token <GH PAT token>'"
fi
fi
if [ ! -d "${execution_directory}" ]; then
usage "'--directory' is set to '${execution_directory}' which is no existing directory. Please specify an existing directory."
elif [ ! -w "${execution_directory}" ]; then
usage "Current user has no writer permissions for '${execution_directory}'. Please execute with a different user."
fi
if [ ! -d "${service_directory}" ]; then
echo "'--service-directory' is set to '${service_directory}' which is no existing directory. Service won't be installed" >&2
service_directory=''
elif [ ! -w "${service_directory}" ]; then
echo "Current user has no writer permissions for '${service_directory}'. Service won't be installed" >&2
service_directory=''
elif [ -n "${service_binary}" ]; then
if [ ! -d "${configuration_directory}" ]; then
echo "'--configuration-directory' (${configuration_directory}) not found. Try to create it" >&2
mkdir -p "${configuration_directory}"
fi
if [ -n "${log_directory}" ] && [ ! -d "${log_directory}" ]; then
echo "'--log-directory' (${log_directory}) not found. Try to create it" >&2
mkdir -p "${log_directory}"
fi
if [ ! -w "${configuration_directory}" ]; then
echo "Current user has no writer permissions for '${configuration_directory}'. Service won't be installed" >&2
service_directory=''
elif ! id "$service_user" >/dev/null 2>&1; then
echo "User '$service_user' does not exist. Service won't be installed" >&2
service_directory=''
fi
fi
authorization_args=()
if [ "$authorization_token" ]; then
authorization_args=(--header "Authorization: Bearer ${authorization_token}")
fi
github_args=(--location ${authorization_args[@]+"${authorization_args[@]}"} --header 'X-GitHub-Api-Version: 2022-11-28')
if [ -z "$release_version" ]; then
release_rest_path='latest'
# shellcheck disable=SC2016
query='query ($user: String!, $repo: String!) { repository(owner: $user, name: $repo) { latestRelease { releaseAssets(first: 10) { nodes { contentType url } } } } }'
else
release_rest_path="tags/$release_version"
# shellcheck disable=SC2016
query='query ($user: String!, $repo: String!, $release: String!) { repository(owner: $user, name: $repo) { release(tagName: $release) { releaseAssets(first: 10) { nodes { contentType url } } } } }'
fi
function extractUrlFromGraphqlQuery() {
local response=$1
if [[ -n $(command -v jq || echo '') ]]; then
if [ -z "$release_version" ]; then
jq --raw-output '.data.repository.latestRelease.releaseAssets.nodes | map(select(.contentType == "application/gzip"))[0].url' <<< "$response"
else
jq --raw-output '.data.repository.release.releaseAssets.nodes | map(select(.contentType == "application/gzip"))[0].url' <<< "$response"
fi
else
local response_tmp="${response#*\"contentType\":\"application\/gzip\",\"url\":\"}"
echo "${response_tmp%%\"*}"
fi
}
function downloadBinary() {
echo 'download binary' >&2
curl --progress-bar --header 'Accept: application/octet-stream' --url "${url}" --output "${archive_name}" --fail
}
archive_name='kss.tar.gz'
case "$release_fetch_mode" in
archive)
echo "use release archive from file $archive_file" >&2
cp "$archive_file" "$archive_name"
;;
gh)
echo 'download latest release data via gh commandline tool' >&2
response="$(gh api graphql -F 'user=EdwarDDay' -F 'repo=kotlin-server-scripts' -F "release=$release_version" -f "query=$query")"
url="$(extractUrlFromGraphqlQuery "$response")"
downloadBinary
;;
curl-authenticated)
echo 'download latest release data via curl and graphql api' >&2
data="{
\"query\":\"$query\",
\"variables\":{
\"user\":\"EdwarDDay\",
\"repo\":\"kotlin-server-scripts\",
\"release\":\"$release_version\"
}
}"
response="$(curl --request POST "${github_args[@]}" --fail --silent --url 'https://api.github.com/graphql' --data "$data")"
url="$(extractUrlFromGraphqlQuery "$response")"
downloadBinary
;;
# curl
*)
echo 'download latest release data via github rest endpoint and jq' >&2
url="$(curl --request GET "${github_args[@]}" --fail --silent --url "https://api.github.com/repos/EdwarDDay/kotlin-server-scripts/releases/$release_rest_path" | jq --raw-output '.assets | map(select(.content_type == "application/gzip"))[0].url')"
downloadBinary
esac
echo 'extract binary' >&2
archiveInternalName="$(tar --list --file "${archive_name}" --exclude="*/?*")"
tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${execution_directory}" "${archiveInternalName}bin/"
if [[ -n "${service_directory}" ]]; then
echo 'configure service' >&2
# escape sed escape char
service_user="${service_user/|/\\\|}"
tar --extract --gunzip --file "${archive_name}" --to-stdout "${archiveInternalName}service/$service_file" |\
sed "s|{{DIRECTORY}}|${execution_directory}|g" | \
sed "s|{{WORKING_DIRECTORY}}|${configuration_directory}|g" | \
sed "s|{{LOG_DIRECTORY}}|${log_directory}|g" | \
sed "s|{{USER}}|${service_user}|g" \
> "$service_directory$service_file"
case $service_binary in
systemctl)
systemctl enable kss
echo 'The system service is enabled. Run'
echo ''
echo 'systemctl start kss'
echo ''
echo 'to start the service'
;;
launchctl)
sudo touch "${log_directory}kss.log"
sudo touch "${log_directory}kss-error.log"
sudo chown "$service_user" "${log_directory}kss.log" "${log_directory}kss-error.log"
sudo chmod 600 "$service_directory$service_file"
echo 'The launch daemon is enabled. Run'
echo ''
echo "launchctl load -w $service_directory$service_file"
echo ''
echo 'to start the daemon'
;;
esac
tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${configuration_directory}" "${archiveInternalName}config/"
echo "A sample configuration is in '${configuration_directory}'. Remove the '.sample' extension and edit it as you see fit."
echo "A sample logging file is in '${configuration_directory}'. Remove the '.sample' extension, reference it in the configuration and edit it as you see fit."
fi
echo 'delete archive' >&2
rm "${archive_name}"