-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_all.ps1
247 lines (216 loc) · 9.36 KB
/
run_all.ps1
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
##########################
# 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
##########################
param (
[switch]$t,
[switch]$h,
[switch]$b
)
$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: $PSCommandPath [-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
"
$USRPATH=$(pwd) # get the call directory
$ROOTDIR="$PSScriptRoot" # get the script directory
$PIDS_TO_KILL=@()
# duration for polling (via curl)
$SLEEP=3
# source shared utils
. "$ROOTDIR\dev_tools\shared.ps1"
# exits this scripts while killing all the subprocesses
# used when ctrl+c is received
function exit_killall ()
{
# try to kill all log tailing terminals regardless of the current startup state
$PIDS_TO_KILL = $PIDS_TO_KILL + $PID_TRIPLESTORE_TAIL
$PIDS_TO_KILL = $PIDS_TO_KILL + $PID_ANALYSIS_TAIL
$PIDS_TO_KILL = $PIDS_TO_KILL + $PID_COMPILATION_TAIL
abortAll
}
# used by the script in case of failure
function abortAll ()
{
echo "`nShutting down..."
killAllWithChildren $PIDS_TO_KILL
echo "All done."
[Console]::TreatControlCAsInput = $False
exit 0
}
#
# main
#
# check args
if ($args.length -ne 0) {
invalid_arg $args[0] "$USAGE"
}
if ($h) {
print_help "$HELP" "$USAGE"
}
# build first if requested by args
if ($b)
{
echo "Running build.sh first"
& "${ROOTDIR}\build.ps1"
if ( ! $? ) {
echo "Build failed. Aborting start.`n"
exit $LastExitCode
}
}
# otherwise just update conf
else {
processConfFiles "$ROOTDIR"
}
# get and output version
$VERSION=$(cat "$ROOTDIR\VERSION.md" 2> $null)
echo ""
echo "########################################################"
echo " Unite $VERSION"
echo "########################################################"
echo ""
# lookup config URLs
$triplestore_url = lookupTriplestoreURL "$ROOTDIR"
$compilation_url = lookupCompilationURL "$ROOTDIR"
$analysis_url = lookupAnalysisURL "$ROOTDIR"
# create log files and append headings
mkdir -Force "$ROOTDIR\logs" > $null
$CURTIME=$(Get-Date).ToString("yyyy-MM-dd_HH.mm.ss")
$CURTIME_FORLOG=$(Get-Date).ToString("yyyy-MM-dd_HH:mm:ss")
echo "########################################################`r`n Running version: $VERSION`r`n Started at: $CURTIME_FORLOG`r`n########################################################`r`n" > "$ROOTDIR\logs\triplestore_$CURTIME.log"
echo "########################################################`r`n Running version: $VERSION`r`n Started at: $CURTIME_FORLOG`r`n########################################################`r`n" > "$ROOTDIR\logs\compilation_$CURTIME.log"
echo "########################################################`r`n Running version: $VERSION`r`n Started at: $CURTIME_FORLOG`r`n########################################################`r`n" > "$ROOTDIR\logs\analysis_$CURTIME.log"
# rotate log files (delete all but last 5)
cd "$ROOTDIR/logs"
foreach( $log in ls -name | Sort-Object | out-string -stream | Select-String -Pattern triplestore_.*\.log | select -SkipLast 5) { rm "$log"}
foreach( $log in ls -name | Sort-Object | out-string -stream | Select-String -Pattern compilation_.*\.log | select -SkipLast 5) { rm "$log"}
foreach( $log in ls -name | Sort-Object | out-string -stream | Select-String -Pattern analysis_.*\.log | select -SkipLast 5) { rm "$log"}
cd $USRPATH
# treat ctrl+c as input so that we can kill subprocesses (sleep and flush to properly flush input)
[Console]::TreatControlCAsInput = $True
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
############################################################################################################
# Triplestore
############################################################################################################
# open new terminal that tails the log file and record its PID to kill later
if ($t)
{
$process = Start-Process powershell.exe "(Get-Host).ui.RawUI.WindowTitle='tail: Triplestore Log (feel free to close this window)'; Get-Content '$ROOTDIR\logs\..\logs\..\logs\triplestore_$CURTIME.log' -Wait -Tail 10; pause" -passthru
$PID_TRIPLESTORE_TAIL = $process.id
}
# start the triplestore
Write-Host -NoNewline "Starting the Triplestore"
$process = Start-Process -WindowStyle Minimized powershell.exe "(Get-Host).ui.RawUI.WindowTitle='Triplestore'; cd '$ROOTDIR\sparql_triplestore'; .\run.ps1 >> '$ROOTDIR\logs\triplestore_$CURTIME.log' 2>&1" -passthru
$PIDS_TO_KILL = $PIDS_TO_KILL + $process.id
$pid_as_string = $process.id
echo " (PID: $pid_as_string)"
echo "Waiting for the Triplestore to finish startup"
$ret = waitForUrlOnline $triplestore_url $process.id $SLEEP
if ($ret -eq 0) { # OK
echo "Triplestore running (1/3)`n at $triplestore_url`n"
$PIDS_TO_KILL = $PIDS_TO_KILL + $PID_TRIPLESTORE_TAIL
} elseif ($ret -eq 1) { # startup fail
echo "Triplestore ${RED}failed${NC} to start!"
if ($t)
{
echo "See the `"tail: Triplestore Log`" powershell window for the startup log."
echo "Or try checking the logs: `"$ROOTDIR\logs\triplestore_$CURTIME.log`""
} else {
echo "Try checking the logs: `"$ROOTDIR\logs\triplestore_$CURTIME.log`""
echo "Or run again with '-t' to see the logs during startup."
}
abortAll
} else { # ctrl+c pressed
exit_killall
}
############################################################################################################
# Compilation
############################################################################################################
# open new terminal that tails the log file and record its PID to kill later
if ($t)
{
$process = Start-Process powershell.exe "(Get-Host).ui.RawUI.WindowTitle='tail: Compilation Log (feel free to close this window)'; Get-Content '$ROOTDIR\logs\..\logs\..\logs\compilation_$CURTIME.log' -Wait -Tail 10; pause" -passthru
$PID_COMPILATION_TAIL = $process.id
}
# start the compilation adapter
Write-Host -NoNewline "Starting the Compilation adapter"
$process = Start-Process -WindowStyle Minimized powershell.exe "(Get-Host).ui.RawUI.WindowTitle='Compilation Adapter'; cd '$ROOTDIR\compilation' ; .\run.ps1 >> '$ROOTDIR\logs\compilation_$CURTIME.log' 2>&1" -passthru
$PIDS_TO_KILL = $PIDS_TO_KILL + $process.id
$pid_as_string = $process.id
echo " (PID: $pid_as_string)"
echo "Waiting for the Compilation adapter to finish startup"
$ret = waitForUrlOnline $compilation_url $process.id $SLEEP
if ($ret -eq 0) {
echo "Compilation adapter running (2/3)`n at $compilation_url`n"
$PIDS_TO_KILL = $PIDS_TO_KILL + $PID_COMPILATION_TAIL
} elseif ($ret -eq 1) {
echo "Compilation adapter failed to start!"
if ($t)
{
echo "See the `"tail: Compilation Log`" powershell window for the startup log."
echo "Or try checking the logs: `"$ROOTDIR\logs\compilation_$CURTIME.log`""
} else {
echo "Try checking the logs: `"$ROOTDIR\logs\compilation_$CURTIME.log`""
echo "Or run again with '-t' to see the logs during startup."
}
abortAll
} else {
exit_killall
}
############################################################################################################
# Analysis
############################################################################################################
# open new terminal that tails the log file and record its PID to kill later
if ($t)
{
$process = Start-Process powershell.exe "(Get-Host).ui.RawUI.WindowTitle='tail: Analysis Log (feel free to close this window)'; Get-Content '$ROOTDIR\logs\..\logs\..\logs\analysis_$CURTIME.log' -Wait -Tail 10; pause" -passthru
$PID_ANALYSIS_TAIL = $process.id
}
# start the analysis adapter
Write-Host -NoNewline "Starting the Analysis adapter"
$process = Start-Process -WindowStyle Minimized powershell.exe "(Get-Host).ui.RawUI.WindowTitle='Analysis Adapter'; cd '$ROOTDIR\analysis' ; .\run.ps1 >> '$ROOTDIR\logs\analysis_$CURTIME.log' 2>&1" -passthru
$PIDS_TO_KILL = $PIDS_TO_KILL + $process.id
$pid_as_string = $process.id
echo " (PID: $pid_as_string)"
echo "Waiting for the Analysis adapter to finish startup"
$ret = waitForUrlOnline $analysis_url $process.id $SLEEP
if ($ret -eq 0) {
echo "Analysis adapter running (3/3)`n at $analysis_url`n"
$PIDS_TO_KILL = $PIDS_TO_KILL + $PID_ANALYSIS_TAIL
} elseif ($ret -eq 1) {
echo "Analysis adapter failed to start!"
if ($t)
{
echo "See the `"tail: Analysis Log`" powershell window for the startup log."
echo "Or try checking the logs: `"$ROOTDIR\logs\analysis_$CURTIME.log`""
} else {
echo "Try checking the logs: `"$ROOTDIR\logs\analysis_$CURTIME.log`""
echo "Or run again with '-t' to see the logs during startup."
}
abortAll
} else {
exit_killall
}
echo "Ready to go!"
echo "Use ctrl+c to exit..."
# loop to catch ctrl+c
While ($true) {
Start-Sleep -s 1 # sleep for 1 second
$ret = checkForCtrlC
if ($ret -eq 1) {
exit_killall
}
}