Sha256: 23ec942ec65fd6e34f61a78f46fe771aadb0a87362d6fdf3a7fdafd937a12a35

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

module Sprinkle
  module Installers
    # The runner installer is great for running a simple command.
    #
    # == Example Usage
    #
    #   package :magic_beans do
    #     runner "make world"
    #   end
    #
    # You can also pass multiple commands as arguments or an array.
    #
    #   package :magic_beans do
    #     runner "make world", "destroy world"
    #     runner [ "make world", "destroy world" ]
    #   end
    #
    # Environment variables can be supplied throught the :env option.
    #
    #   package :magic_beans do
    #     runner "make world", :env => {
    #       :PATH => '/this/is/my/path:$PATH'
    #     }
    #   end
    #
    class Runner < Installer

      api do
        def runner(*cmds, &block)
          options = cmds.extract_options!
          install Runner.new(self, cmds, options, &block)
        end

        # runs 'echo noop' on the remote host
        def noop
          install Runner.new(self, "echo noop")
        end
      end

      attr_accessor :cmds #:nodoc:
      def initialize(parent, cmds, options = {}, &block) #:nodoc:
        super parent, options, &block
        @env = options.delete(:env)
        @cmds = [*cmds].flatten
        raise "you need to specify a command" if cmds.nil?
      end

      protected

        def env_str #:nodoc:
          @env_str ||= @env.inject("env ") do |s, (k,v)|
            s << "#{k.to_s.upcase}=#{v} "
          end
        end

        def install_commands #:nodoc:
          cmds = @env ? @cmds.map { |cmd| "#{env_str}#{cmd}"} : @cmds

          sudo? ?
            cmds.map { |cmd| "#{sudo_cmd}#{cmd}"} :
            cmds
        end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sprinkle-0.7.7 lib/sprinkle/installers/runner.rb
sprinkle-0.7.6.2 lib/sprinkle/installers/runner.rb
sprinkle-0.7.6.1 lib/sprinkle/installers/runner.rb
sprinkle-0.7.6 lib/sprinkle/installers/runner.rb
sprinkle-0.7.5 lib/sprinkle/installers/runner.rb
sprinkle-0.7.4 lib/sprinkle/installers/runner.rb
sprinkle-0.7.3 lib/sprinkle/installers/runner.rb