Module: Kitchen::Driver::PowerShellScripts

Included in:
Hyperv
Defined in:
lib/kitchen/driver/powershell.rb

Instance Method Summary collapse

Instance Method Details

#delete_vm_psObject



106
107
108
109
110
111
112
113
# File 'lib/kitchen/driver/powershell.rb', line 106

def delete_vm_ps
  "\n$null = Get-VM -ID \"\#{@state[:id]}\" |\nStop-VM -Force -TurnOff -PassThru |\nRemove-VM -Force\n"
end

#encode_command(script) ⇒ Object



25
26
27
28
# File 'lib/kitchen/driver/powershell.rb', line 25

def encode_command(script)
  encoded_script = script.encode('UTF-16LE', 'UTF-8')
  Base64.strict_encode64(encoded_script)
end

#ensure_vm_running_psObject



74
75
76
77
78
79
# File 'lib/kitchen/driver/powershell.rb', line 74

def ensure_vm_running_ps
  "\nAssert-VmRunning -ID \"\#{@state[:id]}\" | ConvertTo-Json\n"
end

#execute_command(cmd, options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



57
58
59
60
61
62
63
64
# File 'lib/kitchen/driver/powershell.rb', line 57

def execute_command(cmd, options = {})
  debug("#Local Command BEGIN (#{cmd})")
  sh = Mixlib::ShellOut.new(cmd, options)
  sh.run_command
  debug("Local Command END #{Util.duration(sh.execution_time)}")
  fail "Failed: #{sh.stderr}" if sh.error?
  JSON.parse(sh.stdout) if sh.stdout.length > 2
end

#new_differencing_disk_psObject

rubocop:enable Metrics/AbcSize



67
68
69
70
71
72
# File 'lib/kitchen/driver/powershell.rb', line 67

def new_differencing_disk_ps
  "\nNew-DifferencingDisk -Path \"\#{differencing_disk_path}\" -ParentPath \"\#{parent_vhd_path}\"\n"
end

#new_vm_psObject

rubocop:disable Metrics/MethodLength



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kitchen/driver/powershell.rb', line 82

def new_vm_ps
  "\n$NewVMParams = @{\nGeneration = 1\nMemoryStartupBytes = \#{config[:memory_startup_bytes]}\nName = \"\#{instance.name}\"\nPath = \"\#{kitchen_vm_path}\"\nVHDPath = \"\#{differencing_disk_path}\"\nSwitchName = \"\#{config[:vm_switch]}\"\nProcessorCount = \#{config[:processor_count]}\n}\nNew-KitchenVM @NewVMParams | ConvertTo-Json\n"
end

#powershell_64_bitObject



30
31
32
# File 'lib/kitchen/driver/powershell.rb', line 30

def powershell_64_bit
  'c:\windows\sysnative\windowspowershell\v1.0\powershell.exe'
end

#run_ps(cmd, options = {}) ⇒ 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.

Convenience method to run a powershell command locally.

Parameters:

  • cmd (String)

    command to run locally

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

    options hash

See Also:

  • ShellOut.run_command


48
49
50
51
52
53
54
# File 'lib/kitchen/driver/powershell.rb', line 48

def run_ps(cmd, options = {})
  cmd = "echo #{cmd}" if config[:dry_run]
  debug('Preparing to run: ')
  debug("  #{cmd}")
  wrapped_command = wrap_command cmd
  execute_command wrapped_command, options
end

#set_vm_ipaddress_psObject



115
116
117
118
119
120
121
122
# File 'lib/kitchen/driver/powershell.rb', line 115

def set_vm_ipaddress_ps
  "\n(Get-VM -id \"\#{@state[:id]}\").NetworkAdapters |\nSet-VMNetworkConfiguration -ipaddress \"\#{config[:ip_address]}\" -subnet 255.255.255.0 |\nConvertTo-Json\n"
end

#vm_default_switch_psObject



124
125
126
127
128
# File 'lib/kitchen/driver/powershell.rb', line 124

def vm_default_switch_ps
  "Get-DefaultVMSwitch | ConvertTo-Json\n"
end

#vm_details_psObject

rubocop:enable Metrics/MethodLength



99
100
101
102
103
104
# File 'lib/kitchen/driver/powershell.rb', line 99

def vm_details_ps
  "\nGet-VmDetail -id \"\#{@state[:id]}\" | ConvertTo-Json\n"
end

#wrap_command(script) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/kitchen/driver/powershell.rb', line 34

def wrap_command(script)
  base_script_path = File.join(File.dirname(__FILE__), '/../../../support/hyperv.ps1')
  debug("Loading functions from #{base_script_path}")
  new_script = ". #{base_script_path}; " << script
  debug("Wrapped script: #{new_script}")
  "#{powershell_64_bit} -encodedcommand #{encode_command new_script} -outputformat Text"
end