Module: Unix::Exec
Instance Attribute Summary
Attributes included from Beaker::CommandFactory
Instance Method Summary collapse
-
#add_env_var(key, val) ⇒ Object
Add the provided key/val to the current ssh environment.
-
#clear_env_var(key) ⇒ Object
Delete the environment variable from the current ssh environment.
-
#delete_env_var(key, val) ⇒ Object
Delete the provided key/val from the current ssh environment.
- #echo(msg, abs = true) ⇒ Object
-
#get_env_var(key) ⇒ Object
Return the value of a specific env var.
- #get_ip ⇒ Object
-
#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.
-
#mkdir_p(dir) ⇒ Boolean
Create the provided directory structure on the host.
-
#mv(orig, dest, rm = true) ⇒ Object
Move the origin to destination.
- #path ⇒ Object
-
#ping(target, attempts = 5) ⇒ Boolean
Attempt to ping the provided target hostname.
- #reboot ⇒ Object
-
#rm_rf(path) ⇒ Object
Recursively remove the path provided.
- #touch(file, abs = true) ⇒ Object
Methods included from Beaker::CommandFactory
Instance Method Details
#add_env_var(key, val) ⇒ Object
Add the provided key/val to the current ssh environment
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/beaker/host/unix/exec.rb', line 100 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
151 152 153 154 155 156 157 158 159 |
# File 'lib/beaker/host/unix/exec.rb', line 151 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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/beaker/host/unix/exec.rb', line 123 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
12 13 14 |
# File 'lib/beaker/host/unix/exec.rb', line 12 def echo(msg, abs=true) (abs ? '/bin/echo' : 'echo') + " #{msg}" end |
#get_env_var(key) ⇒ Object
Return the value of a specific env var
142 143 144 145 |
# File 'lib/beaker/host/unix/exec.rb', line 142 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_ip ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/beaker/host/unix/exec.rb', line 24 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/beaker/host/unix/exec.rb', line 75 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
35 36 37 38 39 |
# File 'lib/beaker/host/unix/exec.rb', line 35 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.
51 52 53 54 |
# File 'lib/beaker/host/unix/exec.rb', line 51 def mv orig, dest, rm=true rm_rf dest unless !rm execute("mv #{orig} #{dest}") end |
#path ⇒ Object
20 21 22 |
# File 'lib/beaker/host/unix/exec.rb', line 20 def path '/bin:/usr/bin' end |
#ping(target, attempts = 5) ⇒ Boolean
Attempt to ping the provided target hostname
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/beaker/host/unix/exec.rb', line 60 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 |
#reboot ⇒ Object
4 5 6 7 8 9 10 |
# 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 end |
#rm_rf(path) ⇒ Object
Recursively remove the path provided
43 44 45 |
# File 'lib/beaker/host/unix/exec.rb', line 43 def rm_rf path execute("rm -rf #{path}") end |
#touch(file, abs = true) ⇒ Object
16 17 18 |
# File 'lib/beaker/host/unix/exec.rb', line 16 def touch(file, abs=true) (abs ? '/bin/touch' : 'touch') + " #{file}" end |