Class: Beaker::Host

Inherits:
Object
  • Object
show all
Includes:
DSL::Helpers, DSL::Patterns
Defined in:
lib/beaker/host.rb

Direct Known Subclasses

Unix::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

Methods included from DSL::Patterns

#block_on

Methods included from DSL::Helpers::WebHelpers

#link_exists?, #port_open_within?

Methods included from DSL::Helpers::TKHelpers

#modify_tk_config

Methods included from DSL::Helpers::TestHelpers

#current_step_name, #current_test_filename, #current_test_name, #set_current_step_name, #set_current_test_filename, #set_current_test_name

Methods included from DSL::Helpers::PuppetHelpers

#apply_manifest, #apply_manifest_on, #create_tmpdir_for_user, #puppet_group, #puppet_user, #run_agent_on, #sign_certificate, #sign_certificate_for, #sleep_until_nc_started, #sleep_until_puppetdb_started, #sleep_until_puppetserver_started, #stop_agent, #stop_agent_on, #stub_forge, #stub_forge_on, #stub_hosts, #stub_hosts_on, #wait_for_host_in_dashboard, #with_forge_stubbed, #with_forge_stubbed_on, #with_host_stubbed_on, #with_puppet_running, #with_puppet_running_on

Methods included from DSL::Helpers::HostHelpers

#add_system32_hosts_entry, #backup_the_file, #check_for_package, #create_remote_file, #create_tmpdir_on, #curl_on, #curl_with_retries, #deploy_package_repo, #echo_on, #execute_powershell_script_on, #exit_code, #install_package, #on, #retry_on, #rsync_to, #run_script, #run_script_on, #scp_from, #scp_to, #shell, #stderr, #stdout, #upgrade_package

Methods included from DSL::Helpers::FacterHelpers

#fact, #fact_on

Constructor Details

#initialize(name, host_hash, options) ⇒ Host

Returns a new instance of Host.



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

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

  @host_hash = self.platform_defaults.merge(@host_hash)
  pkg_initialize
end

Instance Attribute Details

#host_hashObject (readonly)

Returns the value of attribute host_hash.



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

def host_hash
  @host_hash
end

#loggerObject

Returns the value of attribute logger.



67
68
69
# File 'lib/beaker/host.rb', line 67

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.create(name, host_hash, options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/beaker/host.rb', line 43

def self.create name, host_hash, options
  case host_hash['platform']
  when /windows/
    cygwin = host_hash['is_cygwin']
    if cygwin.nil? or cygwin == true
      Windows::Host.new name, host_hash, options
    else
      PSWindows::Host.new name, host_hash, options
    end
  when /aix/
    Aix::Host.new name, host_hash, options
  when /osx/
    Mac::Host.new name, host_hash, options
  when /freebsd/
    FreeBSD::Host.new name, host_hash, options
  when /eos/
    Eos::Host.new name, host_hash, options
  when /cisco/
    Cisco::Host.new name, host_hash, options
  else
    Unix::Host.new name, host_hash, options
  end
end

Instance Method Details

#+(other) ⇒ Object



157
158
159
# File 'lib/beaker/host.rb', line 157

def + other
  @name + other
end

#[](k) ⇒ Object

Does this host have this key? Either as defined in the host itself, or globally?



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

def [] k
  host_hash[k] || options[k]
end

#[]=(k, v) ⇒ Object



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

def []= k, v
  host_hash[k] = v
end

#closeObject



256
257
258
259
260
261
262
263
264
265
# File 'lib/beaker/host.rb', line 256

def close
  if @connection
    @connection.close
    # update connection information
    @connection.ip         = self['ip'] if self['ip']
    @connection.vmhostname = self['vmhostname'] if self['vmhostname']
    @connection.hostname   = @name
  end
  @connection = nil
end

#connectionObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/beaker/host.rb', line 238

def connection
  # create new connection object if necessary
  @connection ||= SshConnection.connect( { :ip => self['ip'], :vmhostname => self['vmhostname'], :hostname => @name },
                                         self['user'],
                                         self['ssh'], { :logger => @logger } )
  # update connection information
  if self['ip'] && (@connection.ip != self['ip'])
    @connection.ip = self['ip']
  end
  if self['vmhostname'] && (@connection.vmhostname != self['vmhostname'])
    @connection.vmhostname = self['vmhostname']
  end
  if @name && (@connection.hostname != @name)
    @connection.hostname = @name
  end
  @connection
end

#delete(k) ⇒ Object



137
138
139
# File 'lib/beaker/host.rb', line 137

def delete k
  host_hash.delete(k)
end

#do_rsync_to(from_path, to_path, opts = {}) ⇒ Object

rsync a file or directory from the localhost to this test host

Parameters:

  • from_path (String)

    The path to the file/dir to upload

  • to_path (String)

    The destination path on the host

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution

Options Hash (opts):

  • :ignore (Array<String>)

    An array of file/dir paths that will not be copied to the host



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/beaker/host.rb', line 461

def do_rsync_to from_path, to_path, opts = {}
  ssh_opts = self['ssh']
  rsync_args = []
  ssh_args = []

  if not File.file?(from_path) and not File.directory?(from_path)
    raise IOError, "No such file or directory - #{from_path}"
  end

  # We enable achieve mode and compression
  rsync_args << "-az"

  if not self['user']
    user = "root"
  else
    user = self['user']
  end
  hostname_with_user = "#{user}@#{reachable_name}"

  Rsync.host = hostname_with_user

  # vagrant uses temporary ssh configs in order to use dynamic keys
  # without this config option using ssh may prompt for password
  if ssh_opts[:config] and File.exists?(ssh_opts[:config])
    ssh_args << "-F #{ssh_opts[:config]}"
  else
    if ssh_opts.has_key?('keys') and
        ssh_opts.has_key?('auth_methods') and
        ssh_opts['auth_methods'].include?('publickey')

      key = ssh_opts['keys']

      # If an array was set, then we use the first value
      if key.is_a? Array
        key = key.first
      end

      # We need to expand tilde manually as rsync can be
      # funny sometimes
      key = File.expand_path(key)

      ssh_args << "-i #{key}"
    end
  end

  if ssh_opts.has_key?(:port)
    ssh_args << "-p #{ssh_opts[:port]}"
  end

  # We disable prompt when host isn't known
  ssh_args << "-o 'StrictHostKeyChecking no'"

  if not ssh_args.empty?
    rsync_args << "-e \"ssh #{ssh_args.join(' ')}\""
  end

  if opts.has_key?(:ignore) and not opts[:ignore].empty?
    opts[:ignore].map! do |value|
      "--exclude '#{value}'"
    end
    rsync_args << opts[:ignore].join(' ')
  end

  # We assume that the *contents* of the directory 'from_path' needs to be
  # copied into the directory 'to_path'
  if File.directory?(from_path) and not from_path.end_with?('/')
    from_path += '/'
  end

  @logger.notify "rsync: localhost:#{from_path} to #{hostname_with_user}:#{to_path} {:ignore => #{opts[:ignore]}}"
  result = Rsync.run(from_path, to_path, rsync_args)
  @logger.debug("rsync returned #{result.inspect}")
  result
end

#do_scp_from(source, target, options) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/beaker/host.rb', line 439

def do_scp_from source, target, options
  # use the value of :dry_run passed to the method unless
  # undefined, then use parsed @options hash.
  options[:dry_run] ||= @options[:dry_run]

  if options[:dry_run]
    scp_cmd = "scp #{@name}:#{source} #{target}"
    @logger.debug "\n Running in :dry_run mode. localhost $ #{scp_cmd} not executed."
    return  NullResult.new(self, scp_cmd)
  end

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

#do_scp_to(source, target_path, options) ⇒ Object

scp files from the localhost to this test host, if a directory is provided it is recursively copied. If the provided source is a directory both the contents of the directory and the directory itself will be copied to the host, if you only want to copy directory contents you will either need to specify the contents file by file or do a separate ‘mv’ command post scp_to to create the directory structure as desired. To determine if a file/dir is ‘ignored’ we compare to any contents of the source dir and NOT any part of the path to that source dir.

Examples:

do_scp_to('source/dir1/dir2/dir3', 'target')
-> will result in creation of target/source/dir1/dir2/dir3 on host

do_scp_to('source/file.rb', 'target', { :ignore => 'file.rb' }
-> will result in not files copyed to the host, all are ignored

Parameters:

  • source (String)

    The path to the file/dir to upload

  • target_path (String)

    The destination path on the host

  • options (Hash{Symbol=>String})

    Options to alter execution

Options Hash (options):

  • :ignore (Array<String>)

    An array of file/dir paths that will not be copied to the host



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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/beaker/host.rb', line 355

def do_scp_to source, target_path, options
  target = self.scp_path( target_path )

  # use the value of :dry_run passed to the method unless
  # undefined, then use parsed @options hash.
  options[:dry_run] ||= @options[:dry_run]

  if options[:dry_run]
    scp_cmd = "scp #{source} #{@name}:#{target}"
    @logger.debug "\n Running in :dry_run mode. localhost $ #{scp_cmd} not executed."
    return NullResult.new(self, scp_cmd)
  end

  @logger.notify "localhost $ scp #{source} #{@name}:#{target} {:ignore => #{options[:ignore]}}"

  result = Result.new(@name, [source, target])
  has_ignore = options[:ignore] and not options[:ignore].empty?
  # construct the regex for matching ignored files/dirs
  ignore_re = nil
  if has_ignore
    ignore_arr = Array(options[:ignore]).map do |entry|
      "((\/|\\A)#{Regexp.escape(entry)}(\/|\\z))"
    end
    ignore_re = Regexp.new(ignore_arr.join('|'))
    @logger.debug("going to ignore #{ignore_re}")
  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, options)
      @logger.trace result.stdout
    end
  else # a directory with ignores
    dir_source = Dir.glob("#{source}/**/*").reject do |f|
      f.gsub(/\A#{Regexp.escape(source)}/, '') =~ ignore_re #only match against subdirs, not full path
    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(/#{Regexp.escape(File.dirname(File.absolute_path(source)))}/, '')))
      else
        mkdir_p( File.join(target, dir) )
      end
    end
    @logger.quiet(false)

    # copy each file to the host
    dir_source.each do |s|
      # Copy files, not directories (as they are copied recursively)
      next if File.directory?(s)

      s_path = Pathname.new(s)
      if s_path.absolute?
        file_path = File.join(target, File.dirname(s).gsub(/#{Regexp.escape(File.dirname(File.absolute_path(source)))}/,''))
      else
        file_path = File.join(target, File.dirname(s))
      end
      result = connection.scp_to(s, file_path, options)
      @logger.trace result.stdout
    end
  end

  self.scp_post_operations( target, target_path )
  return result
end

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



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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/beaker/host.rb', line 267

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

  # use the value of :dry_run passed to the method unless
  # undefined, then use parsed @options hash.
  options[:dry_run] ||= @options[:dry_run]

  if options[:dry_run]
    @logger.debug "\n Running in :dry_run mode. Command #{cmdline} not executed."
    result = Beaker::NullResult.new(self, command)
    return result
  end

  if options[:silent]
    output_callback = nil
  else
    @logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{cmdline}"
    if @options[:color_host_output]
      output_callback = logger.method(:color_host_output)
    else
      output_callback = logger.method(:host_output)
    end
  end

  unless options[: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

    @logger.step_in()
    seconds = Benchmark.realtime {
      result = connection.execute(cmdline, options, output_callback)
    }
    @logger.step_out()

    if not options[:silent]
      @logger.debug "\n#{log_prefix} executed in %0.2f seconds" % seconds
    end

    unless options[:silent]
      # What?
      result.log(@logger)
      if !options[:expect_connection_failure] && !result.exit_code
        # no exit code was collected, so the stream failed
        raise CommandFailure, "Host '#{self}' connection failure running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}"

      end
      if options[:expect_connection_failure] && result.exit_code
        # should have had a connection failure, but didn't
        # wait to see if the connection failure will be generation, otherwise raise error
        if not connection.wait_for_connection_failure(options, output_callback)
          raise CommandFailure,  "Host '#{self}' should have resulted in a connection failure running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}"
        end
      end
      # 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 options[:accept_all_exit_codes] && options[:acceptable_exit_codes]
        @logger.warn ":accept_all_exit_codes & :acceptable_exit_codes set. :acceptable_exit_codes overrides, but they shouldn't both be set at once"
        options[:accept_all_exit_codes] = false
      end
      if !options[:accept_all_exit_codes] && !result.exit_code_in?(Array(options[:acceptable_exit_codes] || [0, nil]))
        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
  end
  result
end

#get_ipObject

Determine the ip address of this host



223
224
225
# File 'lib/beaker/host.rb', line 223

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).

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
# File 'lib/beaker/host.rb', line 185

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

Does this host have this key? Either as defined in the host itself, or globally?

Returns:

  • (Boolean)


133
134
135
# File 'lib/beaker/host.rb', line 133

def has_key? k
  host_hash.has_key?(k) || options.has_key?(k)
end

#hostnameObject

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.



153
154
155
# File 'lib/beaker/host.rb', line 153

def hostname
  host_hash['vmhostname'] || @name
end

#ipObject

Return the ip address of this host Always pull fresh, because this can sometimes change



229
230
231
# File 'lib/beaker/host.rb', line 229

def ip
  self['ip'] = get_ip
end

#is_cygwin?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/beaker/host.rb', line 165

def is_cygwin?
  self.class == Windows::Host
end

#is_pe?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/beaker/host.rb', line 161

def is_pe?
  self['type'] && self['type'].to_s =~ /pe/
end

#is_powershell?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/beaker/host.rb', line 169

def is_powershell?
  self.class == PSWindows::Host
end

#is_using_passenger?Boolean

True if this is a PE run, or if the host’s ‘passenger’ property has been set.

Returns:

  • (Boolean)


210
211
212
# File 'lib/beaker/host.rb', line 210

def is_using_passenger?
  is_pe? || self['passenger']
end

#is_x86_64?Boolean

Returns true if x86_64, false otherwise.

Returns:

  • (Boolean)

    true if x86_64, false otherwise



234
235
236
# File 'lib/beaker/host.rb', line 234

def is_x86_64?
  @x86_64 ||= determine_if_x86_64
end

#log_prefixObject



214
215
216
217
218
219
220
# File 'lib/beaker/host.rb', line 214

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

#node_nameObject



82
83
84
85
86
87
# File 'lib/beaker/host.rb', line 82

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_configprint['node_name_value'].strip
end

#pkg_initializeObject



77
78
79
80
# File 'lib/beaker/host.rb', line 77

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

#platformObject



173
174
175
# File 'lib/beaker/host.rb', line 173

def platform
  self['platform']
end

#port_open?(port) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
# File 'lib/beaker/host.rb', line 89

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_configprint(command = 'agent') ⇒ Object Also known as: puppet

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’)



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

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

#reachable_nameObject

Return the preferred method to reach the host, will use IP is available and then default to #hostname.



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

def reachable_name
  self['ip'] || hostname
end

#to_sObject

The #hostname of this host.



147
148
149
# File 'lib/beaker/host.rb', line 147

def to_s
  hostname
end

#to_strObject

The #hostname of this host.



142
143
144
# File 'lib/beaker/host.rb', line 142

def to_str
  hostname
end

#up?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/beaker/host.rb', line 100

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.

Returns:

  • (Boolean)


178
179
180
# File 'lib/beaker/host.rb', line 178

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.

Parameters:

  • puppetservice (String) (defaults to: 'apache2')

    Name of the service script that should be called to stop/startPuppet on this host. Defaults to ‘apache2’.



200
201
202
203
204
205
206
207
# File 'lib/beaker/host.rb', line 200

def uses_passenger!(puppetservice = 'apache2')
  if !is_pe?
    self['passenger'] = true
    self['puppetservice'] = puppetservice
    self['use-service'] = true
  end
  return true
end