This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kat
executable file
·404 lines (333 loc) · 10.5 KB
/
kat
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env bash
set -euo pipefail
## About
#
# A shim for starting the Katamari server, and sending it requests
## Helpers
function _get_conf() {
# $1 f
# $2 k
# $3 default
if [ -f "${1}" ]; then
v=$(awk -F = "/$2/ {print \$2; exit}" "$1")
if [ -z "$v" ]; then v=$3; fi
echo "$v" | tr -d " \t"
else
echo "$3"
fi
}
## Configuration
export KAT="${0}"
# Find java executable
if [ -z "${JAVA_CMD+x}" ]; then
set +e
export JAVA_CMD=$(type -p java)
set -e
if [[ ! -n "$JAVA_CMD" ]]; then
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
export JAVA_CMD="$JAVA_HOME/bin/java"
else
>&2 echo "Couldn't find 'java'. Please set JAVA_HOME or JAVA_CMD."
exit 1
fi
fi
fi
# The kat install location
if [ -z "${KAT_BIN_ROOT+x}" ] ; then
export KAT_BIN_ROOT=$(dirname $(realpath "${0}"))
fi
# The kat repo root
if [ -z "${KAT_REPO_ROOT+x}" ]; then
# FIXME (reid.mckenzie 2018-10-16):
# Decouple Kat from git's root?
export KAT_REPO_ROOT=$(realpath $(git rev-parse --show-toplevel))
fi
if [ -z "${KAT_CONFIG+x}" ]; then
KAT_CONFIG="${KAT_REPO_ROOT}/kat.conf"
fi
# Where do the cache, lockfiles and other state live
if [ -z "${KAT_SERVER_WORK_DIR+x}" ]; then
KAT_SERVER_WORK_DIR="${KAT_REPO_ROOT}/"$(_get_conf "${KAT_CONFIG}" server_work_dir .kat.d)
fi
## Bootstrapping
if [ ! -d "${KAT_SERVER_WORK_DIR}" ]; then
mkdir "${KAT_SERVER_WORK_DIR}"
fi
bootstrap_jar="${KAT_SERVER_WORK_DIR}/bootstrap.jar"
if [ ! -f "${bootstrap_jar}" ]; then
curl -L https://github.com/arrdem/katamari/releases/download/v0.0.5/katamari-0.0.5.jar \
--output "${bootstrap_jar}"
fi
if [ ! -f "${KAT_CONFIG}" ]; then
cat <<EOF > "${KAT_CONFIG}"
# Katamari's config file
#### Server options
server_http_port=3636
server_nrepl_port=3637
server_addr=localhost
# How long to wait before declaring the server a failure to start
server_start_sec=15
# the namespace to boot
server_ns=katamari.server.web-server
# A classpath string to use when booting the server
# Used when bootstrapping Kat
#
# FIXME (arrdem 2018-09-29):
# How do I get away from having to code this? Bootstrapping without a dist is HARD
server_classpath=$(realpath "${bootstrap_jar}")
# Where to put cached build products and analysis data
# This cache lives at the repo root
server_work_dir=.kat.d
# The log to record build history and any errors, lives under server_work_dir
server_log_file=kat.log
# server build cache, lives under server_work_dir
server_build_cache=buildcache
# 30 day product TTL
server_build_cache_ttl=2592000000
# (class)paths to (load) after application boot but before the server(s) start
server_extensions=[
clojure.tools.deps.alpha.extensions.maven
clojure.tools.deps.alpha.extensions.local
clojure.tools.deps.alpha.extensions.git
clojure.tools.deps.alpha.extensions.deps
clojure.tools.deps.alpha.extensions.pom
roll.extensions.defaults
roll.extensions.jvm
roll.extensions.clj
roll.extensions.jar
katamari.server.extensions.cheshire
katamari.server.extensions.core-handlers
katamari.server.extensions.fuzzy-not-found
katamari.server.extensions.roll-handlers
katamari.server.extensions.repl-handler
]
### tools.deps
# default config file(s)
deps_defaults_file=kat-deps-defaults.edn
deps_resolve_file=kat-deps-resolve.edn
# optional EDN to bolt onto the value of the defaults
deps_defaults_data={}
#### Compilation options
target_dir=target
EOF
fi
if [ ! -f kat-deps-defaults.edn ]; then
cat <<EOF > kat-deps-defaults.edn
;; An implicit defaults file which is passed to deps.edn as more deps data.
;;
;; This file should be used to configure any Maven or other repositories, to provide global
;; dependencies, aliases, or anything else you may choose to inject globally.
;;
;; WARNING: As paths are only considered when profiles are being applied in the context of a
;; particular working directory or target set, `:paths` as listed here will only provide a default
;; `:paths` list to be considered when expanding targets. Probably not worth messing with.
{:mvn/repos
{
"central" {:url "https://repo1.maven.org/maven2/"}
"clojars" {:url "https://repo.clojars.org/"}
}
}
EOF
fi
if [ ! -f kat-deps-resolve.edn ]; then
cat <<EOF > kat-deps-resolve.edn
;; Deps data which getsapplied as an always-on resolve alias.
;;
;; This file should be used for version pinning and overrides where required.
{:default-deps
{org.clojure/clojure {:mvn/version "1.9.0"}
}
}
EOF
fi
if [ ! -d target ]; then
mkdir target
fi
# Katamari's server port
if [ -z "${KAT_SERVER_HTTP_PORT+x}" ]; then
KAT_SERVER_HTTP_PORT=$(_get_conf "${KAT_CONFIG}" server_http_port 3636)
fi
# Katamari's server port
if [ -z "${KAT_SERVER_ADDR+x}" ]; then
KAT_SERVER_ADDR=$(_get_conf "${KAT_CONFIG}" server_addr localhost)
fi
# Katamari's classpath
if [ -z "${KAT_SERVER_CP+x}" ]; then
KAT_SERVER_CP=$(_get_conf "${KAT_CONFIG}" server_classpath)
# FIXME (arrdem 2018-09-26):
# How to get away from setting this? Can it be made stand-alone?
if [ -z "${KAT_SERVER_CP+x}" ]; then
>&2 echo "Couldn't find kat's server classpath. Please set 'server_classpath'."
exit 1
fi
fi
# How long to wait for the server to come up
if [ -z "${KAT_SERVER_START_SEC+x}" ]; then
KAT_SERVER_START_SEC=$(_get_conf "${KAT_CONFIG}" server_start_sec 15)
fi
# What namespace to boot
if [ -z "${KAT_SERVER_NS+x}" ]; then
KAT_SERVER_NS=$(_get_conf "${KAT_CONFIG}" server_ns 'katamari.server.web-server')
fi
# Where do the logs go
if [ -z "${KAT_SERVER_LOG_FILE+x}" ]; then
KAT_SERVER_LOG_FILE=$(_get_conf "${KAT_CONFIG}" server_log_file kat.log)
fi
## Booting the server
# Ping the server to see if it's up yet
function _ping_server() {
curl -s --connect-timeout 1\
-H "Accept: application/json;" \
"http://${KAT_SERVER_ADDR}:${KAT_SERVER_HTTP_PORT}/api/v0/ping" \
2>&1 > /dev/null
}
# Start the server and wait for it to come back
function _start_server() {
# FIXME (arrdem 2018-09-29):
# Should be possible to time how long it takes the server to become available.
echo "Starting server ..."
# Throw a header in the logs
cat <<EOF >> "${KAT_SERVER_WORK_DIR}/${KAT_SERVER_LOG_FILE}"
--------------------------------------------------------------------------------
$(date)] SERVER RESTARTING
--------------------------------------------------------------------------------
EOF
CWD=${KAT_SERVER_WORK_DIR} "${JAVA_CMD}" -cp "${KAT_SERVER_CP}" \
clojure.main -m "${KAT_SERVER_NS}" "${KAT_CONFIG}" \
>> "${KAT_SERVER_WORK_DIR}/${KAT_SERVER_LOG_FILE}" &
KAT_SERVER_PID="$!"
disown "${KAT_SERVER_PID}"
i=1
sp="/-\|"
echo -n "Waiting for it to become responsive "
while true; do
if ! kill -n 0 "${KAT_SERVER_PID}"; then
>&2 echo "Katamari server failed to start!"
exit 1
fi
if _ping_server; then
break;
fi
# Rotate the spinner
printf "\b${sp:i++%${#sp}:1}"
sleep 0.25
done
# Clear the spinner
printf "\b\n"
}
# Send $@ and the repo config to the server as a request
function _send_request() {
# FIXME (arrdem 2018-09-26):
# This doesn't / can't forward stdin or otherwise achieve bidirectional coms with the server.
#
# Would need to put together a much smarter and presumably python client to pull that off, not
# that it isn't on the table. This is a pretty trivial prototype.
# Slurp $@ into a JSON array so as to preserve whatever shell string wackiness
if [ "$#" -gt 0 ]; then
args=( "$@" )
args=$(printf '%s\0' "${args[@]}" | jq -csR 'split("\u0000")')
else
args="[]"
fi
headers_file=`mktemp`
out_file=`mktemp`
# Send the request
# FIXME (arrdem 2018-09-29): should be able to debug this request out to the user
curl -s \
-H "Content-type: application/json;" \
-H "Accept: application/json;" \
-D "${headers_file}" \
-o "${out_file}" \
--data "@-" \
-XGET "http://${KAT_SERVER_ADDR}:${KAT_SERVER_HTTP_PORT}/api/v0/request" <<EOF
{"repo_root":"${KAT_REPO_ROOT}",
"user_root":"${HOME}",
"config_file":"${KAT_CONFIG}",
"cwd":"${PWD}",
"request":$args}
EOF
status=$?
while true; do
case "${KAT_INTENT:-response}" in
raw)
cat "${out_file}"
break
;;
json)
jq -M . < "${out_file}"
break
;;
msg|message)
jq -r .msg < "${out_file}"
break
;;
sh|subshell)
if [ "${KAT_VERBOSE}" = true ]; then
echo "Attempting to subshell..." >&2
cat "${out_file}" >&2
echo
fi
jq -r .sh < "${out_file}" | bash
break
;;
exec)
if [ "${KAT_VERBOSE}" = true ]; then
echo "Attempting to exec..." >&2
cat "${out_file}" >&2
echo
fi
cmd=$(jq -r .exec < "${out_file}")
eval "exec $cmd"
break
;;
response)
KAT_INTENT=$(jq -r .intent < "${out_file}")
continue
;;
*)
cat <<EOF >&2
Error: Unknown intent '${KAT_INTENT}'
While handling response:
EOF
cat "${out_file}" >&2
rm "${out_file}" "${headers_file}"
return 1
;;
esac
done
rm "${out_file}" "${headers_file}"
return "${status}"
}
## Core behavior
if [ -z "${KAT_VERBOSE+x}" ]; then
KAT_VERBOSE=false
fi
while true; do
[ -z "${1+x}" ] && break
case "${1}" in
-r|--raw)
KAT_INTENT=raw
shift
;;
-j|--json)
KAT_INTENT=json
shift
;;
-m|--message)
KAT_INTENT=message
shift
;;
-v|--verbose)
KAT_VERBOSE=true
shift
;;
*)
break
;;
esac
done
# Try to start the server if one isn't active.
# Note that _start_server hard exits if the server fails to boot, so the script will die there
_ping_server || _start_server
# Having either pinnged or started the server, chuck the request over the wall
_send_request "$@"