VM Retirement after One state fails while Provisioning

When Vm is provisioning if one state is fails how it can automatically call the Retirement workflow rather than manually we can retire the state . ??? so when a state is fails it will be calling the retirement workflow and retire the Vm automatically rather than manually we set.

1 Like

We have something similar. There is a Event in ManageIQ if a ServiceTemplateProvision finishes. If we catch an Event, we lookup the request and check if it’s status. If the Status is “Error” we send an email.

The Event we catch is “/System/Event/MiqEvent/POLICY/service_provisioned”

The code we use to check the status is

  def main
      @event = $evm.root['miq_event']
      @task = $evm.vmdb(:miq_request_task).where(destination_id: @event.target_id).first if @event.target_id.present?
      exit_with_message("Something is wrong. This Event a target without a task. Event ID:#{@event.id} Target ID: #{@event.target_id}") if @task.blank?

      @request = @task.miq_request
      exit_with_message("A Task without a Request. Strange. Task ID: #{@task.try(:id)}") if @request.blank?

      exit MIQ_OK if @request.status == 'Ok'

      exit_with_message("The Request is neither OK nor Error. Request ID: #{@request.id} Status: #{@request.status}") unless @request.status == 'Error'

      # Everything beyond this point is only executed if the request is in "Error" status

Once you know if the request was successful or not, you can call your cleanup

Thank you !