Class: Cindy::Executor::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cindy/executor/base.rb

Direct Known Subclasses

Local, SSH

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Base

X

Parameters:

  • logger (Logger)


12
13
14
# File 'lib/cindy/executor/base.rb', line 12

def initialize(logger)
    @logger = logger
end

Class Method Details

.from_uri(uri, logger) ⇒ Object



16
17
18
19
20
# File 'lib/cindy/executor/base.rb', line 16

def self.from_uri(uri, logger)
    uri = URI.parse(uri)
    ObjectSpace.each_object(Class).select { |v| return v.new(uri, logger) if v.ancestors.include?(self) && v.handle?(uri) }
    raise Exception.new 'Unexpected protocol'
end

Instance Method Details

#closeObject

Close the eventual underlaying connection



61
62
63
# File 'lib/cindy/executor/base.rb', line 61

def close
    # NOP
end

#exec(command, options = {}) ⇒ String, Boolean

Executes the given command

Parameters:

  • command (String)

    the command to execute

  • options (Hash) (defaults to: {})

    the options to execute the command with

Options Hash (options):

  • :stdin (String)

    the string to send as/on stdin

  • :ignore_failure (Boolean)

    don’t raise an exception if true when the command returns a non 0 exit value

  • :env (Hash)

    the environment variables to set/pass for command execution

  • :check_status_only (Boolean)

    simply return true if the command is successful else false

Returns:

  • (String, Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cindy/executor/base.rb', line 31

def exec(command, options = {})
=begin
    stdout, stderr, status = exec_imp command, options[:stdin]
    stdout.chomp!
    # <logging>
    if status.zero?
        if stdout.empty?
            @logger.info 'Command "%s" executed successfully' % command
        else
            @logger.info 'Command "%s" executed successfully (with "%s" returned)' % [ command, stdout ]
        end
    else
        @logger.send(options[:ignore_failure] ? :warn : :error, 'Command "%s" failed with "%s"' % [ command, stderr ])
    end
    # </logging>
    return status.zero? if options[:check_status_only]
    raise CommandFailedError.new "Command '#{command}' failed" if !options[:ignore_failure] && 0 != status
=end
    stdout, stderr, status = exec_and_log command, options
    # ?!?
    stdout
end

#exec!(command, options = {}) ⇒ Object



54
55
56
57
58
# File 'lib/cindy/executor/base.rb', line 54

def exec!(command, options = {})
    stdout, stderr, status = exec_and_log command, options
    raise CommandFailedError.new "Command '#{command}' failed" unless 0 == status
    stdout
end