Class: Beaker::Host
- Inherits:
-
Object
- Object
- Beaker::Host
- Defined in:
- lib/beaker/host.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CommandFailure, PuppetConfigReader
Constant Summary collapse
- SELECT_TIMEOUT =
30
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
-
#add_env_var(key, val) ⇒ Object
Add the provided key/val to the current ssh environment.
- #close ⇒ Object
- #connection ⇒ Object
-
#delete_env_var(key, val) ⇒ Object
Delete the provided key/val from the current ssh environment.
-
#determine_if_x86_64 ⇒ Boolean
Examine the host system to determine the architecture.
- #do_scp_from(source, target, options) ⇒ Object
-
#do_scp_to(source, target, options) ⇒ Object
scp files from the localhost to this test host, if a directory is provided it is recursively copied.
- #exec(command, options = {}) ⇒ Object
-
#get_ip ⇒ Object
Determine the ip address of this host.
-
#graceful_restarts? ⇒ Boolean
Mirrors the true/false value of the host’s ‘graceful-restarts’ property, or falls back to the value of
is_using_passenger?
if ‘graceful-restarts’ is nil, but only if this is not a PE run (foss only). - #has_key?(k) ⇒ Boolean
-
#hostname ⇒ Object
Return the public name of the particular host, which may be different then the name of the host provided in the configuration file as some provisioners create random, unique hostnames.
-
#initialize(name, options) ⇒ Host
constructor
A new instance of Host.
-
#ip ⇒ Object
Return the ip address of this host.
- #is_pe? ⇒ Boolean
-
#is_using_passenger? ⇒ Boolean
True if this is a PE run, or if the host’s ‘passenger’ property has been set.
-
#is_x86_64? ⇒ Boolean
True if x86_64, false otherwise.
- #log_prefix ⇒ Object
- #merge_defaults_for_type(options, type) ⇒ Object
-
#mkdir_p(dir) ⇒ Boolean
Create the provided directory structure on the host.
- #node_name ⇒ Object
- #pkg_initialize ⇒ Object
- #port_open?(port) ⇒ Boolean
-
#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’)‘.
-
#reachable_name ⇒ Object
Return the preferred method to reach the host, will use IP is available and then default to #hostname.
-
#to_s ⇒ Object
The #hostname of this host.
-
#to_str ⇒ Object
The #hostname of this host.
- #up? ⇒ Boolean
-
#use_service_scripts? ⇒ Boolean
True if this is a pe run, or if the host has had a ‘use-service’ property set.
-
#uses_passenger!(puppetservice = 'apache2') ⇒ Object
Modifies the host settings to indicate that it will be using passenger service scripts, (apache2) by default.
Constructor Details
#initialize(name, options) ⇒ Host
Returns a new instance of Host.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/beaker/host.rb', line 48 def initialize name, @logger = [:logger] @name, @options = name.to_s, .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 = @options.get_type @defaults = merge_defaults_for_type @options, type pkg_initialize end |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
47 48 49 |
# File 'lib/beaker/host.rb', line 47 def defaults @defaults end |
#logger ⇒ Object
Returns the value of attribute logger.
46 47 48 |
# File 'lib/beaker/host.rb', line 46 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
47 48 49 |
# File 'lib/beaker/host.rb', line 47 def name @name end |
Class Method Details
.create(name, options) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/beaker/host.rb', line 28 def self.create name, case ['HOSTS'][name]['platform'] when /windows/ cygwin = ['HOSTS'][name]['is_cygwin'] if cygwin.nil? or cygwin == true Windows::Host.new name, else PSWindows::Host.new name, end when /aix/ Aix::Host.new name, when /osx/ Mac::Host.new name, else Unix::Host.new name, end end |
Instance Method Details
#+(other) ⇒ Object
139 140 141 |
# File 'lib/beaker/host.rb', line 139 def + other @name + other end |
#[](k) ⇒ Object
115 116 117 |
# File 'lib/beaker/host.rb', line 115 def [] k @defaults[k] end |
#[]=(k, v) ⇒ Object
111 112 113 |
# File 'lib/beaker/host.rb', line 111 def []= k, v @defaults[k] = v end |
#add_env_var(key, val) ⇒ Object
Add the provided key/val to the current ssh environment
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/beaker/host.rb', line 219 def add_env_var key, val key = key.to_s.upcase escaped_val = Regexp.escape(val).gsub('/', '\/').gsub(';', '\;') env_file = self[:ssh_env_file] #see if the key/value pair already exists if exec(Beaker::Command.new("grep -e #{key}=.*#{escaped_val} #{env_file}"), :acceptable_exit_codes => (0..255) ).exit_code == 0 return #nothing to do here, key value pair already exists #see if the key already exists elsif exec(Beaker::Command.new("grep #{key} #{env_file}"), :acceptable_exit_codes => (0..255) ).exit_code == 0 exec(Beaker::SedCommand.new(self['HOSTS'][name]['platform'], "s/#{key}=/#{key}=#{escaped_val}:/", env_file)) else exec(Beaker::Command.new("echo \"#{key}=#{val}\" >> #{env_file}")) end end |
#close ⇒ Object
253 254 255 256 |
# File 'lib/beaker/host.rb', line 253 def close @connection.close if @connection @connection = nil end |
#connection ⇒ Object
247 248 249 250 251 |
# File 'lib/beaker/host.rb', line 247 def connection @connection ||= SshConnection.connect( reachable_name, self['user'], self['ssh'], { :logger => @logger } ) end |
#delete_env_var(key, val) ⇒ Object
Delete the provided key/val from the current ssh environment
239 240 241 242 243 244 245 |
# File 'lib/beaker/host.rb', line 239 def delete_env_var key, val val = Regexp.escape(val).gsub('/', '\/').gsub(';', '\;') #if the key only has that single value remove the entire line exec(Beaker::SedCommand.new(self['HOSTS'][name]['platform'], "/#{key}=#{val}$/d", self[:ssh_env_file])) #if the key has multiple values and we only need to remove the provided val exec(Beaker::SedCommand.new(self['HOSTS'][name]['platform'], "s/#{key}=\\(.*[:;]*\\)#{val}[:;]*/#{key}=\\1/", self[:ssh_env_file])) end |
#determine_if_x86_64 ⇒ Boolean
Examine the host system to determine the architecture
204 205 206 207 |
# File 'lib/beaker/host.rb', line 204 def determine_if_x86_64 result = exec(Beaker::Command.new("arch | grep x86_64"), :acceptable_exit_codes => (0...127)) result.exit_code == 0 end |
#do_scp_from(source, target, options) ⇒ Object
384 385 386 387 388 389 390 |
# File 'lib/beaker/host.rb', line 384 def do_scp_from source, target, @logger.debug "localhost $ scp #{@name}:#{source} #{target}" result = connection.scp_from(source, target, , $dry_run) @logger.debug result.stdout return result end |
#do_scp_to(source, target, options) ⇒ Object
scp files from the localhost to this test host, if a directory is provided it is recursively copied
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/beaker/host.rb', line 317 def do_scp_to source, target, @logger.notify "localhost $ scp #{source} #{@name}:#{target} {:ignore => #{[:ignore]}}" result = Result.new(@name, [source, target]) has_ignore = [:ignore] and not [:ignore].empty? # construct the regex for matching ignored files/dirs ignore_re = nil if has_ignore ignore_arr = Array([:ignore]).map do |entry| "((\/|\\A)#{entry}(\/|\\z))".gsub(/\./, '\.') end ignore_re = Regexp.new(ignore_arr.join('|')) end # either a single file, or a directory with no ignores if not File.file?(source) and not File.directory?(source) raise IOError, "No such file or directory - #{source}" end if File.file?(source) or (File.directory?(source) and not has_ignore) source_file = source if has_ignore and (source =~ ignore_re) @logger.trace "After rejecting ignored files/dirs, there is no file to copy" source_file = nil result.stdout = "No files to copy" result.exit_code = 1 end if source_file result = connection.scp_to(source_file, target, , $dry_run) @logger.trace result.stdout end else # a directory with ignores dir_source = Dir.glob("#{source}/**/*").reject do |f| f =~ ignore_re end @logger.trace "After rejecting ignored files/dirs, going to scp [#{dir_source.join(", ")}]" # create necessary directory structure on host # run this quietly (no STDOUT) @logger.quiet(true) required_dirs = (dir_source.map{ | dir | File.dirname(dir) }).uniq require 'pathname' required_dirs.each do |dir| dir_path = Pathname.new(dir) if dir_path.absolute? mkdir_p(File.join(target, dir.gsub(source, ''))) else mkdir_p( File.join(target, dir) ) end end @logger.quiet(false) # copy each file to the host dir_source.each do |s| s_path = Pathname.new(s) if s_path.absolute? file_path = File.join(target, File.dirname(s).gsub(source,'')) else file_path = File.join(target, File.dirname(s)) end result = connection.scp_to(s, file_path, , $dry_run) @logger.trace result.stdout end end return result end |
#exec(command, options = {}) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/beaker/host.rb', line 258 def exec command, ={} # I've always found this confusing cmdline = command.cmd_line(self) if [:silent] output_callback = nil else @logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{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, , output_callback) } if not [:silent] @logger.debug "\n#{log_prefix} executed in %0.2f seconds" % seconds end unless [: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?? if ![:accept_all_exit_codes] && !result.exit_code_in?(Array([:acceptable_exit_codes] || 0)) raise CommandFailure, "Host '#{self}' exited with #{result.exit_code} running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}" end end # Danger, so we have to return this result? result end end |
#get_ip ⇒ Object
Determine the ip address of this host
193 194 195 |
# File 'lib/beaker/host.rb', line 193 def get_ip @logger.warn("Uh oh, this should be handled by sub-classes but hasn't been") end |
#graceful_restarts? ⇒ Boolean
Mirrors the true/false value of the host’s ‘graceful-restarts’ property, or falls back to the value of is_using_passenger?
if ‘graceful-restarts’ is nil, but only if this is not a PE run (foss only).
155 156 157 158 159 160 161 162 163 |
# File 'lib/beaker/host.rb', line 155 def graceful_restarts? graceful = if !self['graceful-restarts'].nil? self['graceful-restarts'] else !is_pe? && is_using_passenger? end graceful end |
#has_key?(k) ⇒ Boolean
119 120 121 |
# File 'lib/beaker/host.rb', line 119 def has_key? k @defaults.has_key?(k) end |
#hostname ⇒ Object
Return the public name of the particular host, which may be different then the name of the host provided in the configuration file as some provisioners create random, unique hostnames.
135 136 137 |
# File 'lib/beaker/host.rb', line 135 def hostname @defaults['vmhostname'] || @name end |
#ip ⇒ Object
Return the ip address of this host
198 199 200 |
# File 'lib/beaker/host.rb', line 198 def ip self[:ip] ||= get_ip end |
#is_pe? ⇒ Boolean
143 144 145 |
# File 'lib/beaker/host.rb', line 143 def is_pe? @options.is_pe? end |
#is_using_passenger? ⇒ Boolean
True if this is a PE run, or if the host’s ‘passenger’ property has been set.
180 181 182 |
# File 'lib/beaker/host.rb', line 180 def is_using_passenger? is_pe? || self['passenger'] end |
#is_x86_64? ⇒ Boolean
Returns true if x86_64, false otherwise.
210 211 212 |
# File 'lib/beaker/host.rb', line 210 def is_x86_64? @x86_64 ||= determine_if_x86_64 end |
#log_prefix ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/beaker/host.rb', line 184 def log_prefix if @defaults['vmhostname'] "#{self} (#{@name})" else self.to_s end end |
#merge_defaults_for_type(options, type) ⇒ Object
66 67 68 69 |
# File 'lib/beaker/host.rb', line 66 def merge_defaults_for_type , type defaults = self.class.send "#{type}_defaults".to_sym defaults.merge(.merge((['HOSTS'][name]))) end |
#mkdir_p(dir) ⇒ Boolean
Create the provided directory structure on the host
301 302 303 304 305 306 307 308 309 310 |
# File 'lib/beaker/host.rb', line 301 def mkdir_p dir if self['is_cygwin'].nil? or self['is_cygwin'] == true cmd = "mkdir -p #{dir}" else cmd = "if not exist #{dir.gsub!('/','\\')} (md #{dir.gsub!('/','\\')})" end result = exec(Beaker::Command.new(cmd), :acceptable_exit_codes => [0, 1]) result.exit_code == 0 end |
#node_name ⇒ Object
71 72 73 74 75 76 |
# File 'lib/beaker/host.rb', line 71 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_initialize ⇒ Object
61 62 63 64 |
# File 'lib/beaker/host.rb', line 61 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
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/beaker/host.rb', line 78 def port_open? port begin Timeout.timeout SELECT_TIMEOUT do TCPSocket.new(reachable_name, port).close return true end rescue Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT 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’)‘
107 108 109 |
# File 'lib/beaker/host.rb', line 107 def puppet(command='agent') PuppetConfigReader.new(self, command) end |
#reachable_name ⇒ Object
Return the preferred method to reach the host, will use IP is available and then default to #hostname.
99 100 101 |
# File 'lib/beaker/host.rb', line 99 def reachable_name self['ip'] || hostname end |
#to_s ⇒ Object
The #hostname of this host.
129 130 131 |
# File 'lib/beaker/host.rb', line 129 def to_s hostname end |
#to_str ⇒ Object
The #hostname of this host.
124 125 126 |
# File 'lib/beaker/host.rb', line 124 def to_str hostname end |
#up? ⇒ Boolean
89 90 91 92 93 94 95 96 |
# File 'lib/beaker/host.rb', line 89 def up? begin Socket.getaddrinfo( reachable_name, nil ) return true rescue SocketError return false end end |
#use_service_scripts? ⇒ Boolean
True if this is a pe run, or if the host has had a ‘use-service’ property set.
148 149 150 |
# File 'lib/beaker/host.rb', line 148 def use_service_scripts? is_pe? || self['use-service'] end |
#uses_passenger!(puppetservice = 'apache2') ⇒ Object
Modifies the host settings to indicate that it will be using passenger service scripts, (apache2) by default. Does nothing if this is a PE host, since it is already using passenger.
170 171 172 173 174 175 176 177 |
# File 'lib/beaker/host.rb', line 170 def uses_passenger!(puppetservice = 'apache2') if !is_pe? self['passenger'] = true self['puppetservice'] = puppetservice self['use-service'] = true end return true end |