Module: HostCommon

Extended by:
ActiveSupport::Concern, ApipieDSL::Module
Includes:
BelongsToProxies
Included in:
Host::Managed, Hostgroup
Defined in:
app/models/concerns/host_common.rb

Overview

Common methods between host and hostgroup mostly for template rendering consistency

Instance Method Summary collapse

Instance Method Details

#all_puppetclass_idsObject


210
211
212
# File 'app/models/concerns/host_common.rb', line 210

def all_puppetclass_ids
  cg_class_ids + hg_class_ids + host_class_ids
end

#available_puppetclassesObject


246
247
248
249
# File 'app/models/concerns/host_common.rb', line 246

def available_puppetclasses
  return Puppetclass.where(nil).authorized(:view_puppetclasses) if environment.blank?
  environment.puppetclasses - parent_classes
end

#cg_class_idsObject


188
189
190
191
192
193
194
195
# File 'app/models/concerns/host_common.rb', line 188

def cg_class_ids
  cg_ids = if is_a?(Hostgroup)
             path.each.map(&:config_group_ids).flatten.uniq
           else
             hostgroup ? hostgroup.path.each.map(&:config_group_ids).flatten.uniq : []
           end
  ConfigGroupClass.where(:config_group_id => (config_group_ids + cg_ids)).pluck(:puppetclass_id)
end

#classes(env = environment) ⇒ Object


214
215
216
217
218
219
220
221
# File 'app/models/concerns/host_common.rb', line 214

def classes(env = environment)
  conditions = {:id => all_puppetclass_ids }
  if env
    env.puppetclasses.where(conditions)
  else
    Puppetclass.where(conditions)
  end
end

#classes_in_groupsObject


227
228
229
230
231
232
233
234
# File 'app/models/concerns/host_common.rb', line 227

def classes_in_groups
  conditions = {:id => cg_class_ids }
  if environment
    environment.puppetclasses.where(conditions) - parent_classes
  else
    Puppetclass.where(conditions) - parent_classes
  end
end

#crypt_root_passObject


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/concerns/host_common.rb', line 161

def crypt_root_pass
  # hosts will always copy and crypt the password from parents when saved, but hostgroups should
  # only crypt if the attribute is stored, else will stay blank and inherit
  unencrypted_pass = if is_a?(Hostgroup)
                       self[:root_pass]
                     else
                       root_pass
                     end

  if unencrypted_pass.present?
    is_actually_encrypted = if (operatingsystem.try(:password_hash) == "Base64" || operatingsystem.try(:password_hash) == "Base64-Windows")
                              password_base64_encrypted?
                            elsif PasswordCrypt.crypt_gnu_compatible?
                              unencrypted_pass.match('^\$\d+\$.+\$.+')
                            else
                              unencrypted_pass.starts_with?("$")
                            end

    if is_actually_encrypted
      self.root_pass = self.grub_pass = unencrypted_pass
    else
      self.root_pass = operatingsystem.nil? ? PasswordCrypt.passw_crypt(unencrypted_pass) : PasswordCrypt.passw_crypt(unencrypted_pass, operatingsystem.password_hash)
      self.grub_pass = PasswordCrypt.grub2_passw_crypt(unencrypted_pass)
    end
  end
end

#default_image_fileObject

If the host/hostgroup has a medium then use the path from there Else if the host/hostgroup's operatingsystem has only one media then use the image_path from that as this is automatically displayed when there is only one item Else we cannot provide a default and it is cut and paste time


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/concerns/host_common.rb', line 133

def default_image_file
  return "" unless operatingsystem&.supports_image
  if medium
    nfs_path = medium.try :image_path
    if operatingsystem.try(:media) && operatingsystem.media.size == 1
      nfs_path ||= operatingsystem.media.first.image_path
    end
    # We encode the hw_model into the image file name as not all Sparc flashes can contain all possible hw_models. The user can always
    # edit it if required or use symlinks if they prefer.
    hw_model = model.try :hardware_model if defined?(model_id)
    medium_provider.interpolate_vars(nfs_path) + \
      "#{operatingsystem.file_prefix}.#{architecture}#{hw_model.empty? ? '' : '.' + hw_model.downcase}.#{operatingsystem.image_extension}"
  else
    ""
  end
end

#hg_class_idsObject


197
198
199
200
201
202
203
204
# File 'app/models/concerns/host_common.rb', line 197

def hg_class_ids
  hg_ids = if is_a?(Hostgroup)
             path_ids
           elsif hostgroup
             hostgroup.path_ids
           end
  HostgroupClass.where(:hostgroup_id => hg_ids).pluck(:puppetclass_id)
end

#host_class_idsObject


206
207
208
# File 'app/models/concerns/host_common.rb', line 206

def host_class_ids
  (is_a?(Host::Base) ? host_classes : hostgroup_classes).map(&:puppetclass_id)
end

#image_fileObject


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

def image_file
  super || default_image_file
end

#image_file=(file) ⇒ Object


150
151
152
153
154
155
# File 'app/models/concerns/host_common.rb', line 150

def image_file=(file)
  # We only save a value into the image_file field if the value is not the default path, (which was placed in the entry when it was displayed,)
  # and it is not a directory, (ends in /)
  value = ((default_image_file == file) || (file =~ /\/\Z/) || file == "") ? nil : file
  self[:image_file] = value
end

#individual_puppetclassesObject

Returns Puppetclasses of a Host or Hostgroup

It does not include Puppetclasses of it's ConfigGroupClasses


240
241
242
243
244
# File 'app/models/concerns/host_common.rb', line 240

def individual_puppetclasses
  ids = host_class_ids - cg_class_ids
  return puppetclasses if ids.blank? && new_record?
  Puppetclass.includes(:environments).where(id: ids)
end

#medium_providerObject


83
84
85
# File 'app/models/concerns/host_common.rb', line 83

def medium_provider
  @medium_provider ||= Foreman::Plugin.medium_providers.find_provider(self)
end

#parent_nameObject


75
76
77
78
79
80
81
# File 'app/models/concerns/host_common.rb', line 75

def parent_name
  if is_a?(Host::Base) && hostgroup
    hostgroup.name
  elsif is_a?(Hostgroup) && parent
    parent.name
  end
end

#puppet_ca_serverObject

The Puppet CA server FQDN or an empty string. Exposed as a provisioning macro.


126
127
128
# File 'app/models/concerns/host_common.rb', line 126

def puppet_ca_server
  puppet_ca_server_uri.try(:host) || ''
end

#puppet_ca_server_uriObject


117
118
119
120
121
122
# File 'app/models/concerns/host_common.rb', line 117

def puppet_ca_server_uri
  return unless puppet_ca_proxy
  url = puppet_ca_proxy.setting('Puppet CA', 'puppet_url')
  url ||= "https://#{puppet_ca_proxy}:8140"
  URI(url)
end

#puppet_server_uriObject


105
106
107
108
109
110
# File 'app/models/concerns/host_common.rb', line 105

def puppet_server_uri
  return unless puppet_proxy
  url = puppet_proxy.setting('Puppet', 'puppet_url')
  url ||= "https://#{puppet_proxy}:8140"
  URI(url)
end

#puppetca?Boolean

Returns:

  • (Boolean)

96
97
98
99
# File 'app/models/concerns/host_common.rb', line 96

def puppetca?
  return false if respond_to?(:managed?) && !managed?
  puppetca_exists?
end

#puppetca_exists?Boolean

Returns:

  • (Boolean)

101
102
103
# File 'app/models/concerns/host_common.rb', line 101

def puppetca_exists?
  !!(puppet_ca_proxy && puppet_ca_proxy.url.present?)
end

#puppetclass_idsObject


223
224
225
# File 'app/models/concerns/host_common.rb', line 223

def puppetclass_ids
  classes.reorder('').pluck('puppetclasses.id')
end

#puppetmasterObject

The Puppet server FQDN or an empty string. Exposed as a provisioning macro


113
114
115
# File 'app/models/concerns/host_common.rb', line 113

def puppetmaster
  puppet_server_uri.try(:host) || ''
end

#url_for_boot(file) ⇒ Object


92
93
94
# File 'app/models/concerns/host_common.rb', line 92

def url_for_boot(file)
  os.url_for_boot(medium_provider, file)
end