Class: Channelizer::Channels::Base
- Inherits:
-
Object
- Object
- Channelizer::Channels::Base
- Includes:
- Exceptions, Util::Retryable
- Defined in:
- lib/channelizer/channels/base.rb
Class Method Summary collapse
-
.required_option(value) ⇒ Object
Register a required option.
-
.validate_options(options) ⇒ True
Validates the option set for the channel.
Instance Method Summary collapse
-
#execute(command, options = {}) ⇒ Fixnum
Executes a command on the channel.
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
-
#ready? ⇒ TrueClass, FalseClass
Checks if the channel is ready for action.
-
#shell_execute(command, options = {}) ⇒ Fixnum
Executes a command on the channel.
-
#sudo_command(command) ⇒ String
Returns a command wrapped in a sudo context.
-
#upload(source, destination, options = {}) ⇒ Fixnum
Uploads a file over the channel.
Methods included from Util::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() self.class.() end |
Class Method Details
.required_option(value) ⇒ Object
Register a required option
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
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 () @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 = [] o_set.each do |o_req_set| << o_req_set if [o_req_set] end condition_met = true if .length.eql?(o_set.length) else condition_met = true if [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 [o] if [o].respond_to? :empty? raise ArgumentError, "#{o} cannot be empty" if [o].empty? end end end true end |
Instance Method Details
#execute(command, options = {}) ⇒ Fixnum
Executes a command on the channel
47 48 49 |
# File 'lib/channelizer/channels/base.rb', line 47 def execute(command, = {}) raise NotImplementedError, "Execute not implemented" end |
#ready? ⇒ TrueClass, FalseClass
Checks if the channel is ready for action
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
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, = {}) defaults = {:exit_codes => [0], :check_exit_code => true, :sudo => false} = defaults.merge() new_command = [:sudo] ? sudo_command(command) : command exit_code = execute(new_command, ) if not [:exit_codes].include? exit_code and [: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
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
57 58 59 |
# File 'lib/channelizer/channels/base.rb', line 57 def upload(source, destination, = {} ) raise NotImplementedError, "Upload not implemented" end |