aboutsummaryrefslogtreecommitdiffstats
path: root/pgaas/src/stage/opt/app/pgaas/etc/do-post-install-config
blob: 87689efa201b854d1bdc22186bae5a7a0165dd7b (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
#!/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
}

[ -n "$CFGDIR" ] || die "CFGDIR is not set"
[ -n "$ONAP" ] || die "ONAP is not set"
[ -n "$NOTONAP" ] || die "NOTONAP is not set"

if [ -d ${INSTALL_ROOT}/opt/app/postgresql-9.5.2 ]
then PGDIR=${INSTALL_ROOT}/opt/app/postgresql-9.5.2
elif [ -d ${INSTALL_ROOT}/usr/lib/postgresql/9.6 ]
then PGDIR=${INSTALL_ROOT}/usr/lib/postgresql/9.6
elif [ -d ${INSTALL_ROOT}/usr/lib/postgresql/9.5 ]
then PGDIR=${INSTALL_ROOT}/usr/lib/postgresql/9.5
else die PostgreSQL bin directory not found
fi
export PGDIR
export DBROOT=/dbroot/pgdata/main
export PATH=$PATH:${INSTALL_ROOT}/opt/app/pgaas/bin

$CFGDIR/etc/makecerts 

cat $CFGDIR/lib/profile.additions >> ~postgres/.profile

# Determine which system is the master.
# For central, we look first in /var/config/DCAE/chef/*-postgres.conf. 
#	If we don't find that, we look at the pgnodes list and pick the first one.
# For edge, we ignore /var/config/DCAE/chef/*-postgres.conf and go directly to the pgnodes list. 
#	Each edge site has its own master.
clustertype=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n cluster )
ismaster=no

case $clustertype in
    central )
        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
            # not OpenDCAE/ONAP, but it's better if we STILL require a pgaas-postgres.conf file.
            die "No xyz-postgres.conf files were found in $CONFDIR1 or $CONFDIR2"
	    # Alternatively, the following code MIGHT work:
	    # ismaster=yes
	    # PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
	    # export MASTER=$( gen-repmgr-info -n "$PGNODES" -m )
	else
	    if [ $CONFCOUNT1 -eq 1 ]
	    then CONFDIR=$CONFDIR1
	    elif [ $CONFCOUNT2 -eq 1 ]
	    then CONFDIR=$CONFDIR2
	    else CONFDIR=
            fi
            if [ -n "$CONFDIR" ]
            then # OpenDCAE/ONAP
                # We need the value of master from xyz-postgres.conf
                umask 077
                TMP=$(mktemp /tmp/tmp.pi1.XXXXXXXXXX)
                trap 'rm -f $TMP' 0 1 2 3 15
                cat $CONFDIR/*-postgres.conf |
		    sed -e 's/ *: */="/' -e 's/[ 	]*$/"/' -e 's/=""/="/' -e 's/""$/"/' > $TMP
                . $TMP
                case "$master" in
                    `hostname` | `hostname -f` ) ismaster=yes ;;
                    *?* ) ismaster=no ;;
                    '' ) die "master is not set in $CONF" ;;
                esac
                PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
                export MASTER=$( gen-repmgr-info -n "$PGNODES" -M "$master" )
                [ -n "$MASTER" ] || die "Cannot determine master system. $CONF has '$master' (from env.yaml), which cannot be found in pgnodes."
	    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
	;;
    edge )
        host=$( hostname -f )
	PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
	export MASTER=$( gen-repmgr-info -n "$PGNODES" -C $host )
	case $MASTER in
	    '' ) die "Cannot determine master system. Does cdf.cfg have pgnodes= in it? Is $host listed as a site?" ;;
	    DEFAULT ) ismaster=yes MASTER=$host ;;
	esac
        ;;
esac

ssh_and_cdf_okay=no

if pgrep postgres
then pkill postgres
fi

case $ismaster in
    yes ) # master
	$CFGDIR/etc/create-ssh-master &&
	$CFGDIR/etc/create-cdf-master &&
	ssh_and_cdf_okay=yes
	;;

    no ) # secondary
	$CFGDIR/etc/create-ssh-secondary &&
	$CFGDIR/etc/create-cdf-secondary &&
	touch $CFGDIR/lib/ignore-database-reconfiguration # prevent dcae_admin_db.py from looking at json DB reconfigurations &&
	ssh_and_cdf_okay=yes
	;;
esac

[ "$ssh_and_cdf_okay" = yes ] || die "Could not set up ssh or cdf"

$CFGDIR/etc/common-db-tasks
# check if we have a database already
if [ ! -s $DBROOT/PG_VERSION ]
then
    # need to create it
    case $ismaster in
	yes ) $CFGDIR/etc/create-db-master ;;
	no  ) $CFGDIR/etc/create-db-secondary ;;
    esac
else
    # need to update it
    case $ismaster in
	yes ) $CFGDIR/etc/update-db-master ;;
	no  )
	    $CFGDIR/etc/create-db-secondary # use repmgr clone even if secondary previously existed
	    # $CFGDIR/etc/update-db-secondary
	    ;;
    esac
fi