Module: Foreman::Renderer::Scope::Macros::Base

Extended by:
ApipieDSL::Module
Includes:
ActiveSupport::NumberHelper, ForemanUrlRenderer, Errors
Included in:
Base
Defined in:
lib/foreman/renderer/scope/macros/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ForemanUrlRenderer

#default_url_options, #foreman_url

Instance Attribute Details

#medium_providerObject (readonly)

Returns the value of attribute medium_provider


11
12
13
# File 'lib/foreman/renderer/scope/macros/base.rb', line 11

def medium_provider
  @medium_provider
end

#template_nameObject (readonly)

Returns the value of attribute template_name


11
12
13
# File 'lib/foreman/renderer/scope/macros/base.rb', line 11

def template_name
  @template_name
end

Instance Method Details

#all_host_statusesObject


187
188
189
# File 'lib/foreman/renderer/scope/macros/base.rb', line 187

def all_host_statuses
  @all_host_statuses ||= HostStatus.status_registry.to_a.sort_by(&:status_name)
end

#all_host_statuses_hash(host) ⇒ Object


197
198
199
# File 'lib/foreman/renderer/scope/macros/base.rb', line 197

def all_host_statuses_hash(host)
  all_host_statuses.map { |status| [status.status_name, host_status(host, status.status_name).status] }.to_h
end

#default_template_url(template, hostgroup) ⇒ Object


171
172
173
174
175
176
177
178
179
180
# File 'lib/foreman/renderer/scope/macros/base.rb', line 171

def default_template_url(template, hostgroup)
  uri      = URI.parse(Setting[:unattended_url])
  host     = uri.host
  port     = uri.port
  protocol = uri.scheme

  url_for(:only_path => false, :action => :hostgroup_template, :controller => '/unattended',
          :id => template.name, :hostgroup => hostgroup.title, :protocol => protocol,
          :host => host, :port => port)
end

#dns_lookup(name_or_ip) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/foreman/renderer/scope/macros/base.rb', line 149

def dns_lookup(name_or_ip)
  resolver = Resolv::DNS.new
  resolver.timeouts = Setting[:dns_timeout]
  begin
    resolver.getname(name_or_ip)
  rescue Resolv::ResolvError
    resolver.getaddress(name_or_ip)
  end
rescue StandardError => e
  log_warn "Template helper dns_lookup failed: #{e} (timeout set to #{Setting[:dns_timeout]})"
  raise e
end

#foreman_server_ca_certObject

Raises:


346
347
348
349
350
351
352
353
354
# File 'lib/foreman/renderer/scope/macros/base.rb', line 346

def foreman_server_ca_cert
  raise UndefinedSetting.new(setting: 'SSL CA file') if Setting[:ssl_ca_file].blank?
  begin
    File.read(Setting[:ssl_ca_file])
  rescue => e
    msg = N_("%s, check the 'SSL CA file' in Settings > Authentication") % e.message
    raise Foreman::Exception.new(msg)
  end
end

#foreman_server_fqdnObject


74
75
76
77
# File 'lib/foreman/renderer/scope/macros/base.rb', line 74

def foreman_server_fqdn
  config = URI.parse(Setting[:foreman_url])
  config.host
end

#foreman_server_urlObject


83
84
85
# File 'lib/foreman/renderer/scope/macros/base.rb', line 83

def foreman_server_url
  Setting[:foreman_url]
end

#gem_version_compare(first, second) ⇒ Object


333
334
335
# File 'lib/foreman/renderer/scope/macros/base.rb', line 333

def gem_version_compare(first, second)
  Gem::Version.new(first.to_s) <=> Gem::Version.new(second.to_s)
end

#global_setting(name, blank_default = nil) ⇒ Object


42
43
44
45
46
# File 'lib/foreman/renderer/scope/macros/base.rb', line 42

def global_setting(name, blank_default = nil)
  raise FilteredGlobalSettingAccessed.new(name: name) if Setting[:safemode_render] && !Foreman::Renderer.config.allowed_global_settings.include?(name.to_sym)
  setting = Setting.find_by_name(name.to_sym)
  (setting.settings_type != "boolean" && setting.value.blank?) ? blank_default : setting.value
end

#host_cores(host) ⇒ Object


311
312
313
# File 'lib/foreman/renderer/scope/macros/base.rb', line 311

def host_cores(host)
  host&.cores
end

#host_kernel_release(host) ⇒ Object


271
272
273
# File 'lib/foreman/renderer/scope/macros/base.rb', line 271

def host_kernel_release(host)
  host&.kernel_release&.value
end

#host_memory(host) ⇒ Object


293
294
295
# File 'lib/foreman/renderer/scope/macros/base.rb', line 293

def host_memory(host)
  host&.ram
end

#host_sockets(host) ⇒ Object


302
303
304
# File 'lib/foreman/renderer/scope/macros/base.rb', line 302

def host_sockets(host)
  host&.sockets
end

#host_status(host, name) ⇒ Object


209
210
211
212
213
# File 'lib/foreman/renderer/scope/macros/base.rb', line 209

def host_status(host, name)
  klass = all_host_statuses.find { |status| status.status_name == name }
  raise UnknownHostStatusError.new(status: name, statuses: all_host_statuses.map(&:status_name).join(',')) if klass.nil?
  host.get_status(klass)
end

#host_uptime_seconds(host) ⇒ Object


284
285
286
# File 'lib/foreman/renderer/scope/macros/base.rb', line 284

def host_uptime_seconds(host)
  host&.uptime_seconds
end

#host_virtual(host) ⇒ Object


320
321
322
# File 'lib/foreman/renderer/scope/macros/base.rb', line 320

def host_virtual(host)
  host&.virtual
end

#indent(count, skip1: false) ⇒ Object


126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/foreman/renderer/scope/macros/base.rb', line 126

def indent(count, skip1: false)
  return unless block_given? && (text = yield.to_s)
  prefix = ' ' * count
  result = []
  text.each_line.with_index do |line, line_no|
    if line_no == 0 && skip1
      result << line
    else
      result << prefix + line
    end
  end
  result.join('')
end

#mac_name(mac_address) ⇒ Object


259
260
261
# File 'lib/foreman/renderer/scope/macros/base.rb', line 259

def mac_name(mac_address)
  NameGenerator.new.generate_next_mac_name(mac_address)
end

#plugin_present?(name) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/foreman/renderer/scope/macros/base.rb', line 54

def plugin_present?(name)
  Foreman::Plugin.find(name).present?
end

#preview?Boolean

Returns:

  • (Boolean)

231
232
233
# File 'lib/foreman/renderer/scope/macros/base.rb', line 231

def preview?
  mode == Renderer::PREVIEW_MODE
end

#pxe_kernel_optionsObject


96
97
98
99
100
101
102
# File 'lib/foreman/renderer/scope/macros/base.rb', line 96

def pxe_kernel_options
  return '' unless host || host.operatingsystem
  host.operatingsystem.pxe_kernel_options(host.params).join(' ')
rescue => e
  template_logger.warn "Unable to build PXE kernel options: #{e}"
  ''
end

#rand_hex(n) ⇒ Object


240
241
242
# File 'lib/foreman/renderer/scope/macros/base.rb', line 240

def rand_hex(n)
  SecureRandom.hex(n)
end

#rand_nameObject


249
250
251
# File 'lib/foreman/renderer/scope/macros/base.rb', line 249

def rand_name
  NameGenerator.new.generate_next_random_name
end

#save_to_file(filename, content) ⇒ Object


113
114
115
# File 'lib/foreman/renderer/scope/macros/base.rb', line 113

def save_to_file(filename, content)
  "cat << EOF > #{filename}\n#{content}EOF"
end

#subnet_has_param?(subnet, param_name) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
31
# File 'lib/foreman/renderer/scope/macros/base.rb', line 28

def subnet_has_param?(subnet, param_name)
  validate_subnet(subnet)
  subnet.parameters.exists?(name: param_name)
end

#subnet_param(subnet, param_name) ⇒ Object


64
65
66
67
68
# File 'lib/foreman/renderer/scope/macros/base.rb', line 64

def subnet_param(subnet, param_name)
  validate_subnet(subnet)
  param = subnet.parameters.where(name: param_name).first
  param.nil? ? nil : param.value
end

#user_auth_source_name(user) ⇒ Object


220
221
222
# File 'lib/foreman/renderer/scope/macros/base.rb', line 220

def user_auth_source_name(user)
  user.auth_source&.name
end