1
1
#! /usr/bin/env bash
2
2
3
3
PROJECT_NAME=" ee4"
4
- EE_INSTALL_DIR=" /opt/$PROJECT_NAME "
5
4
ETC_HOSTS=/etc/hosts
6
5
LOCALHOST_IP=" 127.0.0.1"
7
6
7
+ # initOS discovers the operating system for this system.
8
+ initOS () {
9
+ OS=$( echo ` uname` | tr ' [:upper:]' ' [:lower:]' )
10
+ }
11
+
8
12
# runs the given command as root (detects if we are root already)
9
13
runAsRoot () {
10
14
local CMD=" $* "
@@ -18,15 +22,17 @@ runAsRoot() {
18
22
19
23
# help provides possible cli arguments
20
24
help () {
21
- echo -e " \nUsage: ee4 [Global Options]"
25
+ echo -e " \nUsage: ee4 [Global Options|Commands ]"
22
26
echo -e " \nGlobal Options:"
23
27
echo -e " \t[--help|-h ]\t\tprints this help"
24
28
25
29
echo -e " \nCommands: "
26
30
echo -e " \tcreate\t\t\tCreate new site"
31
+ echo -e " \tdb\t\t\tEnter mysql shell"
27
32
echo -e " \tdelete\t\t\tDelete existing site"
28
33
echo -e " \trestart\t\t\tReload configuration for existing site"
29
34
echo -e " \twp\t\t\tUse wp-cli with a site"
35
+ echo -e " \tupdate\t\t\tUpdate ee4"
30
36
31
37
echo -e " \nUsage: ee4 create SITE [Options] [Suboptions]"
32
38
echo -e " \nOptions:"
@@ -42,12 +48,21 @@ help () {
42
48
echo -e " \te.g. ee4 wp example.com theme list"
43
49
}
44
50
45
- singleWordPress () {
46
- echo " Installing WordPress site $SITE_NAME "
51
+ createWebroot ()
52
+ {
47
53
if ! [[ -d " $WEBROOT " ]]; then
48
- runAsRoot mkdir -p " $WEBROOT " > /dev/null 2>&1
54
+ mkdir -p " $WEBROOT " > /dev/null 2>&1
55
+ if [ " $? " -eq 1 ]; then
56
+ runAsRoot mkdir -p " $WEBROOT " > /dev/null 2>&1
57
+ runAsRoot chown -R " $USER " : " $WEBROOT "
58
+ fi
49
59
fi
60
+ }
61
+
62
+ singleWordPress () {
63
+ createWebroot
50
64
65
+ echo " Installing WordPress site $SITE_NAME "
51
66
# ##
52
67
# Setup site configuration
53
68
# ##
@@ -57,13 +72,15 @@ singleWordPress() {
57
72
cp -r " $EE_INSTALL_DIR /config" " $WEBROOT /$SITE_NAME "
58
73
mv " $WEBROOT /$SITE_NAME /config/.env.example" " $WEBROOT /$SITE_NAME /.env"
59
74
sed -i.bak " s/\(VIRTUAL_HOST=\)\(site1.test\)/\1$SITE_NAME /" " $WEBROOT /$SITE_NAME /.env"
75
+ sed -i.bak " /^.*LETSENCRYPT.*/d" " $WEBROOT /$SITE_NAME /docker-compose.yml"
76
+ find " $WEBROOT /$SITE_NAME " -mindepth 1 -maxdepth 3 -name ' *.bak' -exec rm {} \;
60
77
61
78
# ##
62
79
# Setup site networking
63
80
# ##
64
81
echo " Configuring network..." && sleep 1
65
82
runNginxProxyAndLetsEncrypt
66
- docker network create $SITE_NAME
83
+ docker network create $SITE_NAME > /dev/null 2>&1
67
84
docker network connect $SITE_NAME nginx-proxy
68
85
69
86
# ##
@@ -77,12 +94,13 @@ singleWordPress() {
77
94
read -p " Would you like to add domain to /etc/host?(y/N) " -n 1 -r
78
95
if [[ $REPLY =~ ^[Yy]$ ]]; then
79
96
addHost " $SITE_NAME "
97
+ addHost " mail.$SITE_NAME "
80
98
fi
81
99
}
82
100
83
101
singleWordPressLetsEncrypt () {
102
+ createWebroot
84
103
echo " Installing WordPress site $SITE_NAME "
85
- mkdir -p " $WEBROOT " > /dev/null 2>&1
86
104
87
105
# ##
88
106
# Setup site configuration
@@ -93,13 +111,14 @@ singleWordPressLetsEncrypt() {
93
111
cp -r " $EE_INSTALL_DIR /config" " $WEBROOT /$SITE_NAME "
94
112
mv " $WEBROOT /$SITE_NAME /config/.env.example" " $WEBROOT /$SITE_NAME /.env"
95
113
sed -i.bak " s/\(VIRTUAL_HOST=\)\(site1.test\)/\1$SITE_NAME /" " $WEBROOT /$SITE_NAME /.env"
114
+ find " $WEBROOT /$SITE_NAME " -mindepth 1 -maxdepth 3 -name ' *.bak' -exec rm {} \;
96
115
97
116
# ##
98
117
# Setup site networking
99
118
# ##
100
119
echo " Configuring network..." && sleep 1
101
120
runNginxProxyAndLetsEncrypt
102
- docker network create $SITE_NAME
121
+ docker network create $SITE_NAME > /dev/null 2>&1
103
122
docker network connect $SITE_NAME nginx-proxy
104
123
docker network connect $SITE_NAME letsencrypt
105
124
@@ -114,6 +133,7 @@ singleWordPressLetsEncrypt() {
114
133
read -p " Would you like to add domain to /etc/host?(y/N) " -n 1 -r
115
134
if [[ $REPLY =~ ^[Yy]$ ]]; then
116
135
addHost " $SITE_NAME "
136
+ addHost " mail.$SITE_NAME "
117
137
fi
118
138
}
119
139
@@ -139,6 +159,7 @@ deleteSingleWordPress() {
139
159
fi
140
160
docker network rm " $SITE_NAME "
141
161
fi
162
+ removeHost " $SITE_NAME "
142
163
}
143
164
144
165
restartSingleWordPress () {
@@ -153,10 +174,15 @@ runWpCliCommand() {
153
174
popd > /dev/null 2>&1
154
175
}
155
176
177
+ useDbShell () {
178
+ pushd " $WEBROOT /$SITE_NAME " > /dev/null 2>&1
179
+ docker-compose exec db sh -c ' mysql -p$MYSQL_ROOT_PASSWORD'
180
+ popd > /dev/null 2>&1
181
+ }
182
+
156
183
removeHost () {
157
184
HOSTNAME=$1
158
- if [ -n " $( grep $HOSTNAME /etc/hosts) " ]
159
- then
185
+ if [ -n " $( grep $HOSTNAME /etc/hosts) " ]; then
160
186
echo " $HOSTNAME Found in your $ETC_HOSTS , Removing now..." ;
161
187
sudo sed -i" .bak" " /$HOSTNAME /d" $ETC_HOSTS
162
188
else
@@ -188,8 +214,13 @@ portsFree() {
188
214
free=0
189
215
for port in " ${ports[@]} " ; do
190
216
# count the number of occurrences of $port in output: 1 = in use; 0 = not in use
191
- result=$( ss -ln src :$port | grep -Ec -e " \<$port \>" )
192
- if [ " $result " -eq 1 ]; then
217
+ if [[ " $OS " == ' linux' ]]; then
218
+ checkPortCMD=" netstat -lnp tcp | grep "
219
+ elif [[ " $OS " == ' darwin' ]]; then
220
+ checkPortCMD=" netstat -anp tcp | grep LISTEN | grep "
221
+ fi
222
+ runAsRoot $checkPortCMD $port > /dev/null 2>&1
223
+ if [ " $? " -eq 1 ]; then
193
224
free=1
194
225
fi
195
226
done
@@ -229,11 +260,11 @@ runNginxProxyAndLetsEncrypt() {
229
260
echo " Please make sure ports 80 and 443 are free."
230
261
else
231
262
docker run --name nginx-proxy --restart always -d -p 80:80 -p 443:443 \
232
- -v / etc/nginx/htpasswd :/etc/nginx/htpasswd \
233
- -v / etc/nginx/certs :/etc/nginx/certs \
234
- -v / etc/nginx/vhost.d \
235
- -v /usr/share /nginx/html \
236
- -v /etc/ nginx/conf.d:/etc/ nginx/conf.d \
263
+ -v ~ /.ee4/ etc/nginx/certs :/etc/nginx/certs \
264
+ -v ~ /.ee4/ etc/nginx/conf.d :/etc/nginx/conf.d \
265
+ -v ~ /.ee4/ etc/nginx/htpasswd:/etc/nginx/htpasswd \
266
+ -v ~ /.ee4/etc /nginx/vhost.d:/etc/nginx/vhost.d \
267
+ -v ~ /.ee4/usr/share/ nginx/html:/usr/share/ nginx/html \
237
268
-v /var/run/docker.sock:/tmp/docker.sock:ro \
238
269
jwilder/nginx-proxy
239
270
@@ -245,55 +276,45 @@ runNginxProxyAndLetsEncrypt() {
245
276
fi
246
277
}
247
278
248
- # fail_trap is executed if an error occurs.
249
- fail_trap () {
250
- result=$?
251
- if [ " $result " != " 0" ]; then
252
- if [[ -n " $INPUT_ARGUMENTS " ]]; then
253
- echo " Failed to run with the arguments provided: $INPUT_ARGUMENTS "
254
- help
255
- else
256
- help
257
- fi
258
- echo -e " \tFor support, go to https://github.com/EasyEngine/docker-compose-wordpress."
259
- fi
260
- exit $result
261
- }
262
-
263
279
initEE4 () {
264
280
if ! [[ -d ~ /.ee4 ]]; then
265
281
mkdir ~ /.ee4
266
282
fi
267
283
268
284
if [[ $# -eq 0 ]]; then
269
- read -p " Where would you like to create your sites(default: ~/Documents/ Sites): "
270
- WEBROOT=" ${REPLY:- ~/ Documents / Sites} "
285
+ read -p " Where would you like to create your sites(default: ~/Sites): "
286
+ WEBROOT=" ${REPLY:- ~/ Sites} "
271
287
elif [[ $# -eq 1 ]]; then
272
288
WEBROOT=" $1 "
273
289
fi
274
290
275
- echo " WEBROOT=$WEBROOT " > ~ /.ee4/config
291
+ echo " WEBROOT=$WEBROOT " >> ~ /.ee4/config
276
292
}
277
293
278
294
updateEE4 () {
279
295
REPO_URL=" ${1:- https:// github.com/ EasyEngine/ docker-compose-wordpress} "
280
296
VERSION=" ${2:- master} "
297
+ EE_TMP_ROOT=" $( mktemp -dt ee-installer-XXXXXX) "
298
+ EE_TMP_REPO=" $EE_TMP_ROOT /docker-compose-wordpress"
281
299
282
- echo " git pull -b " $VERSION " " $REPO_URL " "
283
-
284
- pushd " $EE_INSTALL_DIR " > /dev/null 2>&1
285
- git pull " $REPO_URL " " $VERSION "
286
- popd > /dev/null 2>&1
300
+ echo " Updating ee4"
301
+ git clone -b " $VERSION " " $REPO_URL " " $EE_TMP_REPO "
302
+ runAsRoot cp -r " $EE_TMP_REPO /*" " $EE_INSTALL_DIR "
303
+ rm -rf " $EE_TMP_ROOT "
287
304
}
288
305
289
306
# Execution
290
307
291
- # Stop execution on any error
292
- trap " fail_trap" EXIT
293
- set -e
294
-
295
- [[ -f ~ /.ee4/config ]] && source ~ /.ee4/config || initEE4
308
+ if [[ -f ~ /.ee4/config ]]; then
309
+ grep ' WEBROOT' ~ /.ee4/config > /dev/null 2>&1
310
+ if [[ $? -eq 0 ]]; then
311
+ source ~ /.ee4/config
312
+ else
313
+ initEE4
314
+ fi
315
+ fi
296
316
317
+ initOS
297
318
# Parsing input arguments (if any)
298
319
export INPUT_ARGUMENTS=" ${@ } "
299
320
set -u
@@ -355,6 +376,17 @@ while [[ $# -gt 0 ]]; do
355
376
exit 0
356
377
fi
357
378
;;
379
+ ' db' )
380
+ shift
381
+ if [[ $# -ne 0 ]]; then
382
+ export SITE_NAME=" ${1} "
383
+ useDbShell
384
+ else
385
+ echo -e " Please provide name of site."
386
+ exit 0
387
+ fi
388
+ ;;
389
+
358
390
' init' )
359
391
shift
360
392
if [[ $# -ne 0 ]]; then
0 commit comments