Permalink
Browse files

Merge pull request #306 from markets/config_file_erb

allow ERB templates in config files
  • Loading branch information...
macournoyer committed Jan 24, 2017
2 parents 4619553 + 1486f90 commit 619befe19a4e8d4e55935a74946b0df63d2667f5
Showing with 27 additions and 12 deletions.
  1. +2 −1 lib/thin/runner.rb
  2. +4 −0 spec/configs/with_erb.yml
  3. +12 −11 spec/controllers/service_spec.rb
  4. +9 −0 spec/runner_spec.rb
@@ -1,6 +1,7 @@
require 'logger'
require 'optparse'
require 'yaml'
require 'erb'

module Thin
# CLI runner.
@@ -219,7 +220,7 @@ def service?
private
def load_options_from_config_file!
if file = @options.delete(:config)
YAML.load_file(file).each { |key, value| @options[key.to_sym] = value }
YAML.load(ERB.new(File.read(file)).result).each { |key, value| @options[key.to_sym] = value }
end
end

@@ -0,0 +1,4 @@
---
timeout: <%= 30 %>
port: <%= 4000 %>
environment: <%= "production" %>
@@ -8,44 +8,45 @@
Service::DEFAULT_CONFIG_PATH = 'tmp/sandbox' + Service::DEFAULT_CONFIG_PATH
end
end

before do
Thin.stub!(:linux?).and_return(true)
FileUtils.mkdir_p 'tmp/sandbox'

@service = Service.new(:all => 'spec/configs')
end

it "should call command for each config file" do
Command.should_receive(:run).with(:start, :config => 'spec/configs/cluster.yml', :daemonize => true)
Command.should_receive(:run).with(:start, :config => 'spec/configs/single.yml', :daemonize => true)

Command.should_receive(:run).with(:start, :config => 'spec/configs/with_erb.yml', :daemonize => true)

@service.start
end

it "should create /etc/init.d/thin file when calling install" do
@service.install

File.exist?(Service::INITD_PATH).should be_true
script_name = File.directory?('/etc/rc.d') ?
'/etc/rc.d/thin' : '/etc/init.d/thin'
File.read(Service::INITD_PATH).should include('CONFIG_PATH=tmp/sandbox/etc/thin',
'SCRIPT_NAME=tmp/sandbox' + script_name,
'DAEMON=' + Command.script)
end

it "should create /etc/thin dir when calling install" do
@service.install

File.directory?(Service::DEFAULT_CONFIG_PATH).should be_true
end

it "should include specified path in /etc/init.d/thin script" do
@service.install('tmp/sandbox/usr/thin')

File.read(Service::INITD_PATH).should include('CONFIG_PATH=tmp/sandbox/usr/thin')
end

after do
FileUtils.rm_rf 'tmp/sandbox'
end
@@ -113,6 +113,15 @@
@runner.options[:servers].should == 3
end

it "should load options from file using an ERB template" do
@runner = Runner.new(%w(start --config spec/configs/with_erb.yml))
@runner.send :load_options_from_config_file!

@runner.options[:timeout].should == 30
@runner.options[:port].should == 4000
@runner.options[:environment].should == 'production'
end

it "should change directory after loading config" do
@orig_dir = Dir.pwd

0 comments on commit 619befe

Please sign in to comment.