-
Notifications
You must be signed in to change notification settings - Fork 13
/
incomplete_deployment.sh
452 lines (362 loc) · 14.7 KB
/
incomplete_deployment.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/bin/bash
# Dynamic DNS Server - Master Name Server Deployment
# Requires: CentOS 7.*
# Note: Incomplete
# By Rahim Khoja (rahim@khoja.ca)
echo
echo -e "\033[0;31m░░░░░░░░▀▀▀██████▄▄▄"
echo "░░░░░░▄▄▄▄▄░░█████████▄ "
echo "░░░░░▀▀▀▀█████▌░▀▐▄░▀▐█ "
echo "░░░▀▀█████▄▄░▀██████▄██ "
echo "░░░▀▄▄▄▄▄░░▀▀█▄▀█════█▀"
echo "░░░░░░░░▀▀▀▄░░▀▀███░▀░░░░░░▄▄"
echo "░░░░░▄███▀▀██▄████████▄░▄▀▀▀██▌"
echo "░░░██▀▄▄▄██▀▄███▀▀▀▀████░░░░░▀█▄"
echo "▄▀▀▀▄██▄▀▀▌█████████████░░░░▌▄▄▀"
echo "▌░░░░▐▀████▐███████████▌"
echo "▀▄░░▄▀░░░▀▀██████████▀"
echo "░░▀▀░░░░░░▀▀█████████▀"
echo "░░░░░░░░▄▄██▀██████▀█"
echo "░░░░░░▄██▀░░░░░▀▀▀░░█"
echo "░░░░░▄█░░░░░░░░░░░░░▐▌"
echo "░▄▄▄▄█▌░░░░░░░░░░░░░░▀█▄▄▄▄▀▀▄"
echo -e "▌░░░░░▐░░░░░░░░░░░░░░░░▀▀▄▄▄▀\033[0m"
echo "---Dynamic DNS Server - Master Name Server Deployment Script---"
echo "---By: Rahim Khoja (rahim@khoja.ca)---"
echo
echo " Please note, this script has only been tested with users who are under 5 foot 8 inches in height!"
echo
# Stop on Error
set -eE # same as: `set -o errexit -o errtrace`
# Dump Vars Function
function dump_vars {
if ! ${STATUS+false};then echo "STATUS = ${STATUS}";fi
if ! ${LOGFILE+false};then echo "LOGFILE = ${LOGFILE}";fi
if ! ${SCRIPTDIR+false};then echo "SCRIPTDIR = ${SCRIPTDIR}";fi
if ! ${DEBUG+false};then echo "DEBUG = ${DEBUG}";fi
if ! ${PUBLICIP+false};then echo "PUBLICIP = ${PUBLICIP}";fi
if ! ${TIMESTAMP+false};then echo "TIMESTAMP = ${TIMESTAMP}";fi
if ! ${finish+false};then echo "finish = ${finish}";fi
if ! ${ddnsserver+false};then echo "ddnsserver = ${ddnsserver}";fi
if ! ${ddnsmaster+false};then echo "ddnsmaster = ${ddnsmaster}";fi
if ! ${MASTERIP+false};then echo "MASTERIP = ${MASTERIP}";fi
if ! ${MYSQLPASSWORD+false};then echo "MYSQLPASSWORD = ${MYSQLPASSWORD}";fi
if ! ${SLAVEID+false};then echo "SLAVEID = ${SLAVEID}";fi
}
# Failure Function
function failure() {
local lineno=$1
local msg=$2
echo ""
echo -e "\033[0;31mError at Line Number $lineno: '$msg'\033[0m"
echo ""
if [[ $DEBUG -eq 1 ]]; then
dump_vars
fi
}
# Failure Function Trap
trap 'failure ${LINENO} "$BASH_COMMAND"' ERR
# Default Variriable Declaration
LOGFILE=/var/log/DDNS-Server-Setup.log
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
DEBUG=1
STATUS="Initializing"
TIMESTAMP="$( date +%s )"
#PUBLICIP="$(dig @resolver1.opendns.com ANY myip.opendns.com +short)"
finish="-1"
ddnsserver="0"
ddnsmaster="0"
middleman="0"
backupconfig="0"
getpagespeed="0"
# Check the bash shell script is being run by root
STATUS="Check - Script Run as Root user"
if [[ $EUID -ne 0 ]];
then
echo "This script must be run as root" 1>&2
exit 1
fi
STATUS="Starting Installation"
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Prompt to Confirm Installation of DDNS Server
STATUS="Prompt - Do you Want To Install DDNS Server (Master or Slave)"
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
echo
read -p "Do you want to set this Install DDNS Server [y/n]? " answer
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y"; ddnsserver="1";;
n | N | no | NO ) answer="n"; ddnsserver="0"; exit 1;;
*) finish="-1";
echo -n 'Invalid Response\n';
esac
fi
done
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Prompt to Confirm Setup of DDNS Master or Slave
STATUS="Prompt - Is This Master Server"
while [ "$finish" = '-1' ]
do
finish="1"
echo
read -p "Do you want to set this system up as a DDNS Master Server [y/n]? " answer
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y"; ddnsmaster="1";;
n | N | no | NO ) answer="n"; ddnsmaster="0";;
*) finish="-1";
echo -n 'Invalid Response\n';
esac
fi
done
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Prompt - Slave Server Prompts.
STATUS="Prompts - Slave Server Inputs"
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
if [[ "$ddnsmaster" == "0" ]]; then
# Prompt - Master Server IP?
STATUS="Prompt - Slave Prompt - What is Master Server IP?"
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
echo
read -p "Please enter DDNS Master Server IP Address?: " MASTERIP
MASTERIP=${MASTERIP:-""}
echo
read -p "DDNS Master Server IP: $MASTERIP [y/n]? " answer
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n"; finish="-1"; ;
*) finish="-1";
echo -n 'Invalid Response\n';
esac
fi
done
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Prompt - Master Server MySQL Password?
STATUS="Prompt - Slave Prompt - What is Master Server MySQL Password?"
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
echo
read -p "Please enter DDNS Master Server MySQL Password?: " MYSQLPASSWORD
MYSQLPASSWORD=${MYSQLPASSWORD:-""}
echo
read -p "DDNS Master Server MySQL Password: $MYSQLPASSWORD [y/n]? " answer
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n"; finish="-1"; ;
*) finish="-1";
echo -n 'Invalid Response\n';
esac
fi
done
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Prompt - Slave Server Number?
STATUS="Prompt - Slave Prompt - What is the Slave Server ID Number?"
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
echo
read -p "Please enter DDNS Slave ID Number that is being Setup?: " SLAVEID
SLAVEID=${SLAVEID:-""}
echo
read -p "DDNS Slave Server ID: $SLAVEID [y/n]? " answer
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n"; finish="-1"; ;
*) finish="-1";
echo -n 'Invalid Response\n';
esac
fi
done
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
fi
# Install Required Packages & Update System
STATUS="Install Yum Packages"
yum -y install epel-release
yum -y groupinstall 'Development Tools'
yum -y install chrony cockpit curl dig firewalld git kexec-tools krb5-devel libselinux-python mail ntp openssh openssl openssl-devel policycoreutils-python postfix sendmail wget yum-utils
STATUS="Update & Upgrade System"
yum -y update && yum -y upgrade
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Clone DDNS Repository
cd /tmp
git clone https://github.com/CanadianRepublican/DDNS_Server.git DDNS
# Start & Enable Cockpit
systemctl start cockpit
systemctl enable cockpit.socket
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Start & Enable NTP
systemctl start ntpd
systemctl enable ntpd
ntpq -p
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Setup Postfix & SendMail
setsebool -P httpd_can_network_connect true
setsebool -P httpd_can_sendmail=1
service postfix start
systemctl enable postfix
service sendmail start
systemctl enable sendmail
# Test Postfix # echo "My message" | postfix -s testpostfix user@domain.com
# Test Sendmail # echo "My message" | sendmail -s testsendmail user@domain.com
echo "$(date \"+%FT%T\") $STATUS" >> "${LOGFILE}"
# Tomcat 9 Install
yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
cd /tmp
wget http://apache.mirror.globo.tech/tomcat/tomcat-9/v9.0.0.M21/bin/apache-tomcat-9.0.0.M21.tar.gz
tar -xzvf apache-tomcat-9.0.0.M21.tar.gz
mv apache-tomcat-9.0.0.M21 /opt/tomcat
# Tomcat User and Group Setup
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
chown -hR tomcat:tomcat /opt/tomcat
# Backup Original Tomcat Files
cp /opt/tomcat/conf/context.xml /opt/tomcat/conf/context.xml.orig
cp /opt/tomcat/conf/web.xml /opt/tomcat/conf/web.xml.orig
cp /opt/tomcat/conf/server.xml /opt/tomcat/conf/server.xml.orig
# stuff
ssh-keygen -t rsa -b 4096
# Install Bind/Named with DLZ Support
# Version 9-11-0-p3 from www.isc.org
curl -o /tmp/bind-9.11.0-P3.tar.gz https://www.isc.org/downloads/file/bind-9-11-0-p3/
cd /tmp
tar -xvzf bind-9.11.0-P3.tar.gz
mv /tmp/bind-9.11.0-P3 /usr/src/
cd /usr/src/bind-9.11.0-P3/
./configure --prefix=/usr --sysconfdir=/etc/bind --localstatedir=/var --mandir=/usr/share/man --infodir=/usr/share/info --enable-threads --enable-largefile --with-libtool --enable-shared --enable-static --with-openssl=/usr --with-gssapi=/usr --with-gnu-ld --with-dlz-postgres=no --with-dlz-mysql=yes --with-dlz-bdb=no --with-dlz-filesystem=yes --with-dlz-ldap=no --with-dlz-stub=yes --enable-ipv6 LDFLAGS="-I/usr/include/mysql -L/usr/lib64/mysql"
make; make install
ldconfig -v
# Named User and Group Setup
groupadd named
useradd -d /var/named -g named -s /bin/false named
mkdir -p /var/named/
chown -R named:named /var/named
chmod -R 755 /var/named
mkdir /var/log/named
chown named:named /var/log/named
mkdir /var/run/named
chown named:named /var/run/named
chown named:named /usr/sbin/named
# Selinux Named Server Commands
semanage permissive -a named_t
setsebool -P named_write_master_zones=1
# Named SystemD Service File
cat <<EOF >>/usr/lib/systemd/system/named.service
[Unit]
Description=Berkeley Internet Name Domain (DNS) with DLZ
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=60
ExecStart=/usr/sbin/named -u named -c /etc/named.conf -f -d 9
[Install]
WantedBy=multi-user.target
EOF
# Reload SystemD Services List & Enabled Named Service on Startup
systemctl daemon-reload
systemctl enable named
# System Update
# Master Database Setup
# Bind Installation
curl -o /tmp/bind-9.11.0-P3.tar.gz https://www.isc.org/downloads/file/bind-9-11-0-p3/
cd /tmp
tar -xvzf bind-9.11.0-P3.tar.gz
mv /tmp/bind-9.11.0-P3 /usr/src/
cd /usr/src/bind-9.11.0-P3/
./configure --prefix=/usr --sysconfdir=/etc/bind --localstatedir=/var --mandir=/usr/share/man --infodir=/usr/share/info --enable-threads --enable-largefile --with-libtool --enable-shared --enable-static --with-openssl=/usr --with-gssapi=/usr --with-gnu-ld --with-dlz-postgres=no --with-dlz-mysql=yes --with-dlz-bdb=no --with-dlz-filesystem=yes --with-dlz-ldap=no --with-dlz-stub=yes --enable-ipv6 LDFLAGS="-I/usr/include/mysql -L/usr/lib64/mysql"
make
make install
ldconfig -v
groupadd ddns
useradd -d /var/ddns -g ddns -s /bin/false ddns
mkdir -p /var/ddns/
mkdir /var/ddns/log
chown -R ddns:ddns /var/ddns
chmod -R 755 /var/ddns
crontab -l -u ddns | echo '*/5 * * * * /var/ddns/ddns-cron.sh' | crontab -u ddns -
systemctl daemon-reload
# Database Setup
systemctl enable mariadb
systemctl start mariadb
mysql_secure_installation
mysql -u root -p -e "create database dyn_server_db;"
mysql -u root -p dyn_server_db < /tmp/DDNS/dyn_server_db.sql
# Setup Slave Databases
mysql -u root -p -e "CHANGE MASTER TO MASTER_HOST='172.16.254.40', MASTER_USER='dyn_replicator', MASTER_PASSWORD='replicator_password'; GRANT SELECT,LOCK TABLES on dyn_server_db.* to 'dyn_dns_user'@'localhost' identified by 'password'; flush privileges;"
# Setup Master Database
# Create Bind w DLZ Database User (change IP to private network range & update password)
# This password relates to the mysql settings within /etc/named.conf
mysql -u root -p -e "GRANT SELECT,LOCK TABLES on dyn_server_db.* to 'dyn_dns_user'@'172.16.254.%' identified by 'password'; GRANT SELECT,LOCK TABLES on dyn_server_db.* to 'dyn_dns_user'@'localhost' identified by 'password';"
# Create Tomcat User (update password)
# This password relates to the mysql settings within Tomcat context file /opt/tomcat/webapps/ddns/WEB-INF/web.xml
mysql -u root -p -e "GRANT SELECT on dyn_server_db.dyn_user_roles to 'dyn_tomcat_user'@'localhost' identified by 'password'; GRANT SELECT on dyn_server_db.dyn_users to 'dyn_tomcat_user'@'localhost' identified by 'password'; GRANT SELECT on dyn_server_db.dyn_user_roles to 'dyn_tomcat_user'@'172.16.254.%' identified by 'password'; GRANT SELECT on dyn_server_db.dyn_users to 'dyn_tomcat_user'@'172.16.254.%' identified by 'password';"
GRANT SELECT,UPDATE,DELETE on dyn_server_db.dyn_users to 'dyn_cron_user'@'localhost' identified by 'password';
flush privileges;
service firewalld stop
// On Slave DNS
mysqldump -h 172.16.254.40 -u dyn_dns_user -p dyn_server_db xfr_table dyn_dns_records > dyn_server_db.sql
create database dyn_server_db;
mysql -u root -p dyn_server_db < dyn_server_db.sql
sudo systemctl restart mariadb
systemctl daemon-reload
# Database Setup
systemctl enable mariadb
cat <<EOF >>/etc/my.cnf.master
bind-address=0.0.0.0
server-id=1
log-bin
binlog-format=row
binlog-do-db=dyn_server_db
log-slave-updates
replicate-do-table=dyn_server_db.dyn_dns_records
replicate-do-table=dyn_server_db.xfr_table
EOF
slave-number=3
cat <<EOF >>/etc/my.cnf.slave-${slave-number}
server-id=${slave-number}
binlog_do_db=dyn_server_db
replicate-do-table=dyn_server_db.dyn_dns_records
replicate-do-table=dyn_server_db.xfr_table
bind-address=127.0.0.1
EOF
GRANT REPLICATION SLAVE ON *.* TO 'dyn_replicator'@'172.16.%.%' IDENTIFIED BY 'Password!';
grant all on dyn_server_db.* to 'dyn_web_app'@'172.16.254.%' identified by 'Password!';
grant all on dyn_server_db.* to 'dyn_web_app'@'localhost' identified by 'Password';
# Setup FirewallD
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --zone=public --permanent --add-service=mysql
firewall-cmd --zone=public --permanent --add-service=dns
firewall-cmd --zone=public --permanent --add-port=8080/tcp
firewall-cmd --zone=public --permanent --add-port=8080/udp
firewall-cmd --reload
firewall-cmd --zone=public --permanent --list-ports
systemctl enable firewalld
service firewalld start