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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/beaker/host/unix/exec.rb', line 101

def add_env_var key, val
  key = key.to_s.upcase
  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

#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



152
153
154
155
156
157
158
159
160
# File 'lib/beaker/host/unix/exec.rb', line 152

def clear_env_var key
  key = key.to_s.upcase
  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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/beaker/host/unix/exec.rb', line 124

def delete_env_var key, val
  key = key.to_s.upcase
  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



13
14
15
# File 'lib/beaker/host/unix/exec.rb', line 13

def echo(msg, abs=true)
  (abs ? '/bin/echo' : 'echo') + " #{msg}"
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



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

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

#get_ipObject



25
26
27
28
29
30
31
# File 'lib/beaker/host/unix/exec.rb', line 25

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 | 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 and debian based hosts.

Parameters:

  • env_file (String)

    The ssh environment file to read from



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/beaker/host/unix/exec.rb', line 76

def mirror_env_to_profile_d env_file
  if self[:platform] =~ /sles-|debian/
    @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/debian 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



36
37
38
39
40
# File 'lib/beaker/host/unix/exec.rb', line 36

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



52
53
54
55
# File 'lib/beaker/host/unix/exec.rb', line 52

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

#pathObject



21
22
23
# File 'lib/beaker/host/unix/exec.rb', line 21

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



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/beaker/host/unix/exec.rb', line 61

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

#rebootObject



4
5
6
7
8
9
10
11
# File 'lib/beaker/host/unix/exec.rb', line 4

def reboot
  if self['platform'] =~ /solaris/
    exec(Beaker::Command.new("reboot"), :expect_connection_failure => true)
  else
    exec(Beaker::Command.new("/sbin/shutdown -r now"), :expect_connection_failure => true)
  end
  sleep(10) #if we attempt a reconnect too quickly we end up blocking ¯\_(ツ)_/¯
end

#rm_rf(path) ⇒ Object

Recursively remove the path provided

Parameters:

  • path (String)

    The path to remove



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

def rm_rf path
  execute("rm -rf #{path}")
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:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/beaker/host/unix/exec.rb', line 187

def ssh_permit_user_environment
  case self['platform']
  when /debian|ubuntu|cumulus/
    exec(Beaker::Command.new("echo '\nPermitUserEnvironment yes' >> /etc/ssh/sshd_config"))
  when /el-7|centos-7|redhat-7|oracle-7|scientific-7|eos-7/
    exec(Beaker::Command.new("echo '\nPermitUserEnvironment yes' >> /etc/ssh/sshd_config"))
  when /el-|centos|fedora|redhat|oracle|scientific|eos/
    exec(Beaker::Command.new("echo '\nPermitUserEnvironment yes' >> /etc/ssh/sshd_config"))
  when /sles/
    exec(Beaker::Command.new("echo '\nPermitUserEnvironment yes' >> /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
    exec(Beaker::Command.new("echo '\nPermitUserEnvironment yes' >> /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} )
  end

  ssh_service_restart()
end

#ssh_service_restartResult

Restarts the SSH service.

Returns:

  • (Result)

    result of restarting the SSH service



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/beaker/host/unix/exec.rb', line 165

def ssh_service_restart
  case self['platform']
  when /debian|ubuntu|cumulus/
    exec(Beaker::Command.new("service ssh restart"))
  when /el-7|centos-7|redhat-7|oracle-7|scientific-7|eos-7/
    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("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"))
  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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/beaker/host/unix/exec.rb', line 216

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



17
18
19
# File 'lib/beaker/host/unix/exec.rb', line 17

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