-
Notifications
You must be signed in to change notification settings - Fork 66
/
run-gow
executable file
·455 lines (409 loc) · 14.8 KB
/
run-gow
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/bash
SCRIPT_NAME=$(basename "$0")
# shellcheck disable=SC2164
SCRIPT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
SCRIPT_ARGV=("$@")
MIN_COMPOSE_VERSION=2.6.0
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/gow"
RC_FILE_NAME=run-gow-rc
readonly SCRIPT_NAME SCRIPT_DIR MIN_COMPOSE_VERSION RC_FILE_NAME
# Configuration values. These will be set based on the command-line arguments we get
launch_env=("env/base.env" "env/build.env")
compose_files=()
apps=()
gpu_type=none
streamer=sunshine # by default, use sunshine to stream to moonlight
platform=host
quiet=false
debug=false
main() {
# This script is deprecated, please use Wolf instead
echo "WARNING: This script is deprecated. Please use Wolf instead!"
echo "For more information, see: https://games-on-whales.github.io/wolf/stable/user/quickstart.html"
echo "press a button to continue anyway..."
read -r
parse_cli SCRIPT_ARGV
# which moonlight host should we be using? Wolf (beta) or Sunshine (default)?
case "$streamer" in
wolf)
compose_files+=("compose/streamers/wolf.yml")
;;
*)
compose_files+=("compose/streamers/sunshine.yml")
;;
esac
# which compose and environment files should we be using? this will depend
# on command-line options as well as what app(s) we're launching
case "$platform" in
headless)
launch_env+=("env/headless.env")
compose_files+=("compose/platforms/headless.yml")
;;
*)
launch_env+=("env/host-desktop.env")
compose_files+=("compose/platforms/host.yml")
;;
esac
launch_env+=("env/$gpu_type.env")
# if there's an app-specific env file, load it too
for app in "${apps[@]}"; do
launch_env+=("env/$app.env")
done
# prefer the user.env in the global config directory, if there is one
if [ -f "$CONFIG_DIR/user.env" ]; then
launch_env+=("$CONFIG_DIR/user.env")
else
launch_env+=("user.env")
fi
if [ "$debug" = "true" ]; then
print_debug_info
else
if ! check_compose_version; then
echo_stderr "Your docker-compose is too old; please install v$MIN_COMPOSE_VERSION or later."
exit 1
fi
# docker-compose >= v2.6.0 can't handle process substitution for the `--env-file` option :-(
# but, it _does_ support loading variables from the process's environment. So,
# load the files we need before we execute docker-compose.
set -o allexport
for env_file in "${launch_env[@]}"; do
local full_file="$env_file"
if [ ! -f "$full_file" ]; then
full_file="$SCRIPT_DIR/$full_file"
fi
if [ -f "$full_file" ]; then
# shellcheck disable=1090
source "$full_file"
fi
done
set +o allexport
# the compose files can use this variable to distinguish between streamers, if needed
export GOW_MOONLIGHT_HOST="$streamer"
echo_stderr "Running docker compose..."
if [ "${#SCRIPT_ARGV}" -gt 0 ]; then
eval "$(get_compose_cmd) ${SCRIPT_ARGV[*]}"
else
eval "$(get_compose_cmd) up"
fi
fi
}
# Print a usage message to the console
usage() {
quiet=false # always print usage, even if quiet is set
echo_stderr "Launch the Games on Whales system"
echo_stderr
echo_stderr "Usage: $SCRIPT_NAME [options] [compose commands]"
echo_stderr "Arguments after the last option will be passed directly to 'docker compose'."
echo_stderr "For example, to launch the containers in the background, try:"
echo_stderr " $ $SCRIPT_NAME --app retroarch up -d"
echo_stderr
echo_stderr "Options:"
echo_stderr " -h, --help"
echo_stderr " Print this help text."
echo_stderr
echo_stderr " -a, --app <app name>"
echo_stderr " Specify an application to launch. Can be used multiple times."
echo_stderr
echo_stderr " -d, --debug"
echo_stderr " Print some extra debugging information before running Docker commands."
echo_stderr
echo_stderr " -e, --env-file <file>"
echo_stderr " Specify an additional file of environment varibles to load before launching 'docker compose'."
echo_stderr
echo_stderr " -g, --gpu <type>"
echo_stderr " Use this option to specify what type of GPU to use with Games on Whales. Not"
echo_stderr " all GPU types require this option (notably, AMD does not)."
echo_stderr " Possible types:"
echo_stderr " nvidia, intel"
echo_stderr
echo_stderr " -p, --platform"
echo_stderr " Use this option to specify whether system services like Xorg should be containerized or not."
echo_stderr " Possible values:"
echo_stderr " headless - run xorg, udev, and pulse in containers"
echo_stderr " host - use system services already running on the host server [default]"
echo_stderr
echo_stderr " -q, --quiet"
echo_stderr " If set, this script will not produce any output of its own. This will not affect output from 'docker compose'."
echo_stderr
echo_stderr " -s, --streamer"
echo_stderr " Use this option to specify which streaming server should be used to connect with Moonlight."
echo_stderr " Possible values:"
echo_stderr " sunshine - the original, more stable option [default]"
echo_stderr " wolf - the bleeding edge option. still in beta."
echo_stderr
echo_stderr " -x, --headless"
echo_stderr " DEPRECATED. If set, has the same effect as '--platform headless'"
echo_stderr
echo_stderr " --"
echo_stderr " Signifies the end of options."
}
# Parse the command line args we were given
parse_cli() {
local -n argv=$1
# read the rc file, if one exists
local rc_file="${CONFIG_DIR}/${RC_FILE_NAME}"
if [ -f "$rc_file" ]; then
# read the first non-empty, non-commented line
local comment_re='^[[:space:]]*#'
local empty_re='^[[:space:]]*$'
while IFS= read -r line; do
if [[ $line =~ $comment_re || $line =~ $empty_re ]]; then
continue
fi
read -r -a rc_args <<< "$line"
argv=("${rc_args[@]}" "${argv[@]}")
break
done < "$rc_file"
fi
if [ "${#argv}" -eq 0 ]; then
usage
exit 1
fi
local idx=0
while [ "$idx" -le "${#argv[@]}" ]; do
case "${argv[$idx]}" in
-h|--help)
usage
exit 0
;;
-a|--app)
apps+=("${argv[$idx+1]}")
idx=$((idx + 1))
;;
-d|--debug)
debug=true
;;
-e|--env-file)
launch_env+=("${argv[$idx+1]}")
idx=$((idx + 1))
;;
-g|--gpu)
local type="${argv[$idx+1]}"
if [ "$type" = "nvidia" ] || [ "$type" = "intel" ]; then
gpu_type="$type"
fi
# even if they gave us one we don't recognize/need, skip over it
idx=$((idx + 1))
;;
-p|--platform)
local plat="${argv[$idx+1]}"
# validate that the given value is one we support
if [ "$plat" = "headless" ] || [ "$plat" = "host" ]; then
platform="$plat"
fi
# even if they gave us one we don't recognize/need, skip over it
idx=$((idx + 1))
;;
-q|--quiet)
quiet="true"
;;
-s|--streamer)
local str="${argv[$idx+1]}"
# validate that the given value is one we support
if [ "$str" = "sunshine" ] || [ "$str" = "wolf" ]; then
streamer="$str"
fi
# even if they gave us one we don't recognize/need, skip over it
idx=$((idx + 1))
;;
-x|--headless)
echo_stderr "WARNING: the run-gow option '--headless' has been deprecated. Please use '--platform headless' instead."
platform="headless"
;;
--)
break
;;
-*)
echo_stderr "Invalid option '${argv[$idx]}'." >&2
usage
exit 1
;;
# non-option arguments should also stop processing; everything after will
# be passed to docker compose.
*)
break
;;
esac
idx=$((idx + 1))
done
argv=( "${argv[@]:$idx}" )
}
# echo the given text to stderr, unless the "quiet" option is set
echo_stderr() {
local txt="$1"
[ "$quiet" != "true" ] && echo "$txt" >&2
}
# Print out some extra debugging info. Currently, this is the list of
# environment variables we're loading, plus the transformed contents of each
# compose file.
print_debug_info() {
echo_stderr "Detected OS: $(os_type)"
local version_status="✓"
if ! check_compose_version; then
version_status="✗"
fi
echo_stderr "Detected Compose version: $(get_compose_version) (min: $MIN_COMPOSE_VERSION) $version_status"
echo_stderr ""
local variable_re='^[[:space:]]*([[:alpha:]][[:alnum:]_]*)='
# Print out the environment variables
echo_stderr "Loading environment variables:"
for env_file in "${launch_env[@]}"; do
local full_file="$env_file"
if [ ! -f "$full_file" ]; then
full_file="$SCRIPT_DIR/$env_file"
fi
if [ -f "$full_file" ]; then
while IFS= read -r line; do
if [[ $line =~ $variable_re ]]; then
echo_stderr " - ${line} (from $env_file)"
fi
done < "$full_file"
fi
done
echo_stderr
# Print out each transformed file
for file in "${compose_files[@]}"; do
if [ -f "$file" ]; then
echo_stderr "Transformed file: $file"
echo_stderr "$(transform_file "$file")"
echo_stderr
fi
done
for app in "${apps[@]}"; do
app_file="compose/apps/$app.yml"
if [ -f "$app_file" ]; then
echo_stderr "Transformed file: $app_file"
echo_stderr "$(transform_file "$app_file")"
echo_stderr
fi
done
}
# read the given text line by line and add the given padding string to the
# front of each line.
pad_lines() {
local text=$1
local space=$2
while IFS= read -r line; do
echo "${space}${line}"
done < <(printf '%s\n' "$text")
}
# Get a line suitable for inserting into an `env_file:` list in a docker
# compose file that will load whatever extra environment is necessary for the
# current gpu type, if such a file exists.
get_gpu_env() {
if [ -f "config/$gpu_type.env" ]; then
echo "- config/${gpu_type}.env"
fi
}
# what OS are we running on? For most Linux distros, this will be whatever
# /etc/lsb-release or /etc/os-release says it is. Unraid's just says
# "slackware" (which is true but not specific enough), so we use a different
# mechanism.
os_type() {
local os="unknown"
if [ -f /etc/unraid-version ]; then
os="unraid"
elif [ -f /etc/lsb-release ]; then
# shellcheck disable=1091
os="$(source /etc/lsb-release; echo "$DISTRIB_ID")"
elif [ -f /etc/os-release ]; then
# shellcheck disable=1091
os="$(source /etc/os-release; echo "$ID")"
fi
# normalize os to be lower case
echo "${os,,}"
}
# These are the mappings necessary to get nvidia Xorg drivers into the xorg
# container from the host. They will only be used if $headless=true and
# $gpu_type=nvidia
declare -A xorg_driver
xorg_driver[unraid]=$(cat - <<END
- /usr/lib64/xorg/modules/drivers/nvidia_drv.so:/nvidia/xorg/nvidia_drv.so:ro
- /usr/lib64/xorg/modules/extensions/libglxserver_nvidia.so:/nvidia/xorg/libglxserver_nvidia.so:ro
END
)
xorg_driver[ubuntu]=$(cat - <<END
- /usr/lib/x86_64-linux-gnu/nvidia/xorg/:/nvidia/xorg/:ro
END
)
xorg_driver[arch]=$(cat - <<END
- /usr/lib/xorg/modules/drivers/nvidia_drv.so:/nvidia/xorg/nvidia_drv.so:ro
- /usr/lib/nvidia/xorg/libglxserver_nvidia.so:/nvidia/xorg/libglxserver_nvidia.so:ro
END
)
xorg_driver[debian]=$(cat - <<END
- /usr/lib/xorg/modules/drivers/nvidia_drv.so:/nvidia/xorg/nvidia_drv.so:ro
- /usr/lib/xorg/modules/extensions/libglxserver_nvidia.so:/nvidia/xorg/libglxserver_nvidia.so:ro
END
)
# Iterate through the given file and insert environment files and volume mounts
# as needed for the desired setup (headless, gpu type, etc)
transform_file() {
local file=$1
local gpu_env_re='([[:space:]]+)# run-gow: gpu_env'
local xorg_driver_re='([[:space:]]+)# run-gow: xorg_driver'
while IFS= read -r line; do
if [[ $line =~ $gpu_env_re ]]; then
pad_lines "$(get_gpu_env)" "${BASH_REMATCH[1]}"
elif [ "$gpu_type" = "nvidia" ] && [[ $line =~ $xorg_driver_re ]]; then
pad_lines "${xorg_driver[$(os_type)]}" "${BASH_REMATCH[1]}"
else
echo "$line"
fi
done < "$file"
}
# return the docker compose cli executable path
get_compose_exec_path() {
local docker_compose_command="$(command -v docker 2> /dev/null) compose"
if command -v docker-compose &> /dev/null; then
docker_compose_command="$(command -v docker-compose 2> /dev/null)"
fi
echo "${docker_compose_command}"
}
# return a command suitable for passing to 'eval' that will launch
# docker-compose with the correct configuration
get_compose_cmd() {
local cmd
cmd="$(get_compose_exec_path)"
local yaml_files
yaml_files=$(
for file in "${compose_files[@]}"; do
if [ -f "$file" ]; then
echo -ne " -f <(transform_file $file)"
fi
done)
yaml_files+=$(
for app in "${apps[@]}"; do
app_file="compose/apps/$app.yml"
if [ -f "$app_file" ]; then
echo -ne " -f <(transform_file $app_file)"
fi
done)
# cat -- <(cat $env_files)
echo "$cmd --project-directory \"${SCRIPT_DIR}\" --project-name gow $yaml_files"
}
get_compose_version() {
local cmd="$(get_compose_exec_path)"
local compose_re='version v?([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)'
if [[ "$($cmd version)" =~ $compose_re ]]; then
echo "${BASH_REMATCH[1]}"
else
echo ""
fi
}
# check that the installed version of docker-compose is new enough.
check_compose_version() {
local min_version installed_version version_string
version_string=$(get_compose_version)
if [[ "${version_string}" != "" ]]; then
local IFS=.
# shellcheck disable=2086
printf -v installed_version %08d ${version_string}
printf -v min_version %08d $MIN_COMPOSE_VERSION
test "$installed_version" -ge "$min_version"
else
echo_stderr "Docker Compose was not found. Please install Docker Compose version $MIN_COMPOSE_VERSION or newer."
false
fi
}
main