Module: PSWindows::Exec
- Includes:
- Beaker::CommandFactory, Beaker::DSL::Wrappers
- Included in:
- Host
- Defined in:
- lib/beaker/host/pswindows/exec.rb
Constant Summary collapse
- ABS_CMD =
'c:\\\\windows\\\\system32\\\\cmd.exe'
- CMD =
'cmd.exe'
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
- #environment_string(env) ⇒ Object
-
#get_env_var(key, clean = false) ⇒ Object
Return the value of a specific env var.
- #get_ip ⇒ Object
-
#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
-
#ssh_permit_user_environment ⇒ Object
Overrides the Windows::Exec#ssh_permit_user_environment method, since no steps are needed in this setup to allow user ssh environments to work.
-
#ssh_set_user_environment(env) ⇒ Object
private
Sets the user SSH environment.
- #touch(file, abs = true) ⇒ Object
Methods included from Beaker::DSL::Wrappers
#cfacter, #encode_command, #facter, #hiera, #powershell, #puppet
Methods included from Beaker::CommandFactory
Instance Method Details
#add_env_var(key, val) ⇒ Object
Add the provided key/val to the current ssh environment
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/beaker/host/pswindows/exec.rb', line 96 def add_env_var key, val key = key.to_s.upcase #see if the key/value pair already exists cur_val = subbed_val = get_env_var(key, true) subbed_val = cur_val.gsub(/#{Regexp.escape(val.gsub(/'|"/, ''))}/, '') if cur_val.empty? exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', '#{val}', 'Machine')")) self.close #refresh the state elsif subbed_val == cur_val #not present, add it exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', '#{val};#{cur_val}', 'Machine')")) self.close #refresh the state end end |
#clear_env_var(key) ⇒ Object
Delete the environment variable from the current ssh environment
153 154 155 156 157 158 159 |
# File 'lib/beaker/host/pswindows/exec.rb', line 153 def clear_env_var key key = key.to_s.upcase exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', $null, 'Machine')")) exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', $null, 'User')")) exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', $null, 'Process')")) self.close #refresh the state end |
#delete_env_var(key, val) ⇒ Object
Delete the provided key/val from the current ssh environment
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/beaker/host/pswindows/exec.rb', line 115 def delete_env_var key, val key = key.to_s.upcase #get the current value of the key cur_val = subbed_val = get_env_var(key, true) subbed_val = (cur_val.split(';') - [val.gsub(/'|"/, '')]).join(';') if subbed_val != cur_val #remove the current key value self.clear_env_var(key) #set to the truncated value self.add_env_var(key, subbed_val) end end |
#echo(msg, abs = true) ⇒ Object
14 15 16 |
# File 'lib/beaker/host/pswindows/exec.rb', line 14 def echo(msg, abs=true) (abs ? ABS_CMD : CMD) + " /c echo #{msg}" end |
#environment_string(env) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/beaker/host/pswindows/exec.rb', line 161 def environment_string env return '' if env.empty? env_array = self.environment_variable_string_pair_array( env ) environment_string = '' env_array.each_with_index do |env| environment_string += "set #{env} && " end environment_string end |
#get_env_var(key, clean = false) ⇒ Object
Return the value of a specific env var
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/beaker/host/pswindows/exec.rb', line 133 def get_env_var key, clean = false self.close #refresh the state key = key.to_s.upcase val = exec(Beaker::Command.new("set #{key}"), :accept_all_exit_codes => true).stdout.chomp if val.empty? return '' else val = val.split(/\n/)[0] # only take the first result if clean val.gsub(/#{key}=/i,'') else val end end end |
#get_ip ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/beaker/host/pswindows/exec.rb', line 44 def get_ip # when querying for an IP this way the return value can be formatted like: # IPAddress= # IPAddress={"129.168.0.1"} # IPAddress={"192.168.0.1","2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001"} ips = execute("wmic nicconfig where ipenabled=true GET IPAddress /format:list") ip = '' ips.each_line do |line| matches = line.split('=') next if matches.length <= 1 matches = matches[1].match(/^{"(.*?)"/) next if matches.nil? || matches.captures.nil? || matches.captures.empty? ip = matches.captures[0] if matches && matches.captures break if ip != '' end ip end |
#mkdir_p(dir) ⇒ Boolean
Create the provided directory structure on the host
84 85 86 87 88 89 |
# File 'lib/beaker/host/pswindows/exec.rb', line 84 def mkdir_p dir windows_dirstring = dir.gsub('/','\\') cmd = "if not exist #{windows_dirstring} (md #{windows_dirstring})" 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.
32 33 34 35 36 37 38 |
# File 'lib/beaker/host/pswindows/exec.rb', line 32 def mv(orig, dest, rm=true) # ensure that we have the right slashes for windows orig = orig.gsub(/\//,'\\') dest = dest.gsub(/\//,'\\') rm_rf dest unless !rm execute("move /y #{orig} #{dest}") end |
#path ⇒ Object
40 41 42 |
# File 'lib/beaker/host/pswindows/exec.rb', line 40 def path 'c:/windows/system32;c:/windows' end |
#ping(target, attempts = 5) ⇒ Boolean
Attempt to ping the provided target hostname
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/beaker/host/pswindows/exec.rb', line 69 def ping target, attempts=5 try = 0 while try < attempts do result = exec(Beaker::Command.new("ping -n 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
5 6 7 8 9 |
# File 'lib/beaker/host/pswindows/exec.rb', line 5 def reboot exec(Beaker::Command.new("shutdown /r /t 0"), :expect_connection_failure => true) # rebooting on windows is slooooow sleep(40) end |
#rm_rf(path) ⇒ Object
22 23 24 25 26 |
# File 'lib/beaker/host/pswindows/exec.rb', line 22 def rm_rf path # ensure that we have the right slashes for windows path = path.gsub(/\//, '\\') execute("del /s /q #{path}") end |
#ssh_permit_user_environment ⇒ Object
Overrides the Windows::Exec#ssh_permit_user_environment method, since no steps are needed in this setup to allow user ssh environments to work.
175 176 |
# File 'lib/beaker/host/pswindows/exec.rb', line 175 def ssh_permit_user_environment 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.
this class doesn’t manipulate an SSH environment file, it just sets
Sets the user SSH environment.
the environment variables on the system.
189 190 191 192 193 194 |
# File 'lib/beaker/host/pswindows/exec.rb', line 189 def ssh_set_user_environment(env) #add the env var set to this test host env.each_pair do |var, value| add_env_var(var, value) end end |