-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_all.sh
executable file
·245 lines (214 loc) · 9.99 KB
/
run_all.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 (C) 2020 Ondřej Vašíček <[email protected]>, <[email protected]>
#
# 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
##########################
HELP="
Launches the sparql triplestore, the analysis adapter and
the compilation adapter. The triplestore needs to finish
startup before both adapters, which is controled by polling
the triplestore using curl until it responds.
Also reloads all configuration files.
"
USAGE=" Usage: $0 [-t|-h|-b]
-t ... tail - Opens tail -f for each output log in new gnome-terminals.
-b ... build - Runs the build script first.
-h ... help
"
# duration for polling (via curl)
SLEEP=3
USRPATH="$PWD" # get the call directory
ROOTDIR="$(dirname "$(realpath "$0")")" # get the script directory
# source shared utils
source "$ROOTDIR/dev_tools/shared.sh"
# variables used to control which log tailing terminals
# can be killed during a failed startup
# (i.e. their corresponding component started succesfully)
unset KILL_TRIPLESTORE_TAIL KILL_COMPILATION_TAIL KILL_ANALYSIS_TAIL
killTailTerminals(){
if [ -n "$KILL_ANALYSIS_TAIL" ] || [ $1 -eq 1 ]; then
kill "$(ps -ef | grep "tail -f .*/logs/../logs/../logs/analysis_" | head -1 | awk '{ print $2 }')" &> /dev/null # funny path /logs/../logs/../logs/ to avoid killing unwanted tail commands
fi
if [ -n "$KILL_COMPILATION_TAIL" ] || [ $1 -eq 1 ]; then
kill "$(ps -ef | grep "tail -f .*/logs/../logs/../logs/compilation_" | head -1 | awk '{ print $2 }')" &> /dev/null # TODO use PIDs instead ($!)
fi
if [ -n "$KILL_TRIPLESTORE_TAIL" ] || [ $1 -eq 1 ]; then
kill "$(ps -ef | grep "tail -f .*/logs/../logs/../logs/triplestore_" | head -1 | awk '{ print $2 }')" &> /dev/null
fi
}
# catch ctrl+c and kill all subprocesses
trap 'killall' INT
killall() {
trap '' INT TERM # ignore INT and TERM while shutting down
echo -e "\nShutting down..."
kill -TERM 0
if [ -n "$ARG_TAIL" ]; then
killTailTerminals 1
fi
wait
echo "All done."
exit 0
}
# used by the script in case of failure
abortAll() {
trap '' INT TERM # ignore INT and TERM while shutting down
echo -e "\nShutting down..."
kill -TERM 0
if [ -n "$ARG_TAIL" ]; then
killTailTerminals 0
fi
wait
echo "All done."
exit 0
}
main () {
# process arguments
unset ARG_TAIL ARG_BUILD
for arg in "$@"
do
case "$arg" in
-t) ARG_TAIL=true ; shift ;;
-b) ARG_BUILD=true ; shift ;;
-h) print_help "$HELP" "$USAGE"; shift ;;
*) invalid_arg "$arg" "$USAGE" ;;
esac
done
# firt check that the required utilities are available
if ! type "curl" &> /dev/null; then
echo -e "\nERORR: The '${RED}curl${NC}' utility is required by Unite but is ${RED}not available${NC}.\n"
exit "$?"
fi
if ! type "mvn" &> /dev/null; then
echo -e "\nERORR: The '${RED}mvn${NC}' utility is required by Unite but is ${RED}not available${NC}.\n"
exit "$?"
fi
# build Unite first if requested by args
if [ -n "$ARG_BUILD" ]; then
echo -e "\nRunning build.sh first"
"$ROOTDIR/build.sh"
if [ "$?" -ne 0 ]; then
echo -e "\nBuild failed. Aborting start.\n"
exit "$?"
fi
else # just update conf
processConfFiles "$ROOTDIR"
fi
# get and output version
VERSION="$(cat "$ROOTDIR/VERSION.md" 2>/dev/null)"
echo -e "\n########################################################"
echo -e " ${BOLD}Unite${NORMAL} ${VERSION}"
echo -e "########################################################\n"
# lookup config URLs
triplestore_url="$(lookupTriplestoreURL "$ROOTDIR")"
compilation_url="$(lookupCompilationURL "$ROOTDIR")"
analysis_url="$(lookupAnalysisURL "$ROOTDIR")"
# create log files and append headings
mkdir "$ROOTDIR/logs" &> /dev/null
CURTIME="$(date +%F_%T)"
echo -e "########################################################\n Running version: $VERSION\n Started at: $CURTIME\n########################################################\n" > "$ROOTDIR/logs/triplestore_$CURTIME.log"
echo -e "########################################################\n Running version: $VERSION\n Started at: $CURTIME\n########################################################\n" > "$ROOTDIR/logs/compilation_$CURTIME.log"
echo -e "########################################################\n Running version: $VERSION\n Started at: $CURTIME\n########################################################\n" > "$ROOTDIR/logs/analysis_$CURTIME.log"
# rotate log files (delete all but last 5)
cd "$ROOTDIR/logs"
for log in `ls | grep triplestore_.*\.log | head -n -5` ; do rm $log ; done
for log in `ls | grep compilation_.*\.log | head -n -5` ; do rm $log ; done
for log in `ls | grep analysis_.*\.log | head -n -5` ; do rm $log ; done
cd - &> /dev/null
############################################################################################################
# Triplestore
############################################################################################################
# open new terminal that tails the log file and record its PID to kill later
if [ -n "$ARG_TAIL" ]; then
gnome-terminal --title="tail: Triplestore Log (feel free to close this window)" -- /bin/bash -c "tail -f \"$ROOTDIR/logs/../logs/../logs/triplestore_$CURTIME.log\"" # funny path /logs/../logs/../logs/ to avoid killing unwanted tail commands
fi
# start the triplestore
echo -n "Starting the Triplestore"
"$ROOTDIR/sparql_triplestore/run.sh" &> "$ROOTDIR/logs/triplestore_$CURTIME.log" &
PROCESS_PID=$!
echo " (PID: $PROCESS_PID)"
echo -e "Waiting for the Triplestore to finish startup"
waitForUrlOnline "$triplestore_url" "$PROCESS_PID" "$SLEEP" 0
ret="$?"
if [ "$ret" -eq 0 ]; then
echo -e "Triplestore ${GREEN}running${NC} (1/3)\n at $triplestore_url\n"
KILL_TRIPLESTORE_TAIL=true
else
echo -e "Triplestore ${RED}failed${NC} to start!"
if [ -n "$ARG_TAIL" ]; then
echo -e "See the \"tail: Triplestore Log\" terminal window for the startup log."
echo -e "Or try checking the logs: \"$ROOTDIR/logs/triplestore_$CURTIME.log\""
else
echo -e "Try checking the logs: \"$ROOTDIR/logs/triplestore_$CURTIME.log\""
echo -e "Or run again with '-t' to see the logs during startup."
fi
abortAll # exit
fi
############################################################################################################
# Compilation
############################################################################################################
# open new terminal that tails the log file and record its PID to kill later
if [ -n "$ARG_TAIL" ]; then
gnome-terminal --title="tail: Compilation Log (feel free to close this window)" -- /bin/bash -c "tail -f \"$ROOTDIR/logs/../logs/../logs/compilation_$CURTIME.log\"" # TODO use PIDs instead ($!)
fi
# start the compilation adapter
echo -n "Starting the Compilation adapter"
"$ROOTDIR/compilation/run.sh" &> "$ROOTDIR/logs/compilation_$CURTIME.log" &
PROCESS_PID=$!
echo " (PID: $PROCESS_PID)"
echo -e "Waiting for the Compilation adapter to finish startup"
waitForUrlOnline "$compilation_url" "$PROCESS_PID" "$SLEEP" 0
ret="$?"
if [ "$ret" -eq 0 ]; then
echo -e "Compilation adapter ${GREEN}running${NC} (2/3)\n at $compilation_url\n"
KILL_COMPILATION_TAIL=true
else
echo -e "Compilation adapter ${RED}failed${NC} to start!"
if [ -n "$ARG_TAIL" ]; then
echo -e "See the \"tail: Compilation Log\" terminal window for the startup log."
echo -e "Or try checking the logs: \"$ROOTDIR/logs/compilation_$CURTIME.log\""
else
echo -e "Try checking the logs: \"$ROOTDIR/logs/compilation_$CURTIME.log\""
echo -e "Or run again with '-t' to see the logs during startup."
fi
abortAll # exit
fi
############################################################################################################
# Analysis
############################################################################################################
# open new terminal that tails the log file and record its PID to kill later
if [ -n "$ARG_TAIL" ]; then
gnome-terminal --title="tail: Analysis Log (feel free to close this window)" -- /bin/bash -c "tail -f \"$ROOTDIR/logs/../logs/../logs/analysis_$CURTIME.log\""
fi
# start the analysis adapter
echo -n "Starting the Analysis adapter"
"$ROOTDIR/analysis/run.sh" &> "$ROOTDIR/logs/analysis_$CURTIME.log" &
PROCESS_PID=$!
echo " (PID: $PROCESS_PID)"
echo -e "Waiting for the Analysis adapter to finish startup"
waitForUrlOnline "$analysis_url" "$PROCESS_PID" "$SLEEP" 0
ret="$?"
if [ "$ret" -eq 0 ]; then
echo -e "Analysis adapter ${GREEN}running${NC} (3/3)\n at $analysis_url\n"
KILL_ANALYSIS_TAIL=true
else
echo -e "Analysis adapter ${RED}failed${NC} to start!"
if [ -n "$ARG_TAIL" ]; then
echo -e "See the \"tail: Analysis Log\" terminal window for the startup log."
echo -e "Or try checking the logs: \"$ROOTDIR/logs/analysis_$CURTIME.log\""
else
echo -e "Try checking the logs: \"$ROOTDIR/logs/analysis_$CURTIME.log\""
echo -e "Or run again with '-t' to see the logs during startup."
fi
abortAll # exit
fi
echo -e "${GREEN}Ready to go!${NC}"
echo "Use ctrl+c to exit..."
wait # wait for jobs to finish (forever)
killall # make sure all terminals get closed and the exit message is printed
}
main "$@"