-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils.sh.erb
137 lines (122 loc) · 3.96 KB
/
utils.sh.erb
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
#!/bin/bash -eu
source /var/vcap/jobs/postgres/bin/pgconfig.sh
function pgexec() {
local database
database="${1}"
local command
command="${2}"
"${PACKAGE_DIR}/bin/psql" \
-U "vcap" \
-p "${PORT}" \
-d "${database}" \
-c "${command}"
}
function create_roles() {
echo "Creating roles..."
"${PACKAGE_DIR}/bin/psql" \
-U "vcap" \
-p "${PORT}" \
-d "postgres" -c "\set ON_ERROR_STOP on" -f "${JOB_DIR}/config/roles.sql"
}
function create_databases() {
echo "Creating databases..."
<% p("databases.databases", []).each do |database| %>
echo "Creating database <%= database["name"] %>..."
"${PACKAGE_DIR}/bin/psql" \
-U "vcap" \
-p "${PORT}" \
-d "postgres" \
-tc "SELECT 1 FROM pg_database WHERE datname = '<%= database["name"] %>'" \
| grep -q 1 || \
"${PACKAGE_DIR}/bin/psql" \
-U "vcap" \
-p "${PORT}" \
-d "postgres" \
-c "CREATE DATABASE \"<%= database["name"] %>\""
<% if database["citext"] %>
echo "Trying to install citext..."
pgexec "<%= database["name"] %>" "CREATE EXTENSION IF NOT EXISTS citext"
<% end %>
echo "Enabling pgcrypto extension..."
pgexec "<%= database["name"] %>" "CREATE EXTENSION IF NOT EXISTS pgcrypto"
<% if p("databases.collect_statement_statistics") %>
echo "Enabling pg_stat_statements extension..."
pgexec "<%= database["name"] %>" "CREATE EXTENSION IF NOT EXISTS pg_stat_statements"
<% end %>
<% p("databases.roles", []).each do |role| %>
echo "Granting public schema access to <%= role["name"] %> on <%= database["name"] %>"
"${PACKAGE_DIR}/bin/psql" \
-U "vcap" \
-p "${PORT}" \
-d "<%= database["name"] %>" \
-c "GRANT ALL ON schema public TO \"<%= role["name"] %>\""
<% end %>
<% end %>
}
function is_major() {
[ "${pgversion_current%.*}" != "${pgversion_upgrade_from%.*}" ]
}
function check_postgresql_versions(){
if [[ $(echo -e "$pgversion_upgrade_from\n$pgversion_current" | sort --version-sort | head --lines=1) != $pgversion_upgrade_from ]]; then
echo "The downgrade of the database instance is not supported."
exit 1
fi
}
function init_data_dir(){
if [ ! -f "${DATA_DIR}/postgresql.conf" ]; then
# initdb creates data directories
echo "Initializing data directory ${DATA_DIR}"
su - vcap -c "${PACKAGE_DIR}/bin/initdb -E utf8 --locale en_US.UTF-8 -D ${DATA_DIR}"
fi
}
function run_major_upgrade(){
if is_major && check_postgresql_versions; then
rm -rf ${DATA_DIR_PREVIOUS}
echo "Running a PostgreSQL major upgrade from ${pgversion_upgrade_from} to ${pgversion_current}"
touch "$POSTGRES_UPGRADE_LOCK"
chmod 755 "$POSTGRES_UPGRADE_LOCK"
init_data_dir
# Move to a directory writeable by vcap user
su - vcap <<EOF
set -e
if [ "$ENABLE_TRACE" != "0" ]; then
set -x
fi
mkdir -p "${PG_STORE_DIR}/pg_upgrade_tmp"
pushd "${PG_STORE_DIR}/pg_upgrade_tmp"
${PACKAGE_DIR}/bin/pg_upgrade \
--old-datadir ${DATA_DIR_OLD} \
--new-datadir ${DATA_DIR} \
--old-bindir ${PACKAGE_DIR_OLD}/bin \
--new-bindir ${PACKAGE_DIR}/bin \
--verbose
popd
mv "${DATA_DIR_OLD}" "${DATA_DIR_PREVIOUS}"
echo ${pgversion_current} > ${VERSION_FILE}
EOF
rm -f "${POSTGRES_UPGRADE_LOCK}"
fi
}
function run_minor_upgrade(){
if ! is_major && check_postgresql_versions; then
rm -rf ${DATA_DIR_PREVIOUS}
echo "Running a PostgreSQL minor upgrade from ${pgversion_upgrade_from} to ${pgversion_current}"
<% if p("databases.skip_data_copy_in_minor") %>
mv "${DATA_DIR_OLD}" "${DATA_DIR}"
<% else %>
mkdir -p "${DATA_DIR}"
chown -R vcap:vcap "${DATA_DIR}"
chmod 700 "${DATA_DIR}"
su - vcap <<EOF
set -e
if [ "$ENABLE_TRACE" != "0" ]; then
set -x
fi
pushd "${DATA_DIR_OLD}" > /dev/null
cp -r * ${DATA_DIR}
popd > /dev/null
mv "${DATA_DIR_OLD}" "${DATA_DIR_PREVIOUS}"
EOF
<% end %>
fi
}