cap-recipes 0.3.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +20 -0
  3. data/README.textile +357 -0
  4. data/Rakefile +45 -0
  5. data/VERSION.yml +4 -0
  6. data/cap-recipes.gemspec +87 -0
  7. data/examples/advanced/deploy.rb +39 -0
  8. data/examples/advanced/deploy/experimental.rb +14 -0
  9. data/examples/advanced/deploy/production.rb +20 -0
  10. data/examples/simple/deploy.rb +36 -0
  11. data/lib/cap_recipes.rb +1 -0
  12. data/lib/cap_recipes/tasks/apache.rb +1 -0
  13. data/lib/cap_recipes/tasks/apache/install.rb +12 -0
  14. data/lib/cap_recipes/tasks/apache/manage.rb +26 -0
  15. data/lib/cap_recipes/tasks/aptitude.rb +1 -0
  16. data/lib/cap_recipes/tasks/aptitude/manage.rb +34 -0
  17. data/lib/cap_recipes/tasks/backgroundrb.rb +1 -0
  18. data/lib/cap_recipes/tasks/backgroundrb/hooks.rb +5 -0
  19. data/lib/cap_recipes/tasks/backgroundrb/manage.rb +64 -0
  20. data/lib/cap_recipes/tasks/delayed_job.rb +1 -0
  21. data/lib/cap_recipes/tasks/delayed_job/hooks.rb +5 -0
  22. data/lib/cap_recipes/tasks/delayed_job/manage.rb +32 -0
  23. data/lib/cap_recipes/tasks/juggernaut.rb +1 -0
  24. data/lib/cap_recipes/tasks/juggernaut/hooks.rb +4 -0
  25. data/lib/cap_recipes/tasks/juggernaut/manage.rb +54 -0
  26. data/lib/cap_recipes/tasks/memcache.rb +1 -0
  27. data/lib/cap_recipes/tasks/memcache/hooks.rb +5 -0
  28. data/lib/cap_recipes/tasks/memcache/install.rb +13 -0
  29. data/lib/cap_recipes/tasks/memcache/manage.rb +35 -0
  30. data/lib/cap_recipes/tasks/passenger.rb +1 -0
  31. data/lib/cap_recipes/tasks/passenger/install.rb +62 -0
  32. data/lib/cap_recipes/tasks/passenger/manage.rb +24 -0
  33. data/lib/cap_recipes/tasks/rails.rb +1 -0
  34. data/lib/cap_recipes/tasks/rails/hooks.rb +8 -0
  35. data/lib/cap_recipes/tasks/rails/manage.rb +53 -0
  36. data/lib/cap_recipes/tasks/rubygems.rb +1 -0
  37. data/lib/cap_recipes/tasks/rubygems/manage.rb +43 -0
  38. data/lib/cap_recipes/tasks/utilities.rb +114 -0
  39. data/lib/cap_recipes/tasks/whenever.rb +1 -0
  40. data/lib/cap_recipes/tasks/whenever/hooks.rb +5 -0
  41. data/lib/cap_recipes/tasks/whenever/manage.rb +10 -0
  42. data/spec/cap/all/Capfile +2 -0
  43. data/spec/cap/helper.rb +2 -0
  44. data/spec/cap_recipes_spec.rb +13 -0
  45. data/spec/spec_helper.rb +16 -0
  46. metadata +106 -0
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'passenger/*.rb')].sort.each { |lib| require lib }
@@ -0,0 +1,62 @@
1
+ require 'cap_recipes/tasks/utilities.rb'
2
+
3
+ #TODO add something like require that a task named apache:restart exists
4
+ Capistrano::Configuration.instance(true).load do
5
+ set :base_ruby_path, '/usr'
6
+ set :confd_passenger_filename, 'passenger'
7
+
8
+ namespace :passenger do
9
+ desc "Installs Phusion Passenger"
10
+ task :install, :roles => :web do
11
+ puts 'Installing passenger module'
12
+ enable_apache_module
13
+ update_config
14
+ end
15
+
16
+ desc "Setup Passenger Module"
17
+ task :enable_apache_module, :roles => :web do
18
+ sudo "#{base_ruby_path}/bin/gem install passenger --no-ri --no-rdoc"
19
+ sudo "#{base_ruby_path}/bin/passenger-install-apache2-module --auto"
20
+ end
21
+
22
+ desc "Configure Passenger"
23
+ task :update_config, :roles => :web do
24
+ version = 'ERROR' # default
25
+
26
+ # passenger (2.X.X, 1.X.X)
27
+ run("gem list | grep passenger") do |ch, stream, data|
28
+ version = data.sub(/passenger \(([^,]+).*?\)/,"\\1").strip
29
+ end
30
+
31
+ puts " passenger version #{version} configured"
32
+
33
+ passenger_config =<<-EOF
34
+ LoadModule passenger_module #{base_ruby_path}/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so
35
+ PassengerRoot #{base_ruby_path}/lib/ruby/gems/1.8/gems/passenger-#{version}
36
+ PassengerRuby #{base_ruby_path}/bin/ruby
37
+ EOF
38
+
39
+ put passenger_config, "/tmp/passenger"
40
+ sudo "mv /tmp/passenger /etc/apache2/conf.d/#{confd_passenger_filename}"
41
+ apache.restart
42
+ end
43
+
44
+ # if you want to add more options, try this, in your own conifg
45
+ # read this first... http://www.modrails.com/documentation/Users%20guide.html
46
+ # namespace :passenger do
47
+ # task :add_custom_configuration, :roles=>:web do
48
+ # #512 - 100(stack) - 4*100(instance)
49
+ # passenger_config = <<EOF
50
+ # PassengerMaxPoolSize 4
51
+ # PassengerPoolIdleTime 3000
52
+ # EOF
53
+ # put passenger_config, "/tmp/passenger"
54
+ # sudo "cat /tmp/passenger >> /etc/apache2/conf.d/passenger"
55
+ # apache.restart
56
+ # end
57
+ # end
58
+ # after "passenger:update_config", *%w(
59
+ # passenger:add_custom_configuration
60
+ # )
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ require 'cap_recipes/tasks/utilities.rb'
2
+
3
+ Capistrano::Configuration.instance(true).load do
4
+ namespace :deploy do
5
+
6
+ desc "Stops the phusion passenger server"
7
+ task :stop, :roles => :web do
8
+ puts "Stopping rails web server"
9
+ apache.stop
10
+ end
11
+
12
+ desc "Starts the phusion passenger server"
13
+ task :start, :roles => :web do
14
+ puts "Starting rails web server"
15
+ apache.start
16
+ end
17
+
18
+ desc "Restarts the phusion passenger server"
19
+ task :restart, :roles => :web do
20
+ run "touch #{current_path}/tmp/restart.txt"
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'rails/*.rb')].sort.each { |lib| require lib }
@@ -0,0 +1,8 @@
1
+ require 'cap_recipes/tasks/utilities.rb'
2
+
3
+ Capistrano::Configuration.instance(true).load do
4
+ after "deploy:update_code", "rails:symlink_db_config" # copy database.yml file to release path
5
+ after "deploy:update_code", "rails:sweep:cache" # clear cache after updating code
6
+ after "deploy:restart" , "rails:repair_permissions" # fix the permissions to work properly
7
+ after "deploy:restart" , "rails:ping" # ping passenger to start the rails instance
8
+ end
@@ -0,0 +1,53 @@
1
+ require 'cap_recipes/tasks/utilities.rb'
2
+
3
+ Capistrano::Configuration.instance(true).load do
4
+ set :local_ping_path, 'http://localhost'
5
+
6
+ namespace :rails do
7
+ # ===============================================================
8
+ # UTILITY TASKS
9
+ # ===============================================================
10
+ desc "Symlinks the shared/config/database yaml to release/config/"
11
+ task :symlink_db_config, :roles => :app do
12
+ puts "Copying database configuration to release path"
13
+ try_sudo "rm #{release_path}/config/database.yml -f"
14
+ try_sudo "ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml"
15
+ end
16
+
17
+ desc "Repair permissions to allow user to perform all actions"
18
+ task :repair_permissions, :roles => :app do
19
+ puts "Applying correct permissions to allow for proper command execution"
20
+ try_sudo "chmod -R 744 #{current_path}/log #{current_path}/tmp"
21
+ try_sudo "chown -R #{user}:#{user} #{current_path}"
22
+ try_sudo "chown -R #{user}:#{user} #{current_path}/tmp"
23
+ end
24
+
25
+ desc "Displays the production log from the server locally"
26
+ task :tail, :roles => :app do
27
+ stream "tail -f #{shared_path}/log/production.log"
28
+ end
29
+
30
+ desc "Pings localhost to startup server"
31
+ task :ping, :roles => :app do
32
+ puts "Pinging the web server to start it"
33
+ run "wget -O /dev/null #{local_ping_path} 2>/dev/null"
34
+ end
35
+
36
+ # ===============================================================
37
+ # MAINTENANCE TASKS
38
+ # ===============================================================
39
+ namespace :sweep do
40
+ desc "Clear file-based fragment and action caching"
41
+ task :log, :roles => :app do
42
+ puts "Sweeping all the log files"
43
+ run "cd #{current_path} && #{sudo} rake log:clear RAILS_ENV=production"
44
+ end
45
+
46
+ desc "Clear file-based fragment and action caching"
47
+ task :cache, :roles => :app do
48
+ puts "Sweeping the fragment and action cache stores"
49
+ run "cd #{release_path} && #{sudo} rake tmp:cache:clear RAILS_ENV=production"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'rubygems/*.rb')].sort.each { |lib| require lib }
@@ -0,0 +1,43 @@
1
+ Capistrano::Configuration.instance(true).load do
2
+ set :rubygem_paths, '/usr/bin/gem'
3
+
4
+ namespace :rubygems do
5
+ desc "Performs a rubygems upgrade, updates all gems and cleans up old ones"
6
+ task :full_update, :roles => :app do
7
+ rubygems.upgrade
8
+ rubygems.update
9
+ rubygems.cleanup
10
+ end
11
+
12
+ desc "Upgrades the rubygem package installation"
13
+ task :upgrade, :roles => :app do
14
+ Array(rubygem_paths).each { |path| sudo "#{path} update --system" }
15
+ end
16
+
17
+ desc "Updates all installed gems"
18
+ task :update, :roles => :app do
19
+ Array(rubygem_paths).each { |path| sudo "#{path} update" }
20
+ end
21
+
22
+ desc "Removes old gems which are now outdated"
23
+ task :cleanup, :roles => :app do
24
+ Array(rubygem_paths).each { |path| sudo "#{path} cleanup" }
25
+ end
26
+
27
+ desc "Install a gem on your servers servers"
28
+ task :install, :roles => :app do
29
+ puts "Enter the name of the gem you'd like to install:"
30
+ gem_name = $stdin.gets.chomp
31
+ logger.info "trying to install '#{gem_name}'"
32
+ Array(rubygem_paths).each {|path| sudo "#{path} install #{gem_name} --no-ri --no-rdoc" }
33
+ end
34
+
35
+ desc "Uninstall a gem from the release servers"
36
+ task :uninstall, :roles => :app do
37
+ puts "Enter the name of the gem you'd like to remove:"
38
+ gem_name = $stdin.gets.chomp
39
+ logger.info "trying to remove '#{gem_name}'"
40
+ Array(rubygem_paths).each { |path| sudo "#{path} uninstall #{gem_name} -x" }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,114 @@
1
+ require 'fileutils'
2
+
3
+ module Utilities
4
+ # utilities.config_gsub('/etc/example', /(.*)/im, "\\1")
5
+ def config_gsub(file, find, replace)
6
+ tmp="/tmp/#{File.basename(file)}"
7
+ get file, tmp
8
+ content=File.open(tmp).read
9
+ content.gsub!(find,replace)
10
+ put content, tmp
11
+ sudo "mv #{tmp} #{file}"
12
+ end
13
+
14
+ def sudo_upload(from, to, options={}, &block)
15
+ top.upload from, "/tmp/#{File.basename(to)}", options, &block
16
+ sudo "mv /tmp/#{File.basename(to)} #{to}"
17
+ sudo "chmod #{options[:mode]} #{to}" if options[:mode]
18
+ sudo "chown #{options[:owner]} #{to}" if options[:owner]
19
+ end
20
+
21
+ def adduser(user, options={})
22
+ options[:shell] ||= '/bin/bash' # new accounts on ubuntu 6.06.1 have been getting /bin/sh
23
+ switches = '--disabled-password --gecos ""'
24
+ switches += " --shell=#{options[:shell]} " if options[:shell]
25
+ switches += ' --no-create-home ' if options[:nohome]
26
+ switches += " --ingroup #{options[:group]} " unless options[:group].nil?
27
+ invoke_command "grep '^#{user}:' /etc/passwd || sudo /usr/sbin/adduser #{user} #{switches}",
28
+ :via => run_method
29
+ end
30
+
31
+ # role = :app
32
+ def with_role(role, &block)
33
+ original, ENV['HOSTS'] = ENV['HOSTS'], find_servers(:roles => role).map{|d| d.host}.join(",")
34
+ begin
35
+ yield
36
+ ensure
37
+ ENV['HOSTS'] = original
38
+ end
39
+ end
40
+
41
+ # options = { :user => 'xxxxx', :password => 'xxxxx' }
42
+ def with_credentials(options={}, &block)
43
+ original_username, original_password = user, password
44
+ begin
45
+ set :user, options[:user] || original_username
46
+ set :password, options[:password] || original_password
47
+ yield
48
+ ensure
49
+ set :user, original_username
50
+ set :password, original_password
51
+ end
52
+ end
53
+
54
+ ##
55
+ # Run a command and ask for input when input_query is seen.
56
+ # Sends the response back to the server.
57
+ #
58
+ # +input_query+ is a regular expression that defaults to /^Password/.
59
+ #
60
+ # Can be used where +run+ would otherwise be used.
61
+ #
62
+ # run_with_input 'ssh-keygen ...', /^Are you sure you want to overwrite\?/
63
+
64
+ def run_with_input(shell_command, input_query=/^Password/, response=nil)
65
+ handle_command_with_input(:run, shell_command, input_query, response)
66
+ end
67
+
68
+ ##
69
+ # Run a command using sudo and ask for input when a regular expression is seen.
70
+ # Sends the response back to the server.
71
+ #
72
+ # See also +run_with_input+
73
+ #
74
+ # +input_query+ is a regular expression
75
+
76
+ def sudo_with_input(shell_command, input_query=/^Password/, response=nil)
77
+ handle_command_with_input(:sudo, shell_command, input_query, response)
78
+ end
79
+
80
+ def invoke_with_input(shell_command, input_query=/^Password/, response=nil)
81
+ handle_command_with_input(run_method, shell_command, input_query, response)
82
+ end
83
+
84
+ private
85
+
86
+ ##
87
+ # Does the actual capturing of the input and streaming of the output.
88
+ #
89
+ # local_run_method: run or sudo
90
+ # shell_command: The command to run
91
+ # input_query: A regular expression matching a request for input: /^Please enter your password/
92
+
93
+ def handle_command_with_input(local_run_method, shell_command, input_query, response=nil)
94
+ send(local_run_method, shell_command, {:pty => true}) do |channel, stream, data|
95
+
96
+ if data =~ input_query
97
+ if response
98
+ logger.info "#{data} #{"*"*(rand(10)+5)}", channel[:host]
99
+ channel.send_data "#{response}\n"
100
+ else
101
+ logger.info data, channel[:host]
102
+ response = ::Capistrano::CLI.password_prompt "#{data}"
103
+ channel.send_data "#{response}\n"
104
+ end
105
+ else
106
+ logger.info data, channel[:host]
107
+ end
108
+ end
109
+ end
110
+
111
+
112
+ end
113
+
114
+ Capistrano.plugin :utilities, Utilities
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'whenever/*.rb')].sort.each { |lib| require lib }
@@ -0,0 +1,5 @@
1
+ Capistrano::Configuration.instance(true).load do
2
+
3
+ after "deploy:symlink", "whenever:update_crontab" # update crontab after symlink
4
+
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'cap_recipes/tasks/utilities.rb'
2
+
3
+ Capistrano::Configuration.instance(true).load do
4
+ namespace :whenever do
5
+ desc "Update the crontab file"
6
+ task :update_crontab, :roles => :db do
7
+ run "cd #{release_path} && whenever --update-crontab #{application}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__),'..','helper')
2
+ require 'cap_recipes'
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__),'..','..','lib')
2
+ def current_path;end
@@ -0,0 +1,13 @@
1
+ require File.expand_path("spec_helper", File.dirname(__FILE__))
2
+
3
+ describe 'loading everything' do
4
+ def run_cap(folder,task)
5
+ folder = File.join(File.dirname(__FILE__),'cap',folder)
6
+ `cd #{folder} && #{task}`
7
+ end
8
+
9
+ it "finds all tasks" do
10
+ tasks = run_cap 'all', 'cap -T'
11
+ tasks.split("\n").size.should >= 20
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # ---- requirements
2
+ require 'rubygems'
3
+ require 'spec'
4
+
5
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
6
+
7
+ # ---- bugfix
8
+ #`exit?': undefined method `run?' for Test::Unit:Module (NoMethodError)
9
+ #can be solved with require test/unit but this will result in extra test-output
10
+ module Test
11
+ module Unit
12
+ def self.run?
13
+ true
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cap-recipes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.18
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Esquenazi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Battle-tested capistrano recipes for debian, passenger, apache, delayed_job, juggernaut, rubygems, backgroundrb, rails and more
17
+ email: nesquena@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.textile
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.textile
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - cap-recipes.gemspec
32
+ - examples/advanced/deploy.rb
33
+ - examples/advanced/deploy/experimental.rb
34
+ - examples/advanced/deploy/production.rb
35
+ - examples/simple/deploy.rb
36
+ - lib/cap_recipes.rb
37
+ - lib/cap_recipes/tasks/apache.rb
38
+ - lib/cap_recipes/tasks/apache/install.rb
39
+ - lib/cap_recipes/tasks/apache/manage.rb
40
+ - lib/cap_recipes/tasks/aptitude.rb
41
+ - lib/cap_recipes/tasks/aptitude/manage.rb
42
+ - lib/cap_recipes/tasks/backgroundrb.rb
43
+ - lib/cap_recipes/tasks/backgroundrb/hooks.rb
44
+ - lib/cap_recipes/tasks/backgroundrb/manage.rb
45
+ - lib/cap_recipes/tasks/delayed_job.rb
46
+ - lib/cap_recipes/tasks/delayed_job/hooks.rb
47
+ - lib/cap_recipes/tasks/delayed_job/manage.rb
48
+ - lib/cap_recipes/tasks/juggernaut.rb
49
+ - lib/cap_recipes/tasks/juggernaut/hooks.rb
50
+ - lib/cap_recipes/tasks/juggernaut/manage.rb
51
+ - lib/cap_recipes/tasks/memcache.rb
52
+ - lib/cap_recipes/tasks/memcache/hooks.rb
53
+ - lib/cap_recipes/tasks/memcache/install.rb
54
+ - lib/cap_recipes/tasks/memcache/manage.rb
55
+ - lib/cap_recipes/tasks/passenger.rb
56
+ - lib/cap_recipes/tasks/passenger/install.rb
57
+ - lib/cap_recipes/tasks/passenger/manage.rb
58
+ - lib/cap_recipes/tasks/rails.rb
59
+ - lib/cap_recipes/tasks/rails/hooks.rb
60
+ - lib/cap_recipes/tasks/rails/manage.rb
61
+ - lib/cap_recipes/tasks/rubygems.rb
62
+ - lib/cap_recipes/tasks/rubygems/manage.rb
63
+ - lib/cap_recipes/tasks/utilities.rb
64
+ - lib/cap_recipes/tasks/whenever.rb
65
+ - lib/cap_recipes/tasks/whenever/hooks.rb
66
+ - lib/cap_recipes/tasks/whenever/manage.rb
67
+ - spec/cap/all/Capfile
68
+ - spec/cap/helper.rb
69
+ - spec/cap_recipes_spec.rb
70
+ - spec/spec_helper.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/nesquena/cap-recipes
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.4
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Battle-tested capistrano recipes for passenger, delayed_job, and more
99
+ test_files:
100
+ - spec/cap/helper.rb
101
+ - spec/cap_recipes_spec.rb
102
+ - spec/spec_helper.rb
103
+ - examples/advanced/deploy/experimental.rb
104
+ - examples/advanced/deploy/production.rb
105
+ - examples/advanced/deploy.rb
106
+ - examples/simple/deploy.rb