Class: ForemanDiscovery::HostConverter

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_discovery/host_converter.rb

Class Method Summary collapse

Class Method Details

.legacy_host(host) ⇒ Object



42
43
44
# File 'app/services/foreman_discovery/host_converter.rb', line 42

def self.legacy_host(host)
  Gem::Version.new(host.facts['discovery_version'] || '1.0.0') < Gem::Version.new('3.0.0')
end

.set_build_clean_facts(host) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/foreman_discovery/host_converter.rb', line 24

def self.set_build_clean_facts(host)
  # set legacy_api flag for post_queue actions
  host.legacy_api = self.legacy_host(host)
  # fact cleaning
  if Setting['discovery_clean_facts']
    # clean all facts except those starting with "discovery_"
    host.define_singleton_method(:clear_facts) do
        keep_ids = FactValue.where("host_id = #{host.id}").joins(:fact_name).where("fact_names.name like 'discovery_%'").pluck("fact_values.id")
        FactValue.where("host_id = #{host.id} and id not in (?)", keep_ids).delete_all
    end
  else
    # clean no facts (default behavior)
    host.define_singleton_method(:clear_facts) {}
  end
  # set build flag
  host.build = true
end

.to_managed(original_host, set_managed = true, set_build = true, added_attributes = {}) ⇒ Object

Converts discovered host to managed host without uptading the database. Record must be saved explicitly (using save! or update_attributes! or similar). Creates shallow copy.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/foreman_discovery/host_converter.rb', line 6

def self.to_managed(original_host, set_managed = true, set_build = true, added_attributes = {})
  host = original_host.becomes(::Host::Managed)
  host.clear_association_cache
  host.type = 'Host::Managed'

  host.attributes = host.apply_inherited_attributes(added_attributes)
  host.set_hostgroup_defaults if host.hostgroup_id.present?

  # the following flags can be skipped when parameters are set to false
  if set_managed
    host.managed = set_managed
    host.primary_interface.managed = set_managed
  end
  # set build only and only on final save (facts are deleted)
  set_build_clean_facts(host) if set_build
  host
end