From e8d2552caa3a9a860aaf59083221733ccf9ad52b Mon Sep 17 00:00:00 2001 From: Michael van Rooijen Date: Sun, 26 Jun 2011 19:02:51 +0200 Subject: [PATCH] Added the ability to use template configuration files using the '--template [-t]' command line option. This allows you to create a directory on the file system containing your configuration files which Foreman will read from instead of the default templates. --- lib/foreman/cli.rb | 1 + lib/foreman/export/base.rb | 9 +++++++-- lib/foreman/export/upstart.rb | 7 ++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index cedd41a..042212b 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -30,6 +30,7 @@ class Foreman::CLI < Thor method_option :log, :type => :string, :aliases => "-l" method_option :port, :type => :numeric, :aliases => "-p" method_option :user, :type => :string, :aliases => "-u" + method_option :template, :type => :string, :aliases => "-t" method_option :concurrency, :type => :string, :aliases => "-c", :banner => '"alpha=5,bar=3"' diff --git a/lib/foreman/export/base.rb b/lib/foreman/export/base.rb index c89cf17..304a81b 100644 --- a/lib/foreman/export/base.rb +++ b/lib/foreman/export/base.rb @@ -4,6 +4,7 @@ require "foreman/utils" class Foreman::Export::Base attr_reader :engine + attr_accessor :template def initialize(engine) @engine = engine @@ -23,8 +24,12 @@ private ###################################################################### puts "[foreman export] %s" % message end - def export_template(name) - File.read(File.expand_path("../../../../data/export/#{name}", __FILE__)) + def export_template(path, file) + if template and File.exist?(file_path = File.join(template, file)) + File.read(file_path) + else + File.read(File.expand_path("../../../../data/export/#{path}/#{file}", __FILE__)) + end end def write_file(filename, contents) diff --git a/lib/foreman/export/upstart.rb b/lib/foreman/export/upstart.rb index 4ff362d..1db930f 100644 --- a/lib/foreman/export/upstart.rb +++ b/lib/foreman/export/upstart.rb @@ -11,6 +11,7 @@ class Foreman::Export::Upstart < Foreman::Export::Base app = options[:app] || File.basename(engine.directory) user = options[:user] || app log_root = options[:log] || "/var/log/#{app}" + self.template = options[:template] Dir["#{location}/#{app}*.conf"].each do |file| say "cleaning up: #{file}" @@ -19,14 +20,14 @@ class Foreman::Export::Upstart < Foreman::Export::Base concurrency = Foreman::Utils.parse_concurrency(options[:concurrency]) - master_template = export_template("upstart/master.conf.erb") + master_template = export_template("upstart", "master.conf.erb") master_config = ERB.new(master_template).result(binding) write_file "#{location}/#{app}.conf", master_config - process_template = export_template("upstart/process.conf.erb") + process_template = export_template("upstart", "process.conf.erb") 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") process_master_config = ERB.new(process_master_template).result(binding) write_file "#{location}/#{app}-#{process.name}.conf", process_master_config