1 #!/usr/bin/ksh 2 # 3 # CDDL HEADER START 4 # 5 # The contents of this file are subject to the terms of the 6 # Common Development and Distribution License (the License). 7 # You may not use this file except in compliance with the License. 8 # 9 # You can obtain a copy of the license at usr/src/CDDL.txt 10 # or http://www.opensolaris.org/os/licensing. 11 # See the License for the specific language governing permissions 12 # and limitations under the License. 13 # 14 # When distributing Covered Code, include this CDDL HEADER in each 15 # file and include the License file at usr/src/CDDL.txt. 16 # If applicable, add the following below this CDDL HEADER, with the 17 # fields enclosed by brackets [] replaced with your own identifying 18 # information: Portions Copyright [yyyy] [name of copyright owner] 19 # 20 # CDDL HEADER END 21 # 22 23 # 24 # Copyright 2007 Sun Microsystems, Inc. All rights reserved. 25 # Use is subject to license terms. 26 # 27 28 #ident "@(#)pgs_db_prep.ksh 1.7 07/06/06 SMI" 29 # 30 # This utility prepares the PostreSQL database to be monitored by the Sun Cluster HA for PostgreSQL agent. 31 # If the database does not exist, it will be created. 32 # 33 # 34 # This script takes 1 option. 35 # -f filename states a config file different from pgs_config. 36 # This file will be sourced instead of pgs_config if -f filename is specified 37 38 # Set generic variables: 39 40 BINDIR=/opt/SUNWscPostgreSQL/bin 41 UTILDIR=/opt/SUNWscPostgreSQL/util 42 EGREP=/usr/bin/grep 43 ECHO=/usr/bin/echo 44 45 MYCONFIG= 46 47 typeset opt 48 49 while getopts 'f:' opt 50 do 51 case "${opt}" in 52 f) MYCONFIG=${OPTARG};; 53 *) ${ECHO} "ERROR: ${MYNAME} Option ${OPTARG} unknown - early End. Only -f is valid" 54 exit 1;; 55 esac 56 done 57 58 # Sourcing the specified config file, either the default one, 59 # or the one supplied with -f 60 61 if [ -n "${MYCONFIG}" ] && [ -f "${MYCONFIG}" ] 62 then 63 ${ECHO} "sourcing ${MYCONFIG} " 64 . ${MYCONFIG} 65 else 66 PKGCONF=`dirname $0`/pgs_config 67 ${ECHO} "sourcing ${PKGCONF}" 68 . ${PKGCONF} 69 fi 70 71 if [ -z "${LD_LIBRARY_PATH}" ] 72 then 73 unset LD_LIBRARY_PATH 74 fi 75 76 77 78 # validate the pgroot variable 79 80 if [ -z "${PGROOT}" ] 81 then 82 ${ECHO} " ERROR: The PGROOT directory variable is not set" 83 exit 1 84 fi 85 86 if [ ! -d ${PGROOT} ] 87 then 88 ${ECHO} "ERROR: The PGROOT directory ${PGROOT} does not exist or is not a directory" 89 exit 1 90 fi 91 92 if [ ! -f ${PGROOT}/bin/psql ] 93 then 94 ${ECHO} "ERROR: The PGROOT directory ${PGROOT} does not contain the psql command" 95 exit 1 96 fi 97 98 # validate by connecting to the database 99 100 ${PGROOT}/bin/psql -p ${PGPORT} -l >/dev/null 2>&1 101 if [ $? -ne 0 ] 102 then 103 ${ECHO} "ERROR: A connect to PostgreSQL is impossible, check 2 conditions: " 104 ${ECHO} "ERROR: 1. is the postmaster running?" 105 ${ECHO} "ERROR: 2. does it listen to port ${PGPORT}?" 106 exit 1 107 fi 108 109 # Validate, that SCDB SCUSER SCTABLE are set 110 111 if [ -z "${SCDB}" ] -o [ -z "${SCUSER}" ] -o [ -z "${SCTABLE}" ] 112 then 113 ${ECHO} "ERROR: At least one of the variables SCDB=${SCDB}, SCUSER=${SCUSER} or SCTABLE=${SCTABLE} is not set" 114 exit 1 115 fi 116 117 if ! ${PGROOT}/bin/psql -p ${PGPORT} -l |${EGREP} " ${SCDB} " >/dev/null 2>&1 118 then 119 export PGPORT PGDATA 120 ${ECHO} "create the database ${SCDB}" 121 ${PGROOT}/bin/createdb ${SCDB} 122 fi 123 124 # Check if the user does exist 125 126 if ${PGROOT}/bin/psql -p ${PGPORT} -d ${SCDB} -c "select usename from pg_user"|${EGREP} " ${SCUSER}$| ${SCUSER} " >/dev/null 2>&1 127 then 128 echo "Warning the user ${SCUSER} exists" 129 else 130 # create the user 131 132 ${PGROOT}/bin/psql -p ${PGPORT} -d ${SCDB} -c "create user ${SCUSER}" 133 134 fi 135 136 if [ -n "${SCPASS}" ] 137 then 138 ${ECHO} "set the password ${SCPASS} for the user ${SCUSER}" 139 PGPASSWORD=${SCPASS} 140 ${PGROOT}/bin/psql -p ${PGPORT} -d ${SCDB} -c "alter user ${SCUSER} with password '${SCPASS}'" 141 fi 142 143 # create the table 144 145 ${ECHO} "create the table ${SCTABLE}" 146 ${PGROOT}/bin/psql -p ${PGPORT} -d ${SCDB} -U ${SCUSER} -c "create table ${SCTABLE} (sccol varchar(30))" 147 148 exit 0 149