aboutsummaryrefslogtreecommitdiffstats
path: root/pgaas/src/stage/opt/app/pgaas/etc/do-post-install-prep
blob: 02eabef0e290b0891de70ddbdbc4208aabb3be38 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this code except in compliance
# with the License. You may obtain a copy of the License
# at http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software  
# distributed under the License is distributed on an "AS IS" BASIS,  
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  
# implied. See the License for the specific language governing  
# permissions and limitations under the License. 

echo "================ $0 ================"
id
set -x

die()
{
    echo "$@" 1>&2
    echo $0: "$@"
    umask 022
    echo $0: "$@" >> /tmp/pgaas-failures
    exit 1
}

umask 022

TMP=$( mktemp /tmp/pgprep.$$.XXXXXXXXXX )
trap 'rm -f $TMP' 0 1 2 3 15

if $ONAP
then INSTALL_ROOT=
fi


# There needs to be a configuration file passed in, named something like pgaas-postgres.conf
# and stored either in /var/config/DCAE/chef or /tmp. We need at least one, and there cannot
# be more than one within the directory. (If one exists in both /var/config and /tmp, we trust
# the one in /var/config.)
#
# The variable "master" needs to be in xyz-postgres.conf. It should be the hostname or 
# hostname -f value of the VM that is the initial PG master.
# In addition, the following values could be in postgres.conf or
# already in the environment:
#    DRTR_NODE_KSTOREFILE: /opt/app/dcae-certificate/keystore.jks
#    DRTR_NODE_KSTOREPASS: "No Certificate"
#    DRTR_NODE_PVTKEYPASS: "No Certificate"
#    PG_NODES : uiopmno1qwpstg00.research.example.com|asbczw1vepstg00.dcae.simpledemo.onap.org
#    PG_JAVA_HOME : /opt/app/java/jdk/jdk170
#    PG_CLUSTER : global/central | site/edge (pick one)

CONFDIR1=/var/config/DCAE/chef
CONFDIR2=/tmp
CONFCOUNT1=$(ls $CONFDIR1/*-postgres.conf 2>/dev/null | wc -l)
CONFCOUNT2=$(ls $CONFDIR2/*-postgres.conf 2>/dev/null | wc -l)
CONFCOUNTBOTH=$(ls $CONFDIR1/*-postgres.conf $CONFDIR2/*-postgres.conf 2>/dev/null | wc -l)

if [ $CONFCOUNTBOTH -eq 0 ]
then
    die "No xyz-postgres.conf files were found in $CONFDIR1 or $CONFDIR2"
else
    if [ $CONFCOUNT1 -eq 1 ]
    then CONFDIR=$CONFDIR1
    elif [ $CONFCOUNT2 -eq 1 ]
    then CONFDIR=$CONFDIR2
    else CONFDIR=
    fi
    if [ -n "$CONFDIR" ]
    then # OpenDCAE/ONAP
	cat $CONFDIR/*-postgres.conf |
	    sed -e 's/ *: */="/' -e 's/[ 	]*$/"/' -e 's/=""/="/' -e 's/""$/"/' > $TMP
	. $TMP
    else
	if [ $CONFCOUNT1 -gt 1 ]
	then die "More than one postgres.conf was found in $CONFDIR1: $(ls $CONFDIR1/*-postgres.conf 2>&1)"
	elif [ $CONFCOUNT2 -gt 1 ]
	then die "More than one postgres.conf was found in $CONFDIR2: $(ls $CONFDIR2/*-postgres.conf 2>&1)"
	fi
    fi
fi

[ -n "$PG_NODES" ] || die "PG_NODES is not set"
[ -n "$PG_CLUSTER" ] || die "PG_CLUSTER is not set"
[ -n "$DRTR_NODE_KSTOREFILE" ] || die "DRTR_NODE_KSTOREFILE is not set"
[ -n "$DRTR_NODE_KSTOREPASS" ] || die "DRTR_NODE_KSTOREPASS is not set"
[ -n "$DRTR_NODE_PVTKEYPASS" ] || die "DRTR_NODE_PVTKEYPASS is not set"

# create various directories with proper permissions
mkdir -p ${INSTALL_ROOT}/dbroot/pgdata/main \
    ${INSTALL_ROOT}/dbroot/pgdata/pgaas \
    ${INSTALL_ROOT}/dbroot/pglogs/main \
    ${INSTALL_ROOT}/var/run/postgresql \
    ${INSTALL_ROOT}/opt/logs/dcae/postgresql/init \
    ${INSTALL_ROOT}/opt/logs/dcae/postgresql/server \
    ${INSTALL_ROOT}/opt/logs/dcae/postgresql/idns 
chmod 700 ${INSTALL_ROOT}/dbroot/pgdata/pgaas 
chmod 700 ${INSTALL_ROOT}/dbroot/pglogs
chmod 700 ${INSTALL_ROOT}/dbroot/pgdata/main

if $ONAP
then
    if [ -d /var/lib/postgresql/9.5 ]
    then VER=9.5
    elif [ -d /var/lib/postgresql/9.6 ]
    then VER=9.6
    else die "Cannot determine version of PostgreSQL. Looking for /var/lib/postgresql/9.[56]."
    fi
    mv /var/lib/postgresql/$VER/main /var/lib/postgresql/$VER/main.sv
    ln -s /dbroot/dbdata/main /var/lib/postgresql/$VER/main

    mv /etc/postgresql/$VER/main /etc/postgresql/$VER/main.sv
    ln -s /opt/app/pgaas/main /etc/postgresql/$VER/main

fi

chown -R postgres:postgres ${INSTALL_ROOT}/dbroot ${INSTALL_ROOT}/var/run/postgresql ${INSTALL_ROOT}/opt/logs/dcae/postgresql

chmod 711 ~postgres
if [ -d /opt/app/postgresql-9.5.2 ]
then PGDIR=/opt/app/postgresql-9.5.2
elif [ -d /usr/lib/postgresql/9.6 ]
then PGDIR=/usr/lib/postgresql/9.6
elif [ -d /usr/lib/postgresql/9.5 ]
then PGDIR=/usr/lib/postgresql/9.5
else echo PostgreSQL bin directory not found 1>&2; exit 1
fi
chmod 751 $PGDIR/bin

# fix up the CDF package so that it works
if [ ! -L /opt/cdf ]
then rm -f /opt/cdf; ln -sf /opt/app/cdf /opt/cdf
fi

PATH=${INSTALL_ROOT}/opt/app/pgaas/bin:$PATH

# and save some values within
(
    echo "allpgnodes=\"$PG_NODES\""
    case "$PG_CLUSTER" in
	global | central )
	    cnodes=$( gen-repmgr-info -n "$PG_NODES" -p )
	    echo "pgnodes=\"$cnodes\""
	    echo "cluster=central"
	    shanodes=$( gen-repmgr-info -n "$PG_NODES" -P )
	    ;;
        site | edge )
	    HOSTNAME=$( hostname -f )
	    lnodes=$( gen-repmgr-info -n "$PG_NODES" -e $HOSTNAME )
	    echo "pgnodes=\"$lnodes\""
	    if [ -z "$lnodes" ]
	    then die "Cannot determine the name of the system. hostname -f ($HOSTNAME) is not found in PG_NODES ($PG_NODES)"
	    fi
	    shanodes=$( gen-repmgr-info -n "$PG_NODES" -E $HOSTNAME )
	    echo "cluster=edge"
	    ;;
        * ) die "Cannot determine what type of cluster this is. PG_CLUSTER should be either 'global/central' or 'site/edge'" ;;
    esac

    echo "drtr_node_kstorefile=$DRTR_NODE_KSTOREFILE"
    echo "ENCRYPTME.AES.drtr_node_kstorepass='$DRTR_NODE_KSTOREPASS'" | ${INSTALL_ROOT}/opt/app/cdf/bin/setencryptedvalues
    echo "ENCRYPTME.AES.drtr_node_pvtkeypass='$DRTR_NODE_PVTKEYPASS'" | ${INSTALL_ROOT}/opt/app/cdf/bin/setencryptedvalues
    echo "ENCRYPTME.AES.wgetpswd=$shanodes" | ${INSTALL_ROOT}/opt/app/cdf/bin/setencryptedvalues
) >> ${INSTALL_ROOT}/opt/app/cdf/lib/cdf.cfg

# install the init scripts for postgresql
# init.d-pgaas init-pgaas-idns.conf init-pgaas-init.conf logrotate

INIT=${INSTALL_ROOT}/opt/app/pgaas/init

# take care of cron jobs
if [ -d ${INSTALL_ROOT}/opt/app/platform/cron ]
then
    CRONDIR=${INSTALL_ROOT}/opt/app/platform/cron/postgres
    mkdir -p $CRONDIR
    chown postgres:postgres $CRONDIR
    chmod 755 $CRONDIR
    cp $INIT/pglogs.cron $CRONDIR/pglogs.cron
    su postgres -c "sh -x ${INSTALL_ROOT}/opt/app/platform/bin/mergeCron"
else
    su postgres -c "crontab $INIT/pglogs.cron"
fi

# take care of PGaaS init job
if [ -d ${INSTALL_ROOT}/opt/app/platform/init.d ]
then
    INITDEST=${INSTALL_ROOT}/opt/app/platform/init.d/pgaas
    cp $INIT/init.d-pgaas $INITDEST
    chown postgres:postgres $INITDEST
    chmod 755 $INITDEST
else
    cd ${INSTALL_ROOT}/opt/app/platform/rc.d
    ln -sf ../init.d/pgaas K20pgaas
    ln -sf ../init.d/pgaas S20pgaas
fi

# take care of /var/run/postgresql directory
if [ -f /usr/lib/tmpfiles.d/postgresql.conf ]
then
    : # no need to create the /var/run/postgresql directory (probably because postgresql package already did it)
elif [ -d /usr/lib/tmpfiles.d ]
then
    (
	echo "# Directory for PostgreSQL sockets, lockfiles and stats tempfiles"
	echo "d /var/run/postgresql 2775 postgres postgres - -"
    ) > /usr/lib/tmpfiles.d/postgresql.conf
else
    # install an init script to create it
    cp $INIT/init-pgaas-init.conf ${INSTALL_ROOT}/etc/init/pgaas-init.conf
    service pgaas-init stop
    sleep 1
    service pgaas-init start
    sleep 1
fi
# double check to make sure it's been created
if [ ! -d /var/run/postgresql ]
then
    mkdir -p /var/run/postgresql
    chown postgres:postgres /var/run/postgresql
fi

# take care of iDNS-responder
if [ -d /lib/systemd/system -a -f /bin/systemctl ]
then
    # install the init script for iDNS in systemd
    cp $INIT/systemd-pgaas-idns.service /lib/systemd/system/pgaas-idns.service
#    if [ -f /usr/sbin/service ]
#    then
#        service pgaas-idns stop
#        sleep 1
#        service pgaas-idns start
#    else
#        systemctl stop pgaas-idns 
        sleep 1
	systemctl daemon-reload
        sleep 1
        systemctl enable pgaas-idns 
        sleep 1
        systemctl start pgaas-idns 
#    fi

else
    # install the init script for iDNS in upstart
    cp $INIT/init-pgaas-idns.conf ${INSTALL_ROOT}/etc/init/pgaas-idns.conf
    service pgaas-idns stop
    sleep 1
    service pgaas-idns start
fi

# take care of log rotation
cp $INIT/logrotate ${INSTALL_ROOT}/etc/logrotate.d/pgaas
chown root:root ${INSTALL_ROOT}/etc/logrotate.d/pgaas
chmod 644 ${INSTALL_ROOT}/etc/logrotate.d/pgaas

chown -R postgres:postgres ${INSTALL_ROOT}/opt/app/pgaas

echo ENDING $0 $(date)
echo ENDING $0 $(date) >> /tmp/pgaas.inst.report