-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot2docker.sh
executable file
·178 lines (168 loc) · 4.87 KB
/
boot2docker.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
#!/bin/bash
REALPATH=`realpath $0`
WORKDIR=`dirname $REALPATH`
MOUNTDIR=$WORKDIR
if [ -z "$1" ]; then
echo -e "Usage:\n $0 [domain] [email] [script] [repository] [mntdir]"
echo -e "\nExample:\n $0 test.org mail@mail.com neticrm-7.sh netivism/docker-debian-php:develop 9"
echo -e "\nError:"
echo -e " Required domain name."
exit 1
fi
if [ -z "$2" ]; then
echo -e "Usage:\n $0 [domain] [email] [script] [repository] [mntdir]"
echo -e "\nExample:\n $0 test.org mail@mail.com neticrm-7.sh netivism/docker-debian-php:develop 9"
echo -e "\nError:"
echo -e " Required email"
exit 1
fi
RESULT=0
MNTDIR="7"
if [ -n "$2" ]; then
MNTDIR=$5
fi
if [ ! -d "$MOUNTDIR/neticrm-$MNTDIR" ]; then
mkdir -p $MOUNTDIR/neticrm-$MNTDIR
RESULT=$?
fi
if [ ! -d "$MOUNTDIR/www/sites/$1/log/supervisor" ]; then
mkdir -p $MOUNTDIR/www/sites/$1/log/supervisor
fi
if [ ! -d "$MOUNTDIR/sql/sites/$1" ]; then
mkdir -p $MOUNTDIR/sql/sites/$1
RESULT=$?
fi
# pickup port
for WWWPORT in $(seq 30000 1 31000); do
RESULT=`docker ps -qa | xargs docker inspect --format='{{ .HostConfig.PortBindings }}' | grep "$WWWPORT"`
if [ -z "$RESULT" ]; then
break
fi
done
DBPORT=`expr $WWWPORT + 1`
# check if remove/rebuild docker container
# should set same port not new one
REBUILD=0
for VAR in "$@"; do
if [ "$VAR" = "--rebuild" ]; then
REBUILD=1
fi
done
if [ "$REBUILD" -eq "1" ]; then
WPORT=`docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' $1`
if [ -n "$WPORT" ]; then
WWWPORT=$WPORT
fi
MPORT=`docker inspect --format='{{(index (index .NetworkSettings.Ports "3306/tcp") 0).HostPort}}' $1`
if [ -n "$MPORT" ]; then
DBPORT=$MPORT
fi
if [ -n "$WPORT" ] && [ -n "$MPORT" ]; then
RESULT=0
echo -e "Removing container $1 to rebuild new one with exists ports :$WPORT / :$MPORT ..."
QUIET=`docker rm -f $1`
RESULT=$?
sleep 2
if [ $RESULT -eq 0 ]; then
echo -e "Success!\n"
else
echo -e "Error: could not remove container $1, abort"
exit 1
fi
else
echo -e "Error: could not find exists container $1. Remove --rebuild parameter and try again"
echo -e " $0 $1 $2 $3 $4"
exit 1
fi
else
EXISTS=`docker ps -q -f "name=$1" | wc -l`
if [ $EXISTS -eq 1 ]; then
echo -e "Error: exists container $1 detected. Add --rebuild parameter and try again"
echo -e "This commend will update repository to latest verion"
echo -e "This will not touch your civicrm / neticrm / neticrmp project code"
echo -e " $0 $1 $2 $3 $4 --rebuild"
exit 1
fi
fi
cd $MOUNTDIR/neticrm-$MNTDIR
if [ ! -d "$MOUNTDIR/neticrm-$MNTDIR/civicrm" ]; then
RESULT=0
git clone -b develop https://github.com/NETivism/netiCRM.git civicrm
cd civicrm
git clone -b 7.x-develop https://github.com/NETivism/netiCRM-neticrm neticrm
git clone -b 7.x-develop https://github.com/NETivism/netiCRM-drupal drupal
RESULT=$?
cd ..
if [ $RESULT -neq 0 ]; then
echo -e "Error: clone civicrm project failed, abort."
exit 1
fi
fi
if [ ! -d "$MOUNTDIR/neticrm-$MNTDIR/neticrmp" ]; then
git clone -b 7.x-develop git@git.netivism.com.tw:netivism/neticrmp.git neticrmp
RESULT=$?
if [ $RESULT -neq 0 ]; then
echo -e "Error: clone neticrmp project failed, abort."
exit 1
fi
fi
if [ ! -L "/mnt/neticrm-$MNTDIR" ]; then
cd /mnt && ln -s "$MOUNTDIR/neticrm-$MNTDIR"
fi
if [ -n "$3" ] && [ -f "$WORKDIR/container/$3" ]; then
SCRIPT="$WORKDIR/container/$3"
else
SCRIPT="$WORKDIR/container/neticrm-7.sh"
fi
REPOS="netivism/docker-debian-php:develop"
if [ -n "$4" ] && [ "$4" != "--rebuild" ]; then
REPOS=$4
fi
if [[ $REPOS =~ "docker-wheezy-php55" ]]; then
MYSQL_VERSION=""
else
MYSQL_VERSION="103"
fi
if [ -n "$WWWPORT" ] && [ -n "$DBPORT" ]; then
echo -e "Updating to latest repository of $REPOS ..."
docker pull $REPOS
echo -e "Success!\n"
HOSTNAME="${1//\./-}"
HOSTIP=`ip route | grep "docker0" | xargs -n 1 | grep -oE "^172\.17\.[0-9]{1,3}\.[0-9]{1,3}$"`
echo -e "Creating container $1 using $REPOS ..."
sleep 2
docker run -d --name $1 \
--add-host=dockerhost:$HOSTIP \
--restart=unless-stopped \
-h $HOSTNAME \
-p $WWWPORT:80 \
-p $DBPORT:3306 \
-v $MOUNTDIR/www/sites/$1:/var/www/html \
-v $MOUNTDIR/sql/sites/$1:/var/lib/mysql \
-v /etc/localtime:/etc/localtime:ro \
-v $SCRIPT:/init.sh \
-v $MOUNTDIR/neticrm-$MNTDIR:/mnt/neticrm-$MNTDIR \
-e INIT_DB=develop \
-e INIT_PASSWD=123456 \
-e INIT_DOMAIN=$1 \
-e INIT_NAME=develop \
-e INIT_MAIL=$2 \
-e HOST_MAIL=mis@netivism.com.tw \
-e "TZ=Asia/Taipei" \
-w "/var/www/html" \
-i -t $REPOS
RESULT=$?
if [ $RESULT -eq 0 ]; then
echo -e "Success!\n"
else
echo -e "Error: create error, abort"
exit 1
fi
docker cp $WORKDIR/mysql/default$MYSQL_VERSION.cnf $1:/etc/mysql/my.cnf
RESULT=0
QUIET=`docker ps -f "name=$1"`
RESULT=$?
if [ $RESULT -eq 0 ]; then
echo -e "$1 is listen on port $WWWPORT"
fi
fi