Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add features to for process management using foreman

  • Loading branch information...
commit 756e07dffdec57cc74666eb227cd456ba600c85d 1 parent c70c2ed
@tomafro tomafro authored
View
17 features/managing-processes.feature
@@ -0,0 +1,17 @@
+
+Feature: Managing processes with foreman
+
+ Scenario: Running an application process
+ Given a new ruby project and a bootstrapped server
+ And the project has an application process defined in a Procfile
+ When I run "cap deploy:setup deploy"
+ Then the project should own the running application process
+
+ Scenario: Running processes can read environment variables
+ Given a new ruby project and a bootstrapped server
+ And the project has an application process defined in a Procfile
+ And the variable "MONSTER" is set to "tricorn"
+ When I run "cap deploy:setup deploy"
+ And I wait for the server to start
+ Then the variable "MONSTER" should be set to "tricorn"
+ Then the running application process should know that "MONSTER" is set to "tricorn"
View
27 features/steps/capistrano_steps.rb
@@ -6,6 +6,10 @@
After do
`bundle exec vagrant sandbox rollback` unless ENV['SKIP_ROLLBACK']
+ if project
+ project.run_on_server "sudo stop #{project.name} || true"
+ project.run_on_server "sudo rm -rf /etc/init/#{project.name}* || true"
+ end
end
Given /^a new (ruby )?project and a bootstrapped server$/ do |project_type|
@@ -25,7 +29,14 @@
end
Given /^the variable "([^"]*)" is set to "([^"]*)"$/ do |name, value|
- project.run_cap 'env:set #{name}=#{value}'
+ project.run_cap "env:set #{name}=#{value}"
+end
+
+Given /^the project has an application process defined in a Procfile$/ do
+ @application_process = 'an-application-process'
+ project.add_foreman_to_bundle
+ project.add_gem_to_bundle @application_process, '1.0.0'
+ project.add_command_to_procfile 'process', "bin/#{@application_process} --server"
end
When /^I update the bundle to require version "([^"]*)" of "([^"]*)"$/ do |version, gem|
@@ -45,6 +56,10 @@
project.run_cap 'deploy'
end
+When /^I wait for the server to start$/ do
+ sleep(5)
+end
+
Then /^the project should be deployed$/ do
project.deployed_version.should eql(project.latest_version)
end
@@ -66,5 +81,13 @@
end
Then /^the variable "([^"]*)" should have no value$/ do |name|
- project.run_on_server("sudo su - #{project.name} -c 'env'").include?("SECRET=").should be_false
+ project.run_on_server("sudo su - #{project.name} -c 'env'").include?("#{name}=").should be_false
+end
+
+Then /^the project should own the running application process$/ do
+ project.run_on_server("ps -U #{project.name} u").include?(@application_process).should be_true
+end
+
+Then /^the running application process should know that "([^"]*)" is set to "([^"]*)"$/ do |name, value|
+ project.run_on_server("/usr/bin/curl localhost:3500/env | grep #{name}").strip.should eql("#{name}=#{value}")
end
View
26 features/support/project.rb
@@ -47,6 +47,7 @@ def initialize(project, options = {})
end
class Gemfile < Template
+ attr_accessor :foreman
attr_accessor :gems
def initialize(gems = {})
@@ -55,6 +56,16 @@ def initialize(gems = {})
end
end
+ class Procfile < Template
+ attr_reader :name, :command
+
+ def initialize(name, command)
+ super('project/Procfile.erb')
+ @name = name
+ @command = command
+ end
+ end
+
class BundledGem
def initialize(gem, version)
@gem = gem
@@ -171,13 +182,26 @@ def gemfile
def add_gem_to_bundle(gem, version)
gemfile.gems[gem] = version
- write_and_commit_file 'Gemfile', gemfile
BundledGem.new(gem, version).generate
+ regenerate_bundle
+ end
+
+ def add_foreman_to_bundle
+ gemfile.foreman = true
+ regenerate_bundle
+ end
+
+ def regenerate_bundle
+ write_and_commit_file 'Gemfile', gemfile
# Nasty hack to generate a Gemfile.lock
@server.run "cd /recap/share/projects/#{name} && bundle install"
commit_files 'Gemfile.lock'
end
+ def add_command_to_procfile(name, command)
+ write_and_commit_file 'Procfile', Procfile.new(name, command)
+ end
+
def commit_changes
write_and_commit_file 'project-file', Faker::Lorem.sentence
end
View
7 features/templates/gem/binary.erb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
require 'webrick'
-VERSION = "<%= version %>"
+GEM_VERSION = "<%= version %>"
class Server
class Action < WEBrick::HTTPServlet::AbstractServlet
@@ -14,7 +14,7 @@ class Server
class Version < Action
def body
- VERSION
+ GEM_VERSION
end
end
@@ -33,9 +33,10 @@ class Server
end
trap("SIGINT") { exit! }
+trap("TERM") { exit! }
case ARGV[0]
-when "--version" then puts VERSION
+when "--version" then puts GEM_VERSION
when "--env" then ENV.keys.sort.each {|k| puts "#{k}=#{ENV[k]}"}
when "--server" then Server.start
else puts "Unknown option"
View
4 features/templates/project/Gemfile.erb
@@ -1,3 +1,7 @@
+source :rubygems
+<% if foreman %>
+gem 'foreman'
+<% end %>
<% gems.each do |gem, version| %>
gem '<%= gem %>', :git => '/recap/share/gems/<%= gem %>', :tag => '<%= version %>'
<% end %>
View
1  features/templates/project/Procfile.erb
@@ -0,0 +1 @@
+<%= name %>: <%= command %>
View
3  lib/recap/env.rb
@@ -33,8 +33,11 @@ def current_environment
task :set do
env = ARGV[1..-1].inject(current_environment) do |env, string|
env.set_string(string)
+ logger.debug "Setting #{string}"
+ logger.debug "Env is now: #{env}"
env
end
+
if env.empty?
as_app "rm -f #{environment_file}", "~"
else
View
8 lib/recap/foreman.rb
@@ -28,7 +28,7 @@ module Recap::Foreman
# To export the scripts, they are first generated in a temporary location, then copied to their final
# destination. This is done because the foreman export command needs to be run as the application user,
# while sudo is required to write to `/etc/init`.
- task :default, :roles => :app do
+ task :default do
if deployed_file_exists?(procfile)
as_app foreman_export_command
sudo "rm -f #{foreman_export_location}/#{application}*"
@@ -38,21 +38,21 @@ module Recap::Foreman
end
# Starts all processes that form the application
- task :start, :roles => :app do
+ task :start do
if deployed_file_exists?(procfile)
sudo "start #{application}"
end
end
# Restarts all processes that form the application
- task :restart, :roles => :app do
+ task :restart do
if deployed_file_exists?(procfile)
sudo "restart #{application} || sudo start #{application}"
end
end
# Stops all processes that form the application
- task :stop, :roles => :app do
+ task :stop do
if deployed_file_exists?(procfile)
sudo "stop #{application}"
end
View
3  lib/recap/ruby.rb
@@ -1,2 +1,3 @@
require 'recap/deploy'
-require 'recap/bundler'
+require 'recap/bundler'
+require 'recap/foreman'
View
9 test-vm/manifests/base.pp
@@ -2,11 +2,16 @@
ensure => 'present'
}
-package { 'git-core':
+package { ['git-core', 'curl']:
ensure => present
}
package { 'bundler':
provider => gem,
ensure => '1.1.rc.7'
-}
+}
+
+package { 'foreman':
+ provider => gem,
+ ensure => present
+}
Please sign in to comment.
Something went wrong with that request. Please try again.