-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.sh
executable file
·351 lines (259 loc) · 8.55 KB
/
cluster.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash -e
# Local config
SCRIPT_PATH=`readlink -m $0`
CLUSTER_SCRIPT_ROOT="$( cd "$( dirname "$SCRIPT_PATH" )" && pwd )"
CALL_ROOT="$( cd "$( dirname "$0" )" && pwd )"
CLUSTER_ROOT=`cd "$CALL_ROOT"; pwd`
if [ ! -d $CALL_ROOT/instances ]; then
CLUSTER_ROOT=`cd "$CALL_ROOT/../.."; pwd`
fi
if [ -f $CALL_ROOT/config.sh ]; then
source $CALL_ROOT/config.sh
fi
# Global config and lib
source $CLUSTER_SCRIPT_ROOT/lib.sh
# Initialize mysql, elasticsearch
function create {
local DIR=${1-.}
mkdir -p $DIR
DIR=`expand_path $DIR`
CLUSTER_NAME=${2-`pwgen 6 1`}
ln -sfn $CALL_ROOT/cluster.sh $DIR/cluster.sh
mkdir -p $DIR/mysql
mkdir -p $DIR/elastic
mkdir -p $DIR/ssmtp
mkdir -p $DIR/nginx
tpl $CLUSTER_SCRIPT_ROOT/templates/ssmtp.conf $DIR/ssmtp/ssmtp.conf
tpl $CLUSTER_SCRIPT_ROOT/templates/revaliases $DIR/ssmtp/revaliases
tpl $CLUSTER_SCRIPT_ROOT/templates/nginx.conf $DIR/nginx/nginx.conf
mkdir -p $DIR/elastic/log
mkdir -p $DIR/elastic/data
mkdir -p $DIR/elastic/work
mkdir -p $DIR/elastic/plugins
DB_USERNAME=root
DB_PASSWORD=`generate_password`
tpl $CLUSTER_SCRIPT_ROOT/templates/service_config.sh $DIR/config.sh
tpl $CLUSTER_SCRIPT_ROOT/templates/mysql.cnf $DIR/mysql.cnf
}
# Start mysql, elasticsearch
function generate_vhost {
INSTANCE_NAME=$1
TARGET=$CLUSTER_ROOT/nginx/vhosts/$INSTANCE_NAME.conf
source $CLUSTER_ROOT/instances/$INSTANCE_NAME/config.sh
tpl $CLUSTER_SCRIPT_ROOT/templates/vhost.conf $TARGET
}
function start_proxy {
mkdir -p $CLUSTER_ROOT/nginx/vhosts
rm -f $CLUSTER_ROOT/nginx/vhosts/*.conf
LINK_PARAM=""
for INSTANCE_NAME in $CLUSTER_ROOT/instances/* ; do
INSTANCE_NAME=$(basename $INSTANCE_NAME)
generate_vhost $INSTANCE_NAME
LINK_PARAM="$LINK_PARAM --link ${CLUSTER_NAME}_instance_${INSTANCE_NAME}:${INSTANCE_NAME}"
done
sudo docker run -d \
--name ${CLUSTER_NAME}_nginx \
-v $CLUSTER_ROOT/nginx:/etc/nginx \
-p $CLUSTER_PORT:$CLUSTER_PORT \
$LINK_PARAM \
--restart unless-stopped \
nginx
}
function stop_proxy {
sudo docker stop ${CLUSTER_NAME}_nginx
sudo docker rm ${CLUSTER_NAME}_nginx
}
function boot {
SSMTP_TPL_CHECKSUM=`sha1sum $CLUSTER_SCRIPT_ROOT/templates/ssmtp.conf | cut -d" " -f 1`
SSMTP_CHECKSUM=`sha1sum $CALL_ROOT/ssmtp/ssmtp.conf | cut -d" " -f 1`
if [ "$SSMTP_TPL_CHECKSUM" = "$SSMTP_CHECKSUM" ]; then
echo "Your SSMTP config file wasn't changed!"
echo "Please adapt it to a working configuration before using this cluster"
exit 1
fi
sudo docker run -d \
--name ${CLUSTER_NAME}_mysql \
--volume $CALL_ROOT/mysql:/var/lib/mysql \
--env MYSQL_ROOT_PASSWORD=$DB_PASSWORD \
--restart unless-stopped \
mysql
sudo docker run -d \
--name ${CLUSTER_NAME}_elastic \
--volume $CALL_ROOT/elastic:/data \
--restart unless-stopped \
elasticsearch:2.2.1
}
function shutdown {
sudo docker stop ${CLUSTER_NAME}_mysql ${CLUSTER_NAME}_elastic
sudo docker rm ${CLUSTER_NAME}_mysql ${CLUSTER_NAME}_elastic
}
# Create a new instance
function new {
NAME=${1-`pwgen 6 1`}
local DIR=$CALL_ROOT/instances/$NAME
mkdir -p $DIR
ln -sfn $CALL_ROOT/cluster.sh $DIR/instance.sh
echo "$VERSION" > $DIR/version.txt
mkdir -p $DIR/data
mkdir -p $DIR/log
touch $DIR/kor.app.yml
KOR_DB_NAME="kor_$NAME"
KOR_DB_USERNAME="kor_$NAME"
KOR_DB_PASSWORD=`pwgen 20 1`
db "grant all on $KOR_DB_NAME.* to '$KOR_DB_USERNAME'@'%' identified by '$KOR_DB_PASSWORD'"
DB_USERNAME=$KOR_DB_USERNAME
DB_PASSWORD=$KOR_DB_PASSWORD
DB_NAME=$KOR_DB_NAME
tpl $CLUSTER_SCRIPT_ROOT/templates/database.yml $DIR/database.yml
tpl $CLUSTER_SCRIPT_ROOT/templates/mysql.cnf $DIR/mysql.cnf
tpl $CLUSTER_SCRIPT_ROOT/templates/config.sh $DIR/config.sh
tpl $CLUSTER_SCRIPT_ROOT/templates/kor.yml $DIR/kor.yml
tpl $CLUSTER_SCRIPT_ROOT/templates/contact.txt.example $DIR/contact.txt
tpl $CLUSTER_SCRIPT_ROOT/templates/help.yml.example $DIR/help.yml
tpl $CLUSTER_SCRIPT_ROOT/templates/legal.txt.example $DIR/legal.txt
$DIR/instance.sh init
}
# Establish database connection (and run SQL statement)
function db {
local SQL=$1
if [ -z "$SQL" ]; then
sudo docker run --rm -ti \
--link ${CLUSTER_NAME}_mysql:mysql \
--volume $CALL_ROOT:/host \
mysql \
mysql --defaults-extra-file=/host/mysql.cnf
fi
if [ ! -z "$SQL" ]; then
sudo docker run --rm \
--link ${CLUSTER_NAME}_mysql:mysql \
--volume $CALL_ROOT:/host \
mysql \
mysql --defaults-extra-file=/host/mysql.cnf -e "$SQL"
fi
}
# Dump the instance database to the instance dir
function snapshot {
local TS=`date +"%Y%m%d_%H%M%S"`
local DIR=$CALL_ROOT/../../snapshots
mkdir -p $DIR
DIR=`expand_path $DIR`
stop || true
sudo docker run --rm \
--link ${CLUSTER_NAME}_mysql:mysql \
--volume $CALL_ROOT:/host \
mysql \
mysqldump --defaults-extra-file=/host/mysql.cnf $DB_NAME \
| gzip -c > $CALL_ROOT/db.sql.gz
tar czf $DIR/$NAME.$TS.$VERSION.tar.gz -C $CALL_ROOT --exclude=instance.sh --exclude=config.sh --exclude=database.yml --exclude=mysql.cnf .
rm $CALL_ROOT/db.sql.gz
start
}
# Import the dump from the instance dir to the database
function import {
local FILE=$1
stop || true
mv $CALL_ROOT $CALL_ROOT.old
mkdir -p $CALL_ROOT
# sudo rm -rf $CALL_ROOT/data $CALL_ROOT/log
tar xzf $FILE -C $CALL_ROOT/
mv $CALL_ROOT.old/{instance.sh,config.sh,database.yml,mysql.cnf} $CALL_ROOT/
zcat $CALL_ROOT/db.sql.gz | sudo docker run --rm -i \
--link ${CLUSTER_NAME}_mysql:mysql \
--volume $CALL_ROOT:/host \
mysql \
mysql --defaults-extra-file=/host/mysql.cnf $DB_NAME
rm $CALL_ROOT/db.sql.gz
rm -rf $CALL_ROOT.old
start
}
# Upgrade
function upgrade {
local TO=$1
stop || true
sed -i -E "s/^VERSION\=.*$/VERSION=$TO/" $CALL_ROOT/config.sh
VERSION=$TO
migrate
start
}
# KOR command
function run {
local COMMAND="$1"
sudo docker run --rm -ti \
--volume $CALL_ROOT:/opt/kor/shared \
--volume $CLUSTER_ROOT/ssmtp:/etc/ssmtp \
--link ${CLUSTER_NAME}_mysql:mysql \
--link ${CLUSTER_NAME}_elastic:elastic \
--add-host dockerhost:`docker_host_ip` \
docker.coneda.net:443/kor:$VERSION \
/bin/bash -c "$COMMAND" kor
}
function run_headless {
local COMMAND="$1"
sudo docker run --rm \
--attach STDOUT --attach STDERR \
--volume $CALL_ROOT:/opt/kor/shared \
--volume $CLUSTER_ROOT/ssmtp:/etc/ssmtp \
--link ${CLUSTER_NAME}_mysql:mysql \
--link ${CLUSTER_NAME}_elastic:elastic \
--add-host dockerhost:`docker_host_ip` \
docker.coneda.net:443/kor:$VERSION \
/bin/bash -c "$COMMAND" kor
}
function job {
local COMMAND="$1"
sudo docker run --rm \
--volume $CALL_ROOT:/opt/kor/shared \
--volume $CLUSTER_ROOT/ssmtp:/etc/ssmtp \
--link ${CLUSTER_NAME}_mysql:mysql \
--link ${CLUSTER_NAME}_elastic:elastic \
--add-host dockerhost:`docker_host_ip` \
docker.coneda.net:443/kor:$VERSION \
/bin/bash -c "$COMMAND" kor
}
# Migrate the instance
function migrate {
run_headless "bundle exec rake db:migrate"
run_headless "bundle exec rails r 'Kor::Elastic.drop_index ; Kor::Elastic.create_index'"
run_headless "bundle exec bin/kor index-all"
}
# Init the instance
function init {
run_headless "bundle exec rake db:create db:setup"
run_headless "bundle exec rails r 'Kor::Elastic.drop_index ; Kor::Elastic.create_index'"
run_headless "bundle exec bin/kor index-all"
}
# Start the instance
function start {
sudo docker run -d \
--name ${CLUSTER_NAME}_bg_$NAME \
--volume $CALL_ROOT:/opt/kor/shared \
--volume $CLUSTER_ROOT/ssmtp:/etc/ssmtp \
--link ${CLUSTER_NAME}_mysql:mysql \
--link ${CLUSTER_NAME}_elastic:elastic \
--add-host dockerhost:`docker_host_ip` \
--restart unless-stopped \
docker.coneda.net:443/kor:$VERSION \
/bin/bash -c "bundle exec bin/delayed_job -n 2 run" kor
sudo docker run -d \
--name ${CLUSTER_NAME}_instance_$NAME \
--volume $CALL_ROOT:/opt/kor/shared \
--volume $CLUSTER_ROOT/ssmtp:/etc/ssmtp \
--link ${CLUSTER_NAME}_mysql:mysql \
--link ${CLUSTER_NAME}_elastic:elastic \
--env SECRET_KEY_BASE=`pwgen 100 1` \
--env RAILS_SERVE_STATIC_FILES=true \
--add-host dockerhost:`docker_host_ip` \
--publish $PORT:8000 \
--restart unless-stopped \
docker.coneda.net:443/kor:$VERSION \
/bin/bash -c "bundle exec puma -e production -p 8000 -t 2:2 config.ru" kor
}
# Stop the instance
function stop {
sudo docker stop ${CLUSTER_NAME}_instance_$NAME
sudo docker stop ${CLUSTER_NAME}_bg_$NAME
sudo docker rm ${CLUSTER_NAME}_instance_$NAME
sudo docker rm ${CLUSTER_NAME}_bg_$NAME
}
# Run
$1 "${@:2}"