-
Notifications
You must be signed in to change notification settings - Fork 2
/
start_containers.sh
executable file
·154 lines (128 loc) · 4.87 KB
/
start_containers.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
#!/bin/bash
################################
# Prevent stopping local service
################################
echo "This script will stop your following services using /etc/init.d: "
echo " memcached"
echo " mysql"
echo " mongo"
echo " rabbitmq-server"
echo "Do you want to continue? "
select yn in "Yes" "No"; do
case $yn in
Yes ) break;;
No ) exit;;
esac
done
###########################
# Service password and user
###########################
default_user="admin"
echo "Choose admin username"
echo "If volumes already exists it won't be overriden"
echo "Only works for new containers"
read -p "Choose usernam [$default_user]:" USER
USER=${USER:-$default_user}
default_pass=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-20};echo;)
echo "Only works for new containers"
read -p "Choose admin password [$default_pass]:" PASS
PASS=${PASS:-$default_pass}
######################
# Ask for persistence
######################
echo "Do you want to use persistence? "
select yn in "Yes" "No"; do
case $yn in
Yes ) persistence=**True**; break;;
No ) exit;;
esac
done
######################
# Prepare persistence
######################
if [ "$persistence" == "**True**" ]; then
# Ask for folder location
default=/data/docker_volumes
read -p "Enter docker volume for persitence storage[$default]: " DATA_VOL
DATA_VOL=${DATA_VOL:-$default}
echo "=> data volume: $DATA_VOL"
# If folder already exists
if [ -d "$DATA_VOL" ]; then
# Control will enter here if $DATA_VOL exists.
echo "=> $DATA_VOL already exists"
echo "Are you sure that you want to continue?"
select yn in "Yes" "No"; do
case $yn in
Yes ) break;;
No ) exit;;
esac
done
fi
# Command with persistence
persistence_mongo="-v $DATA_VOL/mongo:/data/db"
persistence_mysql="-v $DATA_VOL/mysql:/var/lib/mysql"
persistence_rabbit="-v $DATA_VOL/rabbitmq/mnesia:/var/lib/rabbitmq/mnesia"
fi
# Complete CMDs
cmd_mysql="docker run --name mysql-server-5.6 -d -p 3306:3306 -e MYSQL_USER=$USER -e MYSQL_PASS=$PASS $persistence_mysql bastianb/mysql5.6"
cmd_rabbitmq="docker run --name rabbitmq-server -d -p 5672:5672 -p 15672:15672 -h rabbithost -e RABBITMQ_USER=$USER -e RABBITMQ_PASS=$PASS $persistence_rabbit bastianb/rabbitmq"
cmd_mongodb="docker run --name mongodb-2.6 -d -p 27017:27017 -p 28017:28017 -e MONGODB_USER=$USER -e MONGODB_PASS=$PASS $persistence_mongo bastianb/mongod2.6"
cmd_memcached='docker run --name memcached -d -p 11211:11211 bastianb/memcached'
echo "=> Creating docker volume storage.."
mkdir -p $DATA_VOL && echo OK || echo "Failed - Are you trying to create a directory to a protected place?
Try running the script with sudo."
echo "=>Stopping Host Services"
echo "=>Stopping MySQL"
sudo /etc/init.d/mysql stop
sudo service mysql stop
echo "=>Stopping Memcached"
sudo /etc/init.d/memcached stop
sudo service memcached stop
echo "=>Stopping Mongod"
sudo /etc/init.d/mongodb stop
sudo service mongodb stop
echo "=>Stopping RabbitMQ Server"
sudo /etc/init.d/rabbitmq-server stop
sudo service rabbitmq-server stop
echo "=>Starting containers"
RUNNING=$(docker inspect --format="{{ .State.Running }}" mysql-server-5.6 2> /dev/null)
if [ "$RUNNING" == "false" ] || [ "$RUNNING" == "true" ]; then
echo "=>CRITICAL - mysql-server-5.6 already exist removing"
docker stop mysql-server-5.6 &> /dev/null \
|| docker rm mysql-server-5.6 &> /dev/null \
&& docker rm mysql-server-5.6 &> /dev/null \
|| echo "already removed"
echo "=>Stopped and removed"
fi
$cmd_mysql && echo MySQL - OK
RUNNING=$(docker inspect --format="{{ .State.Running }}" mongodb-2.6 2> /dev/null)
if [ "$RUNNING" == "false" ] || [ "$RUNNING" == "true" ]; then
echo "=>CRITICAL - mongodb-2.6 already exist removing"
docker stop mongodb-2.6 &> /dev/null \
|| docker rm mongodb-2.6 &> /dev/null \
&& docker rm mongodb-2.6 &> /dev/null \
|| echo "already removed"
echo "=>Stopped and removed"
fi
$cmd_mongodb && echo Mongod - OK
RUNNING=$(docker inspect --format="{{ .State.Running }}" memcached 2> /dev/null)
if [ "$RUNNING" == "false" ] || [ "$RUNNING" == "true" ]; then
echo "=>CRITICAL - memcached already exist removing"
docker stop memcached &> /dev/null \
|| docker rm memcached &> /dev/null \
&& docker rm memcached &> /dev/null \
|| echo "already removed"
echo "=>Stopped and removed"
fi
$cmd_memcached && echo Memcached - OK
RUNNING=$(docker inspect --format="{{ .State.Running }}" rabbitmq-server 2> /dev/null)
if [ "$RUNNING" == "false" ] || [ "$RUNNING" == "true" ]; then
echo "=>CRITICAL - rabbitmq-server already exist removing"
docker stop rabbitmq-server &> /dev/null \
|| docker rm rabbitmq-server &> /dev/null \
&& docker rm rabbitmq-server &> /dev/null \
|| echo "already removed"
echo "=>Stopped and removed"
fi
$cmd_rabbitmq && echo RabbitMq-Server - OK
echo "Done, $USER $PASS"