forked from jruffet/docker-shogigui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-shogigui
executable file
·200 lines (176 loc) · 6.32 KB
/
docker-shogigui
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
#!/bin/bash
set -euo pipefail
trap 'cleanup' ERR
#######################################################
## STATIC GLOBALS
readonly SHOGIGUI_VERSION=0.0.7.27
readonly YANEURAOU_VERSION=7.10
readonly SCRIPT_NAME="docker-shogigui"
readonly CONF_DIR="$HOME/.shogigui"
#######################################################
## USAGE
usage() {
cat <<EOF
Usage: $SCRIPT_NAME [command] [options]
Examples:
$SCRIPT_NAME --build
$SCRIPT_NAME --run
$SCRIPT_NAME --run -s some_path/settings.xml -g some_path/my_games
Commands:
-b|--build Build docker image (do that first !)
-r|--run Run ShogiGUI inside a container
-u|--update-settings Update engines in settings.xml with those in the repo
-c|--cleanup Cleanup docker build images made by this script
-h|--help Display this help message
Options:
-g|--games-dir Mount local directory to /shogi/games inside the container
-s|--settings Override location of settings.xml file (default: $CONF_DIR/settings.xml)
-i|--image Use a specific image name:tag instead of shogigui:latest
EOF
exit 0
}
#######################################################
## LOGGING FRAMEWORK
readonly NORMAL="\\e[0m"; readonly RED="\\e[1;31m"; readonly YELLOW="\\e[1;33m"; readonly DIM="\\e[2m"; readonly BLUE="\\e[1;34m"
function _log() {
local color="$1"; local level="$2"; local message="$3"
printf "${color}%-s %s\\e[m\\n" "[${level}]" "$message"
}
function debug() { if [ "$verbose" = true ]; then _log "$DIM" "DEBUG" "$*"; fi }
function log() { _log "$NORMAL" "+" "$*"; }
function info() { _log "$BLUE" "+" "$*"; }
function warn() { _log "$YELLOW" "?" "$*"; }
function error() { _log "$RED" "!" "$*"; }
function fatal() { _log "$RED" "FATAL" "$*"; exit 1 ; }
#######################################################
## SCRIPT FUNCTIONS
function preflight_checks() {
[ "$UID" -eq 0 ] && fatal "This program should not be run as root"
for program in docker sed nproc; do
command -v $program >/dev/null 2>&1 || fatal "Command \"$program\" not found on the system"
done
docker ps >/dev/null 2>&1 || fatal "Docker cannot be operated by this user"
}
function cleanup() {
:
}
function build_docker_image() {
cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo)
# try those flags in order of preference
if $(grep -m1 "model name" /proc/cpuinfo | grep -q AMD); then
YANEURAOU_TARGET_CPU=ZEN2
elif $(grep -q avx512 <<< $cpu_flags); then
YANEURAOU_TARGET_CPU=AVX512
elif $(grep -q avx2 <<< $cpu_flags); then
YANEURAOU_TARGET_CPU=AVX2
elif $(grep -q sse4_2 <<< $cpu_flags); then
YANEURAOU_TARGET_CPU=SSE42
elif $(grep -q sse4_1 <<< $cpu_flags); then
YANEURAOU_TARGET_CPU=SSE41
elif $(grep -q sse2 <<< $cpu_flags); then
YANEURAOU_TARGET_CPU=SSE2
else
YANEURAOU_TARGET_CPU=NO_SSE
fi
info "Auto selected CPU arch : $YANEURAOU_TARGET_CPU"
info "Building docker image with tag : shogigui${SHOGIGUI_VERSION}:yaneuraou${YANEURAOU_VERSION}-${YANEURAOU_TARGET_CPU}"
echo
docker build --build-arg NPROC=$(nproc) \
--build-arg SHOGIGUI_VERSION=$SHOGIGUI_VERSION \
--build-arg YANEURAOU_VERSION=$YANEURAOU_VERSION \
--build-arg YANEURAOU_TARGET_CPU=$YANEURAOU_TARGET_CPU \
-t shogigui${SHOGIGUI_VERSION}:yaneuraou${YANEURAOU_VERSION}-${YANEURAOU_TARGET_CPU} \
-t shogigui:latest .
}
function cleanup_docker() {
info "Pruning all images with labels : app=shogigui + stage=build"
docker image prune --filter label=app=shogigui --filter label=stage=build
}
function update_settings_file() {
local updated_settings_file=0
grep -q '<EngineList />' $settings_file && sed -i 's#<EngineList />#<EngineList>\n</EngineList>#' $settings_file
for xml_file in xml/*.xml; do
name=$(grep -Eo '<Name>.*</Name>' $xml_file)
engine_name=$(perl -pe 's#<Name>(.*)</Name>#$1#' <<< $name)
if ! grep -q "$name" $settings_file; then
updated_settings_file=1
info "Adding engine : $engine_name"
sed -n -i -e "/<\/EngineList>/r $xml_file" -e 1x -e '2,${x;p}' -e '${x;p}' $settings_file
fi
done
if [ "$updated_settings_file" -eq 0 ]; then
info "All engines already configured in the settings file, not updating"
fi
}
function run() {
if ! [ -e "$settings_file" ]; then
info "Copying repository settings.xml to $settings_file"
cp settings.xml $settings_file
fi
if docker inspect --type=image $image_name_tag >/dev/null 2>&1; then
docker run --rm --name shogigui --net host --user $UID:$UID -e DISPLAY \
-v $settings_file:/shogi/shogigui/settings.xml \
$extra_mount \
$image_name_tag
else
fatal "Docker image \"$image_name_tag\" not found locally. Please run: shogigui --build"
fi
}
#######################################################
## MAIN
preflight_checks
cd $(dirname $0) || fatal "Cannot chdir to script directory"
[ -d "$CONF_DIR" ] || mkdir "$CONF_DIR" || fatal "Cannot mkdir $CONF_DIR"
command=
extra_mount=
games_dir=
settings_file="$CONF_DIR/settings.xml"
image_name_tag="shogigui:latest"
[ $# -eq 0 ] && usage
while [ $# -ne 0 ]; do
case "$1" in
-h|--help)
usage
;;
-b|--build)
command="build_docker_image"
;;
-c|--cleanup)
command="cleanup_docker"
;;
-r|--run)
command="run"
;;
-u|--update-settings)
command="update_settings_file"
;;
-g|--games)
shift
[ $# -ge 1 ] || usage
readonly games_dir="$1"
;;
-s|--settings)
shift
[ $# -ge 1 ] || usage
readonly settings_file="$1"
;;
-i|--image)
shift
[ $# -ge 1 ] || usage
readonly image_name_tag="$1"
;;
*)
error "Unrecognized command : $1"
usage
;;
esac
shift
done
[ -n "$games_dir" ] && extra_mount="$extra_mount -v $games_dir:/shogi/games"
if [ -n "$command" ]; then
$command
exit 0
else
error "No command specified"
usage
fi