Class: ForemanPuppet::HostPuppetFacet

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Facets::Base, PuppetFacetCommon
Defined in:
app/models/foreman_puppet/host_puppet_facet.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PuppetFacetCommon

#all_puppetclass_ids, #available_puppetclasses, #cg_class_ids, #classes, #classes_in_groups, #hg_class_ids, #host_class_ids, #individual_puppetclasses, #parent_name, #puppetclass_ids

Class Method Details

.inherited_attributes(new_hostgroup, attributes) ⇒ Object



33
34
35
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 33

def self.inherited_attributes(new_hostgroup, attributes)
  { 'environment_id' => new_hostgroup.puppet&.inherited_environment_id }.merge(attributes)
end

.populate_fields_from_facts(host, parser, type, source_proxy) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 17

def self.populate_fields_from_facts(host, parser, type, source_proxy)
  type ||= 'puppet'
  return unless type == 'puppet'

  facet = host.puppet || host.build_puppet
  if Setting[:update_environment_from_facts]
    facet.environment = parser.environment if parser.environment.present?
  elsif parser.environment.present?
    facet.environment ||= parser.environment
  end

  # if proxy authentication is enabled and we have no puppet proxy set and the upload came from puppet,
  # use it as puppet proxy.
  host.puppet_proxy ||= source_proxy
end

Instance Method Details

#clear_puppetinfoObject



37
38
39
40
41
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 37

def clear_puppetinfo
  return if environment
  self.puppetclasses = []
  self.config_groups = []
end

#ensure_puppet_associationsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 54

def ensure_puppet_associations
  status = validate_association_taxonomy(:environment)
  return status unless environment

  puppetclasses.where.not(id: environment.puppetclasses.reorder(nil)).find_each do |puppetclass|
    errors.add(
      :puppetclasses,
      format(_('%{puppetclass} does not belong to the %{environment} environment'), puppetclass: puppetclass, environment: environment)
    )
    status = false
  end
  status
end

#import_puppet_node(nodeinfo) ⇒ Object

this method accepts a puppets external node yaml output and generate a node in our setup it is assumed that you already have the node (e.g. imported by one of the rack tasks) rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 72

def import_puppet_node(nodeinfo)
  myklasses = []
  # puppet classes
  classes = nodeinfo['classes']
  classes = classes.keys if classes.is_a?(Hash)
  classes.each do |klass|
    if (pc = ForemanPuppet::Puppetclass.find_by(name: klass.to_s))
      myklasses << pc
    else
      error = format(_("Failed to import %{klass} for %{name}: doesn't exists in our database - ignoring"), klass: klass, name: name)
      logger.warn error
      $stdout.puts error
    end
    self.puppetclasses = myklasses
  end

  # parameters are a bit more tricky, as some classifiers provide the facts as parameters as well
  # not sure what is puppet priority about it, but we ignore it if has a fact with the same name.
  # additionally, we don't import any non strings values, as puppet don't know what to do with those as well.

  myparams = host.info['parameters']
  nodeinfo['parameters'].each_pair do |param, value|
    next if host.fact_names.exists? name: param
    next unless value.is_a?(String)

    # we already have this parameter
    next if myparams.key?(param) && myparams[param] == value

    unless (hp = host.host_parameters.create(name: param, value: value))
      logger.warn "Failed to import #{param}/#{value} for #{name}: #{hp.errors.full_messages.join(', ')}"
      $stdout.puts $ERROR_INFO
    end
  end

  host.clear_host_parameters_cache!
  save
end

#parent_classesObject

the environment used by #clases nees to be self.environment and not self.parent.environment



44
45
46
47
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 44

def parent_classes
  return [] unless host.hostgroup
  host.hostgroup.puppet&.classes(environment) || []
end

#parent_config_groupsObject



49
50
51
52
# File 'app/models/foreman_puppet/host_puppet_facet.rb', line 49

def parent_config_groups
  return [] unless host.hostgroup
  host.hostgroup.puppet&.all_config_groups || []
end