-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-core.sh
executable file
·302 lines (268 loc) · 6.98 KB
/
start-core.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
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
#!/usr/bin/env bash
SOURCE="$0"
script=${0}
# echo "script: $script"
script=${script##*/}
# echo "$script"
# NOTE: for macOS, it seems I have to leave script folder to run the other scripts
if [[ "$script" != "start-core.sh" ]]; then
# echo "going back .."
cd -P "$(dirname "$SOURCE")"/.. || exit 1 # Enter scripts folder or fail!
else
cd -P "$(dirname "$SOURCE")" || exit 1 # Enter scripts folder or fail!
fi
DIR="$(pwd)"
UI_DIR="$(pwd)/core/ui"
export CONDA_ENV_NAME="core"
function found_exe() {
hash "$1" 2>/dev/null
}
if found_exe tput; then
if [[ $(tput colors) != "-1" && -z $CI ]]; then
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 6)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
HIGHLIGHT=$YELLOW
fi
fi
help() {
echo "${script}: core command/service launcher"
echo "usage: ${script} [COMMAND] [restart] [params]"
echo
echo "Services COMMANDs:"
echo " all runs core services: bus, audio, core, listener"
echo " debug runs core services, then starts the CLI"
echo " audio the audio playback service"
echo " bus the messagebus service"
echo " core the core service"
echo " listener listener capture service"
# echo " enclosure enclosure service"
echo
echo "Tool COMMANDs:"
echo " cli the Command Line Interface"
echo " web the web UI service"
# echo " unittest run core unit tests (requires pytest)"
# echo " skillstest run the skill autotests for all skills (requires pytest)"
# echo " vktest run the Voight Kampff integration test suite"
echo
# echo "Util COMMANDs:"
# echo " audiotest attempt simple audio validation"
# echo " wakewordtest test selected wakeword engine"
# echo " sdkdoc generate sdk documentation"
echo
echo "Options:"
echo " restart (optional) Force the service to restart if running"
echo
echo "Examples:"
echo " ${script} all"
echo " ${script} all restart"
echo " ${script} cli"
# echo " ${script} unittest"
exit 1
}
_module=""
name_to_script_path() {
case ${1} in
"bus") _module="source.messagebus.service" ;;
"core") _module="source.core" ;;
"audio") _module="source.audio" ;;
"listener") _module="source.client.listener" ;;
"cli") _module="source.client.text" ;;
"web") _module="source.client.web" ;;
# "audiotest") _module="core.util.audio_test" ;;
# "wakewordtest") _module="test.wake_word" ;;
# "enclosure") _module="core.client.enclosure" ;;
*)
echo "Error: Unknown name '${1}'"
exit 1
;;
esac
}
source_venv() {
# Enter CONDA virtual environment
# TODO: dynmaically get miniconda path
CONDA_BIN=$(dirname "$CONDA_EXE")
# $CONDA_EXE activate $CONDA_ENV_NAME
source "$CONDA_BIN/activate" $CONDA_ENV_NAME
exit_status=$?
if [[ $exit_status -eq "0" ]]; then
echo $BLUE "Entering virtual environment ${CONDA_DEFAULT_ENV} $RESET"
else
echo $RED "Could not enter virtual environment $RESET"
fi
}
first_time=true
init_once() {
if ($first_time); then
echo "Initializing..."
# "${DIR}/scripts/prepare-msm.sh"
source_venv
first_time=false
fi
}
launch_process() {
init_once
name_to_script_path "${1}"
# Launch process in foreground
echo "Starting $1"
python -m ${_module} "$@"
}
require_process() {
# Launch process if not found
name_to_script_path "${1}"
if ! pgrep -f "(python3|python|Python) (.*)-m ${_module}" >/dev/null; then
# Start required process
launch_background "${1}"
fi
}
launch_background() {
init_once
# Check if given module is running and start (or restart if running)
name_to_script_path "${1}"
if pgrep -f "(python3|python|Python) (.*)-m ${_module}" >/dev/null; then
if ($_force_restart); then
echo "Restarting: ${1}"
"${DIR}/stop-core.sh" "${1}"
else
# Already running, no need to restart
return
fi
else
echo "Starting background service $1"
fi
# Security warning/reminder for the user
if [ "${1}" = "bus" ]; then
echo "$HIGHLIGHT CAUTION: The core bus is an open websocket with no built-in security"
echo " measures. You are responsible for protecting the local port"
echo " 8181 with a firewall as appropriate. $RESET "
fi
# Launch process in background, sending logs to standard location
python -m ${_module} "$@" >>"/var/log/core/${1}.log" 2>&1 &
}
launch_all() {
echo "Starting all core services"
launch_background bus
launch_background audio
# NOTE: provide the opportunity for online listener component to load
sleep 2 # Add a delay of 5 seconds
launch_background listener
launch_background core
# FIX: issue with websocket being none-type at startup
launch_background web
# launch_background enclosure
# cd "${UI_DIR}" || exit
# nohup npm run dev &
# cd ..
# nohup uvicorn core.ui.backend.__main__:app &
}
check_dependencies() {
if [ -f .dev_opts.json ]; then
auto_update=$(jq -r ".auto_update" <.dev_opts.json 2>/dev/null)
else
auto_update="false"
fi
if [ "$auto_update" = "true" ]; then
# Check github repo for updates (e.g. a new release)
git pull
fi
if [ ! -f .installed ] || ! md5sum -c >/dev/null 2>&1 <.installed; then
# Critical files have changed, dev_setup.sh should be run again
if [ "$auto_update" = "true" ]; then
echo "Updating dependencies..."
bash dev_setup.sh
else
echo "Please update dependencies by running ./dev_setup.sh again."
if command -v notify-send >/dev/null; then
# Generate a desktop notification (ArchLinux)
notify-send "core Dependencies Outdated" "Run ./dev_setup.sh again"
fi
exit 1
fi
fi
}
_opt=$1
_force_restart=false
if [ $# -eq 0 ]; then
help
return
fi
shift
if [ "${1}" = "restart" ] || [ "${_opt}" = "restart" ]; then
_force_restart=true
if [ "${_opt}" = "restart" ]; then
# Support "start-core.sh restart all" as well as "start-core.sh all restart"
_opt=$1
fi
if [ $# -gt 0 ]; then
shift
fi
fi
# if [ ! "${_opt}" = "cli" ] ; then
# # check_dependencies
# fi
case ${_opt} in
"all")
launch_all
;;
"bus")
launch_background "${_opt}"
;;
"audio")
launch_background "${_opt}"
;;
"core")
launch_background "${_opt}"
;;
"listener")
launch_background "${_opt}"
;;
"web")
launch_background "${_opt}"
;;
"debug")
# launch_process cli
launch_all
# launch_process web
launch_process cli
;;
"cli")
require_process bus
# require_process web
require_process core
launch_process "${_opt}"
;;
# TODO: Restore support for Wifi Setup on a Picroft, etc.
# "wifi")
# launch_background ${_opt}
# ;;
"unittest")
source_venv
pytest test/unittests/ --cov=core "$@"
;;
"singleunittest")
source_venv
pytest "$@"
;;
"skillstest")
source_venv
pytest test/integrationtests/skills/discover_tests.py "$@"
;;
"vktest")
"$DIR/bin/core-skill-testrunner" vktest "$@"
;;
"audiotest")
launch_process "${_opt}"
;;
"wakewordtest")
launch_process "${_opt}"
;;
"enclosure")
launch_background "${_opt}"
;;
*)
help
;;
esac