-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·245 lines (219 loc) · 7.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
#!/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
service_binary="systemctl"
service_directory='/etc/systemd/system/'
configuration_directory='/usr/share/kss/'
log_directory=''
service_file='kss.service'
elif [[ -n $(command -v launchctl || echo '') ]]; then
service_binary="launchctl"
service_directory='/Library/LaunchDaemons/'
configuration_directory='/Library/Application Support/kss/'
log_directory='/Library/Logs/'
service_file='kss.plist'
else
service_binary=''
service_directory=''
configuration_directory=''
log_directory=''
service_file='kss.service'
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[/usr/bin/] directory for binary files'
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[www-data] 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=''
execution_directory='/usr/bin/'
service_user='www-data'
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"
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
;;
-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 [ ! -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=''
else
if [ ! -d "${configuration_directory}" ]; then
echo "'--configuration-directory' (${configuration_directory}) not found. Try to create it" >&2
mkdir -p "${configuration_directory}"
fi
if [ ! -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 [ ! -w "${log_directory}" ]; then
echo "Current user has no writer permissions for '${log_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')
echo 'test repository access' >&2
auth_test_response_code="$(curl "${github_args[@]}" --header 'Accept: application/vnd.github+json' --head --silent \
--write-out '%{response_code}\n' --output '/dev/null' \
--url 'https://api.github.com/repos/EdwarDDay/kotlin-server-scripts')"
if [ "${auth_test_response_code}" != "200" ]; then
usage "You don't have access to the repository without a GitHub access token."
fi
echo 'download latest release data' >&2
query="{\"query\": \"query { repository(owner: \\\"EdwarDDay\\\", name: \\\"kotlin-server-scripts\\\") { latestRelease { releaseAssets(name: \\\"scripting-host-release.tar.gz\\\", first: 1) { nodes { url } } } } }\" }"
response="$(curl --request POST "${github_args[@]}" --fail --silent --url 'https://api.github.com/graphql' --data "$query")"
response_tmp="${response%\"*}"
url="${response_tmp##*\"}"
echo 'download binary' >&2
archive_name='kss.tar.gz'
curl --progress-bar --url "${url}" --output "${archive_name}" --fail
echo 'extract binary' >&2
tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${execution_directory}" 'scripting-host-release/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 "scripting-host-release/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)
echo 'The launch daemon is enabled. Run'
echo ''
echo "launchctl load $service_directory$service_file"
echo ''
echo 'to start the daemon'
;;
esac
tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${configuration_directory}" 'scripting-host-release/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}"