tweaks to template roots, add testing

This commit is contained in:
David Dollar
2011-06-27 13:17:26 -04:00
parent e8d2552caa
commit c8d0dba1cb
3 changed files with 22 additions and 8 deletions
+3 -4
View File
@@ -4,7 +4,6 @@ require "foreman/utils"
class Foreman::Export::Base class Foreman::Export::Base
attr_reader :engine attr_reader :engine
attr_accessor :template
def initialize(engine) def initialize(engine)
@engine = engine @engine = engine
@@ -24,11 +23,11 @@ private ######################################################################
puts "[foreman export] %s" % message puts "[foreman export] %s" % message
end end
def export_template(path, file) def export_template(exporter, file, template_root)
if template and File.exist?(file_path = File.join(template, file)) if template_root && File.exist?(file_path = File.join(template_root, file))
File.read(file_path) File.read(file_path)
else else
File.read(File.expand_path("../../../../data/export/#{path}/#{file}", __FILE__)) File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__))
end end
end end
+4 -4
View File
@@ -11,7 +11,7 @@ class Foreman::Export::Upstart < Foreman::Export::Base
app = options[:app] || File.basename(engine.directory) app = options[:app] || File.basename(engine.directory)
user = options[:user] || app user = options[:user] || app
log_root = options[:log] || "/var/log/#{app}" log_root = options[:log] || "/var/log/#{app}"
self.template = options[:template] template_root = options[:template]
Dir["#{location}/#{app}*.conf"].each do |file| Dir["#{location}/#{app}*.conf"].each do |file|
say "cleaning up: #{file}" say "cleaning up: #{file}"
@@ -20,14 +20,14 @@ class Foreman::Export::Upstart < Foreman::Export::Base
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency]) concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
master_template = export_template("upstart", "master.conf.erb") master_template = export_template("upstart", "master.conf.erb", template_root)
master_config = ERB.new(master_template).result(binding) master_config = ERB.new(master_template).result(binding)
write_file "#{location}/#{app}.conf", master_config write_file "#{location}/#{app}.conf", master_config
process_template = export_template("upstart", "process.conf.erb") process_template = export_template("upstart", "process.conf.erb", template_root)
engine.processes.values.each do |process| engine.processes.values.each do |process|
process_master_template = export_template("upstart", "process_master.conf.erb") process_master_template = export_template("upstart", "process_master.conf.erb", template_root)
process_master_config = ERB.new(process_master_template).result(binding) process_master_config = ERB.new(process_master_template).result(binding)
write_file "#{location}/#{app}-#{process.name}.conf", process_master_config write_file "#{location}/#{app}-#{process.name}.conf", process_master_config
+15
View File
@@ -21,4 +21,19 @@ describe Foreman::Export::Upstart do
File.read("/tmp/init/app-bravo.conf").should == example_export_file("upstart/app-bravo.conf") File.read("/tmp/init/app-bravo.conf").should == example_export_file("upstart/app-bravo.conf")
File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("upstart/app-bravo-1.conf") File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("upstart/app-bravo-1.conf")
end end
context "with alternate templates" do
let(:template_root) { "/tmp/alternate" }
before do
FileUtils.mkdir_p template_root
File.open("#{template_root}/master.conf.erb", "w") { |f| f.puts "alternate_template" }
end
it "can export with alternate template files" do
upstart.export("/tmp/init", :template => template_root)
File.read("/tmp/init/app.conf").should == "alternate_template\n"
end
end
end end