-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·169 lines (126 loc) · 3.33 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
#!/bin/bash
# Bootstrap script needed to launch the environement
#
DOCKER_IMAGE=benediction/bndsh
DSK_TEST=./bndsh.dsk
# 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="-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --privileged"
;;
windows)
LOCAL_WORKING_DIRECTORY=/$(pwd)
DOCKER_WORKING_DIRECTORY=//home/arnold/project
DOCKER_X11_FORWARDING=""
;;
esac
DOCKER=docker
DOCKER_HOSTNAME=$(basename $(pwd))
DOCKER_BASEIMAGE=$(grep FROM Dockerfile | sed -e 's/^FROM\s*//')
# XXX Lines modified to be compatible with cookie cutter parsing
DOCKER_IMAGE_DATE=$(docker images --format="\{\{.CreatedAt\}\}" $DOCKER_IMAGE)
DOCKER_BASEIMAGE_DATE=$(docker images --format="\{\{.CreatedAt\}\}" $DOCKER_BASEIMAGE)
# XXX Attention here it is not portable at all
# XXX Need to find a way which works everywhere
DOCKER_BASEIMAGE_DATE=$( echo "$DOCKER_BASEIMAGE_DATE" | sed -e 's/..... CES\?T//')
DOCKER_IMAGE_DATE=$( echo "$DOCKER_IMAGE_DATE" | sed -e 's/..... CES\?T//')
DOCKER_BASEIMAGE_DATE=$(date -d "$DOCKER_BASEIMAGE_DATE" '+%s')
DOCKER_IMAGE_DATE=$(date -d "$DOCKER_IMAGE_DATE" '+%s')
function buildImage
{
$DOCKER build -t $DOCKER_IMAGE .
}
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
$AFT_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
}
case "$1" in
rebuildImage)
buildImage
;;
removeImage)
removeImage
;;
help)
doHelp $0
;;
test)
checkImageCoherence
launch
;;
"")
checkImageCoherence
launchImage
;;
*)
checkImageCoherence
launchImage $*
esac