Class: Channelizer::Channels::Base

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Util::Retryable
Defined in:
lib/channelizer/channels/base.rb

Direct Known Subclasses

Ssh, WinRM

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Retryable

#retryable

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/channelizer/channels/base.rb', line 7

def initialize(options)
  self.class.validate_options(options)  
end

Class Method Details

.required_option(value) ⇒ Object

Register a required option

Parameters:

  • value (Symbol)

    the option name that is required



116
117
118
119
120
121
122
123
124
125
# File 'lib/channelizer/channels/base.rb', line 116

def required_option(value)
  @required_options ||= []
  if value.respond_to? :to_sym
    @required_options << value.to_sym
  elsif value.is_a? Array
    @required_options << value
  else
    raise ArgumentError, "#{value.inspect} cannot be added to the validation list"  
  end
end

.validate_options(options) ⇒ True

Validates the option set for the channel

Parameters:

  • options (Hash)

    the options hash to validate

Returns:

  • (True)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/channelizer/channels/base.rb', line 84

def validate_options(options)
  @required_options.each do |o|
    if o.is_a? Array
      # At least one of the items in an array must be set, if the value is
      # an array it is treated as a group of arguments that are required.
      condition_met = false
      o.each do |o_set|
        if o_set.is_a? Array
          included_options = []
          o_set.each do |o_req_set|
            included_options << o_req_set if options[o_req_set]
          end
          condition_met = true if included_options.length.eql?(o_set.length)
        else
          condition_met = true if options[o_set]
        end
      end
      raise ArgumentError, "You must specify one of #{o.inspect}" unless condition_met
    else
      raise ArgumentError, "#{o} is a required option, but was not provided"  unless options[o]

      if options[o].respond_to? :empty?
        raise ArgumentError, "#{o} cannot be empty" if options[o].empty?
      end  
    end
  end
  true
end

Instance Method Details

#execute(command, options = {}) ⇒ Fixnum

Executes a command on the channel

Parameters:

  • command

    the command to execute on the channel

Returns:

  • (Fixnum)

Raises:

  • (NotImplementedError)

    this method has not been implemented



47
48
49
# File 'lib/channelizer/channels/base.rb', line 47

def execute(command, options = {})
  raise NotImplementedError, "Execute not implemented"
end

#ready?TrueClass, FalseClass

Checks if the channel is ready for action

Returns:

  • (TrueClass, FalseClass)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/channelizer/channels/base.rb', line 64

def ready?
  begin
    Timeout.timeout(60) do
      execute "hostname"
    end
    return true
  rescue Timeout::Error => e
    return false
  rescue Errno::ECONNREFUSED => e
    return false
  rescue HTTPClient::KeepAliveDisconnected => e
    return false
  end
end

#shell_execute(command, options = {}) ⇒ Fixnum

Executes a command on the channel

Parameters:

  • command (String)

    the command to execute on the channel

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

    the options string to be used in executing the command

Options Hash (options):

  • :exit_codes (Array) — default: [0]

    the valid exit codes for the command

  • :check_exit_code (TrueClass, FalseClass) — default: true

    make that the command returned a valid exit code

Returns:

  • (Fixnum)

    the exit code of the command

Raises:

  • (BadExitCode)

    The command exited with a bad exitcode



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/channelizer/channels/base.rb', line 18

def shell_execute(command, options = {})
  defaults = {:exit_codes => [0], :check_exit_code => true, :sudo => false}
  options = defaults.merge(options)

  new_command = options[:sudo] ? sudo_command(command) : command

  exit_code = execute(new_command, options)

  if not options[:exit_codes].include? exit_code and options[:check_exit_code]
    raise BadExitCode, "Exit Code: #{exit_code}"
  end

  exit_code.to_i
end

#sudo_command(command) ⇒ String

Returns a command wrapped in a sudo context

Parameters:

  • command (String)

    the command to wrap in a sudo context

Returns:

  • (String)

    the command that has been wrapped in a sudo context

Raises:

  • (NotImplementedError)

    this method has not been implemented



38
39
40
# File 'lib/channelizer/channels/base.rb', line 38

def sudo_command(command)
  raise NotImplementedError, "sudo command not implemented"  
end

#upload(source, destination, options = {}) ⇒ Fixnum

Uploads a file over the channel

Parameters:

  • source

    the file to upload

  • destination

    where to place the file on the remote host

Returns:

  • (Fixnum)

Raises:

  • (NotImplementedError)

    this method has not been implemented



57
58
59
# File 'lib/channelizer/channels/base.rb', line 57

def upload(source, destination, options = {} )
  raise NotImplementedError, "Upload not implemented"
end