-
Notifications
You must be signed in to change notification settings - Fork 14
/
run
executable file
·198 lines (177 loc) · 4.21 KB
/
run
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
#!/bin/bash
# Copyright (c) 2015-2018 BITPlan GmbH
#
# see LICENSE
#
# WF 2015-10-18
#
# set the names
version="0.0.3"
imagename="smw"
containername="smw"
# get the hostname
hostname=`hostname`
imagehostname=$hostname
# states
port="80"
recreate="false"
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show an error message and exit
#
# params:
# 1: l_msg - the message to display
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error: $l_msg" 1>&2
exit 1
}
#
# show usage
#
usage() {
local p_name=`basename $0`
echo "$p_name"
echo " -h |--help : show this usage"
echo " -p |--port [port] : set port to be used"
echo " -rc|--recreate : recreate the container"
exit 1
}
#
# get docker ip
#
get_dockerip() {
# check whether docker-machine is available
which docker-machine > /dev/null
if [ $? -ne 0 ]
then
docker inspect $containername | grep IPAddress | tail -1 | cut -d '"' -f 4
else
# get the docker machine settings
eval `docker-machine env default | grep DOCKER_HOST`
echo $DOCKER_HOST | cut -f2 -d: | cut -f3 -d/
fi
}
#
# set the host
#
sethost() {
grep $DOCKER_IP /etc/hosts | grep $imagehostname > /dev/null
if [ $? -ne 0 ]
then
color_msg $red "$DOCKER_IP $imagehostname"
color_msg $red "missing in /etc/hosts on $hostname"
color_msg $blue "shall I add it y/n?"
read answer
case $answer in
y|yes)
sudo bash -c "echo $DOCKER_IP $imagehostname >> /etc/hosts"
color_msg $green "$DOCKER_IP $imagehostname added to /etc/hosts!" ;;
*) color_msg $blue "the hostname of the mediawiki image will be set to $imagehostname!";;
esac
else
color_msg $green "$imagehostname $DOCKER_IP already in /etc/hosts on $hostname"
fi
}
#
# show link
#
showlink () {
color_msg $green "$containername is at http://$DOCKER_IP:$port/mediawiki"
}
#
# get the container id
#
getcontainerid() {
local l_option="$1"
docker ps -q $l_option --filter "name=^$containername\$"
}
# remember the time we started this
start_date=$(date -u +"%s")
while test $# -gt 0
do
case $1 in
# help
-h|--help)
usage;;
# set port to be used
-p|--port)
shift
port=$1
;;
-rc|--recreate)
recreate="true";
;;
esac
shift
done
# get the DOCKER IP
DOCKER_IP=`get_dockerip`
#
# prepare recreate
#
if [ "$recreate" = "true" ]
then
color_msg $blue "preparing recreate of $containername"
color_msg $blue "stopping $containername"
docker stop $containername
color_msg $blue "remove $containername"
docker rm $containername
fi
#
# run, start or attach to the container
#
containerid=$(getcontainerid -a)
if [ "$containerid" == "" ]
then
color_msg $blue "running new container $containername with imagehostname $imagehostname"
# run docker to install and map port 80
portmap=""
if [ "$port" != "" ]
then
portmap="-p $port:80"
fi
docker run -d $portmap --name $containername --hostname=$imagehostname bitplan/$imagename:$version
sethost
showlink
else
color_msg $green "container $containername already exists with id $containerid."
color_msg $green "Remove this container with 'docker rm $containername' if you need to do so"
color_msg $green "or use the -rc option for this script"
runcontainerid=$(getcontainerid)
# check if the container is running
if [ "$runcontainerid" == "" ]
then
color_msg $blue "container $containername is stopped"
docker start $containername
else
color_msg $blue "container $containername is already running"
fi
showlink
color_msg $green "will start a shell for you ..."
docker exec -it $containername /bin/bash
fi
# stop the end time
end_date=$(date -u +"%s")
# show how long this took
diff=$(($end_date-$start_date))
color_msg $blue "run took $(($diff / 60)) minutes and $(($diff % 60)) seconds."