-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathha_cluster_setup.sh
More file actions
210 lines (179 loc) · 5.72 KB
/
ha_cluster_setup.sh
File metadata and controls
210 lines (179 loc) · 5.72 KB
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
#!/bin/bash
#
# HA Cluster Setup Script (HArmadillium)
# --------------------------------------
# Purpose:
# Non-interactive setup of a High Availability (HA) cluster using Corosync, Pacemaker, and supporting tools.
# Configures firewall (ufw), HAProxy, Fail2Ban, Nginx, and Apache2 with port bindings.
# SSL certificates are created using the `ssl/distinguished.cnf` file for HTTPS.
#
# Usage:
# Set the following environment variables before running:
# STATIC_IP - static IPv4 address for this node (e.g., 192.168.1.100)
# SUBNET_MASK - subnet mask (e.g., 255.255.255.0)
# GATEWAY - gateway address (e.g., 192.168.1.1)
# DNS_SERVERS - comma-separated DNS servers (e.g., 8.8.8.8,8.8.4.4)
#
# Example:
# STATIC_IP=192.168.1.100 SUBNET_MASK=255.255.255.0 GATEWAY=192.168.1.1 DNS_SERVERS=8.8.8.8,8.8.4.4 ./ha_cluster_setup.sh
#
# Author: UniversalBit Development Team
# Version: 1.4.0 (Automation edition)
# License: MIT
set -e
# --- Functions ---
netmask_to_cidr() {
local mask=$1
local IFS=.
local -a octets=($mask)
local cidr=0
for octet in "${octets[@]}"; do
case $octet in
255) ((cidr+=8));;
254) ((cidr+=7));;
252) ((cidr+=6));;
248) ((cidr+=5));;
240) ((cidr+=4));;
224) ((cidr+=3));;
192) ((cidr+=2));;
128) ((cidr+=1));;
0);;
*) echo "Invalid netmask: $mask"; exit 1;;
esac
done
echo "$cidr"
}
# --- Parameter Validation ---
STATIC_IP="${STATIC_IP:-}"
SUBNET_MASK="${SUBNET_MASK:-}"
GATEWAY="${GATEWAY:-}"
DNS_SERVERS="${DNS_SERVERS:-}"
if [[ -z "$STATIC_IP" || ! "$STATIC_IP" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "Error: STATIC_IP must be set to a valid IPv4 address."
exit 1
fi
if [[ -z "$SUBNET_MASK" || ! "$SUBNET_MASK" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "Error: SUBNET_MASK must be set to a valid subnet mask."
exit 1
fi
if [[ -z "$GATEWAY" || ! "$GATEWAY" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "Error: GATEWAY must be set to a valid IPv4 address."
exit 1
fi
if [[ -z "$DNS_SERVERS" ]]; then
echo "Error: DNS_SERVERS must be set to at least one DNS IP."
exit 1
fi
CIDR=$(netmask_to_cidr "$SUBNET_MASK")
if [[ $EUID -ne 0 ]]; then
echo "Please run this script as root."
exit 1
fi
echo "Updating system and installing required packages..."
apt update && apt upgrade -y
apt install -y corosync pacemaker pcs ufw haproxy fail2ban nginx apache2
echo "Configuring static IP for this node..."
# Detect active network interface
NETIF=$(ip -o -4 route show to default | awk '{print $5}')
if [[ -z "$NETIF" ]]; then
echo "Error: Could not detect active network interface."
exit 1
fi
cat <<EOF > /etc/netplan/99-static-ip.yaml
network:
version: 2
ethernets:
$NETIF:
addresses:
- $STATIC_IP/$CIDR
routes:
- to: default
via: $GATEWAY
nameservers:
addresses: [${DNS_SERVERS//,/ }]
EOF
chmod 600 /etc/netplan/99-static-ip.yaml
netplan apply
echo "Verifying static IP configuration..."
ip addr show
echo "Static IP address configured: $STATIC_IP/$CIDR"
echo "Enabling UFW..."
yes | ufw enable
echo "Firewall (UFW) has been enabled."
echo "Configuring HAProxy..."
systemctl enable haproxy
systemctl start haproxy
echo "Generating SSL certificates..."
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SSL_CONFIG_PATH="$SCRIPT_DIR/ssl/distinguished.cnf"
export DYNAMIC_CN=$(hostname)
if [ ! -f "$SSL_CONFIG_PATH" ]; then
echo "Error: SSL configuration file not found at $SSL_CONFIG_PATH."
exit 1
fi
mkdir -p /etc/nginx/ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/nginx/ssl/host.key -out /etc/nginx/ssl/host.cert \
-config "$SSL_CONFIG_PATH"
mkdir -p /etc/apache2/ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/apache2/ssl/host.key -out /etc/apache2/ssl/host.cert \
-config "$SSL_CONFIG_PATH"
echo "Configuring Nginx with HTTPS redirection..."
rm -f /etc/nginx/sites-enabled/default
cat <<EOF > /etc/nginx/sites-enabled/default
server {
listen 80;
server_name $STATIC_IP;
# Redirect all HTTP traffic to HTTPS
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl;
server_name $STATIC_IP;
ssl_certificate /etc/nginx/ssl/host.cert;
ssl_certificate_key /etc/nginx/ssl/host.key;
location / {
root /var/www/html;
index index.html;
}
}
EOF
systemctl restart nginx
echo "Configuring Apache2 with HTTPS redirection..."
cat <<EOF > /etc/apache2/ports.conf
Listen 8080
Listen 4433
EOF
cat <<EOF > /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:8080>
# Redirect all HTTP traffic to HTTPS
Redirect permanent / https://$STATIC_IP:4433/
</VirtualHost>
<VirtualHost $STATIC_IP:4433>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/host.cert
SSLCertificateKeyFile /etc/apache2/ssl/host.key
DocumentRoot /var/www/html
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
systemctl restart apache2
echo "Configuring Fail2Ban..."
fail2ban_main_config="/etc/fail2ban/fail2ban.conf"
if grep -q "^#allowipv6 = auto" "$fail2ban_main_config"; then
echo "Uncommenting allowipv6 = auto in Fail2Ban main configuration..."
sed -i 's/^#allowipv6 = auto/allowipv6 = auto/' "$fail2ban_main_config"
fi
systemctl enable fail2ban
systemctl start fail2ban
echo "Service status:"
for svc in nginx apache2 haproxy fail2ban; do
if systemctl is-active --quiet $svc; then
echo "$svc is running."
else
echo "Error: $svc failed to start."
fi
done
echo "HA Cluster setup with web server configurations and SSL completed successfully!"