Permalink
Browse files

Add runit export format.

Fix #28
  • Loading branch information...
1 parent 615aada commit 676d3ff8d16d3f505aad3d9e4ba3b8988dabea47 @mlangenberg mlangenberg committed Nov 8, 2011
View
@@ -6,7 +6,7 @@ group :development do
gem 'parka'
gem 'rake'
gem 'ronn'
- gem 'fakefs', '~> 0.2.1'
+ gem 'fakefs', '~> 0.3.2'
gem 'rcov', '~> 0.9.8'
gem 'rr', '~> 1.0.2'
gem 'rspec', '~> 2.6.0'
View
@@ -15,7 +15,7 @@ GEM
builder (3.0.0)
crack (0.1.8)
diff-lcs (1.1.2)
- fakefs (0.2.1)
+ fakefs (0.3.2)
hpricot (0.8.2)
mime-types (1.16)
mustache (0.11.2)
@@ -51,7 +51,7 @@ PLATFORMS
DEPENDENCIES
aws-s3
- fakefs (~> 0.2.1)
+ fakefs (~> 0.3.2)
foreman!
parka
rake
@@ -0,0 +1,7 @@
+#!/bin/sh
+set -e
+
+LOG=<%= log_root %>/<%= process.name %>-<%= num %>
+
+test -d "$LOG" || mkdir -p m2750 "$LOG" && chown <%= user %> "$LOG"
+exec chpst -u <%= user %> svlogd "$LOG"
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd <%= engine.directory %>
+exec chpst -u <%= user %> -e <%= process_env_directory %> <%= process.command %>
View
@@ -42,6 +42,7 @@ def export(format, location=nil)
when "inittab" then Foreman::Export::Inittab
when "upstart" then Foreman::Export::Upstart
when "bluepill" then Foreman::Export::Bluepill
+ when "runit" then Foreman::Export::Runit
else error "Unknown export format: #{format}."
end
View
@@ -8,3 +8,4 @@ class Exception < ::Exception; end
require "foreman/export/inittab"
require "foreman/export/upstart"
require "foreman/export/bluepill"
+require "foreman/export/runit"
@@ -0,0 +1,60 @@
+require "erb"
+require "foreman/export"
+
+class Foreman::Export::Runit < Foreman::Export::Base
+ ENV_VARIABLE_REGEX = /([a-zA-Z_]+[a-zA-Z0-9_]*)=(\S+)/
+
+ def export(location, options={})
+ error("Must specify a location") unless location
+
+ app = options[:app] || File.basename(engine.directory)
+ user = options[:user] || app
+ log_root = options[:log] || "/var/log/#{app}"
+ template_root = options[:template]
+
+ concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
+
+ run_template = export_template('runit', 'run.erb', template_root)
+ log_run_template = export_template('runit', 'log_run.erb', template_root)
+
+ engine.processes.each do |process|
+ 1.upto(concurrency[process.name]) do |num|
+ process_directory = "#{location}/#{app}-#{process.name}-#{num}"
+ process_env_directory = "#{process_directory}/env"
+ process_log_directory = "#{process_directory}/log"
+
+ create_directory process_directory
+ create_directory process_env_directory
+ create_directory process_log_directory
+
+ run = ERB.new(run_template).result(binding)
+ write_file "#{process_directory}/run", run
+
+ port = engine.port_for(process, num, options[:port])
+ environment_variables = {'PORT' => port}.
+ merge(engine.environment).
+ merge(inline_variables(process.command))
+
+ environment_variables.each_pair do |var, env|
+ write_file "#{process_env_directory}/#{var.upcase}", env
+ end
+
+ log_run = ERB.new(log_run_template).result(binding)
+ write_file "#{process_log_directory}/run", log_run
+
+ end
+ end
+
+ end
+
+ private
+ def create_directory(location)
+ say "creating: #{location}"
+ FileUtils.mkdir(location)
+ end
+
+ def inline_variables(command)
+ variable_name_regex =
+ Hash[*command.scan(ENV_VARIABLE_REGEX).flatten]
+ end
+end
@@ -0,0 +1,35 @@
+require "spec_helper"
+require "foreman/engine"
+require "foreman/export/runit"
+require "tmpdir"
+
+describe Foreman::Export::Runit do
+ let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile", 'bar=baz') }
+ let(:engine) { Foreman::Engine.new(procfile) }
+ let(:runit) { Foreman::Export::Runit.new(engine) }
+
+ before(:each) { load_export_templates_into_fakefs("runit") }
+ before(:each) { stub(runit).say }
+
+ it "exports to the filesystem" do
+ FileUtils.mkdir_p('/tmp/init')
+ runit.export('/tmp/init', :concurrency => 'alpha=2')
+
+ File.read("/tmp/init/app-alpha-1/run").should == example_export_file('runit/app-alpha-1-run')
+ File.read("/tmp/init/app-alpha-1/log/run").should ==
+ example_export_file('runit/app-alpha-1-log-run')
+ File.read("/tmp/init/app-alpha-1/env/PORT").should == "5000\n"
+ File.read("/tmp/init/app-alpha-1/env/BAR").should == "baz\n"
+
+ File.read("/tmp/init/app-alpha-2/run").should == example_export_file('runit/app-alpha-2-run')
+ File.read("/tmp/init/app-alpha-2/log/run").should ==
+ example_export_file('runit/app-alpha-2-log-run')
+ File.read("/tmp/init/app-alpha-2/env/PORT").should == "5001\n"
+ File.read("/tmp/init/app-alpha-2/env/BAR").should == "baz\n"
+
+ File.read("/tmp/init/app-bravo-1/run").should == example_export_file('runit/app-bravo-1-run')
+ File.read("/tmp/init/app-bravo-1/log/run").should ==
+ example_export_file('runit/app-bravo-1-log-run')
+ File.read("/tmp/init/app-bravo-1/env/PORT").should == "5100\n"
+ end
+end
@@ -0,0 +1,7 @@
+#!/bin/sh
+set -e
+
+LOG=/var/log/app/alpha-1
+
+test -d "$LOG" || mkdir -p m2750 "$LOG" && chown app "$LOG"
+exec chpst -u app svlogd "$LOG"
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd /tmp/app
+exec chpst -u app -e /tmp/init/app-alpha-1/env ./alpha bar=baz
@@ -0,0 +1,7 @@
+#!/bin/sh
+set -e
+
+LOG=/var/log/app/alpha-2
+
+test -d "$LOG" || mkdir -p m2750 "$LOG" && chown app "$LOG"
+exec chpst -u app svlogd "$LOG"
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd /tmp/app
+exec chpst -u app -e /tmp/init/app-alpha-2/env ./alpha bar=baz
@@ -0,0 +1,7 @@
+#!/bin/sh
+set -e
+
+LOG=/var/log/app/bravo-1
+
+test -d "$LOG" || mkdir -p m2750 "$LOG" && chown app "$LOG"
+exec chpst -u app svlogd "$LOG"
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd /tmp/app
+exec chpst -u app -e /tmp/init/app-bravo-1/env ./bravo
View
@@ -28,9 +28,9 @@ def write_foreman_config(app)
end
end
-def write_procfile(procfile="Procfile")
+def write_procfile(procfile="Procfile", alpha_env="")
File.open(procfile, "w") do |file|
- file.puts "alpha: ./alpha"
+ file.puts "alpha: ./alpha" + " #{alpha_env}".rstrip
file.puts "\n"
file.puts "bravo:\t./bravo"
end

0 comments on commit 676d3ff

Please sign in to comment.