-
Notifications
You must be signed in to change notification settings - Fork 27
/
openstack
executable file
·204 lines (150 loc) · 6.78 KB
/
openstack
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
#!/bin/bash
[[ -e openrc ]] || echo "no openrc found"; exit 1
source openrc
#echo some basic house keeping
#echo ------------------------
#cho - Create really small flavor
#FLAVOR_ID=42
#nova flavor-create m1.demo $FLAVOR_ID 256 0 1
#echo - add 3306 and 80 to default security group
#nova secgroup-add-rule default tcp 3306 3306 0.0.0.0/0
#nova secgroup-add-rule default tcp 80 80 0.0.0.0/0
echo
echo MySQL Servers
echo -------------
echo -n "- boot mysql01 ."
tee /tmp/mysql01.txt <<EOF > /dev/null
#docker
image: paulczar/mysql
cmd: "mysqld_safe --server-id=1 --log-bin=mysql-bin --log-slave-updates=1 --auto_increment_increment=2 --auto_increment_offset=1"
EOF
MYSQL01_ID=$(nova boot --flavor m1.tiny --image docker mysql01 --user-data /tmp/mysql01.txt | grep '| id' | awk '{print $4}')
until [ $(nova show $MYSQL01_ID | grep status | awk '{ print $4}') == "ACTIVE" ]; do
sleep 1
echo -n "."
done
MYSQL01_IP=$(nova show $MYSQL01_ID | grep 'public network' | awk '{print $5}')
echo " - $MYSQL01_IP"
echo -n "- boot mysql02 ."
tee /tmp/mysql02.txt <<EOF > /dev/null
#docker
image: paulczar/mysql
cmd: "mysqld_safe --server-id=2 --log-bin=mysql-bin --log-slave-updates=1 --auto_increment_increment=2 --auto_increment_offset=2"
EOF
MYSQL02_ID=$(nova boot --flavor m1.tiny --image docker mysql02 --user-data /tmp/mysql02.txt | grep '| id' | awk '{print $4}')
until [ $(nova show $MYSQL02_ID | grep status | awk '{ print $4}') == "ACTIVE" ]; do
sleep 1
echo -n "."
done
MYSQL02_IP=$(nova show $MYSQL02_ID | grep 'public network' | awk '{print $5}')
echo " - $MYSQL02_IP"
echo "- Sleep for a few seconds, give MySQL a chance to start"
sleep 5
echo "- Creat replication user"
mysql -uroot -proot -h $MYSQL01_IP -AN -e 'GRANT REPLICATION SLAVE ON *.* TO "replication"@"%" IDENTIFIED BY "password";'
mysql -uroot -proot -h $MYSQL01_IP -AN -e 'flush privileges;'
echo "- Export Data from MySQL01 to MySQL02"
mysqldump -uroot -proot -h $MYSQL01_IP --single-transaction --all-databases \
--flush-privileges | mysql -uroot -proot -h $MYSQL02_IP
echo "- Set MySQL01 as master on MySQL02"
MYSQL01_Position=$(mysql -uroot -proot -h $MYSQL01_IP -e "show master status \G" | grep Position | awk '{print $2}')
MYSQL01_File=$(mysql -uroot -proot -h $MYSQL01_IP -e "show master status \G" | grep File | awk '{print $2}')
mysql -uroot -proot -h $MYSQL02_IP -AN -e "CHANGE MASTER TO master_host='$MYSQL01_IP', master_port=3306, \
master_user='replication', master_password='password', master_log_file='$MYSQL01_File', \
master_log_pos=$MYSQL01_Position;"
echo "- Set MySQL02 as master on MySQL01"
MYSQL02_Position=$(mysql -uroot -proot -h $MYSQL02_IP -e "show master status \G" | grep Position | awk '{print $2}')
MYSQL02_File=$(mysql -uroot -proot -h $MYSQL02_IP -e "show master status \G" | grep File | awk '{print $2}')
mysql -uroot -proot -h $MYSQL01_IP -AN -e "CHANGE MASTER TO master_host='$MYSQL02_IP', master_port=3306, \
master_user='replication', master_password='password', master_log_file='$MYSQL02_File', \
master_log_pos=$MYSQL02_Position;"
echo "- Start Slave on both Servers"
mysql -uroot -proot -h $MYSQL01_IP -AN -e "start slave;"
mysql -uroot -proot -h $MYSQL02_IP -AN -e "start slave;"
echo "- Create database 'wordpress' on MySQL01"
mysql -uroot -proot -h $MYSQL01_IP -e "create database wordpress;"
echo "- Fill it with data"
mysql -uroot -proot -h $MYSQL01_IP wordpress < wordpress.sql
echo "- Sleep 2 seconds, then check that database 'wordpress' exists on MySQL02"
sleep 2
mysql -uroot -proot -h $MYSQL02_IP -e "show databases; \G" | grep wordpress
echo
echo "Create MySQL Load Balancer"
echo "--------------------------"
echo -n "- Create HAProxy-MySQL"
mysql -uroot -proot -h $MYSQL01_IP -AN -e "GRANT USAGE ON *.* TO 'haproxy'@'%';"
tee /tmp/haproxy-mysql.txt <<EOF > /dev/null
#docker
image: paulczar/haproxy-mysql
cmd: /haproxy/start $MYSQL01_IP:3306,$MYSQL02_IP:3306
EOF
HAPROXY_MYSQL=$(nova boot --flavor m1.tiny --image docker haproxy-mysql --user-data /tmp/haproxy-mysql.txt | grep '| id' | awk '{print $4}')
until [ $(nova show $HAPROXY_MYSQL | grep status | awk '{ print $4}') == "ACTIVE" ]; do
sleep 1
echo -n "."
done
HAPROXY_MYSQL_IP=$(nova show $HAPROXY_MYSQL | grep 'public network' | awk '{print $5}')
echo " - $HAPROXY_MYSQL_IP"
echo "- Check our haproxy works"
echo " (should show alternating server_id)"
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
echo
echo "Create Wordpress Web Servers"
echo "------------------------"
echo -n "- Create WordPress01"
tee /tmp/wordpress.txt <<EOF > /dev/null
#docker
image: paulczar/apache2-wordpress
cmd: /var/www/start $HAPROXY_MYSQL_IP
EOF
WORDPRESS1=$(nova boot --flavor m1.tiny --image docker wordpress01 --user-data /tmp/wordpress.txt | grep '| id' | awk '{print $4}')
until [ $(nova show $WORDPRESS1 | grep status | awk '{ print $4}') == "ACTIVE" ]; do
sleep 1
echo -n "."
done
WORDPRESS1_IP=$(nova show $WORDPRESS1 | grep 'public network' | awk '{print $5}')
echo " - $WORDPRESS1_IP"
echo -n "- Create WordPress02"
WORDPRESS2=$(nova boot --flavor m1.tiny --image docker wordpress02 --user-data /tmp/wordpress.txt | grep '| id' | awk '{print $4}')
until [ $(nova show $WORDPRESS2 | grep status | awk '{ print $4}') == "ACTIVE" ]; do
sleep 1
echo -n "."
done
WORDPRESS2_IP=$(nova show $WORDPRESS2 | grep 'public network' | awk '{print $5}')
echo " - $WORDPRESS2_IP"
echo
echo "Create Web Load Balancer"
echo "--------------------------"
echo -n "- Create HAProxy-Web"
tee /tmp/haproxy-web.txt <<EOF > /dev/null
#docker
image: paulczar/haproxy-web
cmd: /haproxy/start $WORDPRESS1_IP:80,$WORDPRESS2_IP:80
EOF
HAPROXY_WEB=$(nova boot --flavor m1.tiny --image docker haproxy-web --user-data /tmp/haproxy-web.txt | grep '| id' | awk '{print $4}')
until [ $(nova show $HAPROXY_WEB | grep status | awk '{ print $4}') == "ACTIVE" ]; do
sleep 1
echo -n "."
done
HAPROXY_WEB_IP=$(nova show $HAPROXY_WEB | grep 'public network' | awk '{print $5}')
echo "- Check it works"
curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "PHP API"
echo "Environment Created!"
echo "--------------------"
echo
echo "Browse to http://$HAPROXY_WEB_IP to access your wordpress site"
echo
echo Server Details :-
echo
echo MYSQL01_IP : $MYSQL01_IP
echo MYSQL02_IP : $MYSQL02_IP
echo HAPROXY_MYSQL_IP : $HAPROXY_MYSQL_IP
echo WORDPRESS1_IP : $WORDPRESS1_IP
echo WORDPRESS2_IP : $WORDPRESS2_IP
echo HAPROXY_WEB_IP : $HAPROXY_WEB_IP
echo
echo you may want to run the following :-
echo "# sudo echo \"$HAPROXY_WEB_IP wordpress\" >> /etc/hosts"