[SOLVED] Provisioning Completed when Foreman has run a Puppet run


#1

Hi All,

We provision the Windows VM’s with the PXE options in ManageIQ and doing some post provisioning tasks with The Foreman.
Now we want that ManageIQ says that the provisioning is completed when The Foreman tasks has done instead off the PXE provisioning.

I have allready done some research but couldn’t find anything about this.


#2

The best idea in this case, will be modify the statemachine for the vm provision, and add some method to query foreman API in order to ask it when the puppet run was executed, and if not, retry.

Something like this:

require 'httparty'
@method = 'check_puppet'
@debug = true
$evm.log("info", "#{@method} Started")

attributes = $evm.root.attributes
prov = $evm.root['miq_provision']

if not prov.nil?
 vm = prov.vm
else
 vm = $evm.root['vm'] 
end  

hostname=prov.get_option(:vm_target_hostname)

domain=nil

domain ||= prov.get_option(:global_domain)
domain ||= prov.get_option(:dns_domain)
unless prov.get_option(:ws_values).nil?
  domain ||= prov.get_option(:ws_values)[:global_domain]
end

$evm.log("info","check_puppet: domain: #{domain}")

foremanhost, foremanport, foremanuser, foremanpassword, foremanprotocol, waitfor = nil, nil, nil, nil, nil, nil
foremanhost         ||= $evm.object['foremanhost']
foremanport         ||= $evm.object['foremanport']
foremanprotocol      ||= $evm.object['foremanprotocol']
foremanuser         ||= $evm.object['foremanuser']
foremanpassword     ||= $evm.object['foremanpassword']
foremanhostgroup_default ||= $evm.object['foremanhostgroup_default']
foremanpuppet    ||= $evm.object['foremanpuppet']
foremanroot           = nil
foremanroot         ||= $evm.object['foremanroot']
v2                    = nil
v2                  ||= $evm.object['v2']
v2parameters          = nil
v2parameters        ||= $evm.object['v2parameters']
managed               = nil
managed              ||= $evm.object['managed']
foremanorganization      ||= $evm.object['foremanorganization']
foremanlocation          ||= $evm.object['foremanlocation']
waitfor				||=$evm.object['waitfor']

headers = { 'Content-type' => 'application/json', 'Accept' => 'application/json' }
auth = {:username => foremanuser, :password => foremanpassword}

fqdn = "#{hostname}.#{domain}"

if foremanroot != '/'
  uri =  "#{foremanprotocol}://#{foremanhost}:#{foremanport}/#{foremanroot}/api"  
else  
  uri =  "#{foremanprotocol}://#{foremanhost}:#{foremanport}/api"
end  

if v2
 uri = "#{uri}/v2"
end  
if fqdn.nil?
  $evm.log("info","The hostname cannot be found, exiting")
  exit MIQ_ERROR
end

$evm.log("info","FQDN: #{fqdn}")

hostid = HTTParty.get("#{uri}/hosts?search=#{fqdn}", :headers => headers, :basic_auth => auth)['results'][0]['id']
lastpuppetstatus = HTTParty.get("#{uri}/reports/last?id=#{hostid}", :headers => headers, :basic_auth => auth)['status']

$evm.log("info","Checking puppet status for #{fqdn}")
lastpuppetstatus.each do |status,times|
  $evm.log("info","#{status} -> #{times}")
  if (status == 'applied' || status == 'failed') && times > 0
    $evm.log("info","Last puppet status: #{status}")
    $evm.root['ae_result'] = 'ok'
    exit MIQ_OK
  end
end

$evm.log("info","Not run yet, retrying")
$evm.root['ae_result'] = 'retry'
$evm.root['ae_retry_interval'] = "#{waitfor}.seconds"

#3

conceptioally looks good, you can also consider using

foreman/api/v2/hosts/FQDN/status to get back a list of statuses directly (similar to the host icon status in the web ui).

you can also use api/v2/hosts/FQDN/reports/last for the detailed last report.

more info at the official API Docs


#4

Hi!

Thanks for putting me in the right direction, it works fine right now.
The logic I have choosen is:

case response.code
when 200
	laststatus = JSON.parse(response)

	status = laststatus['status']
	summary = laststatus['summary']

	if summary == 'Success' || summary == 'Modified'
		status.each do |status,times|
	  	if (status == 'skipped') && times == 0
	  		log(:info,"Foreman run is completed without errors")
	    	$evm.root['ae_result'] = 'ok'
	    	exit MIQ_OK
	  	end
	end
	elsif summary == 'Failed'
		log(:error,"Foreman run has errors")
	  	exit MIQ_ERROR
	end
end