Sha256: 8c94056a791f44c3b99c5a1a72a2449dda3ba15fcdb061df54203bae764fd99a

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

class ForemanDiscovery::HostConverter

  # 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.
  def self.to_managed(original_host, set_managed = true, set_build = true, added_attributes = {})
    host = original_host.becomes(::Host::Managed)
    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

  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

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

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
foreman_discovery-9.1.5 app/services/foreman_discovery/host_converter.rb
foreman_discovery-9.1.4 app/services/foreman_discovery/host_converter.rb
foreman_discovery-9.1.3 app/services/foreman_discovery/host_converter.rb
foreman_discovery-9.1.2 app/services/foreman_discovery/host_converter.rb
foreman_discovery-9.1.1 app/services/foreman_discovery/host_converter.rb
foreman_discovery-9.1.0 app/services/foreman_discovery/host_converter.rb