Class: Sprinkle::Actors::Capistrano

Inherits:
Actor show all
Defined in:
lib/sprinkle/actors/capistrano.rb

Overview

The Capistrano actor uses Capistrano to define your roles and deliver commands to your remote servers. You’ll need the capistrano gem installed.

The only configuration option is to specify a recipe.

deployment do
  delivery :capistrano do
    recipe 'deploy'
    recipe 'more'
  end
end

Recipes is given a list of files which capistrano will include and load. These recipes are mainly to set variables such as :user, :password, and to set the app domain which will be sprinkled.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Capistrano

:nodoc:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sprinkle/actors/capistrano.rb', line 23

def initialize(&block) #:nodoc:
  @installer = nil
  @config = ::Capistrano::Configuration.new
  @config.logger.level = Sprinkle::OPTIONS[:verbose] ? ::Capistrano::Logger::INFO : ::Capistrano::Logger::IMPORTANT
  @config.set(:password) { ::Capistrano::CLI.password_prompt }
  # default sudo to false, we must turn it on
  @config.set(:run_method) { @config.fetch(:use_sudo, false) ? :sudo : :run }
  
  @config.set(:_sprinkle_actor, self)
  
  def @config.recipes(script)
    _sprinkle_actor.recipes(script)
  end

  if block
    @config.instance_eval(&block)
  else
    @config.load "Capfile" if File.exist?("Capfile")
  end
end

Instance Attribute Details

#configObject

:nodoc:



21
22
23
# File 'lib/sprinkle/actors/capistrano.rb', line 21

def config
  @config
end

#loaded_recipesObject

:nodoc:



21
22
23
# File 'lib/sprinkle/actors/capistrano.rb', line 21

def loaded_recipes
  @loaded_recipes
end

Class Method Details

.recipes(script) ⇒ Object



33
34
35
# File 'lib/sprinkle/actors/capistrano.rb', line 33

def @config.recipes(script)
  _sprinkle_actor.recipes(script)
end

Instance Method Details

#install(installer, roles, opts = {}) ⇒ Object

:nodoc:



83
84
85
86
87
88
89
90
# File 'lib/sprinkle/actors/capistrano.rb', line 83

def install(installer, roles, opts = {}) #:nodoc:
  @installer = installer
  process(installer.package.name, installer.install_sequence, roles, opts)
rescue ::Capistrano::CommandError => e
  raise_error(e)
ensure
  @installer = nil
end

#process(name, commands, roles, opts = {}) ⇒ Object

:nodoc:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sprinkle/actors/capistrano.rb', line 98

def process(name, commands, roles, opts = {}) #:nodoc:
  inst=@installer
  @log_recorder = log_recorder = Sprinkle::Utility::LogRecorder.new
  commands = commands.map {|x| rewrite_command(x)}
  define_task(name, roles) do
    via = fetch(:run_method)
    commands.each do |command|
      if command.is_a? Commands::Transfer
        upload command.source, command.destination, :via => :scp, 
          :recursive => command.recursive?
      elsif command.is_a? Commands::Reconnect
        teardown_connections_to(sessions.keys)
      else
        # this reset the log
        log_recorder.reset command
        invoke_command(command, {:via => via}) do |ch, stream, out|
          ::Capistrano::Configuration.default_io_proc.call(ch, stream, out) if Sprinkle::OPTIONS[:verbose]
          log_recorder.log(stream, out)
        end
      end
    end
  end
  run_task(name, opts)
end

#recipe(scripts) ⇒ Object

Defines a recipe file which will be included by capistrano. Use these recipe files to set capistrano specific configurations. Default recipe included is “deploy.” But if any other recipe is specified, it will include that instead. Multiple recipes may be specified through multiple recipes calls, an example:

deployment do
  delivery :capistrano do
    recipe 'deploy'
    recipes 'magic_beans', 'normal_beans'
  end
end


71
72
73
74
75
76
77
# File 'lib/sprinkle/actors/capistrano.rb', line 71

def recipe(scripts)
  @loaded_recipes ||= []
  Array(scripts).each do |script|
    @config.load script
    @loaded_recipes << script        
  end
end

#recipes(scripts) ⇒ Object

:nodoc:



79
80
81
# File 'lib/sprinkle/actors/capistrano.rb', line 79

def recipes(scripts) #:nodoc:
  recipe(scripts)
end

#servers_for_role?(roles) ⇒ Boolean

Determines if there are any servers for the given roles

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/sprinkle/actors/capistrano.rb', line 53

def servers_for_role?(roles) #:nodoc:
  roles=Array(roles)
  roles.any? { |r| @config.roles.keys.include?(r) }
end

#sudo?Boolean

:nodoc:

Returns:

  • (Boolean)


44
45
46
# File 'lib/sprinkle/actors/capistrano.rb', line 44

def sudo? #:nodoc:
  @config.fetch(:run_method) == :sudo
end

#sudo_commandObject

:nodoc:



48
49
50
# File 'lib/sprinkle/actors/capistrano.rb', line 48

def sudo_command #:nodoc:
  @config.sudo
end

#verify(verifier, roles, opts = {}) ⇒ Object

:nodoc:



92
93
94
95
96
# File 'lib/sprinkle/actors/capistrano.rb', line 92

def verify(verifier, roles, opts = {}) #:nodoc:
  process(verifier.package.name, verifier.commands, roles)
rescue ::Capistrano::CommandError
  return false
end