Module: DefaultHostgroupBaseHostPatch

Extended by:
ActiveSupport::Concern
Defined in:
lib/default_hostgroup_base_host_patch.rb

Instance Method Summary collapse

Instance Method Details

#find_match(facts_map) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/default_hostgroup_base_host_patch.rb', line 34

def find_match(facts_map)
  facts_map.each do |group_name, facts|
    hg = Hostgroup.find_by_title(group_name)
    return hg if hg.present? && group_matches?(facts)
  end
  Rails.logger.info 'No match ...'
  false
end

#group_matches?(facts) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
# File 'lib/default_hostgroup_base_host_patch.rb', line 43

def group_matches?(facts)
  facts.each do |fact_name, fact_regex|
    fact_regex.gsub!(%r{(\A/|/\z)}, '')
    host_fact_value = 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
  false
end

#host_has_no_hostgroup_or_forced?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
# File 'lib/default_hostgroup_base_host_patch.rb', line 74

def host_has_no_hostgroup_or_forced?
  unless Setting[:force_hostgroup_match]
    if hostgroup.present?
      Rails.logger.debug 'DefaultHostgroupMatch: skipping, host has hostgroup'
      return false
    end
  end
  true
end

#host_new_or_forced?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/default_hostgroup_base_host_patch.rb', line 62

def host_new_or_forced?
  if Setting[:force_hostgroup_match_only_new]
    # hosts have already been saved during import_host, so test the creation age instead
    new_host = ((Time.current - created_at) < 300)
    unless new_host && hostgroup.nil? && reports.empty?
      Rails.logger.debug 'DefaultHostgroupMatch: skipping, host exists'
      return false
    end
  end
  true
end

#import_facts_with_match_hostgroup(facts) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/default_hostgroup_base_host_patch.rb', line 8

def import_facts_with_match_hostgroup(facts)
  # Load the facts anyway, hook onto the end of it
  result = import_facts_without_match_hostgroup(facts)

  return result unless settings_exist?

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

  return result unless host_new_or_forced?
  return result unless host_has_no_hostgroup_or_forced?

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

  return result unless new_hostgroup

  self.hostgroup = new_hostgroup
  if Setting[:force_host_environment] == true
    self.environment = new_hostgroup.environment
  end
  save(validate: false)
  Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"

  result
end

#settings_exist?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/default_hostgroup_base_host_patch.rb', line 54

def settings_exist?
  unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
    Rails.logger.warn 'DefaultHostgroupMatch: Could not load :default_hostgroup map from Settings.'
    return false
  end
  true
end