Module: Unix::Exec

Includes:
Beaker::CommandFactory
Included in:
Host
Defined in:
lib/beaker/host/unix/exec.rb

Instance Attribute Summary

Attributes included from Beaker::CommandFactory

#assertions

Instance Method Summary collapse

Methods included from Beaker::CommandFactory

#execute, #fail_test

Instance Method Details

#add_env_var(key, val) ⇒ Object

Add the provided key/val to the current ssh environment

Examples:

host.add_env_var('PATH', '/usr/bin:PATH')

Parameters:

  • key (String)

    The key to add the value to

  • val (String)

    The value for the key



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/beaker/host/unix/exec.rb', line 200

def add_env_var key, val
  key = key.to_s
  env_file = self[:ssh_env_file]
  escaped_val = Regexp.escape(val).gsub('/', '\/').gsub(';', '\;')
  #see if the key/value pair already exists
  if exec(Beaker::Command.new("grep ^#{key}=.*#{escaped_val} #{env_file}"), :accept_all_exit_codes => true ).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}"), :accept_all_exit_codes => true ).exit_code == 0
    exec(Beaker::SedCommand.new(self['platform'], "s/^#{key}=/#{key}=#{escaped_val}:/", env_file))
  else
    exec(Beaker::Command.new("echo \"#{key}=#{val}\" >> #{env_file}"))
  end
  #update the profile.d to current state
  #match it to the contents of ssh_env_file
  mirror_env_to_profile_d(env_file)
end

#append_commands(command = '', user_ac = '', opts = {}) ⇒ String

Gets the specific append commands as needed for this host

Parameters:

  • command (String) (defaults to: '')

    Command to be executed

  • user_ac (String) (defaults to: '')

    List of user-specified commands to append

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

    optional parameters

Returns:

  • (String)

    Command string as needed for this host



380
381
382
# File 'lib/beaker/host/unix/exec.rb', line 380

def append_commands(command = '', user_ac = '', opts = {})
  user_ac
end

#clear_env_var(key) ⇒ Object

Delete the environment variable from the current ssh environment

Examples:

host.clear_env_var('PATH')

Parameters:

  • key (String)

    The key to delete



251
252
253
254
255
256
257
258
259
# File 'lib/beaker/host/unix/exec.rb', line 251

def clear_env_var key
  key = key.to_s
  env_file = self[:ssh_env_file]
  #remove entire line
  exec(Beaker::SedCommand.new(self['platform'], "/^#{key}=.*$/d", env_file))
  #update the profile.d to current state
  #match it to the contents of ssh_env_file
  mirror_env_to_profile_d(env_file)
end

#delete_env_var(key, val) ⇒ Object

Delete the provided key/val from the current ssh environment

Examples:

host.delete_env_var('PATH', '/usr/bin:PATH')

Parameters:

  • key (String)

    The key to delete the value from

  • val (String)

    The value to delete for the key



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/beaker/host/unix/exec.rb', line 223

def delete_env_var key, val
  key = key.to_s
  env_file = self[:ssh_env_file]
  val = Regexp.escape(val).gsub('/', '\/').gsub(';', '\;')
  #if the key only has that single value remove the entire line
  exec(Beaker::SedCommand.new(self['platform'], "/#{key}=#{val}$/d", env_file))
  #value in middle of list
  exec(Beaker::SedCommand.new(self['platform'], "s/#{key}=\\(.*\\)[;:]#{val}/#{key}=\\1/", env_file))
  #value in start of list
  exec(Beaker::SedCommand.new(self['platform'], "s/#{key}=#{val}[;:]/#{key}=/", env_file))
  #update the profile.d to current state
  #match it to the contents of ssh_env_file
  mirror_env_to_profile_d(env_file)
end

#echo(msg, abs = true) ⇒ Object



102
103
104
# File 'lib/beaker/host/unix/exec.rb', line 102

def echo(msg, abs=true)
  (abs ? '/bin/echo' : 'echo') + " #{msg}"
end

#enable_remote_rsyslog(server = 'rsyslog.ops.puppetlabs.net', port = 514) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/beaker/host/unix/exec.rb', line 423

def enable_remote_rsyslog(server = 'rsyslog.ops.puppetlabs.net', port = 514)
  if self['platform'] !~ /ubuntu/
    @logger.warn "Enabling rsyslog is only implemented for ubuntu hosts"
    return
  end
  commands = [
    "echo '*.* @#{server}:#{port}' >> /etc/rsyslog.d/51-sendrsyslogs.conf",
    'systemctl restart rsyslog'
  ]
  commands.each do |command|
    exec(Beaker::Command.new(command))
  end
  true
end

#environment_string(env) ⇒ String

Construct the environment string for this command

Parameters:

  • env (Hash{String=>String})

    An optional Hash containing key-value pairs to be treated as environment variables that should be set for the duration of the puppet command.

Returns:

  • (String)

    Returns a string containing command line arguments that will ensure the environment is correctly set for the given host.



333
334
335
336
337
338
# File 'lib/beaker/host/unix/exec.rb', line 333

def environment_string env
  return '' if env.empty?
  env_array = self.environment_variable_string_pair_array( env )
  environment_string = env_array.join(' ')
  "env #{environment_string}"
end

#environment_variable_string_pair_array(env) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/beaker/host/unix/exec.rb', line 340

def environment_variable_string_pair_array env
  env_array = []
  env.each_key do |key|
    val = env[key]
    if val.is_a?(Array)
      val = val.join(':')
    else
      val = val.to_s
    end
    # doing this for the key itself & the upcase'd version allows us to remain
    # backwards compatible
    # TODO: (Next Major Version) get rid of upcase'd version
    key_str = key.to_s
    keys = [key_str]
    keys << key_str.upcase if key_str.upcase != key_str
    keys.each do |env_key|
      env_array << "#{env_key}=\"#{val}\""
    end
  end
  env_array
end

#get_env_var(key) ⇒ Object

Return the value of a specific env var

Examples:

host.get_env_var('path')

Parameters:

  • key (String)

    The key to look for



242
243
244
245
# File 'lib/beaker/host/unix/exec.rb', line 242

def get_env_var key
  key = key.to_s
  exec(Beaker::Command.new("env | grep ^#{key}="), :accept_all_exit_codes => true).stdout.chomp
end

#get_ipObject



124
125
126
127
128
129
130
# File 'lib/beaker/host/unix/exec.rb', line 124

def get_ip
  if self['platform'].include?('solaris') || self['platform'].include?('osx')
    execute("ifconfig -a inet| awk '/broadcast/ {print $2}' | cut -d/ -f1 | head -1").strip
  else
    execute("ip a | awk '/global/{print$2}' | cut -d/ -f1 | #{self['hypervisor'] == 'vagrant' ? 'tail' : 'head'} -1").strip
  end
end

#mirror_env_to_profile_d(env_file) ⇒ Object

Converts the provided environment file to a new shell script in /etc/profile.d, then sources that file. This is for sles based hosts.

Parameters:

  • env_file (String)

    The ssh environment file to read from



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/beaker/host/unix/exec.rb', line 175

def mirror_env_to_profile_d env_file
  if self[:platform] =~ /sles-/
    @logger.debug("mirroring environment to /etc/profile.d on sles platform host")
    cur_env = exec(Beaker::Command.new("cat #{env_file}")).stdout
    shell_env = ''
    cur_env.each_line do |env_line|
      shell_env << "export #{env_line}"
    end
    #here doc it over
    exec(Beaker::Command.new("cat << EOF > #{self[:profile_d_env_file]}\n#{shell_env}EOF"))
    #set permissions
    exec(Beaker::Command.new("chmod +x #{self[:profile_d_env_file]}"))
    #keep it current
    exec(Beaker::Command.new("source #{self[:profile_d_env_file]}"))
  else
    #noop
    @logger.debug("will not mirror environment to /etc/profile.d on non-sles platform host")
  end
end

#mkdir_p(dir) ⇒ Boolean

Create the provided directory structure on the host

Parameters:

  • dir (String)

    The directory structure to create on the host

Returns:

  • (Boolean)

    True, if directory construction succeeded, otherwise False



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

def mkdir_p dir
  cmd = "mkdir -p #{dir}"
  result = exec(Beaker::Command.new(cmd), :acceptable_exit_codes => [0, 1])
  result.exit_code == 0
end

#modified_at(file, timestamp = nil) ⇒ Object

Update ModifiedDate on a file

Parameters:

  • file (String)

    Path to the file

  • timestamp (String) (defaults to: nil)

    Timestamp to set



113
114
115
116
117
118
# File 'lib/beaker/host/unix/exec.rb', line 113

def modified_at(file, timestamp = nil)
  require 'date'
  time = timestamp ? DateTime.parse("#{timestamp}") : DateTime.now
  timestamp = time.strftime('%Y%m%d%H%M')
  execute("/bin/touch -mt #{timestamp} #{file}")
end

#mv(orig, dest, rm = true) ⇒ Object

Move the origin to destination. The destination is removed prior to moving.

Parameters:

  • orig (String)

    The origin path

  • dest (String)

    the destination path

  • rm (Boolean) (defaults to: true)

    Remove the destination prior to move



151
152
153
154
# File 'lib/beaker/host/unix/exec.rb', line 151

def mv orig, dest, rm=true
  rm_rf dest unless !rm
  execute("mv #{orig} #{dest}")
end

#parse_uptime(uptime) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/beaker/host/unix/exec.rb', line 87

def parse_uptime(uptime)
  # get String from up to users
  # eg 19:52  up 14 mins, 2 users, load averages: 2.95 4.19 4.31
  # 8:03 up 52 days, 20:47, 3 users, load averages: 1.36 1.42 1.40
  # 22:19 up 54 days, 1 min, 4 users, load averages: 2.08 2.06 2.27
  regexp = /.*up (.*)[[:space:]]+[[:digit:]]+ user.*/
  result = uptime.match regexp
  if self['platform'] =~ /solaris-/ && result[1].empty?
    return "0 min"
  end
  raise "Couldn't parse uptime: #{uptime}" if result.nil?

  result[1].strip.chomp(",")
end

#pathObject



120
121
122
# File 'lib/beaker/host/unix/exec.rb', line 120

def path
  '/bin:/usr/bin'
end

#ping(target, attempts = 5) ⇒ Boolean

Attempt to ping the provided target hostname

Parameters:

  • target (String)

    The hostname to ping

  • attempts (Integer) (defaults to: 5)

    Amount of times to attempt ping before giving up

Returns:

  • (Boolean)

    true of ping successful, overwise false



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/beaker/host/unix/exec.rb', line 160

def ping target, attempts=5
  try = 0
  while try < attempts do
    result = exec(Beaker::Command.new("ping -c 1 #{target}"), :accept_all_exit_codes => true)
    if result.exit_code == 0
      return true
    end
    try+=1
  end
  result.exit_code == 0
end

#prepend_commands(command = '', user_pc = '', opts = {}) ⇒ String

Gets the specific prepend commands as needed for this host

Parameters:

  • command (String) (defaults to: '')

    Command to be executed

  • user_pc (String) (defaults to: '')

    List of user-specified commands to prepend

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

    optional parameters

Returns:

  • (String)

    Command string as needed for this host



369
370
371
# File 'lib/beaker/host/unix/exec.rb', line 369

def prepend_commands(command = '', user_pc = '', opts = {})
  user_pc
end

#reboot(wait_time = 10, max_connection_tries = 9, uptime_retries = 18) ⇒ Object

Reboots the host, comparing uptime values to verify success Will throw an exception RebootFailure if it fails

Parameters:

  • wait_time (Integer) (defaults to: 10)

    How long to wait after sending the reboot command before attempting to check in on the host

  • max_connection_tries (Integer) (defaults to: 9)

    How many times to retry connecting to host after reboot. Note that there is an fibbonacci backoff when attempting retries so the time spent waiting on this can grow quickly.

  • uptime_retries (Integer) (defaults to: 18)

    How many times to check to see if the value of the uptime has reset.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/beaker/host/unix/exec.rb', line 14

def reboot(wait_time=10, max_connection_tries=9, uptime_retries=18)
  require 'time'

  begin
    original_boot_time_str = exec(Beaker::Command.new('who -b'), {:max_connection_tries => max_connection_tries, :silent => true}).stdout
    original_boot_time_line = original_boot_time_str.lines.grep(/boot/).first

    raise Beaker::Host::RebootFailure, "Could not find system boot time using 'who -b': #{original_boot_time_str}" unless original_boot_time_line

    original_boot_time = Time.parse(original_boot_time_line)

    exec(Beaker::Command.new('/bin/systemctl reboot -i || reboot || /sbin/shutdown -r now'), :expect_connection_failure => true)
  rescue Beaker::Host::CommandFailure => e
    raise Beaker::Host::RebootFailure, "Command failed when attempting to reboot: #{e.message}"
  rescue RuntimeError => e
    raise Beaker::Host::RebootFailure, "Unexpected exception in reboot: #{e.message}"
  rescue ArgumentError => e
    raise Beaker::Host::RebootFailure, "Unable to parse time: #{e.message}"
  end

  attempts = 0
  begin
    # give the host a little time to shutdown
    @logger.debug("Waiting #{wait_time} for host to shut down.")
    sleep wait_time

    current_boot_time_str = exec(Beaker::Command.new('who -b'), {:max_connection_tries => max_connection_tries, :silent => true}).stdout
    current_boot_time = Time.parse(current_boot_time_str.lines.grep(/boot/).first)

    @logger.debug("Original Boot Time: #{original_boot_time}")
    @logger.debug("Current Boot Time: #{current_boot_time}")

    unless current_boot_time > original_boot_time
      raise Beaker::Host::RebootFailure, "Boot time did not reset. Reboot appears to have failed."
    end
  rescue Beaker::Host::RebootFailure => e
    attempts += 1
    if attempts < uptime_retries
      @logger.debug("Boot time did not reset. Will retry #{uptime_retries - attempts} more times.")
      retry
    else
      raise
    end
  rescue Beaker::Host::CommandFailure => e
    raise Beaker::Host::RebootFailure, "Command failed when attempting to reboot: #{e.message}"
  rescue RuntimeError => e
    raise Beaker::Host::RebootFailure, "Unexpected exception in reboot: #{e.message}"
  rescue ArgumentError => e
    raise Beaker::Host::RebootFailure, "Unable to parse time: #{e.message}"
  end
end

#rm_rf(path) ⇒ Object

Recursively remove the path provided

Parameters:

  • path (String)

    The path to remove



143
144
145
# File 'lib/beaker/host/unix/exec.rb', line 143

def rm_rf path
  execute("rm -rf #{path}")
end

#selinux_enabled?Boolean

 Checks if selinux is enabled

Returns:

  • (Boolean)

    true if selinux is enabled, false otherwise



419
420
421
# File 'lib/beaker/host/unix/exec.rb', line 419

def selinux_enabled?()
  exec(Beaker::Command.new("sudo selinuxenabled"), :accept_all_exit_codes => true).exit_code == 0
end

#ssh_permit_user_environmentResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the PermitUserEnvironment setting & restarts the SSH service.

Returns:



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
# File 'lib/beaker/host/unix/exec.rb', line 288

def ssh_permit_user_environment
  case self['platform']
  when /debian|ubuntu|cumulus|huaweios|archlinux/
    directory = tmpdir()
    exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
    exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
    exec(Beaker::Command.new("echo '' >/etc/environment")) if self['platform'] =~ /ubuntu-20.04/
  when /el-7|centos-7|redhat-7|oracle-7|scientific-7|eos-7|el-8|centos-8|redhat-8|oracle-8/
    directory = tmpdir()
    exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
    exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
  when /el-|centos|fedora|redhat|oracle|scientific|eos/
    directory = tmpdir()
    exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
    exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
  when /sles/
    directory = tmpdir()
    exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
    exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
  when /solaris/
    # kept solaris here because refactoring it into its own Host module
    # conflicts with the solaris hypervisor that already exists
    directory = tmpdir()
    exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
    exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
  when /(free|open)bsd/
    exec(Beaker::Command.new("sudo perl -pi -e 's/^#?PermitUserEnvironment no/PermitUserEnvironment yes/' /etc/ssh/sshd_config"), {:pty => true} )
  else
    raise ArgumentError, "Unsupported Platform: '#{self['platform']}'"
  end

  ssh_service_restart()
end

#ssh_service_restartResult

Restarts the SSH service.

Returns:

  • (Result)

    result of restarting the SSH service



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/beaker/host/unix/exec.rb', line 264

def ssh_service_restart
  case self['platform']
  when /debian|ubuntu|cumulus|huaweios/
    exec(Beaker::Command.new("service ssh restart"))
  when /el-7|centos-7|redhat-7|oracle-7|scientific-7|eos-7|el-8|centos-8|redhat-8|oracle-8|fedora-(1[4-9]|2[0-9]|3[0-9])|archlinux-/
    exec(Beaker::Command.new("systemctl restart sshd.service"))
  when /el-|centos|fedora|redhat|oracle|scientific|eos/
    exec(Beaker::Command.new("/sbin/service sshd restart"))
  when /sles/
    exec(Beaker::Command.new("/usr/sbin/rcsshd restart"))
  when /solaris/
    exec(Beaker::Command.new("svcadm restart svc:/network/ssh:default"))
  when /(free|open)bsd/
    exec(Beaker::Command.new("sudo /etc/rc.d/sshd restart"))
  else
    raise ArgumentError, "Unsupported Platform: '#{self['platform']}'"
  end
end

#ssh_set_user_environment(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fills the user SSH environment file.

Parameters:

  • env (Hash{String=>String})

    Environment variables to set on the system, in the form of a hash of String variable names to their corresponding String values.

Returns:

  • nil



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/beaker/host/unix/exec.rb', line 392

def ssh_set_user_environment(env)
  #ensure that ~/.ssh/environment exists
  ssh_env_file_dir = Pathname.new(self[:ssh_env_file]).dirname
  mkdir_p(ssh_env_file_dir)
  exec(Beaker::Command.new("chmod 0600 #{ssh_env_file_dir}"))
  exec(Beaker::Command.new("touch #{self[:ssh_env_file]}"))
  #add the constructed env vars to this host
  add_env_var('PATH', '$PATH')
  # FIXME
  if self['platform'] =~ /openbsd-(\d)\.?(\d)-(.+)/
    version = "#{$1}.#{$2}"
    arch = $3
    arch = 'amd64' if ['x64', 'x86_64'].include?(arch)
    add_env_var('PKG_PATH', "http://ftp.openbsd.org/pub/OpenBSD/#{version}/packages/#{arch}/")
  elsif self['platform'] =~ /solaris-10/
    add_env_var('PATH', '/opt/csw/bin')
  end

  #add the env var set to this test host
  env.each_pair do |var, value|
    add_env_var(var, value)
  end
end

#touch(file, abs = true) ⇒ Object



106
107
108
# File 'lib/beaker/host/unix/exec.rb', line 106

def touch(file, abs=true)
  (abs ? '/bin/touch' : 'touch') + " #{file}"
end

#uptime_int(uptime_str) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/beaker/host/unix/exec.rb', line 66

def uptime_int(uptime_str)
  time_array = uptime_str.split(", ")
  accumulated_mins = 0
  time_array.each do |time_segment|
    value, unit = time_segment.split
    if unit.nil?
      # 20:47 case: hours & mins
      hours, mins = value.split(":")
      accumulated_mins += (hours.to_i * 60 + mins.to_i)
    elsif unit =~ /day(s)?/
      accumulated_mins += (value.to_i * 1440) # 60 * 24 = 1440
    elsif unit =~ /min(s)?/
      accumulated_mins += value.to_i
    else
      raise ArgumentError, "can't parse uptime segment: #{time_segment}"
    end
  end

  accumulated_mins
end