New pgs_register.ksh
  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 2008 Sun Microsystems, Inc.  All rights reserved.
 25 # Use is subject to license terms.
 26 #
 27 
 28 #ident  "%Z%%M% %I%     %E% SMI"
 29 
 30 #
 31 #  This script takes 1 options.
 32 #  -f filename states a config file different from pgs_config.
 33 #     This file will be sourced instead of pgs_config if -f filename is specified
 34 #
 35 
 36 
 37 # Set generic variables:
 38 
 39 BINDIR=/opt/SUNWscPostgreSQL/bin
 40 UTILDIR=/opt/SUNWscPostgreSQL/util
 41 SMFUTILDIR=/opt/SUNWsczone/sczsmf/util
 42 DIRNAME=/usr/bin/dirname
 43 ECHO=/usr/bin/echo
 44 STTY=/usr/bin/stty
 45 
 46 MYNAME=`basename ${0}`
 47 MYDIR=`dirname ${0}`
 48 
 49 . ${MYDIR}/../etc/config
 50 . ${MYDIR}/../lib/functions_static
 51 . ${MYDIR}/../bin/functions
 52 
 53 
 54 global_zone()
 55 {
 56         # function to register a resource in the Solaris 10 global zone or on Solaris 8 or 9
 57         
 58         RESOURCEGROUP=${RG}
 59 
 60         if ! create_pfile
 61         then
 62               return 1
 63         fi 
 64 
 65         # define your start, stop, probe an validate command
 66 
 67         start_command="${BINDIR}/control_pgs -R ${RS} -G ${RG} -P ${PFILE} start"
 68         stop_command="${BINDIR}/control_pgs -R ${RS} -G ${RG} -P ${PFILE} stop"
 69         probe_command="${BINDIR}/control_pgs -R ${RS} -G ${RG} -P ${PFILE} probe"
 70         validate_command="${BINDIR}/control_pgs -R ${RS} -G ${RG} -P ${PFILE} validate"
 71 
 72         # determine whether the resource runs in the global zone or in a zone node
 73         # and set the zcommand to the appropriate zlogin comand
 74 
 75         zcommand=
 76         rgs_zone=$(rgs_zonename)
 77         if [ -n "${rgs_zone}" ]
 78         then
 79                 zcommand="${ZLOGIN} ${rgs_zone} " 
 80         fi
 81         
 82         # create the <resorce name>-phrase if required
 83 
 84         if [ -n "${SSH_PASSDIR}" ] 
 85         then
 86                 if ! store_passphrase
 87                 then
 88                         return 1
 89                 fi
 90         fi
 91 
 92         # register your resource
 93 
 94         if /usr/cluster/bin/scrgadm -pvv -t SUNW.gds | /usr/bin/grep Validate_command>/dev/null 2>&1
 95         then
 96 
 97                 # register in 3.2 style
 98 
 99                 if [ -n "${LH}" ]
100                 then
101                         /usr/cluster/bin/scrgadm -a -j ${RS} -g ${RG} -t SUNW.gds \
102                         -x Start_command="${start_command}" \
103                         -x Stop_command="${stop_command}" \
104                         -x Probe_command="${probe_command}" \
105                         -x Validate_command="${validate_command}" \
106                         -y Port_list=${PORT}/tcp -y Network_resources_used=${LH} \
107                         -x Stop_signal=9 -y Retry_interval=900 \
108                         -y Resource_dependencies=${HAS_RS}
109                         
110                         St=$?
111                 else
112                         /usr/cluster/bin/scrgadm -a -j ${RS} -g ${RG} -t SUNW.gds \
113                         -x Start_command="${start_command}" \
114                         -x Stop_command="${stop_command}" \
115                         -x Probe_command="${probe_command}" \
116                         -x Validate_command="${validate_command}" \
117                         -x Network_aware=false \
118                         -x Stop_signal=9 -y Retry_interval=900 \
119                         -y Resource_dependencies=${HAS_RS}
120                         
121                         St=$?
122                 fi
123         else
124 
125                 # register in 3.1 style
126 
127                 if [ -n "${LH}" ]
128                 then
129                         /usr/cluster/bin/scrgadm -a -j ${RS} -g ${RG} -t SUNW.gds \
130                         -x Start_command="${start_command}" \
131                         -x Stop_command="${stop_command}" \
132                         -x Probe_command="${probe_command}" \
133                         -y Port_list=${PORT}/tcp -y Network_resources_used=${LH} \
134                         -x Stop_signal=9 -y Retry_interval=900 \
135                         -y Resource_dependencies=${HAS_RS}
136                         
137                         St=$?
138                 else
139                         /usr/cluster/bin/scrgadm -a -j ${RS} -g ${RG} -t SUNW.gds \
140                         -x Start_command="${start_command}" \
141                         -x Stop_command="${stop_command}" \
142                         -x Probe_command="${probe_command}" \
143                         -x Network_aware=false \
144                         -x Stop_signal=9 -y Retry_interval=900 \
145                         -y Resource_dependencies=${HAS_RS}
146                         
147                         St=$?
148                 fi
149                 
150         fi
151         
152         if [ "${St}" -ne 0 ]; then
153                 ${ECHO} "Registration of resource ${RS} failed, please correct the wrong parameters."
154         
155                 return 1
156         else
157                 ${ECHO} "Registration of resource ${RS} succeeded."
158         fi
159         
160         # validate the resource only, if the validation is not present
161 
162         if ! /usr/cluster/bin/scrgadm -pvv -t SUNW.gds | /usr/bin/grep Validate_command>/dev/null 2>&1
163         then
164 
165                 # VALIDATE RESOURCE
166                 
167                 ${ECHO} "Validate resource ${RS} in resourcegroup ${RG}"
168                 
169                 # validate the contents of the parameter file
170         
171                 ${zcommand} ${BINDIR}/control_pgs -R ${RS} -G ${RG} -P ${PFILE} validate
172                 
173                 St=$?
174                 
175                 if [ "${St}" -ne 0 ]; then
176                         ${ECHO} "Validation of resource ${RS} failed, please correct the wrong parameters."
177                         ${ECHO} "Removing resource ${RS} from the cluster configuration."
178                 
179                         /usr/cluster/bin/scrgadm -r -j ${RS}
180                 
181                         return 1
182                 else
183                         ${ECHO} "Validation of resource ${RS} succeeded."
184                 fi
185         fi
186         
187         return 0
188 
189 }
190 
191 local_zone_smf()
192 {
193 
194 # function to register a smf resource
195 
196         SERVICE_TAG=svc:/application/sczone-agents:${RS}
197 
198         if [ ! -f ${SMFUTILDIR}/sczsmf_config ]
199         then
200                 ${ECHO}  ${SMFUTILDIR}/sczsmf_config does not exist. Make sure, that \
201                       Sun Cluster HA for Solaris container is intalled in the global zone
202                 return 1
203         fi
204 
205         
206         # create the <resorce name>-phrase if required
207 
208         zcommand="${ZLOGIN} ${ZONE} " 
209         
210         # create the <resorce name>-phrase if required
211 
212         if [ -n "${SSH_PASSDIR}" ] 
213         then
214                 if  store_passphrase
215                 then
216                         ${zcommand} ${CHOWN} ${USER} ${SSH_PASSDIR}/${RS}-phrase
217                 else
218                         return 1
219                 fi
220         fi
221                 
222 
223 # prepare the config file
224 
225         cp ${SMFUTILDIR}/sczsmf_config ${SMFUTILDIR}/sczsmf_config.work
226         cat << EOF > ${SMFUTILDIR}/sczsmf_config 
227 
228 #
229 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230 # Use is subject to license terms.
231 #
232 # This file will be sourced in by sczsmf_register and the parameters
233 # listed below will be used.
234 #
235 # These parameters can be customized in (key=value) form
236 #
237 #               RS - Name of the resource
238 #               RG - Name of the resource group containing RS
239 #         SCZBT_RS - Name of the SC Zone boot resource
240 #             ZONE - Name of the Zone
241 #
242 #       For SERVICE, RECURSIVE and STATE, refer to the svcadm(1M)
243 #                 man page
244 #
245 #          SERVICE - {FMRI | pattern}
246 #               FMRI - Fault management resource identifier
247 #               pattern - Pattern matching a service
248 #
249 #        RECURSIVE - {false | true}     Default: true
250 #               False - Just enable the service and no dependents
251 #               True - Enable the service and recursively enable
252 #                 its dependents
253 #
254 #               RECURSIVE=true equates to svcadm enable "-r"
255 #
256 #            STATE - {false | true}     Default: true
257 #               False - Do not wait until service state is reached
258 #               True - Wait until service state is reached
259 #
260 #               STATE=true equates to svcadm enable/disable "-s"
261 #
262 #    SERVICE_PROBE - Script to check the SMF service
263 #
264 #               The optional parameter, SERVICE_PROBE, provides the
265 #                 ability to check that the SMF service is working.
266 #                 This must be a script within the zone and must
267 #                 adhere to these return codes,
268 #
269 #                 0 - The SMF service is working
270 #               100 - The SMF service should be restarted
271 #               201 - The SMF service should initiate a failover of
272 #                       the Resource Group
273 #
274 #               Note: That return code 201, requires that this resource
275 #                 has an appropriate extension property value for
276 #                 FAILOVER_MODE and FAILOVER_ENABLED=TRUE
277 #
278 #               For FAILOVER_MODE refer to the r_properties(5) man page.
279 #
280 
281 RS=${RS}
282 RG=${RG}
283 SCZBT_RS=${ZONE_BT}
284 ZONE=${ZONE}
285 SERVICE=${SERVICE_TAG}
286 RECURSIVE=false
287 STATE=true
288 SERVICE_PROBE="${BINDIR}/control_pgs probe ${SERVICE_TAG}"
289   
290 EOF
291 
292         # determine if a working copy of the config file was created if yes use the -f option
293 
294         REGOPT=""
295         if [ -f ${UTILDIR}/pgs_config.work ]
296         then
297                 /usr/bin/cat ${UTILDIR}/pgs_config.work | /usr/sbin/zlogin ${ZONE} /usr/bin/cat - \>/tmp/pgs_config.work 
298                 REGOPT="-f /tmp/pgs_config.work"
299         else
300                 /usr/bin/cat ${UTILDIR}/pgs_config | /usr/sbin/zlogin ${ZONE} /usr/bin/cat - \>/tmp/pgs_config.work 
301                 REGOPT="-f /tmp/pgs_config.work"
302         fi
303 
304         ${ECHO} clean up the manifest / smf resource
305         ${UTILDIR}/pgs_smf_remove ${REGOPT} 2>/dev/null
306 
307         # register the manifest
308 
309         if /usr/sbin/zlogin ${ZONE} ${UTILDIR}/pgs_smf_register ${REGOPT}
310         then
311                 ${ECHO} "Manifest svc:/application/sczone-agents:${RS} was created in zone ${ZONE}"
312                 ${ECHO} "Registering the zone smf resource"
313                 ksh ${SMFUTILDIR}/sczsmf_register
314                 ret_code=${?}
315                 if [ ${ret_code} -eq 0 ]
316                 then
317                         ${ECHO} "set the Retry_interval to 900"
318                         /usr/cluster/bin/scrgadm -c -j ${RS} -y Retry_interval=900
319                 fi
320                 mv ${SMFUTILDIR}/sczsmf_config.work ${SMFUTILDIR}/sczsmf_config
321         else
322                 ${ECHO} "The registration of the manifest did not complete, fix the errors and retry"
323                 ret_code=0
324         fi
325 
326         /usr/sbin/zlogin ${ZONE} /usr/bin/rm /tmp/pgs_config.work
327         
328         return ${ret_code}
329 
330 }
331 
332 store_passphrase()
333 {
334         
335         # create the <resorce name>-phrase 
336 
337         ${zcommand} ${TEST} ${SSH_PASSDIR}      
338         if [ ${?} -ne 0 ]
339         then
340                 ${ECHO} "The directory ${SSH_PASSDIR} which is necessary to store the passphrase file does not exist"
341                 return 1
342                 
343         fi
344         ${zcommand} ${RM} ${SSH_PASSDIR}/${RS}-phrase 2>/dev/null
345         ${zcommand} ${TOUCH} ${SSH_PASSDIR}/${RS}-phrase
346         ${zcommand} ${CHMOD} 600 ${SSH_PASSDIR}/${RS}-phrase
347         if [ -n "${zcommand}" ]
348         then
349                 ${zcommand} ${ECHO} ${passphrase} \>\>${SSH_PASSDIR}/${RS}-phrase
350         else
351                 ${ECHO} ${passphrase} >>${SSH_PASSDIR}/${RS}-phrase
352         fi
353         ${zcommand} ${CHMOD} 400 ${SSH_PASSDIR}/${RS}-phrase
354         return 0
355                 
356 }
357 
358 MYCONFIG=
359 ZONETYPE=global
360 
361 typeset opt
362 
363 while getopts 'f:' opt
364 do
365         case "${opt}" in
366                 f)      MYCONFIG=${OPTARG};;
367                 *)      ${ECHO} "ERROR: ${MYNAME} Option ${OPTARG} unknown - early End. Only -f is valid"
368                         exit 1;;
369         esac
370 done
371 
372 # Sourcing the specified config file, either the default one,
373 # or the one supplied with -f
374 
375 if [ -n "${MYCONFIG}" ] && [ -f "${MYCONFIG}" ]
376 then
377         ${ECHO} "sourcing ${MYCONFIG} and create a working copy under ${UTILDIR}/pgs_config.work"
378         cp ${MYCONFIG} ${UTILDIR}/pgs_config.work
379         . ${MYCONFIG}
380 else
381         PKGCONF=`dirname $0`/pgs_config
382         ${ECHO} "sourcing ${PKGCONF}"
383         . ${PKGCONF}
384 fi
385 
386 # getting the passphrase if required
387 
388 if [ -n "${SSH_PASSDIR}" ]
389 then
390 
391         ${ECHO} "Enter the passphrase of your PostgreSQL user."  
392         ${STTY} -echo
393         read passphrase
394         ${STTY} echo
395         if [ -z "${passphrase}" ]
396         then
397                 ${ECHO} "The passphrase is missing, rerun the pgs_register script"
398                 exit 1
399         fi
400         ${ECHO} "Verify your passphrase"
401         ${STTY} -echo
402         read passphrase2
403         ${STTY} echo
404         if [ "${passphrase}" != "${passphrase2}" ]
405         then
406                 ${ECHO} "The passphrase verification does not match, rerun the pgs_register script"
407                 exit 1
408         fi
409         
410 fi
411 
412 # Registering the resource
413 
414 if [ -n "${ZONE}" ]
415 then
416         local_zone_smf
417 else
418         global_zone
419 fi
420 
421 if [ -f ${UTILDIR}/pgs_config.work ]
422 then
423         ${ECHO} " remove the working copy ${UTILDIR}/pgs_config.work"
424         rm ${UTILDIR}/pgs_config.work
425 fi
426 
427 exit 0