This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
stack.sh
executable file
·307 lines (238 loc) · 10.1 KB
/
stack.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env bash
# Script to be used instead of plain docker-compose to build and run the Docker stack
# we allow this to come from shell env if already defined :-)
DOCKER_COMPOSE=${DOCKER_COMPOSE:=docker-compose}
# docker-compose already has an env var existing for this
DOCKER_COMPOSE_FILE=${COMPOSE_FILE:=docker-compose.yml}
# we allow this to come from shell env if already defined :-)
DOCKER_COMPOSE_CONFIG_FILE=${DOCKER_COMPOSE_CONFIG_FILE:=docker-compose.config.sh}
php_available_versions=(5.4 5.6 7 7.1)
available_web_servers=(nginx apache)
usage() {
echo "Usage: ./stack.sh start|stop|rm|php_switch|web_server_switch|purgelogs|update|reset"
}
# copy template yml file to final docker-compose.yml file
buildDockerComposeFile() {
if [ ! -f "$DOCKER_COMPOSE_FILE" ]; then
#choose which template should be used for project
#docker_compose_template_type=${docker_compose_template_type:-nginx}
template_file="docker-compose-template.yml"
#if [ ! -f "$template_file" ]; then
# echo "ERROR: wrong template file specified. Aborting ..."
# exit 1;
#fi
echo "No $DOCKER_COMPOSE_FILE found, copying $template_file ..."
cp "$template_file" "$DOCKER_COMPOSE_FILE"
fi
}
buildDockerComposeLocalEnvFileIfNeeded() {
if [ ! -f 'docker-compose.env.local' ]; then
echo "Generating config file docker-compose.env.local ...";
current_uid=`id -u`
current_gid=`id -g`
echo "DEV_UID=$current_uid" > docker-compose.env.local
echo "DEV_GID=$current_gid" >> docker-compose.env.local
fi
}
configurePhpVersion() {
read -p "[?] Which PHP version do you want to use? 5.4, [5.6], 7 or 7.1: " php_version
php_version=${php_version:-5.6}
if [[ ! " ${php_available_versions[@]} " =~ " ${php_version} " ]]; then
echo "ERROR: unsupported PHP version ${php_version}. Aborting ..."
exit 1;
fi
# Register php version Docker env variable
php_version='php'${php_version/./}
if grep -q DOCKER_PHP_VERSION "$DOCKER_COMPOSE_CONFIG_FILE"; then
sed -i '/DOCKER_PHP_VERSION/c\export DOCKER_PHP_VERSION='$php_version "$DOCKER_COMPOSE_CONFIG_FILE" ;
else
echo "export DOCKER_PHP_VERSION=$php_version" >> $DOCKER_COMPOSE_CONFIG_FILE
fi
# Register php config path Docker env variable
if [[ "$php_version" == 'php7' ]]; then
php_config_path="/etc/php/7.0"
else
if [[ "$php_version" == 'php71' ]]; then
php_config_path="/etc/php/7.1"
else
php_config_path="/etc/php5"
fi
fi
if grep -q DOCKER_PHP_CONF_PATH "$DOCKER_COMPOSE_CONFIG_FILE"; then
sed -i '/DOCKER_PHP_CONF_PATH/c\export DOCKER_PHP_CONF_PATH='$php_config_path "$DOCKER_COMPOSE_CONFIG_FILE" ;
else
echo "export DOCKER_PHP_CONF_PATH=$php_config_path" >> $DOCKER_COMPOSE_CONFIG_FILE
fi
source $DOCKER_COMPOSE_CONFIG_FILE
echo "Selected $php_version as PHP version"
}
configureWebServer() {
source $DOCKER_COMPOSE_CONFIG_FILE
read -p "[?] Which web server do you want to use? [apache] or nginx: " web_server_type
web_server_type=${web_server_type:-apache}
if [[ ! " ${available_web_servers[@]} " =~ " ${web_server_type} " ]]; then
echo "ERROR: unsupported web server ${web_server_type}. Aborting ..."
exit 1;
fi
# Check web_server & php combination
if [[ "$DOCKER_PHP_VERSION" == 'php71' && "$web_server_type" != 'nginx' ]]; then
echo "Sorry, PHP 7.1 is only available with nginx for the moment."
echo "Current PHP version: $DOCKER_PHP_VERSION. Aborting ..."
exit 1;
fi
if grep -q DOCKER_WEB_SERVER "$DOCKER_COMPOSE_CONFIG_FILE"; then
sed -i '/DOCKER_WEB_SERVER/c\export DOCKER_WEB_SERVER='$web_server_type "$DOCKER_COMPOSE_CONFIG_FILE" ;
else
echo "export DOCKER_WEB_SERVER=$web_server_type" >> $DOCKER_COMPOSE_CONFIG_FILE
fi
echo "Selected $web_server_type as web server"
}
# Check if some files mounted as volumes exist, and create them if they are not found
checkRequiredFiles() {
if [ ! -f ~/.gitconfig ]; then
echo "~/.gitconfig file not found. Creating empty file"
touch ~/.gitconfig
if [ ! -f ~/.gitconfig ]; then
echo "~/.gitconfig file can not be created! Aborting ..."
exit 1;
fi
fi
if [ ! -f ~/.ssh/config ]; then
echo "~/.ssh/config file not found. Creating empty file"
touch ~/.ssh/config
if [ ! -f ~/.ssh/config ]; then
echo "~/.ssh/config file can not be created! Aborting ..."
exit 1;
fi
fi
}
buildDockerComposeConfigFileIfNeeded() {
if [ ! -f "$DOCKER_COMPOSE_CONFIG_FILE" ]; then
echo "Generating config file $DOCKER_COMPOSE_CONFIG_FILE ...";
read -p "[?] What is your main project name? " DOCKER_PROJECT_NAME
DOCKER_PROJECT_NAME=${DOCKER_PROJECT_NAME:-myproject}
read -p "[?] Will you use this docker stack for only one project? y/[n]: " site_project
site_project=${site_project:-n}
if [ "$site_project" = "y" ]
then
# Unique site stack
www_root="./site/"
www_dest="/var/www/site/"
# Check if site folder exists, otherwise clone project into site folder
if [ ! -d 'site' ]
then
#echo "'site' folder does not exist, remember to clone your project in there"
# In order to make this better than just letting the dev run git clone, we should check many more things...
# F.e. build the git url automatically using the Git repo if the given url is not full.
read -p "[?] 'site' folder does not exist. Please type the Git full url to clone your project: " git_url
git ls-remote "$git_url" &>-
if [ "$?" -ne 0 ]; then
echo "ERROR: invalid or empty git repository. Aborting ..."
exit 1;
fi
git clone $git_url site
fi
else
# Multi site stack
read -p "[?] Full path of your projects on your host machine [/home/$(whoami)/www]: " www_root
www_root=${www_root:-/home/$(whoami)/www}
if [ ! -d "$www_root" ]; then
echo "Root directory $www_root does not exist! Aborting ..."
exit ;
fi
www_dest="/var/www"
fi
# Ask for storage mountpoints
read -p "[?] Path to your ezpublish storages on host [/mnt/\$USER]: " storage_local_path
storage_local_path=${storage_local_path:-/mnt/\$USER}
echo "Your local storage folder will be mounted in '/mnt/$USER' inside containers"
echo "(Don't forget to symlink your storage in your ez5 instance after first run)"
# Ask for timezone for docker args (needs docker-compsoe v2 format)
read -p "[?] Current timezone [Europe/Paris]: " timezone
timezone=${timezone:-Europe/Paris}
echo "Writing timezone to PHP config ..."
echo -e "[Date]\ndate.timezone=$timezone" > config/cli/php5/timezone.ini
echo -e "[Date]\ndate.timezone=$timezone" > config/apache/php5/timezone.ini
echo -e "[Date]\ndate.timezone=$timezone" > config/nginx/php/timezone.ini
# Ask for custom vcl file path
read -p "[?] Path to Varnish vcl file [./config/varnish/ez54.vcl]: " vcl_filepath
vcl_filepath=${vcl_filepath:-./config/varnish/ez54.vcl}
# Ask for custom solr conf folder path
read -p "[?] Path to solr configuration folder [./config/solr]: " solr_conf_path
solr_conf_path=${solr_conf_path:-./config/solr}
# Solr tag hard coded for now..
solr_image_tag="ezfind"
# Save all env vars in a file that will be included at every call
echo "# in this file we define all env variables used by docker-compose.yml" > $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_WWW_ROOT=$www_root" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_WWW_DEST=$www_dest" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_PROJECT_NAME=$DOCKER_PROJECT_NAME" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_VARNISH_VCL_FILE=$vcl_filepath" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_SOLR_CONF_PATH=$solr_conf_path" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_STORAGE_LOCAL_PATH=$storage_local_path" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_STORAGE_MOUNT_POINT=/mnt/\$USER" >> $DOCKER_COMPOSE_CONFIG_FILE
echo "export DOCKER_SOLR_VERSION=$solr_image_tag" >> $DOCKER_COMPOSE_CONFIG_FILE
#Configure PHP version
configurePhpVersion
configureWebServer
fi
}
purgeLogs() {
# q: why are we limiting to .log files and depth ?
echo "Deleting existing log files:"
find logs/ -maxdepth 2 -name "*.log*"
find logs/ -maxdepth 2 -name "*.log*" -delete
}
update() {
git pull
}
# ### Live code starts here ###
buildDockerComposeFile
buildDockerComposeConfigFileIfNeeded
buildDockerComposeLocalEnvFileIfNeeded
checkRequiredFiles
source $DOCKER_COMPOSE_CONFIG_FILE
case "$1" in
start|run)
if [ ! $DOCKER_PHP_VERSION ]; then
configurePhpVersion
fi
if [ ! $DOCKER_WEB_SERVER ]; then
configureWebServer
fi
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" down
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" up -d
;;
stop)
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" stop
;;
rm)
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" rm --force
;;
php_switch)
configurePhpVersion
;;
web_server_switch)
configureWebServer
;;
reset)
rm $DOCKER_COMPOSE_CONFIG_FILE
rm docker-compose.env.local
rm $DOCKER_COMPOSE_FILE
;;
purgelogs)
purgeLogs
;;
update)
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" down
update
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" up -d
;;
'')
usage
exit
;;
*)
# any other variation, let it go directly through to docker-compose
$DOCKER_COMPOSE -p "$DOCKER_PROJECT_NAME" $@
esac