capistrano-fanfare 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ .rbx/
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby
5
+ - 1.9.2
6
+ - ruby-head
7
+ - 1.8.7
8
+ - rbx-18mode
9
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-fanfare.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake', '~> 0.9'
8
+ gem 'foreman'
9
+ end
10
+
11
+ platforms :jruby do
12
+ gem 'jruby-openssl'
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Fletcher Nichol
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Capistrano::Fanfare [![Build Status](https://secure.travis-ci.org/fnichol/capistrano-fanfare.png)](http://travis-ci.org/fnichol/capistrano-fanfare)
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'capistrano-fanfare'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install capistrano-fanfare
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs.push "lib"
8
+ t.test_files = FileList['spec/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => 'test'
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/capistrano/fanfare/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Fletcher Nichol"]
6
+ gem.email = ["fnichol@nichol.ca"]
7
+ gem.description = %q{Capistrano recipes (with full test suite) for fanfare application deployment framework}
8
+ gem.summary = %q{Capistrano recipes (with full test suite) for fanfare application deployment framework}
9
+ gem.homepage = "https://github.com/fnichol/capistrano-fanfare"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "capistrano-fanfare"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Capistrano::Fanfare::VERSION
17
+
18
+ gem.add_dependency "capistrano", "2.10.0.pre"
19
+ gem.add_dependency "capistrano_colors", "~> 0.5"
20
+
21
+ gem.add_development_dependency "minitest", "~> 2.10.0"
22
+ gem.add_development_dependency "minitest-capistrano", "~> 0.0"
23
+ gem.add_development_dependency "timecop", "~> 0.3"
24
+ end
@@ -0,0 +1,84 @@
1
+ require 'capistrano'
2
+
3
+ module Capistrano::Fanfare::Bundler
4
+ def self.load_into(configuration)
5
+ configuration.load do
6
+ set :bundle_cmd, "bundle"
7
+ set :bundle_shebang, "ruby-local-exec"
8
+
9
+ set(:default_environment) {
10
+ { 'PATH' => "#{current_path}/bin:$PATH" }
11
+ }
12
+
13
+ set(:bundle_flags) do
14
+ flags = "--deployment"
15
+ flags << " --quiet" unless ENV['VERBOSE']
16
+ flags << " --binstubs"
17
+ flags << " --shebang #{bundle_shebang}"
18
+ flags
19
+ end
20
+
21
+ set(:bundle_without) do
22
+ without = [:development, :test]
23
+ if exists?(:os_type) && exists?(:os_types)
24
+ without += (fetch(:os_types) - Array(fetch(:os_type)))
25
+ end
26
+ without
27
+ end
28
+
29
+ set :bundle_binstub_template do
30
+ <<-BINSTUB.gsub(/^ {10}/, '')
31
+ #!/usr/bin/env #{bundle_shebang}
32
+ #
33
+ # This file was generated by capistrano.
34
+ #
35
+
36
+ require 'pathname'
37
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
38
+ Pathname.new(__FILE__).realpath)
39
+
40
+ require 'rubygems'
41
+
42
+ load Gem.bin_path('bundler', 'bundle')
43
+ BINSTUB
44
+ end
45
+
46
+ require 'bundler/capistrano'
47
+
48
+ set(:rake) { "rake" }
49
+
50
+ # =========================================================================
51
+ # These are the tasks that are available to help with deploying web apps.
52
+ # You can have cap give you a summary of them with `cap -T'.
53
+ # =========================================================================
54
+
55
+ namespace :bundle do
56
+ desc <<-DESC
57
+ [internal] Creates a binstub script for the bundle command.
58
+ DESC
59
+ task :create_binstub_script, :roles => :app, :except => { :no_release => true } do
60
+ run "mkdir -p #{shared_path}/bin"
61
+ put bundle_binstub_template, "#{shared_path}/bin/bundle", { :mode => "0755" }
62
+ end
63
+
64
+ desc <<-DESC
65
+ [internal] Copies bin/bundle from shared_path into current_path.
66
+ DESC
67
+ task :cp_bundle_binstub, :roles => :app, :except => { :no_release => true } do
68
+ run [
69
+ "mkdir -p #{current_path}/bin",
70
+ "cp #{shared_path}/bin/bundle #{current_path}/bin/bundle",
71
+ "chmod 0755 #{current_path}/bin/bundle"
72
+ ].join(" && ")
73
+ end
74
+ end
75
+
76
+ after "deploy:setup", "bundle:create_binstub_script"
77
+ before "deploy:finalize_update", "bundle:cp_bundle_binstub"
78
+ end
79
+ end
80
+ end
81
+
82
+ if Capistrano::Configuration.instance
83
+ Capistrano::Fanfare::Bundler.load_into(Capistrano::Configuration.instance)
84
+ end
@@ -0,0 +1,66 @@
1
+ require 'capistrano'
2
+
3
+ module Capistrano::Fanfare::Defaults
4
+ def self.load_into(configuration)
5
+ configuration.load do
6
+ set :scm, :git
7
+ set :use_sudo, false
8
+ set :user, "deploy"
9
+ set :rake, "bundle exec rake"
10
+ set(:branch) { ENV['BRANCH'] ? ENV['BRANCH'] : "master" }
11
+ set(:deploy_to) { "/srv/#{application}_#{deploy_env}" }
12
+ set :ssh_options, { :forward_agent => true }
13
+ set :os_types, [:darwin, :linux, :sunos]
14
+ set(:os_type) { capture("uname -s").chomp.downcase.to_sym }
15
+
16
+ set :shared_children, %w{public/system log tmp/pids tmp/sockets tmp/sessions}
17
+
18
+ default_run_options[:pty] = true
19
+
20
+ ##
21
+ # Determines deployment environment or run mode to help database naming,
22
+ # deploy directories, etc.
23
+ #
24
+ # Thanks to capistrano-recipies for the great idea:
25
+ # https://github.com/webficient/capistrano-recipes
26
+ set(:deploy_env) {
27
+ if exists?(:stage)
28
+ stage
29
+ elsif exists?(:rails_env)
30
+ rails_env
31
+ elsif ENV['RAILS_ENV']
32
+ ENV['RAILS_ENV']
33
+ elsif exists?(:rack_env)
34
+ rack_env
35
+ elsif ENV['RACK_ENV']
36
+ ENV['RACK_ENV']
37
+ else
38
+ "production"
39
+ end
40
+ }
41
+
42
+ namespace :deploy do
43
+ desc <<-DESC
44
+ Deploys and starts a `cold' application. This is useful if you have not \
45
+ deployed your application before, or if your application is (for some \
46
+ other reason) not currently running. It will deploy the code, run any \
47
+ pending migrations, and then instead of invoking `deploy:restart', it \
48
+ will invoke `deploy:start' to fire up the application servers.
49
+
50
+ [NOTE] This overides the capistrano default by calling the "db:seed" \
51
+ task, if it is defined.
52
+ DESC
53
+ task :cold do
54
+ update
55
+ migrate
56
+ db.seed if respond_to?(:db) && db.respond_to?(:seed)
57
+ start
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ if Capistrano::Configuration.instance
65
+ Capistrano::Fanfare::Defaults.load_into(Capistrano::Configuration.instance)
66
+ end
@@ -0,0 +1,54 @@
1
+ module Capistrano
2
+ module Fanfare
3
+ module Foreman
4
+ module Strategy
5
+
6
+ ##
7
+ # This class defines the abstract interface for all Capistrano
8
+ # foreman deployment strategies. Subclasses must implement at least the
9
+ # #export, #start, and #stop methods.
10
+
11
+ class Base
12
+ attr_reader :configuration
13
+
14
+ def initialize(config = {})
15
+ @configuration = config
16
+ end
17
+
18
+ def export
19
+ raise NotImplementedError, "`export' is not implemented by #{self.class.name}"
20
+ end
21
+
22
+ def start(proc_group = nil)
23
+ raise NotImplementedError, "`start' is not implemented by #{self.class.name}"
24
+ end
25
+
26
+ def stop(proc_group = nil)
27
+ raise NotImplementedError, "`stop' is not implemented by #{self.class.name}"
28
+ end
29
+
30
+ def register
31
+ # no-op
32
+ end
33
+
34
+ def restart(proc_group = nil)
35
+ stop(proc_group)
36
+ start(proc_group)
37
+ end
38
+
39
+ protected
40
+
41
+ # This is to allow helper methods like "run" and "put" to be more
42
+ # easily accessible to strategy implementations.
43
+ def method_missing(sym, *args, &block)
44
+ if configuration.respond_to?(sym)
45
+ configuration.send(sym, *args, &block)
46
+ else
47
+ super
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,63 @@
1
+ require 'capistrano/fanfare/foreman/strategy/base'
2
+
3
+ module Capistrano
4
+ module Fanfare
5
+ module Foreman
6
+ module Strategy
7
+ class Runit < Base
8
+ def export
9
+ run [
10
+ "cd #{configuration[:current_release]} &&",
11
+ "if [ -f Procfile ] ; then",
12
+ "mkdir -p #{configuration[:runit_sv_path]} &&",
13
+ "#{configuration[:foreman_cmd]} export runit",
14
+ "#{configuration[:runit_sv_path]}",
15
+ "--app=#{configuration[:runit_app_name]}",
16
+ "--log=#{configuration[:shared_path]}/log",
17
+ "--user=#{configuration[:user]} &&",
18
+ "find #{configuration[:runit_sv_path]} -type f -name run",
19
+ "-exec chmod 755 {} \\; ; else",
20
+ "echo '>>>> A Procfile must exist in this project.' && exit 10 ;",
21
+ "fi"
22
+ ].join(' ')
23
+ end
24
+
25
+ def register
26
+ run [
27
+ "ln -snf #{configuration[:runit_sv_path]}/*",
28
+ "#{configuration[:runit_service_path]}/"
29
+ ].join(' ')
30
+ end
31
+
32
+ def start(proc_group = nil)
33
+ run [
34
+ "sv start #{configuration[:runit_service_path]}/",
35
+ "#{configuration[:runit_app_name]}-*"
36
+ ].join
37
+ end
38
+
39
+ def stop(proc_group = nil)
40
+ run [
41
+ "sv stop #{configuration[:runit_service_path]}/",
42
+ "#{configuration[:runit_app_name]}-*"
43
+ ].join
44
+ end
45
+
46
+ def restart(proc_group = nil)
47
+ run [
48
+ "sv restart #{configuration[:runit_service_path]}/",
49
+ "#{configuration[:runit_app_name]}-*"
50
+ ].join
51
+ end
52
+
53
+ def ps
54
+ run [
55
+ "sv status #{configuration[:runit_service_path]}/",
56
+ "#{configuration[:runit_app_name]}-*"
57
+ ].join
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,21 @@
1
+ module Capistrano
2
+ module Fanfare
3
+ module Foreman
4
+ module Strategy
5
+ def self.new(strategy, config={})
6
+ strategy_file = "capistrano/fanfare/foreman/strategy/#{strategy}"
7
+ require(strategy_file)
8
+
9
+ strategy_const = strategy.to_s.capitalize.gsub(/_(.)/) { $1.upcase }
10
+ if const_defined?(strategy_const)
11
+ const_get(strategy_const).new(config)
12
+ else
13
+ raise Capistrano::Error, "could not find `#{name}::#{strategy_const}' in `#{strategy_file}'"
14
+ end
15
+ rescue LoadError
16
+ raise Capistrano::Error, "could not find any strategy named `#{strategy}'"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,155 @@
1
+ require 'capistrano'
2
+ require 'capistrano/fanfare/foreman/strategy'
3
+
4
+ begin
5
+ require 'foreman/procfile'
6
+
7
+ rescue LoadError => error
8
+ raise "Foreman gem could not be loaded: (#{error.message}). " +
9
+ "Please ensure it is in your Gemfile."
10
+ end
11
+
12
+ module Capistrano::Fanfare::Foreman
13
+ def self.load_into(configuration)
14
+ configuration.load do
15
+ set(:local_procfile) { ENV['PROCFILE'] || "Procfile" }
16
+ set(:user_home) { capture("echo $HOME").chomp }
17
+
18
+ set :foreman_cmd, "foreman"
19
+ set :foreman_export_via, :runit
20
+
21
+ set(:foreman_strategy) do
22
+ Capistrano::Fanfare::Foreman::Strategy.new(foreman_export_via, self)
23
+ end
24
+
25
+ set(:runit_app_name) { "#{application}_#{deploy_env}" }
26
+ set(:runit_sv_path) { "#{shared_path}/sv" }
27
+ set(:runit_service_path) { "#{user_home}/service" }
28
+
29
+ # =========================================================================
30
+ # These are the tasks that are available to help with deploying web apps.
31
+ # You can have cap give you a summary of them with `cap -T'.
32
+ # =========================================================================
33
+
34
+ namespace :foreman do
35
+ desc <<-DESC
36
+ [internal] Copies env from shared path into current_path as .env
37
+ DESC
38
+ task :cp_env, :roles => :app, :except => { :no_release => true } do
39
+ run "cp #{shared_path}/env #{current_release}/.env"
40
+ end
41
+
42
+ desc <<-DESC
43
+ [internal]
44
+ DESC
45
+ task :run_cmd, :roles => :app, :except => { :no_release => true } do
46
+ command = ENV['COMMAND'] || ""
47
+ raise "Please specify a command to execute via the COMMAND " +
48
+ "environment variable" if command.empty?
49
+
50
+ run "cd #{current_path} && #{foreman_cmd} run '#{command}'"
51
+ end
52
+
53
+ desc <<-DESC
54
+ blah
55
+ DESC
56
+ task :export, :roles => :app, :except => { :no_release => true } do
57
+ foreman_strategy.export
58
+ end
59
+
60
+ desc <<-DESC
61
+ [internal]
62
+ DESC
63
+ task :register, :roles => :app, :except => { :no_release => true } do
64
+ foreman_strategy.register
65
+ end
66
+
67
+ namespace :start do
68
+ desc <<-DESC
69
+ flaw
70
+ DESC
71
+ task :default, :roles => :app, :except => { :no_release => true } do
72
+ foreman_strategy.start
73
+ end
74
+ end
75
+
76
+ namespace :stop do
77
+ desc <<-DESC
78
+ flaw
79
+ DESC
80
+ task :default, :roles => :app, :except => { :no_release => true } do
81
+ foreman_strategy.stop
82
+ end
83
+ end
84
+
85
+ namespace :restart do
86
+ desc <<-DESC
87
+ flaw
88
+ DESC
89
+ task :default, :roles => :app, :except => { :no_release => true } do
90
+ foreman_strategy.restart
91
+ end
92
+ end
93
+
94
+ desc <<-DESC
95
+ Procs
96
+ DESC
97
+ task :ps, :roles => :app, :except => { :no_release => true } do
98
+ foreman_strategy.ps
99
+ end
100
+
101
+ if File.exists?(fetch(:local_procfile))
102
+ def procfile
103
+ @procfile ||= Foreman::Procfile.new(fetch(:local_procfile))
104
+ end
105
+
106
+ namespace :start do
107
+ procfile.entries.each do |entry|
108
+ send :desc, <<-DESC
109
+ Starts #{entry.name} processes managed by the Procfile.
110
+ DESC
111
+ send :task, entry.name, :roles => :app, :except => { :no_release => true } do
112
+ foreman_strategy.start(entry.name)
113
+ end
114
+ end
115
+ end
116
+
117
+ namespace :stop do
118
+ procfile.entries.each do |entry|
119
+ send :desc, <<-DESC
120
+ Stops #{entry.name} processes managed by the Procfile.
121
+ DESC
122
+ send :task, entry.name, :roles => :app, :except => { :no_release => true } do
123
+ foreman_strategy.stop(entry.name)
124
+ end
125
+ end
126
+ end
127
+
128
+ namespace :restart do
129
+ procfile.entries.each do |entry|
130
+ send :desc, <<-DESC
131
+ Restarts #{entry.name} processes managed by the Procfile.
132
+ DESC
133
+ send :task, entry.name, :roles => :app, :except => { :no_release => true } do
134
+ foreman_strategy.restart(entry.name)
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ desc <<-DESC
142
+ Runnings
143
+ DESC
144
+ task :frun, :roles => :app, :except => { :no_release => true } do
145
+ foreman.run_cmd
146
+ end
147
+
148
+ after "deploy:update_code", "foreman:cp_env"
149
+ end
150
+ end
151
+ end
152
+
153
+ if Capistrano::Configuration.instance
154
+ Capistrano::Fanfare::Foreman.load_into(Capistrano::Configuration.instance)
155
+ end