-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabinit
More file actions
executable file
·141 lines (116 loc) · 4.88 KB
/
abinit
File metadata and controls
executable file
·141 lines (116 loc) · 4.88 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
#!/bin/bash
#
# abinit [ port ]
# this script sets up the database with users, database, and seeds it with initial data
# run this once, first, before running the sql container.
# then, run this again, as a new container, mounting the container you created with init.
#
# when finished it can idle on a port with a web server that does nothing.
# this can be handy for orchestrating initial startup
#
set -e
. /usr/local/etc/abenv
# fix permissions and ownership of ${PG_HOME}
mkdir -p -m 0700 ${PG_HOME}
chown -R postgres:postgres ${PG_HOME}
# fix permissions and ownership of /run/postgresql
mkdir -p -m 0755 /run/postgresql /run/postgresql/${PG_VERSION}-main.pg_stat_tmp
chown -R postgres:postgres /run/postgresql
chmod g+s /run/postgresql
#
# make sure the etc directory exists
#
if [ ! -d ${PG_SRC_CONF_DIR}/${PG_VERSION} ]; then
(cd ${PG_SRC_CONF_DIR}; tar xzvf ${PG_SRC_CONF})
chown -R postgres:postgres ${PG_SRC_CONF_DIR}
fi
# disable ssl
sed 's/ssl = true/#ssl = true/' -i ${PG_CONFDIR}/postgresql.conf
# listen on all interfaces
cat >> ${PG_CONFDIR}/postgresql.conf <<EOF
listen_addresses = '*'
EOF
#
# samenet is also 10.1.0.0/16 for fleet installations (many 10.1.X.0/24 networks)
if [ "${PSQL_TRUST_LOCALNET}" == "true" ]; then
echo "Enabling trust samenet in pg_hba.conf..."
cat >> ${PG_CONFDIR}/pg_hba.conf <<EOF
host all all 10.1.0.0/16 trust
host all all samenet trust
EOF
fi
# allow remote connections to postgresql database
cat >> ${PG_CONFDIR}/pg_hba.conf <<EOF
host all all 0.0.0.0/0 md5
EOF
cd ${PG_HOME}
# initialize PostgreSQL data directory
if [ ! -d ${PG_DATADIR} ]; then
# check if we need to perform data migration
PG_OLD_VERSION=$(find ${PG_HOME}/[0-9].[0-9]/main -maxdepth 1 -name PG_VERSION 2>/dev/null | sort -r | head -n1 | cut -d'/' -f5)
if [ ! -f "${PG_HOME}/pwfile" ]; then
PG_PASSWORD=$(pwgen -c -n -1 14)
echo "${PG_PASSWORD}" > ${PG_HOME}/pwfile
fi
echo "Initializing database..."
sudo -u postgres -H "${PG_BINDIR}/initdb" \
--pgdata="${PG_DATADIR}" --pwfile=${PG_HOME}/pwfile \
--username=postgres --encoding=unicode --auth=trust >/dev/null
fi
if [ -f ${PG_HOME}/pwfile ]; then
PG_PASSWORD=$(cat ${PG_HOME}/pwfile)
echo "|------------------------------------------------------------------|"
echo "| PostgreSQL User: postgres, Password: ${PG_PASSWORD} |"
echo "| |"
echo "| To remove the PostgreSQL login credentials from the logs, please |"
echo "| make a note of password and then delete the file pwfile |"
echo "| from the data store. |"
echo "|------------------------------------------------------------------|"
fi
if [ -n "${DB_USER}" ]; then
if [ -z "${DB_PASS}" ]; then
echo ""
echo "WARNING: "
echo " Please specify a password for \"${DB_USER}\". Skipping user creation..."
echo ""
DB_USER=
else
echo "Creating user \"${DB_USER}\"..."
echo "CREATE ROLE ${DB_USER} with LOGIN CREATEDB SUPERUSER PASSWORD '${DB_PASS}';" |
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null
fi
fi
if [ -n "${DB_NAME}" ]; then
for db in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_NAME}"); do
echo "Creating database \"${db}\"..."
echo "CREATE DATABASE ${db};" | \
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null
if [ "${DB_UNACCENT}" == "true" ]; then
echo "Installing unaccent extension..."
echo "CREATE EXTENSION IF NOT EXISTS unaccent;" | \
sudo -u postgres -H ${PG_BINDIR}/postgres --single ${db} \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null
fi
if [ -n "${DB_USER}" ]; then
echo "Granting access to database \"${db}\" for user \"${DB_USER}\"..."
echo "GRANT ALL PRIVILEGES ON DATABASE ${db} to ${DB_USER};" |
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null
fi
done
fi
echo "Done with initialization ... now seeding database with starting data"
echo "Starting PostgreSQL server..."
/etc/init.d/postgresql start
psql -Upostgres -h $(hostname -i) -f /usr/local/etc/PG.sql ${DB_NAME}
/etc/init.d/postgresql stop
echo "Done with initialization ... database "$DB_NAME" is ready to use"
echo "chaining abadm.py" "$@"
# this is our signal that we are done, if the wait type is "file"
if [ "${WAIT_TYPE-$DEFAULT_WAIT_TYPE}" == "file" ]; then
touch "${WAIT_FILE-$DEFAULT_WAIT_FILE}"
fi
# either way, we start up a local web service and just sit there. this is for the wait type "ethernet"
exec "abadm.py" "$@"