-
Notifications
You must be signed in to change notification settings - Fork 0
/
azurepxc.sh
executable file
·349 lines (311 loc) · 10.6 KB
/
azurepxc.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
#!/bin/bash
# set -ex
# This script is only tested on CentOS 6.5 and Ubuntu 12.04 LTS with Percona XtraDB Cluster 5.6.
# You can customize variables such as MOUNTPOINT, RAIDCHUNKSIZE and so on to your needs.
# You can also customize it to work with other Linux flavours and versions.
# If you customize it, copy it to either Azure blob storage or Github so that Azure
# custom script Linux VM extension can access it, and specify its location in the
# parameters of DeployPXC powershell script or runbook or Azure Resource Manager CRP template.
CLUSTERADDRESS=${1}
NODEADDRESS=${2}
NODENAME=$(hostname)
MYSQLSTARTUP=${3}
MYCNFTEMPLATE=${4}
SECONDNIC=${5}
MOUNTPOINT="/datadrive"
RAIDCHUNKSIZE=512
RAIDDISK="/dev/md127"
RAIDPARTITION="/dev/md127p1"
# An set of disks to ignore from partitioning and formatting
BLACKLIST="/dev/sda|/dev/sdb"
check_os() {
grep ubuntu /proc/version > /dev/null 2>&1
isubuntu=${?}
grep centos /proc/version > /dev/null 2>&1
iscentos=${?}
}
scan_for_new_disks() {
# Looks for unpartitioned disks
declare -a RET
DEVS=($(ls -1 /dev/sd*|egrep -v "${BLACKLIST}"|egrep -v "[0-9]$"))
for DEV in "${DEVS[@]}";
do
# Check each device if there is a "1" partition. If not,
# "assume" it is not partitioned.
if [ ! -b ${DEV}1 ];
then
RET+="${DEV} "
fi
done
echo "${RET}"
}
get_disk_count() {
DISKCOUNT=0
for DISK in "${DISKS[@]}";
do
DISKCOUNT+=1
done;
echo "$DISKCOUNT"
}
create_raid0_ubuntu() {
dpkg -s mdadm
if [ ${?} -eq 1 ];
then
echo "installing mdadm"
# wget --no-cache http://mirrors.cat.pdx.edu/ubuntu/pool/main/m/mdadm/mdadm_3.2.5-5ubuntu4_amd64.deb
# dpkg -i mdadm_3.2.5-5ubuntu4_amd64.deb
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get -y -q --no-install-recommends install mdadm
fi
echo "Creating raid0"
udevadm control --stop-exec-queue
echo "yes" | mdadm --create "$RAIDDISK" --name=data --level=0 --chunk="$RAIDCHUNKSIZE" --raid-devices="$DISKCOUNT" "${DISKS[@]}"
udevadm control --start-exec-queue
mdadm --detail --verbose --scan > /etc/mdadm.conf
}
create_raid0_centos() {
echo "Creating raid0"
yes | mdadm --create "$RAIDDISK" --name=data --level=0 --chunk="$RAIDCHUNKSIZE" --raid-devices="$DISKCOUNT" "${DISKS[@]}"
mdadm --detail --verbose --scan > /etc/mdadm.conf
}
do_partition() {
# This function creates one (1) primary partition on the
# disk, using all available space
DISK=${1}
echo "Partitioning disk $DISK"
echo "n
p
1
w
" | fdisk "${DISK}"
#> /dev/null 2>&1
#
# Use the bash-specific $PIPESTATUS to ensure we get the correct exit code
# from fdisk and not from echo
if [ ${PIPESTATUS[1]} -ne 0 ];
then
echo "An error occurred partitioning ${DISK}" >&2
echo "I cannot continue" >&2
exit 2
fi
}
add_to_fstab() {
UUID=${1}
MOUNTPOINT=${2}
grep "${UUID}" /etc/fstab >/dev/null 2>&1
if [ ${?} -eq 0 ];
then
echo "Not adding ${UUID} to fstab again (it's already there!)"
else
LINE="UUID=${UUID} ${MOUNTPOINT} ext4 defaults,noatime 0 0"
echo -e "${LINE}" >> /etc/fstab
fi
}
configure_disks() {
ls "${MOUNTPOINT}"
if [ ${?} -eq 0 ]
then
return
fi
DISKS=($(scan_for_new_disks))
echo "Disks are ${DISKS[@]}"
declare -i DISKCOUNT
DISKCOUNT=$(get_disk_count)
echo "Disk count is $DISKCOUNT"
if [ $DISKCOUNT -gt 1 ];
then
if [ $iscentos -eq 0 ];
then
create_raid0_centos
elif [ $isubuntu -eq 0 ];
then
create_raid0_ubuntu
fi
do_partition ${RAIDDISK}
PARTITION="${RAIDPARTITION}"
else
DISK="${DISKS[0]}"
do_partition ${DISK}
PARTITION=$(fdisk -l ${DISK}|grep -A 1 Device|tail -n 1|awk '{print $1}')
fi
echo "Creating filesystem on ${PARTITION}."
mkfs -t ext4 ${PARTITION}
mkdir "${MOUNTPOINT}"
read UUID FS_TYPE < <(blkid -u filesystem ${PARTITION}|awk -F "[= ]" '{print $3" "$5}'|tr -d "\"")
add_to_fstab "${UUID}" "${MOUNTPOINT}"
echo "Mounting disk ${PARTITION} on ${MOUNTPOINT}"
mount "${MOUNTPOINT}"
}
open_ports() {
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 4444 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 4567 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 4568 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 9200 -j ACCEPT
iptables-save
}
disable_apparmor_ubuntu() {
/etc/init.d/apparmor teardown
update-rc.d -f apparmor remove
}
disable_selinux_centos() {
sed -i 's/^SELINUX=.*/SELINUX=disabled/I' /etc/selinux/config
setenforce 0
}
activate_secondnic_centos() {
if [ -n "$SECONDNIC" ];
then
cp /etc/sysconfig/network-scripts/ifcfg-eth0 "/etc/sysconfig/network-scripts/ifcfg-${SECONDNIC}"
sed -i "s/^DEVICE=.*/DEVICE=${SECONDNIC}/I" "/etc/sysconfig/network-scripts/ifcfg-${SECONDNIC}"
defaultgw=$(ip route show |sed -n "s/^default via //p")
declare -a gateway=(${defaultgw// / })
sed -i "\$aGATEWAY=${gateway[0]}" /etc/sysconfig/network
service network restart
fi
}
configure_network() {
open_ports
if [ $iscentos -eq 0 ];
then
activate_secondnic_centos
disable_selinux_centos
elif [ $isubuntu -eq 0 ];
then
disable_apparmor_ubuntu
fi
}
create_mycnf() {
wget "${MYCNFTEMPLATE}" -O /etc/my.cnf
sed -i "s/^wsrep_cluster_address=.*/wsrep_cluster_address=gcomm:\/\/${CLUSTERADDRESS}/I" /etc/my.cnf
sed -i "s/^wsrep_node_address=.*/wsrep_node_address=${NODEADDRESS}/I" /etc/my.cnf
sed -i "s/^wsrep_node_name=.*/wsrep_node_name=${NODENAME}/I" /etc/my.cnf
# generate a pseud pass for my.cnf
# to make sure all nodes have the same pass uses date until hour
# potencial bug when deploying near hour change
local pseudo_pass=$(date -u +'%Y%m%d%H' | sha256sum | base64 | head -c 32)
sed -i "s/SST_AUTH_PLACEHOLDER/${pseudo_pass}/I" /etc/my.cnf
if [ $isubuntu -eq 0 ];
then
sed -i "s/^wsrep_provider=.*/wsrep_provider=\/usr\/lib\/libgalera_smm.so/I" /etc/my.cnf
fi
}
install_mysql_ubuntu() {
dpkg -s percona-xtradb-cluster-56
if [ ${?} -eq 0 ];
then
return
fi
#
# INSTALL INSTRUCTIONS FROM:
# https://www.percona.com/doc/percona-server/5.6/installation/apt_repo.html
echo "Downloading Percona Cluster Package"
wget https://repo.percona.com/apt/percona-release_0.1-3.$(lsb_release -sc)_all.deb
dpkg -i percona-release_0.1-3.$(lsb_release -sc)_all.deb
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install percona-xtradb-cluster-56
/etc/init.d/mysql stop
kill -9 $(pgrep mysqld)
rm /var/log/mysql?.* /var/log/mysql/error.log
# Remove Package Install Configuration
# we already have a good config
rm -fr /etc/mysql
apt-get -y install xinetd
}
install_mysql_centos() {
yum list installed Percona-XtraDB-Cluster-56
if [ ${?} -eq 0 ];
then
return
fi
echo "installing mysql"
yum -y install http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
wget --no-cache http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/socat-1.7.2.4-1.el6.rf.x86_64.rpm
rpm -Uvh socat-1.7.2.4*rpm
wget --no-cache https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -Uvh epel-release-latest-6.noarch.rpm
yum -y install libev
yum -y install Percona-XtraDB-Cluster-56
yum -y install xinetd
}
configure_mysql() {
/etc/init.d/mysql status
if [ ${?} -eq 0 ];
then
return
fi
create_mycnf
mkdir "${MOUNTPOINT}/mysql"
ln -s "${MOUNTPOINT}/mysql" /var/lib/mysql
chmod o+x /var/lib/mysql
if [ $iscentos -eq 0 ];
then
install_mysql_centos
elif [ $isubuntu -eq 0 ];
then
install_mysql_ubuntu
fi
/etc/init.d/mysql stop
chmod o+x "${MOUNTPOINT}/mysql"
grep "mysqlchk" /etc/services >/dev/null 2>&1
if [ ${?} -ne 0 ];
then
sed -i "\$amysqlchk 9200\/tcp #mysqlchk" /etc/services
fi
service xinetd restart
sstmethod=$(sed -n "s/^wsrep_sst_method=//p" /etc/my.cnf)
sst=$(sed -n "s/^wsrep_sst_auth=//p" /etc/my.cnf | cut -d'"' -f2)
declare -a sstauth=(${sst//:/ })
if [ $sstmethod == "mysqldump" ]; #requires root privilege for sstuser on every node
then
/etc/init.d/mysql bootstrap-pxc
echo "CREATE USER '${sstauth[0]}'@'localhost' IDENTIFIED BY '${sstauth[1]}';" > /tmp/mysqldump-pxc.sql
echo "GRANT ALL PRIVILEGES ON *.* TO '${sstauth[0]}'@'localhost' with GRANT OPTION;" >> /tmp/mysqldump-pxc.sql
echo "CREATE USER '${sstauth[0]}'@'%' IDENTIFIED BY '${sstauth[1]}';" >> /tmp/mysqldump-pxc.sql
echo "GRANT ALL PRIVILEGES ON *.* TO '${sstauth[0]}'@'%' with GRANT OPTION;" >> /tmp/mysqldump-pxc.sql
echo "FLUSH PRIVILEGES;" >> /tmp/mysqldump-pxc.sql
mysql < /tmp/mysqldump-pxc.sql
/etc/init.d/mysql stop
fi
/etc/init.d/mysql $MYSQLSTARTUP
if [ $MYSQLSTARTUP == "bootstrap-pxc" ];
then
if [ $sstmethod != "mysqldump" ];
then
echo "CREATE USER '${sstauth[0]}'@'localhost' IDENTIFIED BY '${sstauth[1]}';" > /tmp/bootstrap-pxc.sql
echo "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '${sstauth[0]}'@'localhost';" >> /tmp/bootstrap-pxc.sql
fi
echo "CREATE USER 'clustercheckuser'@'localhost' identified by 'clustercheckpassword!';" >> /tmp/bootstrap-pxc.sql
echo "GRANT PROCESS on *.* to 'clustercheckuser'@'localhost';" >> /tmp/bootstrap-pxc.sql
echo "CREATE USER 'test'@'10.3.%' identified by '${sstauth[1]}';" >> /tmp/bootstrap-pxc.sql
echo "GRANT SELECT on *.* to 'test'@'10.3.%';" >> /tmp/bootstrap-pxc.sql
echo "FLUSH PRIVILEGES;" >> /tmp/bootstrap-pxc.sql
mysql < /tmp/bootstrap-pxc.sql
fi
}
# allow_passwordssh() {
# Using SSH-Key now
#
# grep -q '^PasswordAuthentication yes' /etc/ssh/sshd_config
# if [ ${?} -eq 0 ];
# then
# return
# fi
# sed -i "s/^#PasswordAuthentication.*/PasswordAuthentication yes/I" /etc/ssh/sshd_config
# sed -i "s/^PasswordAuthentication no.*/PasswordAuthentication yes/I" /etc/ssh/sshd_config
# /etc/init.d/sshd reload
# }
# temporary workaround form CRP
# allow_passwordssh
check_os
if [ $iscentos -ne 0 ] && [ $isubuntu -ne 0 ];
then
echo "unsupported operating system"
exit 1
else
configure_network
configure_disks
configure_mysql
fi