Module: DefaultHostgroupManagedHostPatch::ClassMethods

Defined in:
lib/default_hostgroup_managed_host_patch.rb

Instance Method Summary collapse

Instance Method Details

#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
50
51
52
53
54
55
56
57
58
59
60
61
# 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][: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

  map = SETTINGS[:default_hostgroup][:map]
  new_hostgroup = nil

  map.each do |hostgroup, regex|
    unless valid_hostgroup?(hostgroup)
      Rails.logger.error "DefaultHostgroupMatch: #{hostgroup} is not a valid hostgroup, skipping."
      next
    end
    regex.gsub!(/(\A\/|\/\z)/, '')
    if Regexp.new(regex).match(hostname)
      new_hostgroup = Hostgroup.find_by_title(hostgroup)
      break
    end
  end

  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)


63
64
65
# File 'lib/default_hostgroup_managed_host_patch.rb', line 63

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