aboutsummaryrefslogtreecommitdiffstats
path: root/postgresql-config/src/stage/opt/app/postgresql-config/etc/do-post-install
blob: a20edc6cb54f218f1d9967ce4791e1e71be30d83 (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
#!/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. 


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 "$OPENECOMP" ] || die "OPENECOMP is not set"
[ -n "$NOTOPENECOMP" ] || die "NOTOPENECOMP 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/postgresql-prep/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 )
	CONFDIR=/var/config/DCAE/chef
	CONFCOUNT=$(ls $CONFDIR/*-postgres.conf 2>/dev/null | wc -l)
	case "$CONFCOUNT" in
	1 ) # OpenDCAE
	    umask 077
	    TMP=$(mktemp /tmp/tmp.pi1.XXXXXXXXXX)
	    trap 'rm -f $TMP' 0 1 2 3 15
	    sed -e 's/ *: */="/' -e 's/ *$/"/' -e 's/=""/="/' -e 's/""$/"/' < $CONFDIR/*-postgres.conf > $TMP
	    . $TMP
	    case `hostname` in
		$master ) 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."
	    ;;
	0 ) # not OpenDCAE
	    ismaster=yes
	    PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
	    export MASTER=$( gen-repmgr-info -n "$PGNODES" -m )
	    ;;
	* ) die "More than one postgres.conf was found: $(ls $CONFDIR/*-postgres.conf 2>&1)"
	    ;;
	esac
	;;
    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