Cristian Mammoli - Apra Sistemi wrote:
I know guys, bash scripting is not exactly rocket science, but I'm doing my best here ;-)
Indeed I made a total mess with output redirection inb those functions ... Fixed patch attached :-( -- Cristian Mammoli APRA SISTEMI srl Via Brodolini,6 Jesi (AN) tel dir. 0731 719822 Web www.apra.it e-mail c.mamm...@apra.it
--- vmware.old 2010-03-01 13:35:23.000000000 +0100 +++ vmware 2010-03-01 23:30:21.000000000 +0100 @@ -2,16 +2,16 @@ # # VMware OCF resource agent # -# Copyright (c) 2008 Apra Sistemi s.r.l. +# Copyright (c) 2010 Apra Sistemi s.r.l. # All Rights Reserved. # -# Description: Manages a VMware server 2.0 as a High-Availability -# resource +# Description: Manages VMware server 2.0 virtual machines +# as High-Availability resources # # # Author: Cristian Mammoli <c.mammoli AT apra DOT it> # License: GNU General Public License (GPL) -# Copyright: (C) 2008 Apra Sistemi s.r.l. +# Copyright: (C) 2010 Apra Sistemi s.r.l. # # See usage() function below for more details... # @@ -20,12 +20,16 @@ # * OCF_RESKEY_VIMSHBIN (mandatory, full path to th vmware-vim-cmd executable) # # Requirements/caveats: -# * vmware-server 2.0 RC1 installed and autostarted on all nodes +# * vmware-server 2.0 installed and autostarted on all nodes # * vmdk files must be in the same directory of the vmx file # * vmx filenames must be unique, even if stored in different directories # * Default_Action_Timeout stock value (20 sec) isn't enough if you are -# dealing with many virtual machines: raise it to something around 300 secs +# dealing with many virtual machines: raise it to something around 600 secs # or use operation attributes with the proposed values +# * Moving a vm among nodes will cause its mac address to change: if you need +# to preserve the mac address set it manually in the nic options +# * The script should be able to deal with paths and filenames with spaces, +# anyway try to avoid it # Initialization ################################################################# @@ -43,7 +47,7 @@ # Path to the vmware-vim-cmd executable VIMSHBIN="$OCF_RESKEY_vimshbin" -# global variables +# Global variables VMXDIR= RELVMXPATH= VMID= @@ -55,64 +59,64 @@ # Get virtual machine vid vmware_get_vid() { - $VIMSHBIN vmsvc/getallvms 2>/dev/null \ + $VIMSHBIN vmsvc/getallvms \ | awk '/\/'"$1"'/ {print $1}' } # Is the vm waiting for input after a migration? vmware_uuid_alt() { - $VIMSHBIN vmsvc/message $1 2>/dev/null \ + $VIMSHBIN vmsvc/message $1 \ | awk /^msg.uuid.altered/ } # Get message id vmware_get_msgid() { - $VIMSHBIN vmsvc/message $1 2>/dev/null \ - | awk '/^Virtual machine message/ {print $4}' \ - | awk -F : '{print $1}' + $VIMSHBIN vmsvc/message $1 \ + | awk '/^Virtual machine message/ {print $4}' \ + | awk -F : '{print $1}' } # Answers message vmware_answer_msg() { - $VIMSHBIN vmsvc/message $1 $2 $3 >/dev/null 2>&1 + $VIMSHBIN vmsvc/message $1 $2 $3 >/dev/null } # Register a virtual machine vmware_register_vm() { - $VIMSHBIN solo/registervm '"'$1'"' >/dev/null 2>&1 + $VIMSHBIN solo/registervm '"'$1'"' >/dev/null } # Unregister a virtual machine vmware_unregister_vm() { - $VIMSHBIN vmsvc/unregister $1 >/dev/null 2>&1 + $VIMSHBIN vmsvc/unregister $1 >/dev/null } # Start a virtual machine vmware_poweron_vm() { - $VIMSHBIN vmsvc/power.on $1 >/dev/null 2>&1 + $VIMSHBIN vmsvc/power.on $1 >/dev/null } # Suspend a virtual machine vmware_suspend_vm() { - $VIMSHBIN vmsvc/power.suspend $1 >/dev/null 2>&1 + $VIMSHBIN vmsvc/power.suspend $1 >/dev/null } # Get virtual machine power state vmware_get_status() { - $VIMSHBIN vmsvc/power.getstate $1 2>/dev/null \ + $VIMSHBIN vmsvc/power.getstate $1 \ | awk '/^Powered on/ || /^Powered off/ || /^Suspended/' } # Get vid of missing virtual machines vmware_get_broken() { $VIMSHBIN vmsvc/getallvm 2>&1 \ - | awk -F "'" '/^Skipping/ {print $2}' + | awk -F \' '/^Skipping/ {print $2}' } # Variables depending on the above functions ################################################################# -set_environment() { +vmware_set_env() { # Directory containing the virtual machine VMXDIR="`dirname "$VMXPATH"`" @@ -126,7 +130,7 @@ VM="`awk -F '"' '/^displayName/ {print $2}' "$VMXPATH"`" # msg.autoAnswer value in config file - VMAUTOMSG="`awk -F '"' '/^msg.autoAnswer/ {print $2}' "$VMXPATH"`" + VMAUTOMSG="`awk -F '"' '/^msg.autoAnswer/ {print toupper($2)}' "$VMXPATH"`" } # Main functions @@ -141,27 +145,30 @@ END } -# Check for dependencies # Check for mandatory files presence and consistency vmware_validate() { if [ -z "`pidof vmware-hostd`" ]; then - ocf_log warn "vmware-hostd is not running" - return $OCF_ERR_GENERIC + ocf_log err "vmware-hostd is not running" + exit $OCF_ERR_GENERIC fi if [ ! -x "$VIMSHBIN" ]; then - ocf_log warn "vmware-vimsh executable missing or not in path ($VIMSHBIN)" - return $OCF_ERR_INSTALLED + ocf_log err "vmware-vim-cmd executable missing or not in path ($VIMSHBIN)" + exit $OCF_ERR_ARGS fi if [ ! -f "$VMXPATH" ]; then - ocf_log warn "Specified vmx file ($VMXPATH) does not exist" - return $OCF_ERR_ARGS + ocf_log err "Specified vmx file ($VMXPATH) does not exist" + exit $OCF_ERR_ARGS fi + # Now we can safely setup variables... + vmware_set_env + + # ... and verify them if [ -z "$VM" ]; then - ocf_log warn "Could not find out virtual machine name" - return $OCF_ERR_ARGS + ocf_log err "Could not find out virtual machine name" + exit $OCF_ERR_ARGS fi if [ "$VMAUTOMSG" != "TRUE" ]; then @@ -174,6 +181,22 @@ return $OCF_SUCCESS } +# More relaxed checking in case of probes +vmware_validate_probe() { + if [ ! -x "$VIMSHBIN" ]; then + ocf_log warn "vmware-vim-cmd executable missing or not in path ($VIMSHBIN)" + exit $OCF_NOT_RUNNING + fi + + if [ ! -f "$VMXPATH" ]; then + ocf_log warn "Specified vmx file ($VMXPATH) does not exist" + exit $OCF_NOT_RUNNING + fi + + # Now we can safely setup variables... + vmware_set_env +} + # Start a virtual machine vmware_start() { # Don't start a VM if it's already running @@ -198,7 +221,7 @@ vmware_register_vm "$VMXPATH" VMID=`vmware_get_vid "$RELVMXPATH"` if [ -z "$VMID" ]; then - ocf_log err "Could not register virtual machine $VM: aborting." + ocf_log err "Could not register virtual machine $VM" exit $OCF_ERR_GENERIC fi ocf_log info "Virtual machine $VM registered with ID $VMID" @@ -221,7 +244,7 @@ # Check if the VM is running. We don't bother # with timeouts: we rely on the CRM for that. while :; do - vmware_monitor && break + vmware_monitor && break ocf_log info "Virtual machine $VM is still stopped: delaying 10 seconds" sleep 10 done @@ -253,13 +276,17 @@ else ocf_log info "Virtual machine $VM is already stopped" fi + # VMware randomly fails to unregister VMs, + # so we loop until we have success or timeout ocf_log info "Unregistering virtual machine $VM" vmware_unregister_vm $VMID VMID=`vmware_get_vid "$RELVMXPATH"` - if [ -n "$VMID" ]; then - ocf_log err "Could not unregister virtual machine $VM: aborting." - exit $OCF_ERR_GENERIC - fi + while [ -n "$VMID" ]; do + ocf_log warn "Could not unregister virtual machine $VM: retrying." + sleep 10 + vmware_unregister_vm $VMID + VMID=`vmware_get_vid "$RELVMXPATH"` + done ocf_log info "Virtual machine $VM is stopped" return $OCF_SUCCESS fi @@ -267,7 +294,7 @@ # Monitor a virtual machine vmware_monitor() { - if [ "`vmware_get_status $VMID`" = "Powered on" ]; then + if [ -n "$VMID" ] && [ "`vmware_get_status $VMID`" = "Powered on" ]; then ocf_log debug "Virtual machine $VM (ID $VMID) is running..." return $OCF_SUCCESS else @@ -278,11 +305,11 @@ # Print metadata informations meta_data() { - cat <<END + cat <<END <?xml version="1.0"?> <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> <resource-agent name="vmware"> -<version>0.1</version> +<version>0.2</version> <longdesc lang="en"> OCF compliant script to control vmware server 2.0 virtual machines. </longdesc> @@ -301,15 +328,15 @@ <longdesc lang="en"> vmware-vim-cmd executable path </longdesc> -<shortdesc lang="en">vmware-vimsh path</shortdesc> +<shortdesc lang="en">vmware-vim-cmd path</shortdesc> <content type="string" default="/usr/bin/vmware-vim-cmd"/> </parameter> </parameters> <actions> -<action name="start" timeout="300" /> -<action name="stop" timeout="300" /> -<action name="monitor" timeout="30" interval="300" depth="0" /> +<action name="start" timeout="600" /> +<action name="stop" timeout="600" /> +<action name="monitor" timeout="30" interval="300" depth="0"/> <action name="meta-data" timeout="5" /> </actions> </resource-agent> @@ -319,51 +346,41 @@ # See how we were called ################################################################# -case "$1" in +case $1 in meta-data) meta_data exit $OCF_SUCCESS ;; -usage|help) - vmware_usage - exit $OCF_SUCCESS - ;; -esac - -set_environment -vmware_validate -rc=$? -if [ $rc -ne 0 ]; then - case "$1" in - stop) ocf_log info "validate failed, vmware considered stopped" - exit $OCF_SUCCESS;; - monitor) exit $OCF_NOT_RUNNING;; - status) exit $LSB_STATUS_STOPPED;; - *) # it's safe to bail out now - ocf_log err "vmware_validate failed" - exit $rc - ;; - esac -elif [ "$1" = "validate-all" ]; then - exit $OCF_SUCCESS -fi -case "$1" in start) + vmware_validate vmware_start ;; stop) + vmware_validate vmware_stop ;; status|monitor) + vmware_validate_probe vmware_monitor ;; +usage|help) + vmware_usage + exit $OCF_SUCCESS + ;; + +validate-all) + vmware_validate + ;; + *) vmware_usage exit $OCF_ERR_UNIMPLEMENTED ;; esac + +exit $?
_______________________________________________________ Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev Home Page: http://linux-ha.org/