Module: DefaultHostgroupManagedHostPatch::ClassMethods

Defined in:
lib/default_hostgroup_managed_host_patch.rb

Instance Method Summary collapse

Instance Method Details

#find_match(facts_map) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/default_hostgroup_managed_host_patch.rb', line 62

def find_match(facts_map)
  facts_map.each do |group_name, facts|
    return Hostgroup.find_by_title(group_name) if group_matches?(facts) and valid_hostgroup?(group_name)
  end
  Rails.logger.info "No match ..."
  return false
end

#group_matches?(fact) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
# File 'lib/default_hostgroup_managed_host_patch.rb', line 51

def group_matches?(fact)
  fact.each do |fact_name, fact_regex|
    fact_regex.gsub!(/(\A\/|\/\z)/, '')
    host_fact_value = @host.facts_hash[fact_name]
    Rails.logger.info "Fact = #{fact_name}"
    Rails.logger.info "Regex = #{fact_regex}"
    return true if Regexp.new(fact_regex).match(host_fact_value)
  end
  return false
end

#import_host_and_facts_with_match_hostgroup(hostname, facts, certname = nil, proxy_id = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/default_hostgroup_managed_host_patch.rb', line 12

def import_host_and_facts_with_match_hostgroup hostname, facts, certname = nil, proxy_id = nil
  @host, result = import_host_and_facts_without_match_hostgroup(hostname, facts, certname, proxy_id)

  unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
    Rails.logger.warn "DefaultHostgroupMatch: Could not load default_hostgroup map from settings, check config."
    return @host, result
  end

  Rails.logger.debug "DefaultHostgroupMatch: performing Hostgroup match"

  if Setting[:force_hostgroup_match_only_new]
    # host.new_record? will only test for the early return in the core method, a real host
    # will have already been saved at least once.
    unless @host.present? && !@host.new_record? && @host.hostgroup.nil? && @host.reports.empty?

      Rails.logger.debug "DefaultHostgroupMatch: skipping, host exists"
      return @host, result
    end
  end

  unless Setting[:force_hostgroup_match]
    if @host.hostgroup.present?
      Rails.logger.debug "DefaultHostgroupMatch: skipping, host has hostgroup"
      return @host, result
    end
  end

  facts_map = SETTINGS[:default_hostgroup][:facts_map]
  new_hostgroup = find_match(facts_map)

  return @host, result unless new_hostgroup

  @host.hostgroup = new_hostgroup
  @host.save(:validate => false)
  Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"

  return @host, result
end

#valid_hostgroup?(hostgroup) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/default_hostgroup_managed_host_patch.rb', line 70

def valid_hostgroup?(hostgroup)
  Hostgroup.find_by_title(hostgroup) ? true : false
end