From 21a041527ca33b15444cf1c35a0f1f2876ea5e4c Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Mon, 22 Aug 2011 21:24:34 +1000 Subject: [PATCH 1/2] Added basic support for Bluepill --- data/export/bluepill/master.pill.erb | 25 +++++++++++ lib/foreman/export/bluepill.rb | 29 ++++++++++++ spec/foreman/export/bluepill_spec.rb | 20 +++++++++ spec/resources/export/bluepill/app.pill | 59 +++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 data/export/bluepill/master.pill.erb create mode 100644 lib/foreman/export/bluepill.rb create mode 100644 spec/foreman/export/bluepill_spec.rb create mode 100644 spec/resources/export/bluepill/app.pill diff --git a/data/export/bluepill/master.pill.erb b/data/export/bluepill/master.pill.erb new file mode 100644 index 0000000..6c44eca --- /dev/null +++ b/data/export/bluepill/master.pill.erb @@ -0,0 +1,25 @@ +Bluepill.application("<%= app %>", :foreground => false) do |app| + + app.uid = "<%= user %>" + app.gid = "<%= user %>" + +<% engine.processes.values.each do |process| %> +<% 1.upto(concurrency[process.name]) do |num| %> +<% port = engine.port_for(process, num, options[:port]) %> + app.process("<%= process.name %>-<%=num%>") do |process| + process.start_command = "<%= process.command %>" + + process.working_dir = "<%= engine.directory %>" + process.pid_file = "/var/run/<%= app %>-<%= process.name %>-<%=num%>.pid" + process.daemonize = true + process.environment = {"PORT" => "<%= port %>"} + + process.stdout = process.stderr = "<%= log_root %>/<%= app %>-<%= process.name %>-<%=num%>.log" + + process.monitor_children do |children| + children.stop_command "kill -QUIT {{PID}}" + end + end +<% end %> +<% end %> +end \ No newline at end of file diff --git a/lib/foreman/export/bluepill.rb b/lib/foreman/export/bluepill.rb new file mode 100644 index 0000000..8cf8e5a --- /dev/null +++ b/lib/foreman/export/bluepill.rb @@ -0,0 +1,29 @@ +require "erb" +require "foreman/export" + +class Foreman::Export::Bluepill < Foreman::Export::Base + + def export(location, options={}) + error("Must specify a location") unless location + + FileUtils.mkdir_p location + + app = options[:app] || File.basename(engine.directory) + user = options[:user] || app + log_root = options[:log] || "/var/log/#{app}" + template_root = options[:template] + + Dir["#{location}/#{app}.pill"].each do |file| + say "cleaning up: #{file}" + FileUtils.rm(file) + end + + concurrency = Foreman::Utils.parse_concurrency(options[:concurrency]) + + master_template = export_template("bluepill", "master.pill.erb", template_root) + master_config = ERB.new(master_template).result(binding) + write_file "#{location}/#{app}.pill", master_config + + end + +end diff --git a/spec/foreman/export/bluepill_spec.rb b/spec/foreman/export/bluepill_spec.rb new file mode 100644 index 0000000..35d74e6 --- /dev/null +++ b/spec/foreman/export/bluepill_spec.rb @@ -0,0 +1,20 @@ +require "spec_helper" +require "foreman/engine" +require "foreman/export/bluepill" +require "tmpdir" + +describe Foreman::Export::Bluepill do + let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile") } + let(:engine) { Foreman::Engine.new(procfile) } + let(:bluepill) { Foreman::Export::Bluepill.new(engine) } + + before(:each) { load_export_templates_into_fakefs("bluepill") } + before(:each) { stub(bluepill).say } + + it "exports to the filesystem" do + bluepill.export("/tmp/init") + + File.read("/tmp/init/app.pill").should == example_export_file("bluepill/app.pill") + end + +end \ No newline at end of file diff --git a/spec/resources/export/bluepill/app.pill b/spec/resources/export/bluepill/app.pill new file mode 100644 index 0000000..9e01527 --- /dev/null +++ b/spec/resources/export/bluepill/app.pill @@ -0,0 +1,59 @@ +Bluepill.application("app", :foreground => false) do |app| + + app.uid = "app" + app.gid = "app" + + + + + app.process("alpha-1") do |process| + process.start_command = "./alpha" + + process.working_dir = "/tmp/app" + process.pid_file = "/var/run/app-alpha-1.pid" + process.daemonize = true + process.environment = {"PORT" => "5000"} + + process.stdout = process.stderr = "/var/log/app/app-alpha-1.log" + + process.monitor_children do |children| + children.stop_command "kill -QUIT {{PID}}" + end + end + + + app.process("alpha-2") do |process| + process.start_command = "./alpha" + + process.working_dir = "/tmp/app" + process.pid_file = "/var/run/app-alpha-2.pid" + process.daemonize = true + process.environment = {"PORT" => "5001"} + + process.stdout = process.stderr = "/var/log/app/app-alpha-2.log" + + process.monitor_children do |children| + children.stop_command "kill -QUIT {{PID}}" + end + end + + + + + app.process("bravo-1") do |process| + process.start_command = "./bravo" + + process.working_dir = "/tmp/app" + process.pid_file = "/var/run/app-bravo-1.pid" + process.daemonize = true + process.environment = {"PORT" => "5100"} + + process.stdout = process.stderr = "/var/log/app/app-bravo-1.log" + + process.monitor_children do |children| + children.stop_command "kill -QUIT {{PID}}" + end + end + + +end From 9db97abb10c79f3772d874efb9bd7fcdeab20e50 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 25 Aug 2011 21:17:00 +1000 Subject: [PATCH 2/2] Updates to get Bluepill export working & tweaks to output --- data/export/bluepill/master.pill.erb | 8 +++++--- lib/foreman/cli.rb | 1 + lib/foreman/export.rb | 1 + spec/resources/export/bluepill/app.pill | 14 ++++++++++---- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/data/export/bluepill/master.pill.erb b/data/export/bluepill/master.pill.erb index 6c44eca..e02785f 100644 --- a/data/export/bluepill/master.pill.erb +++ b/data/export/bluepill/master.pill.erb @@ -1,4 +1,4 @@ -Bluepill.application("<%= app %>", :foreground => false) do |app| +Bluepill.application("<%= app %>", :foreground => false, :log_file => "/var/log/bluepill.log") do |app| app.uid = "<%= user %>" app.gid = "<%= user %>" @@ -7,18 +7,20 @@ Bluepill.application("<%= app %>", :foreground => false) do |app| <% 1.upto(concurrency[process.name]) do |num| %> <% port = engine.port_for(process, num, options[:port]) %> app.process("<%= process.name %>-<%=num%>") do |process| - process.start_command = "<%= process.command %>" + process.start_command = "<%= process.command.gsub("$PORT", port.to_s) %>" process.working_dir = "<%= engine.directory %>" - process.pid_file = "/var/run/<%= app %>-<%= process.name %>-<%=num%>.pid" process.daemonize = true process.environment = {"PORT" => "<%= port %>"} + process.stop_signals = [:quit, 30.seconds, :term, 5.seconds, :kill] process.stdout = process.stderr = "<%= log_root %>/<%= app %>-<%= process.name %>-<%=num%>.log" process.monitor_children do |children| children.stop_command "kill -QUIT {{PID}}" end + + process.group = "<%= app %>-<%= process.name %>" end <% end %> <% end %> diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 042212b..e9e76ba 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -40,6 +40,7 @@ class Foreman::CLI < Thor formatter = case format when "inittab" then Foreman::Export::Inittab when "upstart" then Foreman::Export::Upstart + when "bluepill" then Foreman::Export::Bluepill else error "Unknown export format: #{format}." end diff --git a/lib/foreman/export.rb b/lib/foreman/export.rb index ab206c1..7976694 100644 --- a/lib/foreman/export.rb +++ b/lib/foreman/export.rb @@ -7,3 +7,4 @@ end require "foreman/export/base" require "foreman/export/inittab" require "foreman/export/upstart" +require "foreman/export/bluepill" diff --git a/spec/resources/export/bluepill/app.pill b/spec/resources/export/bluepill/app.pill index 9e01527..bef2f13 100644 --- a/spec/resources/export/bluepill/app.pill +++ b/spec/resources/export/bluepill/app.pill @@ -1,4 +1,4 @@ -Bluepill.application("app", :foreground => false) do |app| +Bluepill.application("app", :foreground => false, :log_file => "/var/log/bluepill.log") do |app| app.uid = "app" app.gid = "app" @@ -10,15 +10,17 @@ Bluepill.application("app", :foreground => false) do |app| process.start_command = "./alpha" process.working_dir = "/tmp/app" - process.pid_file = "/var/run/app-alpha-1.pid" process.daemonize = true process.environment = {"PORT" => "5000"} + process.stop_signals = [:quit, 30.seconds, :term, 5.seconds, :kill] process.stdout = process.stderr = "/var/log/app/app-alpha-1.log" process.monitor_children do |children| children.stop_command "kill -QUIT {{PID}}" end + + process.group = "app-alpha" end @@ -26,15 +28,17 @@ Bluepill.application("app", :foreground => false) do |app| process.start_command = "./alpha" process.working_dir = "/tmp/app" - process.pid_file = "/var/run/app-alpha-2.pid" process.daemonize = true process.environment = {"PORT" => "5001"} + process.stop_signals = [:quit, 30.seconds, :term, 5.seconds, :kill] process.stdout = process.stderr = "/var/log/app/app-alpha-2.log" process.monitor_children do |children| children.stop_command "kill -QUIT {{PID}}" end + + process.group = "app-alpha" end @@ -44,15 +48,17 @@ Bluepill.application("app", :foreground => false) do |app| process.start_command = "./bravo" process.working_dir = "/tmp/app" - process.pid_file = "/var/run/app-bravo-1.pid" process.daemonize = true process.environment = {"PORT" => "5100"} + process.stop_signals = [:quit, 30.seconds, :term, 5.seconds, :kill] process.stdout = process.stderr = "/var/log/app/app-bravo-1.log" process.monitor_children do |children| children.stop_command "kill -QUIT {{PID}}" end + + process.group = "app-bravo" end