14 Replies Latest reply on May 30, 2014 7:29 AM by Bill Robinson

    NSH while loop not functioning correct

    Raja Mohan

      I have a virtual guest job created and executed, wanted to wait for the job to complete creating the vm guest. This while loop doesn't seem to get out of it. Any help why it is not assigning the value? what am i missing? Alternate question would be, how do you wait for the VM Guest to be created to obtain the mac address and execute a provisioning job against the mac.

       

      #!/opt/bsa_dev/bladelogic/NSH/bin/nsh

      HOST="Bsatest2_RHEL6"

      echo $HOST

      GUEST_POWER_STATUS=""

      echo $GUEST_POWER_STATUS

      while [ -z "${GUEST_POWER_STATUS}" ]

      echo $GUEST_POWER_STATUS

      do

      (

        if [ -n $HOST ]

         then

          VIRTUAL_GUEST1_NAME=$HOST

          VIRT_ENTITY_KEY=`blcli -v nshuser-dev -r BLAdmins Virtualization  getVirtualGuestKeyByServerName $VIRTUAL_GUEST1_NAME`

          echo $VIRT_ENTITY_KEY

          GUEST_POWER_STATUS=`blcli -v nshuser-dev -r BLAdmins Virtualization getVirtualGuestPowerStatus "$VIRT_ENTITY_KEY"`

          echo "status is set to \"$GUEST_POWER_STATUS\""

          blcli_storeenv $GUEST_POWER_STATUS

        fi

      echo "exit if condition"

      #  sleep 1m

      )

      done

       

      results

      Bsatest2_RHEL6

       

       

       

       

      2007900 VMwareVirtualMachine vm-3588

      status is set to "Stopped"

      No result has been created yet

      exit if condition

       

       

      2007900 VMwareVirtualMachine vm-3588

      status is set to "Stopped"

      No result has been created yet

      exit if condition

        • 1. Re: NSH while loop not functioning correct
          Bill Robinson

          why are you running the if in a subshell ?

          • 2. Re: NSH while loop not functioning correct
            Raja Mohan

            I was just testing it outside on a NSH command line to ensure my loop was functioning properly     

            • 3. Re: NSH while loop not functioning correct
              Bill Robinson

              So ?  why do you need to spawn a subshell ?  i’m pretty sure because you do the variable set inside the subshell, the parent isn’t seeing it.

              • 4. Re: NSH while loop not functioning correct
                Raja Mohan

                not sure I completely follow you. bsa novice here

                • 5. Re: NSH while loop not functioning correct
                  Bill Robinson

                  this has nothing to do w/ bsa.  the ( ) in the loop spawn a subshell.

                   

                  try running this in bash, ksh, csh, etc to see what i mean:

                  FOO=""

                  count=1

                  while [ -z "${FOO}" ]

                  do

                  echo "count outside:${count}"

                  (

                  echo "count inside: ${count}"

                  if [ ${count} -eq 6 ]

                      then

                      FOO="bar"

                  fi

                  count=$((${count}+1))

                  )

                  done

                   

                   

                  vs

                  FOO=""

                  count=1

                  while [ -z "${FOO}" ]

                  do

                  echo "count: ${count}"

                  if [ ${count} -eq 6 ]

                      then

                      FOO="bar"

                  fi

                  count=$((${count}+1))

                  done

                  1 of 1 people found this helpful
                  • 6. Re: NSH while loop not functioning correct
                    Raja Mohan

                    ok, Now I get different results. Time to go check my while condition. Thanks

                     

                    Bsatest2_RHEL6

                     

                     

                     

                     

                    2007900 VMwareVirtualMachine vm-3588

                    status is set to "Stopped"

                    No result has been created yet

                    exit if condition

                    Stopped

                    2007900 VMwareVirtualMachine vm-3588

                    status is set to "Stopped"

                    No result has been created yet

                    exit if condition

                    Stopped

                    2007900 VMwareVirtualMachine vm-3588

                    • 7. Re: NSH while loop not functioning correct
                      Raja Mohan

                      Got it working. The following is the working script

                       

                      #!/opt/bsa_dev/bladelogic/NSH/bin/nsh

                      HOST="Bsatest2_RHEL6"

                      #echo $HOST

                      GUEST_POWER_STATUS=""

                      #echo $GUEST_POWER_STATUS

                      # Wait for 1 minute and then check to see the status; If the status is not valid wait again.

                      #

                      while [ -z "${GUEST_POWER_STATUS}" ] ; do

                      #echo $GUEST_POWER_STATUS $HOST

                      # Comment sleep during test

                      #  sleep 60

                        if [ ! -z "${HOST}" ]

                         then

                          VIRTUAL_GUEST1_NAME=$HOST

                          echo $VIRTUAL_GUEST1_NAME

                          VIRT_ENTITY_KEY=`blcli -v nshuser-dev -r BLAdmins Virtualization  getVirtualGuestKeyByServerName $VIRTUAL_GUEST1_NAME`

                          echo $VIRT_ENTITY_KEY

                          GUEST_POWER_STATUS=`blcli -v nshuser-dev -r BLAdmins Virtualization getVirtualGuestPowerStatus "$VIRT_ENTITY_KEY"`

                          echo "status is set to \"$GUEST_POWER_STATUS\""

                      #    blcli_storeenv $GUEST_POWER_STATUS

                        else

                          echo "no host name provided"

                          exit 99

                        fi

                      done

                       

                       

                      if [ "${GUEST_POWER_STATUS}" != "Started" ] ; then

                        blcli_execute Virtualization changeVirtualGuestPowerStatus "$VIRT_ENTITY_KEY" Start

                        if [ $? != 0 ]; then

                          echo "Failed powering on the VM "

                      #    blcli_disconnect

                          exit 99

                        fi

                      else

                          echo "VM is already in START status "

                          exit 99

                      fi

                      • 8. Re: NSH while loop not functioning correct
                        Joe Piotrowski

                        Thank you for sharing that.

                        • 9. Re: NSH while loop not functioning correct
                          Barry McQuillan


                          If time is a factor here you might also want to think about replacing the blcli commands with performance commands, blcli_execute and blcli_storeenv.

                           

                          So,

                          VIRT_ENTITY_KEY=`blcli -v nshuser-dev -r BLAdmins Virtualization  getVirtualGuestKeyByServerName $VIRTUAL_GUEST1_NAME`

                          And

                          GUEST_POWER_STATUS=`blcli -v nshuser-dev -r BLAdmins Virtualization getVirtualGuestPowerStatus "$VIRT_ENTITY_KEY"`

                           

                          Would become:

                          blcli_execute Virtualization  getVirtualGuestKeyByServerName $VIRTUAL_GUEST1_NAME

                          blcli_storeenv VIRT_ENTITY_KEY

                          And

                          blcli_execute Virtualization getVirtualGuestPowerStatus "$VIRT_ENTITY_KEY"`

                          blcli_storeenv GUEST_POWER_STATUS

                           

                          This would stop it from spawning and shutting down  x2 JVM's to run the blcli commands.

                          • 10. Re: NSH while loop not functioning correct
                            Raja Mohan

                            Barry McQuillan Thanks for your insight. I had to change it on my master script to blcli_execute because i was getting an error "INFO [MESSAGE] Starting CLI session for single command [/MESSAGE]" and the script was failing. What is the difference in executing command this way? can you point me to where it is documented?

                             


                            • 11. Re: NSH while loop not functioning correct
                              Barry McQuillan

                              Raja,

                               

                              A breakdown of the NSH performance commands and an explanation of how they work can be found here:

                              https://docs.bmc.com/docs/display/public/bsa83/NSH+performance+commands

                               

                              I can't see the blcli_connect or blcli_setoption commands in your script? So I'm not sure how your blcli_execute command actually runs?

                               

                              Basically if you had x3 blcli commands in your script it would function like this:

                              • Start JVM instance
                              • run blcli 1
                              • Shutdown JVM instance
                              • Start JVM instance
                              • run blcli 2
                              • Shutdown JVM instance
                              • Start JVM instance
                              • run blcli 3
                              • Shutdown JVM instance

                              As we all know starting and stopping a JVM takes a considerable amount of time.

                               

                              Using the performance commands this changes to:

                              • Start JVM instance
                              • blcli_execute 1
                              • blcli_execute 2
                              • blcli_execute 3
                              • Shutdown JVM

                               

                              As you can see this method can save a huge amount of time, especially if you have a blcli command inside a loop statement.

                              1 of 1 people found this helpful
                              • 12. Re: NSH while loop not functioning correct
                                Bill Robinson

                                If it runs as a nsh job, you don’t need the setoptions.  If the connect hasn’t been done, the 1st blcli_execute will do it (as a manual script or job)

                                • 13. Re: NSH while loop not functioning correct
                                  Raja Mohan

                                  Thanks Bill, I was wondering why sometimes it requires the setoptions and sometimes it doesn't. It clears my understanding.

                                  • 14. Re: NSH while loop not functioning correct
                                    Bill Robinson

                                    right - in a job those are ignored as the appserver supplies all of that information and the credential to be used and you can't override it (maybe if you spawn off another process or something)

                                     

                                    as a script you run from the commandline you do need to get creds w/ blcred first, and then tell the blcli how to connect to the appserver - the same is true w/ the blcli - you must supply the profile name and rolename if you run the blcli in a script manually, but not as a job.