Class: Beaker::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/host.rb

Direct Known Subclasses

Unix::Host, Windows::Host

Defined Under Namespace

Classes: CommandFailure, PuppetConfigReader

Constant Summary collapse

SELECT_TIMEOUT =
30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Host

Returns a new instance of Host.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/beaker/host.rb', line 45

def initialize name, options
  @logger = options[:logger]
  @name, @options = name.to_s, options.dup

  # This is annoying and its because of drift/lack of enforcement/lack of having
  # a explict relationship between our defaults, our setup steps and how they're
  # related through 'type' and the differences between the assumption of our two
  # configurations we have for many of our products
  type = is_pe? ? :pe : :foss
  @defaults = merge_defaults_for_type @options, type
  pkg_initialize
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



44
45
46
# File 'lib/beaker/host.rb', line 44

def defaults
  @defaults
end

#loggerObject

Returns the value of attribute logger.



43
44
45
# File 'lib/beaker/host.rb', line 43

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



44
45
46
# File 'lib/beaker/host.rb', line 44

def name
  @name
end

Class Method Details

.create(name, options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/beaker/host.rb', line 32

def self.create name, options
  case options['HOSTS'][name]['platform']
  when /windows/
    Windows::Host.new name, options
  when /aix/
    Aix::Host.new name, options
  else
    Unix::Host.new name, options
  end
end

Instance Method Details

#+(other) ⇒ Object



127
128
129
# File 'lib/beaker/host.rb', line 127

def + other
  @name + other
end

#[](k) ⇒ Object



111
112
113
# File 'lib/beaker/host.rb', line 111

def [] k
  @defaults[k]
end

#[]=(k, v) ⇒ Object



107
108
109
# File 'lib/beaker/host.rb', line 107

def []= k, v
  @defaults[k] = v
end

#closeObject



149
150
151
152
# File 'lib/beaker/host.rb', line 149

def close
  @connection.close if @connection
  @connection = nil
end

#connectionObject



143
144
145
146
147
# File 'lib/beaker/host.rb', line 143

def connection
  @connection ||= SshConnection.connect( reachable_name,
                                         self['user'],
                                         self['ssh'] )
end

#do_scp_from(source, target, options) ⇒ Object



198
199
200
201
202
203
# File 'lib/beaker/host.rb', line 198

def do_scp_from source, target, options

  @logger.debug "localhost $ scp #{@name}:#{source} #{target} #{options.to_s}"
  result = connection.scp_from(source, target, options, $dry_run)
  return result
end

#do_scp_to(source, target, options) ⇒ Object



192
193
194
195
196
# File 'lib/beaker/host.rb', line 192

def do_scp_to source, target, options
  @logger.debug "localhost $ scp #{source} #{@name}:#{target} #{options.to_s}"
  result = connection.scp_to(source, target, options, $dry_run)
  return result
end

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/beaker/host.rb', line 154

def exec command, options={}
  # I've always found this confusing
  cmdline = command.cmd_line(self)

  if options[:silent]
    output_callback = nil
  else
    @logger.debug "\n#{log_prefix} $ #{cmdline}"
    output_callback = logger.method(:host_output)
  end

  unless $dry_run
    # is this returning a result object?
    # the options should come at the end of the method signature (rubyism)
    # and they shouldn't be ssh specific
    result = nil

    seconds = Benchmark.realtime {
      result = connection.execute(cmdline, options, output_callback)
    }
    @logger.debug "\n#{log_prefix} executed in %0.2f seconds" % seconds

    unless options[:silent]
      # What?
      result.log(@logger)
      # No, TestCase has the knowledge about whether its failed, checking acceptable
      # exit codes at the host level and then raising...
      # is it necessary to break execution??
      unless result.exit_code_in?(Array(options[:acceptable_exit_codes] || 0))
        limit = 10
        raise CommandFailure, "Host '#{self}' exited with #{result.exit_code} running:\n #{cmdline}\nLast #{limit} lines of output were:\n#{result.formatted_output(limit)}"
      end
    end
    # Danger, so we have to return this result?
    result
  end
end

#has_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/beaker/host.rb', line 115

def has_key? k
  @defaults.has_key?(k)
end

#is_pe?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/beaker/host.rb', line 131

def is_pe?
  @options.is_pe?
end

#log_prefixObject



135
136
137
138
139
140
141
# File 'lib/beaker/host.rb', line 135

def log_prefix
  if @defaults['vmhostname']
    "#{self} (#{@name})"
  else
    self.to_s
  end
end

#merge_defaults_for_type(options, type) ⇒ Object



63
64
65
66
# File 'lib/beaker/host.rb', line 63

def merge_defaults_for_type options, type
  defaults = self.class.send "#{type}_defaults".to_sym
  defaults.merge(options.merge((options['HOSTS'][name])))
end

#node_nameObject



68
69
70
71
72
73
# File 'lib/beaker/host.rb', line 68

def node_name
  # TODO: might want to consider caching here; not doing it for now because
  #  I haven't thought through all of the possible scenarios that could
  #  cause the value to change after it had been cached.
  result = puppet['node_name_value'].strip
end

#pkg_initializeObject



58
59
60
61
# File 'lib/beaker/host.rb', line 58

def pkg_initialize
  # This method should be overridden by platform-specific code to
  # handle whatever packaging-related initialization is necessary.
end

#port_open?(port) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
# File 'lib/beaker/host.rb', line 75

def port_open? port
  begin
    Timeout.timeout SELECT_TIMEOUT do
      TCPSocket.new(reachable_name, port).close
      return true
    end
  rescue Errno::ECONNREFUSED, Timeout::Error
    return false
  end
end

#puppet(command = 'agent') ⇒ Object

Returning our PuppetConfigReader here allows users of the Host class to do things like ‘host.puppet` to query the ’main’ section or, if they want the configuration for a particular run type, ‘host.puppet(’agent’)



103
104
105
# File 'lib/beaker/host.rb', line 103

def puppet(command='agent')
  PuppetConfigReader.new(self, command)
end

#reachable_nameObject



95
96
97
# File 'lib/beaker/host.rb', line 95

def reachable_name
  self['ip'] || self['vmhostname'] || name
end

#to_sObject



123
124
125
# File 'lib/beaker/host.rb', line 123

def to_s
  @defaults['vmhostname'] || @name
end

#to_strObject



119
120
121
# File 'lib/beaker/host.rb', line 119

def to_str
  @defaults['vmhostname'] || @name
end

#up?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/beaker/host.rb', line 86

def up?
  begin
    Socket.getaddrinfo( reachable_name, nil )
    return true
  rescue SocketError
    return false
  end
end