foreman/lib/foreman/renderer.rb @ 6807d423
dd42df0a | Ohad Levy | require 'tempfile'
|
|
218bd6e0 | Justin Sherrill | module Foreman
|
|
module Renderer
|
|||
869bbffe | Ivan Nečas | ||
ALLOWED_HELPERS = [ :foreman_url, :grub_pass, :snippet, :snippets,
|
|||
:snippet_if_exists, :ks_console, :root_pass,
|
|||
:multiboot, :jumpstart_path, :install_path, :miniroot,
|
|||
6807d423 | Stephen Benjamin | :media_path, :param_true?, :param_false? ]
|
|
869bbffe | Ivan Nečas | ||
ALLOWED_VARIABLES = [ :arch, :host, :osver, :mediapath, :static,
|
|||
:repos, :dynamic, :kernel, :initrd,
|
|||
:preseed_server, :preseed_path ]
|
|||
218bd6e0 | Justin Sherrill | def render_safe template, allowed_methods = [], allowed_vars = {}
|
|
76607ed5 | Ohad Levy | if Setting[:safemode_render]
|
|
218bd6e0 | Justin Sherrill | box = Safemode::Box.new self, allowed_methods
|
|
box.eval(ERB.new(template, nil, '-').src, allowed_vars)
|
|||
else
|
|||
0f7cd323 | Dominic Cleal | allowed_vars.each { |k,v| instance_variable_set "@#{k}", v }
|
|
218bd6e0 | Justin Sherrill | ERB.new(template, nil, '-').result(binding)
|
|
end
|
|||
end
|
|||
017e1049 | Ohad Levy | ||
b8c81182 | Paul Kelly | #returns the URL for Foreman Built status (when a host has finished the OS installation)
|
|
def foreman_url(action = "built")
|
|||
8c618ae8 | Greg Sutcliffe | # Get basic stuff
|
|
config = URI.parse(Setting[:unattended_url])
|
|||
protocol = config.scheme || 'http'
|
|||
host = config.host || request.host
|
|||
port = config.port || request.port
|
|||
dd838ac2 | Joseph Mitchell Magen | url_for :only_path => false, :controller => "/unattended", :action => action,
|
|
8c618ae8 | Greg Sutcliffe | :protocol => protocol, :host => host, :port => port,
|
|
9f6de1e7 | Greg Sutcliffe | :token => (@host.token.value unless @host.token.nil?)
|
|
b8c81182 | Paul Kelly | end
|
|
# provide embedded snippets support as simple erb templates
|
|||
def snippets(file)
|
|||
67799065 | Ohad Levy | if ConfigTemplate.where(:name => file, :snippet => true).empty?
|
|
b8c81182 | Paul Kelly | render :partial => "unattended/snippets/#{file}"
|
|
67799065 | Ohad Levy | else
|
|
return snippet(file.gsub(/^_/, ""))
|
|||
b8c81182 | Paul Kelly | end
|
|
end
|
|||
3701a8d1 | Lukas Zapletal | def snippet name, options = {}
|
|
67799065 | Ohad Levy | if (template = ConfigTemplate.where(:name => name, :snippet => true).first)
|
|
b8c81182 | Paul Kelly | 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
|
|||
3701a8d1 | Lukas Zapletal | if options[:silent]
|
|
nil
|
|||
else
|
|||
raise "The specified snippet '#{name}' does not exist, or is not a snippet."
|
|||
end
|
|||
b8c81182 | Paul Kelly | end
|
|
end
|
|||
3701a8d1 | Lukas Zapletal | def snippet_if_exists name
|
|
snippet name, :silent => true
|
|||
end
|
|||
b8c81182 | Paul Kelly | def unattended_render template
|
|
869bbffe | Ivan Nečas | allowed_variables = ALLOWED_VARIABLES.reduce({}) do |mapping, var|
|
|
mapping.update(var => instance_variable_get("@#{var}"))
|
|||
end
|
|||
render_safe template, ALLOWED_HELPERS, allowed_variables
|
|||
b8c81182 | Paul Kelly | end
|
|
alias_method :pxe_render, :unattended_render
|
|||
dd42df0a | Ohad Levy | ||
81e0a301 | Joseph Mitchell Magen | def unattended_render_to_temp_file content, prefix = id.to_s, options = {}
|
|
dd42df0a | Ohad Levy | 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
|
|||
218bd6e0 | Justin Sherrill | end
|
|
end
|