aboutsummaryrefslogtreecommitdiffstats
path: root/postgresql-prep/src/common/postinst
blob: 1f8eb391da71dcfc0e99e97a18391a4de430c7a0 (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
#!/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. 


exec 1> /tmp/postgresql-prep.out 2>&1
set -x
if [ -d /opt/app/postgresql-9.5.2 ]
then export OPENECOMP=false NOTOPENECOMP=true
else export OPENECOMP=true NOTOPENECOMP=false
fi

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

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

id
umask 022

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

if $OPENECOMP
then INSTALL_ROOT=
fi

CONFDIR=/var/config/DCAE/chef
CONFDIR2=/tmp
CONFCOUNT=$(ls $CONFDIR/*-postgres.conf $CONFDIR2/*-postgres.conf 2>/dev/null | wc -l)
case "$CONFCOUNT" in
1 )
    # master: masternode
    # master2: secondmasternode
    # in DCAE, these 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.openecomp.org
    # PG_JAVA_HOME : /opt/app/java/jdk/jdk170
    # PG_CLUSTER : global
    cat $CONFDIR/*-postgres.conf $CONFDIR2/*-postgres.conf 2>/dev/null |
	sed -e 's/ *: */="/' -e 's/[ 	]*$/"/' -e 's/=""/="/' -e 's/""$/"/' > $TMP
    . $TMP
    ;;
0 ) ;;
* )
    die "More than one postgres.conf was found: $(ls $CONFDIR/*-postgres.conf $CONFDIR2/*-postgres.conf 2>/dev/null)"
esac

[ -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/app/log/postgresql/init \
    ${INSTALL_ROOT}/opt/app/log/postgresql/server \
    ${INSTALL_ROOT}/opt/app/log/postgresql/idns 
chmod 700 ${INSTALL_ROOT}/dbroot/pgdata/pgaas 
chmod 700 ${INSTALL_ROOT}/dbroot/pglogs
chmod 700 ${INSTALL_ROOT}/dbroot/pgdata/main

if $OPENECOMP
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/postgresql-config/main /etc/postgresql/$VER/main

fi

chown -R postgres:postgres ${INSTALL_ROOT}/dbroot ${INSTALL_ROOT}/var/run/postgresql ${INSTALL_ROOT}/opt/app/log/postgresql ${INSTALL_ROOT}/opt/app/log/postgresql/idns

chmod 711 ~postgres

# 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

# and save some values within
(
    echo "allpgnodes=\"$PG_NODES\""
    case "$PG_CLUSTER" in
	global | central )
	    cnodes=$( ${INSTALL_ROOT}/opt/app/postgresql-prep/bin/gen-repmgr-info -n "$PG_NODES" -p )
	    echo "pgnodes=\"$cnodes\""
	    echo "cluster=central"
	    shanodes=$( ${INSTALL_ROOT}/opt/app/postgresql-prep/bin/gen-repmgr-info -n "$PG_NODES" -P )
	    ;;
        site | edge )
	    HOSTNAME=$( hostname -f )
	    lnodes=$( ${INSTALL_ROOT}/opt/app/postgresql-prep/bin/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=$( ${INSTALL_ROOT}/opt/app/postgresql-prep/bin/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/postgresql-prep/init

if [ -d /lib/systemd/system -a -f /bin/systemctl ]
then
    su postgres -c "crontab $INIT/pglogs.cron"

    # no need to create the /var/run directory because postgresql package already does it

    # install the init script for iDNS
    cp $INIT/systemd-pgaas-idns.service /lib/systemd/system/pgaas-idns.service
    systemctl stop pgaas-idns 
    sleep 1
    systemctl start pgaas-idns 

else
    INITDEST=${INSTALL_ROOT}/opt/app/platform/init.d/pgaas
    cp $INIT/init.d-pgaas $INITDEST
    chown postgres:postgres $INITDEST
    chmod 755 $INITDEST

    cd ${INSTALL_ROOT}/opt/app/platform/rc.d
    ln -sf ../init.d/pgaas K20pgaas
    ln -sf ../init.d/pgaas S20pgaas

    CRONDIR=${INSTALL_ROOT}/opt/app/platform/cron/postgres
    mkdir $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"

    # install the init script for init
    cp $INIT/init-pgaas-init.conf ${INSTALL_ROOT}/etc/init/pgaas-init.conf
    service pgaas-init stop
    sleep 1
    service pgaas-init start

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

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

echo ENDING $0 $(date)
echo ENDING $0 $(date) >> /tmp/pgaas.inst.report
if $NOTOPENECOMP
then sed -n '/^STARTING/,/^ENDING/p' `dirname $0`/../../proc_out >> /tmp/pgaas.inst.report 
fi