-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
212 lines (171 loc) · 8.31 KB
/
install
File metadata and controls
212 lines (171 loc) · 8.31 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
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
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
source ./bits/bootstrap/logging
#-----------
# Configurations
#-----------
LAUNCHER_OWNER=${LAUNCHER_OWNER-$USER}
export LAUNCHER_DIR=${LAUNCHER_DIR-/var/local}
export POSTGRES_LAUNCHER_DIR=${POSTGRES_LAUNCHER_DIR-$LAUNCHER_DIR/postgres}
export DATA_DIR=${DATA_DIR-/var/local/data}
export LOG_DIR=${LOG_DIR-/var/log}
export SENSITIVE_DIR=${SENSITIVE_DIR-/var/local/sensitive}
export POSTGRES_LOG_DIR=${POSTGRES_LOG_DIR-$LOG_DIR/postgres}
export POSTGRES_DATA_DIR=${POSTGRES_DATA_DIR-$DATA_DIR/postgres}
export POSTGRES_SENSITIVE_DIR=${POSTGRES_SENSITIVE_DIR-$SENSITIVE_DIR/postgres}
export POSTGRES_PORT=${POSTGRES_PORT-5432}
export POSTGRES_HOST=${POSTGRES_HOST-"127.0.0.1:"}
export POSTGRES_MAJOR=${POSTGRES_MAJOR-9.4}
export POSTGRES_MINOR=${POSTGRES_MINOR-9.4.4}
export POSTGRES_VERSION=${POSTGRES_VERSION-$POSTGRES_MINOR-1.pgdg70+1}
POSTGRES_ADMIN_USERNAME=${POSTGRES_ADMIN_USERNAME-postgres}
POSTGRES_ADMIN_PASSWORD=${POSTGRES_ADMIN_PASSWORD-yth3fn0t}
POSTGRES_CONF=${POSTGRES_CONF-./bits/postgres/files/postgresql.conf}
POSTGRES_MAX_CONNECTIONS=${POSTGRES_MAX_CONNECTIONS-100}
POSTGRES_SHARED_BUFFERS=${POSTGRES_SHARED_BUFFERS-256MB}
POSTGRES_MAX_FILES_PER_PROCESS=${POSTGRES_MAX_FILES_PER_PROCESS-1000}
POSTGRES_DISPLAY=${POSTGRES_DISPLAY-standalone} # or internal
#-----------
# Install Script
#-----------
if [[ "$POSTGRES_DISPLAY" == "standalone" ]]; then
notify "Installing POSTGRES..."
else
notify " -- Installing POSTGRES..."
fi
PSQL_OUTPUT=/tmp/psql.log
if [[ "`which psql 2> /dev/null`" == "" ]] || [[ "`psql --version | grep 9.4`" == "" ]]; then
if [[ "$OS" == "redhat" ]] || [[ "$OS" == "centos" ]]; then
notify " -- Installing PSQL client on RHEL/CENTOS"
mkdir -p /var/tmp
yum install -y http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm
yum install -y postgresql93-server postgresql93-contrib
cp -f /usr/pgsql-9.3/bin/* /usr/local/bin/
elif [[ "$OS" == "ubuntu" ]]; then
notify " -- Installing PSQL client on Ubuntu"
$SUDO apt-get install -y postgresql-client > $PSQL_OUTPUT 2>&1
ERROR="Unable to install postgresql-client due to ..." ./bits/bootstrap/failonerrors $? $PSQL_OUTPUT
[ $? -ne 0 ] && exit 1
else
debug " -- Skipping PSQL client for Mac"
fi
else
debug " -- Psql client already installed on host"
fi
notify " -- Copying config files"
OWNER=$LAUNCHER_OWNER ./bits/bootstrap/mkdir \
LAUNCHER_DIR \
POSTGRES_LAUNCHER_DIR \
POSTGRES_LOG_DIR \
POSTGRES_DATA_DIR \
POSTGRES_SENSITIVE_DIR \
$POSTGRES_LAUNCHER_DIR/db \
$POSTGRES_LAUNCHER_DIR/config \
$POSTGRES_LAUNCHER_DIR/bin
cp ./bits/postgres/files/postgres.dockerfile ${POSTGRES_LAUNCHER_DIR}/Dockerfile
OWNER=$LAUNCHER_OWNER LAUNCHER_DIR=$POSTGRES_LAUNCHER_DIR BIT=postgres ./bits/docker/copydockerfile \
@POSTGRES_MAJOR@ $POSTGRES_MAJOR \
@POSTGRES_VERSION@ $POSTGRES_VERSION
TEMPLATE=./bits/postgres/files/bin LOCATION=$POSTGRES_LAUNCHER_DIR/bin ./bits/docker/copyallif
debug " -- Making logger available on node"
TEMPLATE=./bits/bootstrap/logging LOCATION=$POSTGRES_LAUNCHER_DIR/bin/logging ./bits/docker/copyif
TEMPLATE=./bits/bootstrap/failonerrors LOCATION=$POSTGRES_LAUNCHER_DIR/bin/failonerrors ./bits/docker/copyif
LAUNCHER_DIR=$POSTGRES_LAUNCHER_DIR NAME=postgres VERSION=$POSTGRES_MINOR ./bits/docker/build
echo "#!/bin/bash
# Don't run under -i as this script is called remotely via capistrano
# And it gets locked if you do
docker run -t \\
-v ${POSTGRES_LOG_DIR}:/log \\
-v ${POSTGRES_DATA_DIR}:/var/lib/postgresql/data \\
-e POSTGRES_PASSWORD=$POSTGRES_ADMIN_PASSWORD \\
postgres init_psql
exit 0
" > ${POSTGRES_LAUNCHER_DIR}/init
printf "%b" "#!/bin/bash
USER=\${USER-$POSTGRES_ADMIN_USERNAME}
PASSWORD=\${PASSWORD-$POSTGRES_ADMIN_PASSWORD}
DATABASE=\${DATABASE}
PGPASSFILE=${POSTGRES_SENSITIVE_DIR}/\${DATABASE}.pgpass
mkdir -p ${POSTGRES_SENSITIVE_DIR}
echo \"\" > \$PGPASSFILE
echo \"#hostname:port:database:username:password\" >> \$PGPASSFILE
echo \"*:*:\$DATABASE:\$USER:\$PASSWORD\" >> \$PGPASSFILE
chmod 600 \$PGPASSFILE
" > ${POSTGRES_LAUNCHER_DIR}/pgpass
printf "%b" "#!/bin/bash
DATABASE=\${1-mydb}
USER=\${2-$POSTGRES_ADMIN_USERNAME}
PASSWORD=\${3-$POSTGRES_ADMIN_PASSWORD}
TEMPLATE=\${4-DEFAULT}
DROP=\${DROP-false}
DEBUG=\${DEBUG-false}
PGPASSFILE=${POSTGRES_SENSITIVE_DIR}/\${DATABASE}.pgpass
DBFILE=${POSTGRES_LAUNCHER_DIR}/db/\$DATABASE
DUMPFILE=${POSTGRES_LAUNCHER_DIR}/db/dump_\$DATABASE
RESTOREFILE=${POSTGRES_LAUNCHER_DIR}/db/restore_\$DATABASE
# USE a pgpassfile when passwords are set, e.g. PGPASSFILE=/cenx/sensitive/postgres.pgpass
if [[ \"\$DEBUG\" == true ]] ; then
exec 3>&1
else
exec 3>&1 &>/dev/null
fi
if [[ \"\$DROP\" == true ]]; then
PGUSER=$POSTGRES_ADMIN_USERNAME PGHOST=postgres.local PGPORT=$POSTGRES_PORT psql -c \"DROP DATABASE \$DATABASE;\"
PGUSER=$POSTGRES_ADMIN_USERNAME PGHOST=postgres.local PGPORT=$POSTGRES_PORT psql -c \"DROP USER \$USER;\"
fi
PGUSER=$POSTGRES_ADMIN_USERNAME PGHOST=postgres.local PGPORT=$POSTGRES_PORT psql -c \"CREATE USER \$USER WITH SUPERUSER;\"
PGUSER=$POSTGRES_ADMIN_USERNAME PGHOST=postgres.local PGPORT=$POSTGRES_PORT psql -c \"ALTER USER \$USER WITH PASSWORD '\$PASSWORD';\"
PGUSER=$POSTGRES_ADMIN_USERNAME PGHOST=postgres.local PGPORT=$POSTGRES_PORT psql -c \"CREATE DATABASE \$DATABASE OWNER \$USER TEMPLATE \$TEMPLATE;\"
DATABASE=\$DATABASE USER=\$USER PASSWORD=\$PASSWORD ${POSTGRES_LAUNCHER_DIR}/pgpass
mkdir -p ${POSTGRES_LAUNCHER_DIR}/db
echo \"PGUSER=\$USER PGHOST=postgres.local PGDATABASE=\$DATABASE PGPASSFILE=\$PGPASSFILE psql \\\$@\" > \$DBFILE
echo \"PGDATABASE=\$DATABASE PGPASSFILE=\$PGPASSFILE pg_dump --host=postgres.local --dbname=\$DATABASE --username=\$USER --role=\$USER --no-password --format=c --file=\\\$@\" > \$DUMPFILE
echo \"PGHOST=postgres.local pg_restore --dbname=postgres --username=\$USER --role=\$USER --create --no-privileges --no-owner --jobs=10 \\\$@\" > \$RESTOREFILE
chmod 755 \$DBFILE \$DUMPFILE \$RESTOREFILE
" > ${POSTGRES_LAUNCHER_DIR}/newdb
printf "%b" "#!/bin/bash
DATABASE=\${1-mydb}
USER=\${2-$POSTGRES_ADMIN_USERNAME}
PASSWORD=\${3-$POSTGRES_ADMIN_PASSWORD}
TEMPLATE=\${4-DEFAULT}
DEBUG=\${DEBUG-false}
PGPASSFILE=${POSTGRES_SENSITIVE_DIR}/\${DATABASE}.pgpass
DBFILE=${POSTGRES_LAUNCHER_DIR}/db/\$DATABASE
DUMPFILE=${POSTGRES_LAUNCHER_DIR}/db/dump_\$DATABASE
RESTOREFILE=${POSTGRES_LAUNCHER_DIR}/db/restore_\$DATABASE
echo \"PGUSER=\$USER PGHOST=postgres.local PGDATABASE=\$DATABASE PGPASSFILE=\$PGPASSFILE psql \\\$@\" > \$DBFILE
echo \"PGDATABASE=\$DATABASE PGPASSFILE=\$PGPASSFILE pg_dump --host=postgres.local --dbname=\$DATABASE --username=\$USER --role=\$USER --no-password --format=c --file=\\\$@\" > \$DUMPFILE
echo \"PGHOST=postgres.local pg_restore --dbname=postgres --username=\$USER --role=\$USER --create --no-privileges --no-owner --jobs=10 \\\$@\" > \$RESTOREFILE
chmod 755 \$DBFILE \$DUMPFILE \$RESTOREFILE
" > ${POSTGRES_LAUNCHER_DIR}/accessdb
NAME=postgres HOST=postgres.local DIR=$POSTGRES_LAUNCHER_DIR BIT=postgres ./bits/docker/helpers
chmod 755 $POSTGRES_LAUNCHER_DIR/init
chmod 755 $POSTGRES_LAUNCHER_DIR/start
chmod 755 $POSTGRES_LAUNCHER_DIR/newdb
chmod 755 $POSTGRES_LAUNCHER_DIR/accessdb
chmod 755 $POSTGRES_LAUNCHER_DIR/pgpass
notify " -- Initializing DB ($POSTGRES_LAUNCHER_DIR/init)"
SCRIPT_OUTPUT=$($POSTGRES_LAUNCHER_DIR/init > $PSQL_OUTPUT 2>&1)
ERROR="Unable to init postgres due to ..." ./bits/bootstrap/failonerrors $? $PSQL_OUTPUT
[ $? -ne 0 ] && exit 1
if [[ "$OS" == "mac" ]]; then
debug " -- Permissions hack on $POSTGRES_DATA_DIR setting full access"
$SUDO chmod -R 700 $POSTGRES_DATA_DIR
$SUDO chown -R $LAUNCHER_OWNER $POSTGRES_DATA_DIR
fi
if [[ -e ${POSTGRES_DATA_DIR}/PG_VERSION ]]; then
notify " -- Setting up postgres conf"
TEMPLATE=${POSTGRES_CONF} \
LOCATION=${POSTGRES_DATA_DIR}/postgresql.conf \
./bits/docker/copyif @POSTGRES_MAX_CONNECTIONS@ $POSTGRES_MAX_CONNECTIONS @POSTGRES_SHARED_BUFFERS@ $POSTGRES_SHARED_BUFFERS @POSTGRES_MAX_FILES_PER_PROCESS@ $POSTGRES_MAX_FILES_PER_PROCESS
else
error "Initializing postgres db did not appear to work, please investigate $POSTGRES_LAUNCHER_DIR/init"
exit 1
fi
notify " -- Configuring helper access to postgres"
$POSTGRES_LAUNCHER_DIR/accessdb postgres $POSTGRES_ADMIN_USERNAME $POSTGRES_ADMIN_PASSWORD
DIR=./bits/postgres ./bits/bootstrap/cleanup
if [[ "$POSTGRES_DISPLAY" == "standalone" ]]; then
notify "Done, Installing POSTGRES."
else
notify " -- Done, Installing POSTGRES."
fi