Watch a tag until it changes

Hi All,

I am writing a tag autoprovision/initialized, and waiting for the provision to finish. Once done, they will do an API call setting the tag to autoprovision/complete.

I am trying to watch that tag in a loop, and break on the update with absolutely no luck.

If anyone has an idea, I’d really appreciate it. Thanks.

Adding more information

My automation script is as follows:

#
# Description: Scan tags to see if server has been provisioned
#

$evm.log(:info, "PXE Provision status started")
$evm.log(:info, "PXE Checking netboot provisioning status")

begin

  prov = $evm.root['miq_provision'] || \
         $evm.root['miq_provision_request'] || \
         $evm.root['miq_provision_request_template']
  vm = prov.vm
  while true
      category = "autoprovision"
      tag = "completed"
      provisiontag = vm.tagged_with?(category, tag).to_s
      $evm.log(:info, "PXE Provision tag initiated: #{provisiontag}")
      case provisiontag 
      when /false/
          $evm.log(:info, "PXE Provisioning False test")
      when /true/
        $evm.log(:info, "PXE Provision tag complete: #{provisiontag}")
        $evm.log(:info, "PXE Provisioning Complete")
		exit MIQ_STOP
        break
    else
        $evm.log(:info, "PXE Provisioning Else was reached")
    end
    category = "autoprovision"
    tag = "completed"
    provisiontag = vm.tagged_with?(category, tag).to_s
    case provisiontag 
    when /true/
      $evm.log(:info, "PXE Provision tag failed: #{provisiontag}")
      $evm.log(:info, "PXE Provisioning Failed")
      exit MIQ_ERROR
       break
    end
    sleep 60
  end
  $evm.log(:info, "PXE Provision status completed")
end
exit MIQ_OK

I set the tag from initiated to completed via an API call, check the tag, and it is chamged:

      "name": "/managed/autoprovision/completed",

and the automation log

Next State=[PXEIsProvisioned]
Processing State=[PXEIsProvisioned]
<AEMethod is_provisioned> PXE Provision status started
<AEMethod is_provisioned> PXE Checking netboot provisioning status
<AEMethod is_provisioned> PXE Provision tag initiated: false
<AEMethod is_provisioned> PXE Provision tag initiated: false
<AEMethod is_provisioned> PXE Provision tag initiated: false

<---- cahnge tag here via api ---->


<AEMethod is_provisioned> PXE Provision tag initiated: false
<AEMethod is_provisioned> PXE Provision tag initiated: false

The loop never breaks. Any ideas will help

It’s never a good idea to use a sleep in an automate method - much better to use a state machine with a retry stage. Automate methods have a maximum of 10 minutes to run, and sleeping one for 60 seconds at a time just locks up a generic worker which could be doing other things.

I suspect that if you implement this as a state machine it’ll work, as you’ll be picking up a fresh copy of the VM object each time, so the tag status should reflect any changes that have been made during the retry period.

Hope this helps,
pemcg

Thanks, pemcg.

How would I go about implementing this as a state machine?

Cheers

Rather than your sleep 60 command, you’d perform something like this in your method:

$evm.root['ae_result']         = 'retry'
$evm.root['ae_retry_interval'] = '1.minute'

Your entire method will then be re-run from the start in one minute, so you’ll need to handle re-loading of any variables that you need, and also terminating ‘normally’ without a retry once you’ve detected that the tag has changed.

There’s an overview of state machines here

Hope this helps,
pemcg

Yes, really did help. I was just looking through the book, and was tinkering with doing this, taken from waiting for an ip address section. Thanks again.

Peter,
I am maintaining some of the code zombiefish wrote using this method. Is there a way to track the retry count from $evm.root[‘ae_result’]? Is there a evm.root variable for that?

Are all the evm.root variable defined/documented somewhere? I have looked to now avail. Does evm dump give them all?

Hi, yes there’s a $evm.root['ae_state_retries'] variable that tracks the retry count.

The challenge with $evm.root variables is that they will vary from one workflow point in time to another - for example only state machines will have the $evm.root[‘state’] variables. The best way to find out what variables you have to work with at any point in your automation workflow is to use something like InspectMe or object_walker to dump out the automation workspace variables.

Hope this helps,
pemcg