forked from rhasspy/rhasspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-rhasspy.sh
executable file
·154 lines (121 loc) · 3.97 KB
/
get-rhasspy.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
#!/usr/bin/env bash
# Verify presence of docker and docker-compose
if [[ -z "$(command -v docker)" ]]; then
echo "Docker is required"
exit 1
fi
if [[ -z "$(command -v docker-compose)" ]]; then
echo "Docker compose is required"
exit 1
fi
# Docker container versions
rhasspy_supervisor_version='latest'
rhasspy_server_version='latest'
# -----------------------------------------------------------------------------
# Name of profile (e.g., "en")
profile=''
# Parse command-line arguments
while [[ -n "$1" ]];
do
if [ "$1" == "--profile" ] || [ "$1" == "-p" ]; then
profile="$2"
shift 2
fi
shift 1
done
if [[ -z "${profile}" ]]; then
echo "--profile or -p is required"
exit 1
fi
# -----------------------------------------------------------------------------
# Determine where to write user profile files.
# Normally this is in $HOME/.config, but we should check $XDG_CONFIG_HOME first.
config_home="${XDG_CONFIG_HOME}"
if [[ -z "${config_home}" ]]; then
config_home="${HOME}/.config"
fi
user_profiles="${config_home}/rhasspy/profiles"
# Runs Docker container with rhasspy-supervisor tool.
# This reads a Rhasspy JSON profile and writes a Docker compose YAML file.
function rhasspy_supervisor {
# Run rhasspy/rhasspy-supervisor tool
docker run -it \
--user "$(id -u):$(id -g)" \
-v "${user_profiles}:${user_profiles}" \
"rhasspy/rhasspy-supervisor:${rhasspy_supervisor_version}" \
"$@"
}
if [[ -z "${RHASSPY_BASE_DIR}" ]]; then
# Base directory is inside rhasspy-server-hermes container
export RHASSPY_BASE_DIR='/usr/lib/rhasspy-server-hermes'
fi
# Runs a Docker container with rhasspy-server-hermes service.
# The web server frontend to Rhasspy, available on HTTP port 12101.
function rhasspy_server {
# Run rhasspy/rhasspy-server-hermes web server
docker run -it \
--user "$(id -u):$(id -g)" \
--network host \
-v "${user_profiles}:${user_profiles}" \
"rhasspy/rhasspy-server-hermes:${rhasspy_server_version}" \
--web-dir '/usr/lib/rhasspy-server-hermes/web' \
"$@"
}
# -----------------------------------------------------------------------------
# Clean up supervisord artifacts.
# If a supervisord.pid file is present, Rhasspy will think it's running under
# supervisord instead.
pid_file="${user_profiles}/${profile}/supervisord.pid"
rm -f "${pid_file}"
export running_file="${user_profiles}/${profile}/.docker_runnning"
export restart_file="${user_profiles}/${profile}/.restart_docker"
rm -f "${running_file}" "${restart_file}"
# Generate docker-compose.yml
rhasspy_supervisor \
--user-profiles "${user_profiles}" \
--profile "${profile}" \
--debug
compose_file="${user_profiles}/${profile}/docker-compose.yml"
# Stop on error going forward
set -e
# Start Rhasspy services
docker-compose --file "${compose_file}" up --detach
# Signal successful startup
touch "${running_file}"
# Watch for a restart signal in the background
{
while true;
do
sleep 0.5
# Check for presence of restart file
if [[ -f "${restart_file}" ]];
then
# Restart Rhasspy services
echo "Restarting"
docker-compose --file "${compose_file}" down --timeout 5
rm -f "${running_file}"
# Start back up
docker-compose --file "${compose_file}" up --detach
touch "${running_file}"
# Signal web server that restart is complete
rm -f "${restart_file}"
fi
done
} &
export restart_pid="$!"
function finish {
echo "Exiting"
if [[ -n "${restart_pid}" ]]; then
kill "${restart_pid}"
fi
if [[ -f "${running_file}" ]]; then
# Shut down Docker services if still running
docker-compose --file "${compose_file}" down
fi
}
trap finish EXIT
# Run web server
rhasspy_server \
--mqtt-host 'localhost' \
--user-profiles "${user_profiles}" \
--profile "${profile}"