Florian Haas wrote:

How can I get an up to date version of your RA?

Here:
http://hg.linux-ha.org/agents/raw-file/tip/heartbeat/vmware



I made some additional modification and created a patch file against this script. I moved the "set_env" function inside the "validate-all" function.



--
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	2010-03-01 10:58:56.138352097 +0100
+++ VMwareVM	2010-03-01 11:30:59.302347806 +0100
@@ -1,17 +1,17 @@
 #!/bin/sh
 #
-# VMware OCF resource agent
+# 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,13 +20,17 @@
 #  * 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
+#  * Default_Action_Timeout stock value (20 sec) isn't enough if you are 
+#    dealing with many virtual machines: raise it to something around 900 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
 #################################################################
 
@@ -38,12 +42,12 @@
 #################################################################
 
 # Path to the virtual machine configuration file
-VMXPATH="$OCF_RESKEY_vmxpath"
+VMXPATH="$OCF_RESKEY_vmxpath"    
 
 # Path to the vmware-vim-cmd executable
 VIMSHBIN="$OCF_RESKEY_vimshbin"
 
-# global variables
+# Global variables
 VMXDIR=
 RELVMXPATH=
 VMID=
@@ -69,32 +73,32 @@
 vmware_get_msgid() {
   $VIMSHBIN vmsvc/message $1 2>/dev/null \
          | awk '/^Virtual machine message/ {print $4}' \
-     | awk -F : '{print $1}'
+         | 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
@@ -106,27 +110,27 @@
 # 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
+# Variables depending on the above functions 
 #################################################################
 
-set_environment() {
-	# Directory containing the virtual machine
-	VMXDIR="`dirname "$VMXPATH"`"
+vmware_set_env() {  
+  # Directory containing the virtual machine
+  VMXDIR="`dirname "$VMXPATH"`" 
 
-	# Basename of the configuration file
-	RELVMXPATH="`basename "$VMXPATH"`"
+  # Basename of the configuration file 
+  RELVMXPATH="`basename "$VMXPATH"`"                        
 
-	# Vid of the virtual machine (can be empty if the vm is not registered)
-	VMID=`vmware_get_vid "$RELVMXPATH"`
+  # Vid of the virtual machine (can be empty if the vm is not registered)
+  VMID=`vmware_get_vid "$RELVMXPATH"`
 
-	# Virtual machine name
-	VM="`awk -F '"' '/^displayName/ {print $2}' "$VMXPATH"`"
+  # Virtual machine name
+  VM="`awk -F '"' '/^displayName/ {print $2}' "$VMXPATH"`"
 
-	# msg.autoAnswer value in config file
-	VMAUTOMSG="`awk -F '"' '/^msg.autoAnswer/ {print $2}' "$VMXPATH"`"
+  # msg.autoAnswer value in config file
+  VMAUTOMSG="`awk -F '"' '/^msg.autoAnswer/ {print toupper($2)}' "$VMXPATH"`"
 }
 
 # Main functions
@@ -134,41 +138,44 @@
 
 # Print usage summary
 vmware_usage() {
-    cat <<END
+  cat <<END
 usage: $0 {start|stop|status|monitor|meta-data|validate-all}
 
 Expects to have a fully populated OCF RA-compliant environment set.
 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: aborting"
+    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
-  fi
+    ocf_log err "vmware-vim-cmd executable missing or not in path ($VIMSHBIN): aborting"
+    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: aborting"
+    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: aborting"
+    exit $OCF_ERR_ARGS
   fi
 
   if [ "$VMAUTOMSG" != "TRUE" ]; then
     ocf_log warn "Please set msg.autoAnswer = \"TRUE\" in your config file"
   fi
 
-  # $VMID is allowed to be empty in case we are validating a
+  # $VMID is allowed to be empty in case we are validating a 
   # virtual machine which is not registered
 
   return $OCF_SUCCESS
@@ -189,28 +196,28 @@
     find "$VMXDIR" -name \*.lck -type f -exec rm "{}" \;
     for BVM in `vmware_get_broken`; do
       ocf_log info "Unregistering missing virtual machine $BVM"
-      vmware_unregister_vm $BVM
+      vmware_unregister_vm $BVM 
     done
     if [ -z "$VMID" ]; then
       # VM is not registered, need to register
       ocf_log info "Virtual machine $VM is not registered"
       ocf_log info "Registering Virtual machine $VM"
-      vmware_register_vm "$VMXPATH"
+      vmware_register_vm "$VMXPATH" 
       VMID=`vmware_get_vid "$RELVMXPATH"`
       if [ -z "$VMID" ]; then
         ocf_log err "Could not register virtual machine $VM: aborting."
         exit $OCF_ERR_GENERIC
       fi
       ocf_log info "Virtual machine $VM registered with ID $VMID"
-    fi
+    fi     
     ocf_log info "Powering on virtual machine $VM"
-    vmware_poweron_vm $VMID
+    vmware_poweron_vm $VMID 
     # Give the VM some time to initialize
     sleep 10
-
-    if [ "$VMAUTOMSG" != "TRUE" ]; then
+  
+    if [ "$VMAUTOMSG" != "TRUE" ]; then 
       # msg.autoAnswer is not set: we try to deal with the
-      # most common question: msg.uuid.altered
+      # most common question: msg.uuid.altered 
       ocf_log info  "Checking msg.uuid.altered on VM $VM"
       if [ -n "`vmware_uuid_alt $VMID`" ]; then
         MSGID=`vmware_get_msgid $VMID`
@@ -218,14 +225,14 @@
       fi
     fi
 
-    # Check if the VM is running. We don't bother
+    # 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
-
+    
     ocf_log info "Virtual machine $VM is running"
     return $OCF_SUCCESS
   fi
@@ -237,14 +244,14 @@
   if [ -z "$VMID" ]; then
     ocf_log info "Virtual machine $VM is not registered"
     return $OCF_SUCCESS
-  else
+  else  
     # Don't stop a VM if it's already stopped
     if vmware_monitor; then
       # If the VM is running send a suspend signal and wait
-      # until it is off. We don't bother with timeouts: we
+      # until it is off. We don't bother with timeouts: we 
       # rely on the CRM for that.
       ocf_log info "Virtual machine $VM is running: suspending it"
-      vmware_suspend_vm $VMID
+      vmware_suspend_vm $VMID 
       sleep 5
       while vmware_monitor; do
         ocf_log info "Virtual machine $VM is still running: delaying 10 seconds"
@@ -253,12 +260,20 @@
     else
       ocf_log info "Virtual machine $VM is already stopped"
     fi
+    # VMware randomly fails to unregister VMs,
+    # so we send the command twice
     ocf_log info "Unregistering virtual machine $VM"
-    vmware_unregister_vm $VMID
+    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
+      ocf_log warn "Could not unregister virtual machine $VM: retrying."
+      sleep 10
+      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
     fi
     ocf_log info "Virtual machine $VM is stopped"
     return $OCF_SUCCESS
@@ -278,11 +293,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>
+<resource-agent name="VMwareVM">
+<version>0.2</version>
 <longdesc lang="en">
 OCF compliant script to control vmware server 2.0 virtual machines.
 </longdesc>
@@ -301,69 +316,60 @@
 <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="900" />
+<action name="stop"         timeout="900" />
+<action name="monitor"      timeout="30" interval="300" depth="0" start-delay="60" />
 <action name="meta-data"    timeout="5" />
 </actions>
 </resource-agent>
 END
 }
 
-# See how we were called
+# See how we were called 
 #################################################################
 
-case "$1" in
-meta-data)
+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)
+status|monitor)	
+  vmware_validate
   vmware_monitor
   ;;
 
+usage|help)
+  vmware_usage
+  exit $OCF_SUCCESS
+  ;;
+
+validate-all)
+  vmware_validate
+  ;;
+
 *)
   vmware_usage
   exit $OCF_ERR_UNIMPLEMENTED
   ;;
 
 esac
+
+exit $?
+
_______________________________________________
Pacemaker mailing list
Pacemaker@oss.clusterlabs.org
http://oss.clusterlabs.org/mailman/listinfo/pacemaker

Reply via email to