Class: Sprinkle::Verify

Inherits:
Object show all
Includes:
Attributes, Package::Rendering::Helpers, Sudo
Defined in:
lib/sprinkle/verify.rb

Overview

Verify Blocks

As documented in Sprinkle::Package, you may define a block on a package which verifies that a package was installed correctly. If this verification block fails, Sprinkle will stop the script gracefully, raising the error.

In addition to checking post install if it was successfully, verification blocks are also run before an install to see if a package is already installed. If this is the case, the package is skipped and Sprinkle continues with the next package. This behavior can be overriden by setting the -f flag on the sprinkle script or setting Sprinkle::OPTIONS to true if you're using sprinkle programmatically.

An Example

The following verifies that rails was installed correctly be checking to see if the 'rails' command is available on the command line:

package :rails do
  gem 'rails'

  verify do
    has_executable 'rails'
  end
end

Available Verifiers

There are a variety of available methods for use in the verification block. The standard methods are defined in the Sprinkle::Verifiers module, so see their corresponding documentation.

Custom Verifiers

If you feel that the built-in verifiers do not offer a certain aspect of verification which you need, you may create your own verifier! Simply wrap any method in a module which you want to use:

module MagicBeansVerifier
  def has_magic_beans(sauce)
    @commands << '[ -z "`echo $' + sauce + '`"]'
  end
end

The method can append as many commands as it wishes to the @commands array. These commands will be run on the remote server and MUST give an exit status of 0 if successful or other if unsuccessful.

To register your verifier, call the register method on Sprinkle::Verify:

Sprinkle::Verify.register(MagicBeansVerifier)

And now you may use it like any other verifier:

package :magic_beans do
  gem 'magic_beans'

  verify { has_magic_beans('ranch') }
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sudo

#sudo?, #sudo_cmd, #sudo_stack

Methods included from Package::Rendering::Helpers

#md5

Methods included from Attributes

#defaults, #set_defaults

Constructor Details

#initialize(package, description = '', &block) ⇒ Verify

:nodoc:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sprinkle/verify.rb', line 81

def initialize(package, description = '', &block) #:nodoc:
  raise 'Verify requires a block.' unless block
  
  @package = package
  @description = description
  @commands = []
  @options ||= {}
  @options[:padding] = 4
  @delivery = nil
  @block = block
end

Instance Attribute Details

#descriptionObject

:nodoc:



65
66
67
# File 'lib/sprinkle/verify.rb', line 65

def description
  @description
end

#optionsObject

:nodoc:



65
66
67
# File 'lib/sprinkle/verify.rb', line 65

def options
  @options
end

#packageObject

:nodoc:



65
66
67
# File 'lib/sprinkle/verify.rb', line 65

def package
  @package
end

Class Method Details

.register(new_module) ⇒ Object

Register a verification module



74
75
76
# File 'lib/sprinkle/verify.rb', line 74

def register(new_module)
  class_eval { include new_module }
end

Instance Method Details

#commandsObject



93
94
95
96
# File 'lib/sprinkle/verify.rb', line 93

def commands
  prepare
  @commands
end

#prepareObject



98
99
100
101
102
103
# File 'lib/sprinkle/verify.rb', line 98

def prepare
  return if @prepared
  @commands = []
  self.instance_eval(&@block)
  @prepared = true
end

#process(roles, pre = false) ⇒ Object

:nodoc:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sprinkle/verify.rb', line 116

def process(roles, pre = false) #:nodoc:
  description = @description.empty? ? " (#{@package.name})" : @description
  
  if logger.debug?
    logger.debug "#{@package.name}#{description} verification sequence: #{@commands.join('; ')} for roles: #{roles}\n"
  end
  
  if Sprinkle::OPTIONS[:testing]
    # always fail when testing to force an install
    raise Sprinkle::VerificationFailed.new(@package, description) if pre
  else
    logger.debug "#{" " * @options[:padding]}--> Verifying #{description}..."
    
    unless @delivery.verify(self, roles)
      # Verification failed, halt sprinkling gracefully.
      raise Sprinkle::VerificationFailed.new(@package, description)
    end
  end
end

#runner(*cmds) ⇒ Object



105
106
107
108
109
110
# File 'lib/sprinkle/verify.rb', line 105

def runner(*cmds)
  ActiveSupport::Deprecation.warn 
    "runner inside verify is depreciated and will removed in the future\n" +
    "use runs_without_error instead."
  runs_without_error(*cmds)
end

#runs_without_error(*cmds) ⇒ Object



112
113
114
# File 'lib/sprinkle/verify.rb', line 112

def runs_without_error(*cmds)
  @commands += cmds
end