Class: Hostgroup

Defined Under Namespace

Classes: Jail

Constant Summary

Constants included from Foreman::Observable

Foreman::Observable::DEFAULT_NAMESPACE

Constants included from Taxonomix

Taxonomix::TAXONOMY_JOIN_TABLE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PxeLoaderSuggestion

#suggest_default_pxe_loader

Methods included from PxeLoaderValidator

#validate_pxe_loader

Methods included from ParameterValidators

#parameters_symbol, #validate_parameters_names

Methods included from SelectiveClone

#selective_clone

Methods included from Facets::HostgroupExtensions

#hostgroup_ancestry_cache, #inherited_facet_attributes

Methods included from NestedAncestryCommon

#get_title, #nested, #parent_name, #title, #to_param

Methods included from Foreman::ObservableModel

#event_payload_for

Methods included from Foreman::Observable

event_name_for, #trigger_hook

Methods included from HostCommon

#all_puppetclass_ids, #available_puppetclasses, #cg_class_ids, #classes, #classes_in_groups, #crypt_root_pass, #default_image_file, #hg_class_ids, #host_class_ids, #image_file, #image_file=, #individual_puppetclasses, #medium_provider, #parent_name, #puppet_ca_server, #puppet_ca_server_uri, #puppet_server_uri, #puppetca?, #puppetca_exists?, #puppetclass_ids, #puppetmaster, #url_for_boot

Methods included from Taxonomix

#add_current_location?, #add_current_organization?, #add_current_taxonomy?, #children_of_selected_location_ids, #children_of_selected_organization_ids, #ensure_taxonomies_not_escalated, #set_current_taxonomy, #used_location_ids, #used_or_selected_location_ids, #used_or_selected_organization_ids, #used_organization_ids

Methods included from DirtyAssociations

#reset_dirty_cache_state

Methods included from Authorizable

#authorized?, #check_permissions_after_save

Methods included from PermissionName

#permission_name

Methods inherited from ApplicationRecord

graphql_type, #logger, logger

Methods included from AuditAssociations::AssociationsDefinitions

#audit_associations, #audited, #configure_dirty_associations, #normalize_associations

Class Method Details

.search_by_config_group(key, operator, value) ⇒ Object


72
73
74
75
76
77
78
79
# File 'app/models/hostgroup.rb', line 72

def self.search_by_config_group(key, operator, value)
  conditions = sanitize_sql_for_conditions(["config_groups.name #{operator} ?", value_to_sql(operator, value)])
  hostgroup_ids = Hostgroup.unscoped.with_taxonomy_scope.joins(:config_groups).where(conditions).map(&:subtree_ids).flatten.uniq

  opts = 'hostgroups.id < 0'
  opts = "hostgroups.id IN(#{hostgroup_ids.join(',')})" if hostgroup_ids.present?
  {:conditions => opts}
end

.title_nameObject


141
142
143
# File 'app/models/hostgroup.rb', line 141

def self.title_name
  "title".freeze
end

Instance Method Details

#all_config_groupsObject


157
158
159
# File 'app/models/hostgroup.rb', line 157

def all_config_groups
  (config_groups + parent_config_groups).uniq
end

#children_hosts_countObject


263
264
265
266
# File 'app/models/hostgroup.rb', line 263

def children_hosts_count
  counter = HostCounter.new(:hostgroup)
  subtree_ids.map { |child_id| counter.fetch(child_id, 0) }.sum
end

#clone(name = "") ⇒ Object

Clone the hostgroup


246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/hostgroup.rb', line 246

def clone(name = "")
  new = selective_clone
  new.name = name
  new.title = name
  new.lookup_values.each do |lv|
    lv.match = new.lookup_value_match
    lv.host_or_hostgroup = new
  end

  new.config_groups = config_groups
  new
end

#disk_layout_sourceObject


145
146
147
148
149
150
# File 'app/models/hostgroup.rb', line 145

def disk_layout_source
  @disk_layout_source ||= if ptable.present?
                            Foreman::Renderer::Source::String.new(name: ptable.name,
                                                                  content: ptable.layout.tr("\r", ''))
                          end
end

#diskLayoutObject


152
153
154
155
# File 'app/models/hostgroup.rb', line 152

def diskLayout
  raise Foreman::Renderer::Errors::RenderingError, 'Partition table not defined for hostgroup' unless disk_layout_source
  disk_layout_source.content
end

#explicit_pxe_loaderObject


234
235
236
# File 'app/models/hostgroup.rb', line 234

def explicit_pxe_loader
  self[:pxe_loader].presence
end

#global_parametersObject


211
212
213
# File 'app/models/hostgroup.rb', line 211

def global_parameters
  Hostgroup.sort_by_ancestry(Hostgroup.includes(:group_parameters).find(ancestor_ids + id)).map(&:group_parameters).uniq
end

#hostgroupObject

TODO: add a method that returns the valid os for a hostgroup


137
138
139
# File 'app/models/hostgroup.rb', line 137

def hostgroup
  self
end

#hosts_countObject


259
260
261
# File 'app/models/hostgroup.rb', line 259

def hosts_count
  HostCounter.new(:hostgroup)[self]
end

#inherited_lookup_value(key) ⇒ Object


176
177
178
179
180
181
182
183
184
185
# File 'app/models/hostgroup.rb', line 176

def inherited_lookup_value(key)
  if key.path_elements.flatten.include?("hostgroup") && Setting["matchers_inheritance"]
    ancestors.reverse_each do |hg|
      if (v = LookupValue.find_by(:lookup_key_id => key.id, :id => hg.lookup_values))
        return v.value, hg.to_label
      end
    end
  end
  [key.default_value, _("Default value")]
end

#parameters(include_source = false) ⇒ Object

returns self and parent parameters as a hash


203
204
205
206
207
208
209
# File 'app/models/hostgroup.rb', line 203

def parameters(include_source = false)
  hash = parent_params(include_source)
  group_parameters.each do |p|
    hash[p.name] = include_source ? p.hash_for_include_source(p.associated_type, title) : p.value
  end
  hash
end

#paramsObject


215
216
217
218
219
220
221
222
223
224
# File 'app/models/hostgroup.rb', line 215

def params
  parameters = {}
  # read common parameters
  CommonParameter.where(nil).find_each { |p| parameters.update Hash[p.name => p.value] }
  # read OS parameters
  operatingsystem&.os_parameters&.each { |p| parameters.update Hash[p.name => p.value] }
  # read group parameters only if a host belongs to a group
  parameters.update self.parameters if hostgroup
  parameters
end

#parent_classesObject

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


171
172
173
174
# File 'app/models/hostgroup.rb', line 171

def parent_classes
  return [] unless parent
  parent.classes(environment)
end

#parent_config_groupsObject


161
162
163
164
165
166
167
168
# File 'app/models/hostgroup.rb', line 161

def parent_config_groups
  return [] unless parent
  groups = []
  ancestors.each do |hostgroup|
    groups += hostgroup.config_groups
  end
  groups.uniq
end

#parent_params(include_source = false) ⇒ Object


187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/models/hostgroup.rb', line 187

def parent_params(include_source = false)
  hash = {}
  ids = ancestor_ids
  # need to pull out the hostgroups to ensure they are sorted first,
  # otherwise we might be overwriting the hash in the wrong order.
  groups = Hostgroup.sort_by_ancestry(Hostgroup.includes(:group_parameters).find(ids))
  groups.each do |hg|
    params_arr = hg.group_parameters.authorized(:view_params)
    params_arr.each do |p|
      hash[p.name] = include_source ? p.hash_for_include_source(p.associated_type, hg.title) : p.value
    end
  end
  hash
end

#pxe_loaderObject


238
239
240
# File 'app/models/hostgroup.rb', line 238

def pxe_loader
  explicit_pxe_loader || nested(:pxe_loader).presence
end

#recreate_hosts_config(only = nil, children_hosts = false) ⇒ Object

rebuilds orchestration configuration for hostgroup's hosts takes all the methods from Orchestration modules that are registered for configuration rebuild arguments:

> only : Array of rebuild methods to execute (Example: ['TFTP'])

> children_hosts : Boolean that if true will operate on children hostgroup's hosts

returns : Hash with 'true' if rebuild was a success for a given key (Example: {“TFTP” => true, “DNS” => false})


274
275
276
277
278
279
280
281
# File 'app/models/hostgroup.rb', line 274

def recreate_hosts_config(only = nil, children_hosts = false)
  result = {}

  Host::Managed.authorized.where(:hostgroup => (children_hosts ? subtree_ids : id)).find_each do |host|
    result[host.name] = host.recreate_config(only)
  end
  result
end

#render_template(template:, **params) ⇒ Object


283
284
285
# File 'app/models/hostgroup.rb', line 283

def render_template(template:, **params)
  template.render(host: self, **params)
end

#root_passObject

no need to store anything in the db if the password is our default


227
228
229
230
231
232
# File 'app/models/hostgroup.rb', line 227

def root_pass
  return self[:root_pass] if self[:root_pass].present?
  npw = nested_root_pw
  return npw if npw.present?
  Setting[:root_pass]
end