Module: Foreman::Renderer

Instance Method Summary collapse

Instance Method Details

#foreman_url(action = "built") ⇒ Object

returns the URL for Foreman Built status (when a host has finished the OS installation)


17
18
19
20
21
22
# File 'lib/foreman/renderer.rb', line 17

def foreman_url(action = "built")
  url_for :only_path => false, :controller => "/unattended", :action => action,
          :host      => (Setting[:foreman_url] unless Setting[:foreman_url].blank?),
          :protocol  => 'http',
          :token     => (@host.token.value unless @host.token.nil?)
end

#render_safe(template, allowed_methods = [], allowed_vars = {}) ⇒ Object


5
6
7
8
9
10
11
12
13
14
# File 'lib/foreman/renderer.rb', line 5

def render_safe template, allowed_methods = [], allowed_vars = {}

  if Setting[:safemode_render]
    box = Safemode::Box.new self, allowed_methods
    box.eval(ERB.new(template, nil, '-').src, allowed_vars)
  else
    allowed_vars.each { |k,v| instance_variable_set "@#{k}", v }
    ERB.new(template, nil, '-').result(binding)
  end
end

#snippet(name) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/foreman/renderer.rb', line 33

def snippet name
  if (template = ConfigTemplate.where(:name => name, :snippet => true).first)
    logger.debug "rendering snippet #{template.name}"
    begin
      return unattended_render(template.template)
    rescue Exception => exc
      raise "The snippet '#{name}' threw an error: #{exc}"
    end
  else
    raise "The specified snippet '#{name}' does not exist, or is not a snippet."
  end
end

#snippets(file) ⇒ Object

provide embedded snippets support as simple erb templates


25
26
27
28
29
30
31
# File 'lib/foreman/renderer.rb', line 25

def snippets(file)
  if ConfigTemplate.where(:name => file, :snippet => true).empty?
    render :partial => "unattended/snippets/#{file}"
  else
    return snippet(file.gsub(/^_/, ""))
  end
end

#unattended_render(template) ⇒ Object Also known as: pxe_render


46
47
48
49
50
51
52
53
# File 'lib/foreman/renderer.rb', line 46

def unattended_render template
  allowed_helpers   = [ :foreman_url, :grub_pass, :snippet, :snippets, :ks_console, :root_pass, :multiboot, :jumpstart_path, :install_path,
                        :miniroot, :media_path]
  allowed_variables = ({:arch => @arch, :host => @host, :osver => @osver, :mediapath => @mediapath, :static => @static,
                       :yumrepo => @yumrepo, :repos => @repos, :dynamic => @dynamic, :epel => @epel, :kernel => @kernel, :initrd => @initrd,
                       :preseed_server => @preseed_server, :preseed_path => @preseed_path })
  render_safe template, allowed_helpers, allowed_variables
end

#unattended_render_to_temp_file(content, prefix = id.to_s, options = {}) ⇒ Object


56
57
58
59
60
61
62
63
64
65
# File 'lib/foreman/renderer.rb', line 56

def unattended_render_to_temp_file content, prefix = id.to_s, options = {}
  file = ""
  Tempfile.open(prefix, Rails.root.join('tmp') ) do |f|
    f.print(unattended_render(content))
    f.flush
    f.chmod options[:mode] if options[:mode]
    file = f
  end
  file
end