-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·107 lines (79 loc) · 4.29 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
#!/bin/bash
if [ ! -f "/pgdata/16/data/PG_VERSION" ]
then
thisHost=$(hostname)
### role of db baseded on hostname
role=$(echo $thisHost | cut -f2 -d "-")
hostPrefix=$(echo $thisHost | cut -f1 -d "-")
primaryHost="${hostPrefix}-1"
echo -e "node_id=$role" > /etc/repmgr.conf
echo -e "node_name=$thisHost" >> /etc/repmgr.conf
echo -e "conninfo='host=$thisHost user=repmgr password=repmgr dbname=repmgr connect_timeout=2' " >> /etc/repmgr.conf
echo -e "data_directory='/pgdata/16/data'" >> /etc/repmgr.conf
echo -e "pg_basebackup_options='--checkpoint=fast'" >> /etc/repmgr.conf
echo -e "pg_bindir='/usr/pgsql-16/bin/'" >> /etc/repmgr.conf
cp /pgsqlProfile /var/lib/pgsql/.pgsql_profile
chmod 666 /etc/repmgr.conf
chown postgres:postgres /etc/repmgr.conf
### If it's a primary, init the db otherwise, don't populate data dir for repmgr to setup clone
if [ $role -eq 1 ]; then
sudo -u postgres /usr/pgsql-16/bin/initdb -D /pgdata/16/data
echo "include = 'pg_custom.conf'" >> /pgdata/16/data/postgresql.conf
cp /pg_custom.conf /pgdata/16/data/
cp /pg_hba.conf /pgdata/16/data/
chown postgres:postgres /var/lib/pgsql/.pgsql_profile
chown postgres:postgres /pgdata/16/data/pg_custom.conf
chown postgres:postgres /pgdata/16/data/pg_hba.conf
### Start postgres and create some roles and voodoo
sudo -u postgres /usr/pgsql-16/bin/pg_ctl -D /pgdata/16/data start
sudo -u postgres psql -c "ALTER ROLE postgres PASSWORD 'postgres';"
sudo -u postgres psql -c "CREATE ROLE repmgr WITH SUPERUSER LOGIN PASSWORD 'repmgr';"
sudo -u postgres psql -c 'ALTER USER repmgr SET search_path TO repmgr, "$user", public;'
sudo -u postgres psql -c "CREATE DATABASE repmgr WITH OWNER repmgr;"
fi
### --- Lets create pgpass for repmgr to use. Pre populate with a few nodes
### --- Same password. Or just trust in pg_hba.conf
for i in {1..6};
do
dbhost="${hostPrefix}-${i}"
echo -e "${dbhost}:5432:replication:repmgr:repmgr" >> /var/lib/pgsql/.pgpass
echo -e "${dbhost}:5432:repmgr:repmgr:repmgr" >> /var/lib/pgsql/.pgpass
done
chmod 600 /var/lib/pgsql/.pgpass
chown postgres:postgres /var/lib/pgsql/.pgpass
### --- If this is a primary based on hostname ending in -1
### --- db should have been started above when identified as a primary role
if [ $role -eq 1 ]; then
sudo -u postgres /usr/pgsql-16/bin/repmgr -f /etc/repmgr.conf primary register
fi
### --- If its a standby, lets clone it form the primary
if [ $role -gt 1 ]; then
if [ -z "${STREAMFROM}" ]; then
sudo -u postgres /usr/pgsql-16/bin/repmgr -h $primaryHost -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
sudo -u postgres /usr/pgsql-16/bin/pg_ctl -D /pgdata/16/data start
sudo -u postgres /usr/pgsql-16/bin/repmgr -f /etc/repmgr.conf standby register
else
upstreamId=$(echo $STREAMFROM | cut -f2 -d "-")
sudo -u postgres /usr/pgsql-16/bin/repmgr -h $STREAMFROM -U repmgr -d repmgr -f /etc/repmgr.conf --upstream-node-id=$upstreamId standby clone
sudo -u postgres /usr/pgsql-16/bin/pg_ctl -D /pgdata/16/data start
sudo -u postgres /usr/pgsql-16/bin/repmgr -f /etc/repmgr.conf --upstream-node-id=$upstreamId standby register
fi
fi
else
### -- Jusat start postgres if the database was already there before
sudo -u postgres /usr/pgsql-16/bin/pg_ctl -D /pgdata/16/data start
fi
cp /id_rsa /var/lib/pgsql/.ssh/
cp /id_rsa.pub /var/lib/pgsql/.ssh/
cp /authorized_keys /var/lib/pgsql/.ssh/
chown -R postgres:postgres /var/lib/pgsql/.ssh
chmod 600 /var/lib/pgsql/.ssh/id_rsa
chmod 644 /var/lib/pgsql/.ssh/id_rsa.pub
chmod 644 /var/lib/pgsql/.ssh/authorized_keys
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config
/usr/sbin/sshd
rm -f /run/nologin
exec tail -f /dev/null