@@ -6,18 +6,42 @@ set -o pipefail
6
6
function mask2cidr() {
7
7
NBITS=0
8
8
IFS=.
9
- for DEC in $1 ; do
9
+ for DEC in $1 ; do
10
10
case $DEC in
11
- 255) let NBITS+=8;;
12
- 254) let NBITS+=7 ; break ;;
13
- 252) let NBITS+=6 ; break ;;
14
- 248) let NBITS+=5 ; break ;;
15
- 240) let NBITS+=4 ; break ;;
16
- 224) let NBITS+=3 ; break ;;
17
- 192) let NBITS+=2 ; break ;;
18
- 128) let NBITS+=1 ; break ;;
19
- 0);;
20
- * ) echo " Error: ${DEC} is not recognised" ; exit 1
11
+ 255) let NBITS+=8 ;;
12
+ 254)
13
+ let NBITS+=7
14
+ break
15
+ ;;
16
+ 252)
17
+ let NBITS+=6
18
+ break
19
+ ;;
20
+ 248)
21
+ let NBITS+=5
22
+ break
23
+ ;;
24
+ 240)
25
+ let NBITS+=4
26
+ break
27
+ ;;
28
+ 224)
29
+ let NBITS+=3
30
+ break
31
+ ;;
32
+ 192)
33
+ let NBITS+=2
34
+ break
35
+ ;;
36
+ 128)
37
+ let NBITS+=1
38
+ break
39
+ ;;
40
+ 0) ;;
41
+ * )
42
+ echo " Error: ${DEC} is not recognised"
43
+ exit 1
44
+ ;;
21
45
esac
22
46
done
23
47
echo " ${NBITS} "
@@ -40,45 +64,45 @@ function create_hba() {
40
64
done
41
65
}
42
66
43
- function initializePostgreSQL() {
44
-
45
- # set stage for health check
46
- doguctl state installing
67
+ function write_pg_hba_conf() {
68
+ create_hba > " ${PGDATA} " /pg_hba.conf
69
+ }
47
70
48
- # install database
49
- gosu postgres initdb
71
+ function initializePostgreSQL() {
72
+ # set stage for health check
73
+ doguctl state installing
50
74
51
- # postgres user
52
- POSTGRES_USER= " postgres"
75
+ # install database
76
+ gosu postgres initdb
53
77
54
- # store the user
55
- doguctl config user " ${ POSTGRES_USER} "
78
+ # postgres user
79
+ POSTGRES_USER= " postgres "
56
80
57
- # create random password
58
- POSTGRES_PASSWORD= $( doguctl random )
81
+ # store the user
82
+ doguctl config user " ${POSTGRES_USER} "
59
83
60
- # store the password encrypted
61
- doguctl config -e password " ${POSTGRES_PASSWORD} "
84
+ # create random password
85
+ POSTGRES_PASSWORD= $( doguctl random )
62
86
63
- # open port
64
- sed -ri " s/^#(listen_addresses\s*=\s*)\S+/\1'*'/ " " $PGDATA " /postgresql.conf
87
+ # store the password encrypted
88
+ doguctl config -e password " ${POSTGRES_PASSWORD} "
65
89
66
- # set generated password
67
- echo " ALTER USER ${POSTGRES_USER} WITH SUPERUSER PASSWORD ' ${POSTGRES_PASSWORD} '; " | 2> /dev/null 1>&2 gosu postgres postgres --single -jE
90
+ # open port
91
+ sed -ri " s/^#(listen_addresses\s*=\s*)\S+/\1'*'/ " " $PGDATA " /postgresql.conf
68
92
69
- # generate pg_hba.conf
70
- create_hba > " ${PGDATA} " /pg_hba.conf
93
+ # set generated password
94
+ echo " ALTER USER ${POSTGRES_USER} WITH SUPERUSER PASSWORD ' ${POSTGRES_PASSWORD} '; " | gosu 2> /dev/null 1>&2 postgres postgres --single -jE
71
95
}
72
96
73
97
function waitForPostgreSQLStartup() {
74
- while ! pg_isready > /dev/null; do
98
+ while ! pg_isready > /dev/null; do
75
99
# Postgres is not ready yet to accept connections
76
100
sleep 0.1
77
101
done
78
102
}
79
103
80
104
function waitForPostgreSQLShutdown() {
81
- while pgrep -x postgres > /dev/null ; do
105
+ while pgrep -x postgres > /dev/null; do
82
106
# Postgres is still running
83
107
sleep 0.1
84
108
done
@@ -92,6 +116,7 @@ chown postgres:postgres /run/postgresql
92
116
93
117
if [ -z " $( ls -A " $PGDATA " ) " ]; then
94
118
initializePostgreSQL
119
+ write_pg_hba_conf
95
120
elif [ -e " ${PGDATA} " /postgresqlFullBackup.dump ]; then
96
121
# Moving backup and emptying PGDATA directory
97
122
mv " ${PGDATA} " /postgresqlFullBackup.dump /tmp/postgresqlFullBackup.dump
@@ -115,6 +140,10 @@ elif [ -e "${PGDATA}"/postgresqlFullBackup.dump ]; then
115
140
kill ${PID}
116
141
waitForPostgreSQLShutdown
117
142
echo " Database dump successfully restored"
143
+
144
+ write_pg_hba_conf
145
+ else
146
+ write_pg_hba_conf
118
147
fi
119
148
120
149
# set stage for health check
0 commit comments