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

#assertions

Instance Method Summary collapse

Methods included from Beaker::DSL::Wrappers

#cfacter, #facter, #hiera, #powershell, #puppet

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



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/beaker/host/pswindows/exec.rb', line 79

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

Examples:

host.clear_env_var('PATH')

Parameters:

  • key (String)

    The key to delete



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

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

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



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/beaker/host/pswindows/exec.rb', line 98

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



12
13
14
# File 'lib/beaker/host/pswindows/exec.rb', line 12

def echo(msg, abs=true)
  (abs ? ABS_CMD : CMD) + " /c echo #{msg}"
end

#get_env_var(key, clean = false) ⇒ Object

Return the value of a specific env var

Examples:

host.get_env_var('path')

Parameters:

  • key (String)

    The key to look for

  • clean (Boolean) (defaults to: false)

    Remove the ‘KEY=’ and only return the value of the env var



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/beaker/host/pswindows/exec.rb', line 116

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
    if clean
      val.gsub(/#{key}=/,'')
    else
      val
    end
  end
end

#get_ipObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/beaker/host/pswindows/exec.rb', line 37

def get_ip
  ip = execute("for /f \"tokens=14\" %f in ('ipconfig ^| find \"IP Address\"') do @echo %f", :accept_all_exit_codes => true).strip
  if ip == ''
    ip = execute("for /f \"tokens=14\" %f in ('ipconfig ^| find \"IPv4 Address\"') do @echo %f", :accept_all_exit_codes => true).strip
  end
  if ip == ''
    ip = execute("for /f \"tokens=14\" %f in ('ipconfig ^| find \"IPv6 Address\"') do @echo %f").strip
  end
  ip
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



67
68
69
70
71
72
# File 'lib/beaker/host/pswindows/exec.rb', line 67

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.

Parameters:

  • orig (String)

    The origin path

  • dest (String)

    the destination path

  • rm (Boolean) (defaults to: true)

    Remove the destination prior to move



28
29
30
31
# File 'lib/beaker/host/pswindows/exec.rb', line 28

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

#pathObject



33
34
35
# File 'lib/beaker/host/pswindows/exec.rb', line 33

def path
  'c:/windows/system32;c:/windows'
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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/beaker/host/pswindows/exec.rb', line 52

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

#rebootObject



5
6
7
# File 'lib/beaker/host/pswindows/exec.rb', line 5

def reboot
  exec(Beaker::Command.new("shutdown /r /t 0"), :expect_connection_failure => true)
end

#rm_rf(path) ⇒ Object



20
21
22
# File 'lib/beaker/host/pswindows/exec.rb', line 20

def rm_rf path
  execute("del /s /q #{path}")
end

#touch(file, abs = true) ⇒ Object



16
17
18
# File 'lib/beaker/host/pswindows/exec.rb', line 16

def touch(file, abs=true)
  (abs ? ABS_CMD : CMD) + " /c echo. 2> #{file}"
end