-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdocker_functions
executable file
·418 lines (360 loc) · 15.1 KB
/
docker_functions
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/bash
######################################################################################
# Copyright EnterpriseDB Cooperation
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of PostgreSQL nor the names of its contributors
# may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Vibhor Kumar
# E-mail ID: [email protected]
######################################################################################
# quit on any error
# verify any undefined shell variables
set -u
### The three DOCKER_* environment variables below should be commented out when not
### using docker-machine or boot2docker
export DOCKER_HOST=tcp://$(/usr/local/bin/boot2docker ip 2>/dev/null):2376
export DOCKER_CERT_PATH=/Users/${USER}/.boot2docker/certs/boot2docker-vm
export DOCKER_TLS_VERIFY=1
PGPORT=5432
PGHOME=/usr/ppas-9.4
PSQL=${PGHOME}/bin/psql
PPFM_PORT=5052
SSH_PORT=22
PEM_PORT=80
PGUSER=postgres
PG_VERSION=9.4
PGDATA="/var/lib/ppas/9.4/data"
export PSQL PPFM_PORT PGHOME PGPORT
alias ssh='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
######################################################################################
# function: print messages with process id
######################################################################################
function plog()
{
echo "PID: $$ [RUNTIME: $(date +'%m-%d-%y %H:%M:%S')]: $*" >&2
}
######################################################################################
# function: exit_error
######################################################################################
function exit_error()
{
plog "ERROR: $*"
exit 1
}
######################################################################################
# function: print_info
######################################################################################
function pinfo()
{
plog ""
plog "INFO: $*"
plog ""
}
######################################################################################
# function: exit_success
######################################################################################
function exit_success()
{
plog "SUCCESS: $*"
exit 0
}
######################################################################################
# function: if_error
######################################################################################
function if_error
{
typeset rc=$1
shift
typeset msg="$*"
if [[ ${rc} -ne 0 ]]
then
exit_error "$msg; rc=${rc}"
else
return 0
fi
}
######################################################################################
# function: is_container_exists
######################################################################################
function is_container_exists()
{
local C_NAME="$1"
local c_exists=$(docker inspect --format='{{(index .ID) }}' ${C_NAME} 2>&1)
if_error "$?" "${c_exists}"
}
######################################################################################
# function: get container image id
######################################################################################
function container_id()
{
local C_NAME="$1"
local container_id=$(docker inspect --format='{{(index .ID) }}' ${C_NAME} 2>&1)
if_error "$?" "${container_id}"
echo "${container_id}"
}
######################################################################################
# function: get container port number
######################################################################################
function container_port()
{
local C_NAME="$1"
local container_port=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "22/tcp") 0).HostPort}}' ${C_NAME} 2>&1)
if_error "$?" "${container_port}"
echo "${container_port}"
}
######################################################################################
# function: get container ip address
######################################################################################
function container_ipaddr ()
{
local C_NAME="$1"
local container_ip=$(docker inspect --format='{{.NetworkSettings.IPAddress}}' ${C_NAME} 2>&1)
if_error "$?" "${container_ip}"
echo "${container_ip}"
}
######################################################################################
# function: list_containers
######################################################################################
function list_containers ()
{
docker ps -a
}
######################################################################################
# function: list_containers
######################################################################################
function container_status
{
local C_NAME="$1"
local container_status=$( docker inspect --format='{{ (index .State.Running)}}' "${C_NAME}" 2>&1)
if_error $? "${container_status}"
echo ${container_status}
}
######################################################################################
# function: list_images
######################################################################################
function list_images ()
{
docker images
}
######################################################################################
# function: is_images_exists
######################################################################################
function is_image_exists ()
{
local I_NAME="$1"
local i_exists=$(docker inspect --format='{{(index .id) }}' ${C_NAME} 2>&1)
if_error "$?" "${i_exists}"
}
######################################################################################
# function: image_id
######################################################################################
function image_id()
{
local I_NAME="$1"
local image_id=$(docker inspect --format='{{(index .id) }}' ${C_NAME} 2>&1)
if_error "$?" "${image_id}"
echo "${image_id}"
}
######################################################################################
# function: ssh-to-container
######################################################################################
function ssh_to_container ()
{
# local C_USER="$1"
local C_NAME="$1"
# port=$(container_port ${C_NAME} 2>&1)
# if_error "$?" "${port}"
# ssh -p $port ${C_USER}@localhost
docker exec -it ${C_NAME} "/bin/bash"
}
######################################################################################
# function: execute commands on container
######################################################################################
function exec_on_container ()
{
C_USER="$1"
C_NAME="$2"
shift 2
local COMMANDS="$@"
port=$(container_port ${C_NAME} 2>&1)
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p $port "${C_USER}"@localhost "${COMMANDS}"
}
######################################################################################
# function: create_container
######################################################################################
function create_container ()
{
local C_NAME="$1"
local I_NAME="$2"
messg=$(docker run --privileged=true --publish-all=true --interactive=false --tty=true -v /Users/${USER}/Desktop:/Desktop --hostname=${C_NAME} --detach=true --name=${C_NAME} ${I_NAME} 2>&1)
#if_error "$?" "${messg}"
echo "$?" "${messg}"
plog "${C_NAME} created successfully."
}
######################################################################################
# function: destroy_container
######################################################################################
function destroy_containers ()
{
local C_NAMES="$@"
local messg=$(docker kill "${C_NAMES}" 2>&1)
if_error "$?" "${messg}"
local messg=$(docker rm "${C_NAMES}" 2>&1)
if_error "$?" "${messg}"
plog "${C_NAMES} destroy successfully."
}
######################################################################################
# function: start_container
######################################################################################
function start_containers ()
{
local C_NAMES="$@"
local messg=$(docker start ${C_NAMES} 2>&1)
if_error "$?" "${messg}"
plog "${C_NAMES} started successfully."
}
######################################################################################
# function: stop_containers
######################################################################################
function stop_container ()
{
local C_NAMES="$@"
local messg=$(docker stop --time=10 ${C_NAMES} 2>&1)
if_error "$?" "${messg}"
plog "${C_NAMES} stopped successfully."
}
######################################################################################
# function: create docker file for ppfm_test
######################################################################################
function ppas93_base_dockerfile()
{
echo "FROM centos:6.4"
echo "MAINTAINER [email protected]"
echo "RUN yum update -y"
echo "RUN yum install -y which"
echo "RUN yum install -y perl"
echo "RUN yum install -y python-paramiko"
echo "RUN yum install -y vim"
echo "ADD id_rsa.pub /tmp/pub_key"
echo "RUN mkdir -p ~root/.ssh"
echo "RUN cat /tmp/pub_key >> ~root/.ssh/authorized_keys"
echo "RUN chown -R root:root ~root/.ssh/; chmod -R 0700 ~root/.ssh/"
echo "RUN yum install -y openssh-server"
echo "RUN yum install -y openssh-clients"
echo "RUN service sshd start && service sshd stop"
echo "RUN perl -i -pe 's|^UsePAM yes|UsePAM no|' /etc/ssh/sshd_config"
echo "ADD ppas.repo /etc/yum.repos.d/ppas.repo"
echo "RUN yum install -y ppas93"
echo "RUN userdel enterprisedb"
echo "RUN adduser --home-dir /home/postgres --create-home postgres"
echo "ADD ppas_bash_profile.sh ~postgres/.bash_profile"
echo "RUN chown -R postgres:postgres ~postgres/.bash_profile"
echo "ADD ppas93_initd.sh /etc/init.d/ppas-9.4"
echo "RUN chown -R postgres:postgres /var/lib/ppas"
echo "RUN service ppas-9.4 initdb"
echo "ADD pgconf/pg_hba.conf /var/lib/ppas/9.4/data/pg_hba.conf"
echo "ADD pgconf/postgresql.conf /var/lib/ppas/9.4/data/postgresql.conf"
echo "RUN chown postgres:postgres /var/lib/ppas/9.4/data/postgresql.conf"
echo "RUN chown postgres:postgres /var/lib/ppas/9.4/data/pg_hba.conf"
echo "RUN mkdir -p ~postgres/.ssh"
echo "RUN cat /tmp/pub_key >> ~postgres/.ssh/authorized_keys"
echo "RUN chown -R postgres:postgres ~postgres/.ssh/; chmod -R 0700 ~postgres/.ssh/"
echo "EXPOSE 5432"
echo "EXPOSE 22"
echo "EXPOSE 5052"
echo "EXPOSE 80"
echo "CMD service ppas-9.4 start ; echo starting SSH server in foreground; /usr/sbin/sshd -D"
}
#######################################################################################
# build image function
######################################################################################
function build_image()
{
local IMAGE_NAME=$1
local DIRECTORY=$2
docker build --force-rm=true --rm=true --tag=${IMAGE_NAME} ${DIRECTORY}
}
#######################################################################################
# remove broken containers
######################################################################################
function remove_broken_containers()
{
echo "The following containers will be removed (giving you 10sec to ctrl-C and abort)":
docker ps -a | grep Exited | grep -v '(0)'
sleep 10
docker ps -a | grep Exited | grep -v '(0)' | awk '{ print $1 }' | xargs docker rm
}
#######################################################################################
# remove broken images
######################################################################################
function remove_broken_images()
{
echo "The following images will be removed (giving you 10sec to ctrl-C and abort)":
docker images | grep none.*none
sleep 10
docker images | grep none.*none | awk '{ print $3 }' | xargs docker rmi
}
#######################################################################################
# destroy image function
######################################################################################
function remove_image()
{
local IMAGE_NAME=$1
docker rmi ${IMAGE_NAME}
}
#######################################################################################
# standby create function
#######################################################################################
function create_standby()
{
local MASTER=$1
local STANDBY_NAME=$2
local STANDBY_IMAGE=$3
MASTER_IP=$(docker exec -it ${MASTER} ifconfig | grep Bcast | awk '{ print $2 }' | cut -f2 -d':' | xargs echo -n)
plog "creating repuser on ${MASTER}"
docker exec -it ${MASTER} /usr/ppas-9.4/bin/psql -c "CREATE USER repuser WITH REPLICATION;" -U enterprisedb -p 5432 edb >>/dev/null
plog "creating container ${STANDBY_NAME}"
create_container ${STANDBY_NAME} ${STANDBY_IMAGE}
plog "cleaning old data directory."
docker exec -it ${STANDBY_NAME} service ppas-9.4 stop
sleep 5 # Associated with race condition below
docker exec -it ${STANDBY_NAME} rm -rf /var/lib/ppas/9.4/data
docker exec -it ${STANDBY_NAME} mkdir -p /var/lib/ppas/9.4/data
docker exec -it ${STANDBY_NAME} chmod 700 /var/lib/ppas/9.4/data
plog "pulling base backup"
docker exec -it ${STANDBY_NAME} /usr/ppas-9.4/bin/pg_basebackup -h ${MASTER_IP} -U repuser -p 5432 -R -D /var/lib/ppas/9.4/data/
sleep 10 # Some kind of race condition keeps ppas from listening on the network interface, therfore prevents it from starting up
docker exec -it ${STANDBY_NAME} chown -R enterprisedb:enterprisedb /var/lib/ppas/9.4/data/
docker exec -it ${STANDBY_NAME} sed -i "s/^#hot_standby = off/hot_standby = on/" /var/lib/ppas/9.4/data/postgresql.conf
docker exec -it ${STANDBY_NAME} service ppas-9.4 start
plog "starting standby"
while [[ $(docker exec -it ${STANDBY_NAME} ps -eaf|grep "wal receiver process"|grep -v grep |wc -l) -ne 1 ]]
do
sleep 2
done
plog "replication status:"
docker exec -it ${MASTER} /usr/ppas-9.4/bin/psql -c "SELECT client_addr,backend_start, state, sync_state FROM pg_stat_replication" -U enterprisedb -p 5432 edb
}