-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·202 lines (164 loc) · 5.28 KB
/
entrypoint.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
#!/bin/bash
PGHOME=$(su postgres -c 'echo $HOME')
PGCONFDIR=/etc/postgresql/10/main
CLUSTERCONF=${CLUSTERCONF:-/var/run/cluster_members}
get_cluster_nodes () {
cat $CLUSTERCONF | sed -E 's/^\* //'
}
count_cluster_nodes () {
get_cluster_nodes | wc -l
}
get_master_node () {
cat $CLUSTERCONF | grep '^*' | sed -E 's/^\* //'
}
is_master_node () {
[ "$(get_master_node)" == "$HOSTNAME" ]
}
get_node_id () {
grep -ne "$HOSTNAME" $CLUSTERCONF | cut -f1 -d:
}
cluster_exit () {
echo "---> EXIT <---"
echo "$(grep -ve "$HOSTNAME" $CLUSTERCONF)" \
> $CLUSTERCONF
exit
}
export -f cluster_exit
trap cluster_exit SIGSTOP SIGTERM
if [ $(count_cluster_nodes) -eq 0 ]; then
echo "---> NEW MASTER <---"
echo "* $HOSTNAME" >> $CLUSTERCONF
elif ! grep -qe "$HOSTNAME" $CLUSTERCONF; then
echo "---> NEW NODE <---"
echo "$HOSTNAME" >> $CLUSTERCONF
else
echo "---> KNOWN NODE <---"
fi
if [ ! -d "$PGHOME/.ssh" ]; then
mkdir $PGHOME/.ssh
ssh-keygen -t rsa -b 2048 -N '' -f $PGHOME/.ssh/id_rsa
echo "StrictHostKeyChecking no" > $PGHOME/.ssh/config
chown postgres:postgres -R $PGHOME/.ssh
fi
# Start the first process
service ssh start
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start sshd: $status"
exit $status
else
ssh-keyscan $(get_cluster_nodes) \
> $PGHOME/.ssh/known_hosts
chown postgres:postgres $PGHOME/.ssh/known_hosts
if ! is_master_node; then
until su postgres -c "ssh $(get_master_node) exit"; do
echo "[ssh] Waiting to connect to master..."
echo "[ssh] Maybe you didn't copy the public keys."
sleep 1
done
fi
fi
if [ ! -f $PGCONFDIR/bootstraped ]; then
pg_createcluster --datadir=$PGDATA 10 main
sed -Ei "s/#?listen_addresses.*/listen_addresses = '\*'/" $PGCONFDIR/postgresql.conf
sed -Ei "s/#?shared_preload_libraries.*/shared_preload_libraries = 'repmgr'/" $PGCONFDIR/postgresql.conf
echo "include '$PGCONFDIR/postgresql.replication.conf'" >> $PGCONFDIR/postgresql.conf
cat <<-EOF > $PGCONFDIR/postgresql.replication.conf
max_wal_senders = 15
max_replication_slots = 15
wal_level = 'replica'
hot_standby = on
archive_mode = on
archive_command = '/bin/true'
wal_keep_segments = 500
wal_log_hints = on
EOF
cat <<-EOF >> $PGCONFDIR/pg_hba.conf
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr $PRIVNET trust
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host repmgr repmgr $PRIVNET trust
EOF
cat <<-EOF > /etc/repmgr.conf
node_id=$(get_node_id)
node_name='$HOSTNAME'
conninfo='host=$HOSTNAME port=5432 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='$PGDATA'
use_replication_slots=yes
monitoring_history=yes
service_start_command = 'sudo /usr/bin/pg_ctlcluster 10 main start'
service_stop_command = 'sudo /usr/bin/pg_ctlcluster 10 main stop'
service_restart_command = 'sudo /usr/bin/pg_ctlcluster 10 main restart'
service_reload_command = 'sudo /usr/bin/pg_ctlcluster 10 main reload'
service_promote_command = 'sudo /usr/bin/pg_ctlcluster 10 main promote'
promote_check_timeout = 15
failover=automatic
promote_command='/usr/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
follow_command='/usr/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
log_file='/var/log/postgresql/repmgrd.log'
repmgrd_service_start_command = 'sudo /etc/init.d/repmgrd start'
repmgrd_service_stop_command = 'sudo /etc/init.d/repmgrd stop'
EOF
cat <<-EOF > /etc/default/repmgrd
REPMGRD_ENABLED=yes
REPMGRD_CONF="/etc/repmgr.conf"
REPMGRD_OPTS="--daemonize=false"
REPMGRD_USER=postgres
REPMGRD_BIN=/usr/bin/repmgrd
REPMGRD_PIDFILE=/var/run/repmgrd.pid
EOF
if is_master_node; then
service postgresql start
until su postgres -c 'psql -tc "select current_timestamp;"'; do
echo "[psql] Waiting to connect to postgres..."
sleep 1
done
su postgres -c 'createuser -s repmgr'
su postgres -c 'createdb repmgr -O repmgr'
su postgres -c 'repmgr -f /etc/repmgr.conf primary register'
service postgresql stop
else
until psql -h $(get_master_node) -U repmgr -tc "select current_timestamp;"; do
echo "[psql] Waiting to connect to master..."
sleep 2
done
rm -rf $PGDATA/*
su postgres -c "repmgr -h $(get_master_node) -U repmgr -d repmgr -f /etc/repmgr.conf standby clone"
service postgresql start
su postgres -c 'repmgr standby register --force'
service postgresql stop
fi
fi
service postgresql start
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start postgresql: $status"
exit $status
else
until su postgres -c 'psql -tc "select current_timestamp;"'; do
echo "[psql] Waiting to connect to postgres..."
sleep 1
done
su postgres -c 'repmgr daemon start'
fi
if [ ! -f $PGCONFDIR/bootstraped ]; then
echo "---> BOOTSTRAPED <---"
touch $PGCONFDIR/bootstraped
else
echo "---> START <---"
fi
while /bin/true; do
ps aux | grep -q [s]shd
PROCESS_1_STATUS=$?
ps aux | grep -q [p]ostgresql
PROCESS_2_STATUS=$?
ps aux | grep -q [r]epmgrd
PROCESS_3_STATUS=$?
if [ $PROCESS_1_STATUS -ne 0 -a $PROCESS_2_STATUS -ne 0 -a $PROCESS_3_STATUS -ne 0 ]; then
echo "No main processes running"
exit -1
fi
sleep 1
done