-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·201 lines (155 loc) · 4.12 KB
/
bootstrap.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
#!/bin/bash
# Bootstrap script needed to launch the environement
#
DOCKER_IMAGE=benediction/automation_player
# Common code for all the projects
# OS detection code
UNAME=$(uname -s)
DETECTED_OS=linux
if test "$(expr substr $(uname -s) 1 5)" == "MINGW"
then
DETECTED_OS=windows
fi
# Sadly, there are some differences between windows and linux
case $DETECTED_OS in
linux)
LOCAL_WORKING_DIRECTORY=$(pwd)
DOCKER_WORKING_DIRECTORY=/home/arnold/project
DOCKER_X11_FORWARDING="--env=DISPLAY --env=QT_X11_NO_MITSHM=1 --volume=/tmp/.X11-unix:/tmp/.X11-unix:rw --privileged"
DOCKER_SOUND_CONFIGURATION="-v /dev/snd:/dev/snd -v /dev/shm:/dev/shm -v /etc/machine-id:/etc/machine-id -v /run/user/$(id -u)/pulse:/run/user/$(id -u)/pulse -v /var/lib/dbus:/var/lib/dbus -v $HOME/.pulse:/home/arnold/.pulse"
;;
windows)
LOCAL_WORKING_DIRECTORY=/$(pwd)
DOCKER_WORKING_DIRECTORY=//home/arnold/project
DOCKER_X11_FORWARDING=""
DOCKER_SOUND_CONFIGURATION=""
;;
esac
case $HOST in
rgiot-laptop)
DOCKER=nvidia-docker
;;
*)
DOCKER=docker
;;
esac
DOCKER_HOSTNAME=$(basename $(pwd))
DOCKER_USER_OPTION="-e LOCAL_USER_ID=$(id -u $USER)"
DOCKER_BASEIMAGE=$(grep FROM Dockerfile | sed -e 's/^FROM\s*//')
if [[ $DOCKER_BASEIMAGE == *:* ]]
then
DOCKER_BASEIMAGE="$DOCKER_BASEIMAGE"
else
DOCKER_BASEIMAGE="$DOCKER_BASEIMAGE:latest"
fi
# Extract the construction date of a Docker container
# Input:
# - $1 Container name
function docker_date()
{
docker inspect "$1" | grep -i created | sed -e 's/^.*: "//' -e 's/",.*$//'
}
# Get docker container dates (docker format)
DOCKER_IMAGE_DATE=$(docker_date $DOCKER_IMAGE)
DOCKER_BASEIMAGE_DATE=$(docker_date $DOCKER_BASEIMAGE)
# Impose a specific format to ease comparison
DATE_FORMAT_EXPECTED="%s"
DOCKER_BASEIMAGE_DATE=$(date -d "$DOCKER_BASEIMAGE_DATE" "+$DATE_FORMAT_EXPECTED")
DOCKER_IMAGE_DATE=$(date -d "$DOCKER_IMAGE_DATE" "+$DATE_FORMAT_EXPECTED")
DOCKERFILE_DATE=$(date -r Dockerfile "+$DATE_FORMAT_EXPECTED")
function buildImage
{
$DOCKER build -t $DOCKER_IMAGE . || exit 1
}
function launchImage
{
# create the image if it does not exists
if test "$($DOCKER images -q $DOCKER_IMAGE 2> /dev/null)" = ""
then
buildImage
fi
# Manage parameters for aft
# XXX Implement a better and more robust way to do things ...
case $DETECTED_OS in
linux) AFT_PORT=/dev/ttyUSB0 ;;
windows) AFT_PORT=/dev/ttyS3 ;;
esac
if test -e "$AFT_PORT"
then
AFT_OPTION="--device $AFT_PORT"
else
echo "aft unusable ($AFT_PORT does not exists)" >&2
AFt_OPTION=""
fi
ALL_OPTIONS=" -h $DOCKER_HOSTNAME
-v $LOCAL_WORKING_DIRECTORY:$DOCKER_WORKING_DIRECTORY
-w $DOCKER_WORKING_DIRECTORY
$DOCKER_X11_FORWARDING
$DOCKER_AFT_OPTION
$DOCKER_SOUND_CONFIGURATION
$AFT_OPTION
$DOCKER_USER_OPTION
--rm=true
-i
-t
$DOCKER_IMAGE
$*"
# launch the image
echo Use these options: "$ALL_OPTIONS"
$DOCKER run $ALL_OPTIONS
}
function removeImage
{
$DOCKER rmi $DOCKER_IMAGE
}
function doHelp
{
echo "$1 [help | rebildImage | removeImage | launchImage | <cmd ...>]"
echo -e '\thelp: display help'
echo -e '\trebuildImage: reconstruct the Docker image'
echo -e '\tremoveImage: delete the Docker image'
echo -e '\tlaunchImage: launch the docker image and provides a prompt'
echo -e '\ttest: launch an emulator on the result'
echo -e '\t<cmd ...>: execute the command on the docker image'
echo
echo The operation launchImage is operated if no option is given
}
function launch
{
xdg-open $DSK_TEST
}
function checkImageCoherence
{
if test "$DOCKER_BASEIMAGE_DATE" -gt "$DOCKER_IMAGE_DATE"
then
echo "Parent image is newer -- I need to rebuild mine !!\n"
buildImage
fi
if test "$DOCKERFILE_DATE" -gt "$DOCKER_IMAGE_DATE"
then
echo "Docker file is newer the container -- I need to rebuild it !!\n"
buildImage
fi
}
case "$1" in
rebuildImage)
buildImage
;;
removeImage)
removeImage
;;
help)
doHelp $0
;;
test)
checkImageCoherence
launch
;;
"")
checkImageCoherence
launchImage
;;
*)
checkImageCoherence
launchImage $*
esac