-
Notifications
You must be signed in to change notification settings - Fork 0
/
oslc_master
executable file
·247 lines (212 loc) · 6.33 KB
/
oslc_master
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
#!/bin/bash
##########################
# Copyright (C) 2020 Ondřej Vašíček <[email protected]>, <[email protected]>
#
# Author: Aleš Smrčka <[email protected]>
# Updates: Ondřej Vašíček <[email protected]>(keeping up to date)
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
##########################
# root dir of this script
ROOTDIR=$(dirname $(realpath $0))
# dir path for logs
LOGDIR=$ROOTDIR/logs
# filepath of PID file (containing list of pids)
PIDFILE=$LOGDIR/pid
# number of logs to prevail (last + current)
LOG_HISTORY=4
MASTER_LOG=master.log
TRIPLESTORE_INI=$ROOTDIR/sparql_triplestore/start.ini
COMPILATION_INI=$ROOTDIR/compilation/conf/VeriFitCompilation.properties
ANALYSIS_INI=$ROOTDIR/analysis/conf/VeriFitAnalysis.properties
# duration for polling (via curl)
SLEEP=3
# adapter version
VERSION=$(cat $ROOTDIR/VERSION.md 2>/dev/null)
# source shared utils
source $ROOTDIR/dev_tools/shared.sh
# $1... = (optional) message
usage()
{
if [ "$#" -gt 0 ]; then
echo "$*"
fi
cat <<EOF
usage: $0 (start|stop|status|log)
start:
Launches the sparql triplestore, and then the analysis adapter and the
compilation adapter. The triplestore needs to finish startup before
both adapters, which is controled by regular polling the triplestore's
tcp port.
Logs are stored in: $LOGDIR
PIDs of running services are stored in: $PIDFILE
EOF
}
# $1 = (optional) -v = be verbose
# $2... = log message
log() {
if [ "x$1" = x-v ]; then
shift
echo "$@"
fi
echo "[$(date +%F_%T)] $@" >>$LOGDIR/$MASTER_LOG
}
stop()
{
log -v "Shutting down"
pids=$(cat $PIDFILE 2>/dev/null)
log -v Killing $pids
kill $pids &>/dev/null
sleep 2
rm $PIDFILE 2>/dev/null
log -v "Stop"
}
status()
{
log "Checking for status"
if [ -s $PIDFILE ]; then
for pid in $(cat $PIDFILE); do
if cmd=$(ps -p $pid -o comm=); then
log -v "Process $pid ($cmd) running"
else
log -v "Process $pid not running"
fi
done
else
echo "stopped"
fi
}
# Gets value of a key from INI file
# $1 = .ini filename
# $2 = key
get_inivalue()
{
cat "$1" | grep "^ *$2" | cut -d= -f2
}
# Rotates (renames) old logs, last log will be deleted.
# name.log => name.0.log; name.0.log => name.1.log; name.1.log => name.2.log ...
# $1 = log dir
# $2 = logname without .log
rotate_logs()
{
local last=$((LOG_HISTORY-2))
[ $last -lt 0 ] && return
local above below
for above in $(seq $last -1 1); do
below=$((above-1))
[ -f "$1/$2.$below.log" ] || continue
mv "$1/$2.$below.log" "$1/$2.$above.log"
done
if [ -f "$1/$2.log" ]; then
mv "$1/$2.log" "$1/$2.0.log"
fi
}
# Rotates logs and prepare a new (current) one (in LOGDIR)
# $2 = logname without .log
mklog()
{
mkdir -p $LOGDIR || exit 1
rotate_logs "$LOGDIR" "$1"
(
echo "########################################################"
echo " Running version: $VERSION"
echo " Started at: $(date +%F_%T)"
echo "########################################################"
) >"$LOGDIR/$1.log"
}
# Polls an address using curl until the request returns zero (i.e. the address responds)
# $1 = web address (for curl)
curl_poll()
{
while ! curl $1 &>/dev/null; do
sleep $SLEEP
echo -n "."
done
echo active
}
start()
{
trap 'stop; exit 0' INT TERM HUP
# make the LOGDIR
mkdir -p $LOGDIR || exit 1
# output version
log -v
log -v "########################################################"
log -v " OSLC Universal Analysis, $VERSION"
log -v "########################################################"
log -v
# load configuration files
processConfFiles "$ROOTDIR" # TODO conf output is not logged (issues with new lines)
echo
# lookup config -- TODO replace with: triplestore_url=$(lookupTriplestoreURL "$ROOTDIR") ? etc
t_host=$(get_inivalue $TRIPLESTORE_INI jetty.http.host | sed 's|/$||')
t_port=$(get_inivalue $TRIPLESTORE_INI jetty.http.port)
triplestore_url="$t_host:$t_port/fuseki/"
c_host=$(get_inivalue $COMPILATION_INI adapter_host | sed 's|/$||')
c_port=$(get_inivalue $COMPILATION_INI adapter_port)
compilation_url="$c_host:$c_port/compilation"
a_host=$(get_inivalue $ANALYSIS_INI adapter_host | sed 's|/$||')
a_port=$(get_inivalue $ANALYSIS_INI adapter_port)
analysis_url="$a_host:$a_port/analysis/"
mklog triplestore
mklog compilation
mklog analysis
true >$PIDFILE
log -v "Starting the Triplestore"
cd sparql_triplestore
./run.sh &>>"$LOGDIR/triplestore.log" &
echo $! >>$PIDFILE
disown
log -v "Waiting for the Triplestore to finish startup"
curl_poll "$triplestore_url"
log -v "Triplestore running"
cd ..
# start the compilation adapter
log -v "Starting the Compilation adapter"
cd compilation
mvn jetty:run-exploded &>>"$LOGDIR/compilation.log" &
echo $! >>$PIDFILE
disown
log -v "Waiting for the Compilation adapter to finish startup"
curl_poll "$compilation_url"
log -v "Compilation adapter running"
# start the analysis adapter
log -v "Starting the Analysis adapter"
cd ../analysis
mvn jetty:run-exploded &>>"$LOGDIR/analysis.log" &
echo $! >>$PIDFILE
disown
log -v "Waiting for the Analysis adapter to finish startup"
curl_poll "$analysis_url"
log -v "Analysis adapter running"
}
monitor_log()
{
cd $LOGDIR
trap 'kill $(jobs -p) 2>/dev/null; exit 0' INT TERM HUP EXIT
tail -f triplestore.log 2>/dev/null | sed 's/^/[triplestore] /' &
tail -f compilation.log 2>/dev/null | sed 's/^/[compilation] /' &
tail -f analysis.log 2>/dev/null | sed 's/^/[analysis] /' &
tail -f $MASTER_LOG 2>/dev/null | sed 's/^/[master] /'
exit 0
}
####################################################3
# Main
# process arguments
if [ $# -eq 0 ]; then
usage "error: Missing arguments"
exit 1
fi
cd $ROOTDIR
case "$1" in
start) start;;
stop) stop;;
status) status;;
log) monitor_log;;
*) usage "error: unknown argument(s): $*"
exit 1;;
esac