Module: Kitchen::Driver::PowerShellScripts
- Included in:
- Hyperv
- Defined in:
- lib/kitchen/driver/powershell.rb
Instance Method Summary collapse
- #copy_vm_file_ps(source, dest) ⇒ Object
- #delete_vm_ps ⇒ Object
- #encode_command(script) ⇒ Object
- #ensure_vm_running_ps ⇒ Object
-
#execute_command(cmd, options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #is_64bit? ⇒ Boolean
- #mount_vm_iso ⇒ Object
-
#new_differencing_disk_ps ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#new_vm_ps ⇒ Object
rubocop:disable Metrics/MethodLength.
- #powershell_64_bit ⇒ Object
-
#run_ps(cmd, options = {}) ⇒ Object
private
Convenience method to run a powershell command locally.
- #set_vm_ipaddress_ps ⇒ Object
- #vm_default_switch_ps ⇒ Object
-
#vm_details_ps ⇒ Object
rubocop:enable Metrics/MethodLength.
- #wrap_command(script) ⇒ Object
Instance Method Details
#copy_vm_file_ps(source, dest) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/kitchen/driver/powershell.rb', line 148 def copy_vm_file_ps(source, dest) <<-FILECOPY Function CopyFile ($VM, [string]$SourcePath, [string]$DestPath) { #Write-Host "Copying file to VM - Source: $SourcePath Destination $DestPath" $VM | Copy-VMFile -SourcePath $SourcePath -DestinationPath $DestPath -CreateFullPath -FileSource Host -Force } $sourceLocation = '#{source}' $destinationLocation = '#{dest}' $vmId = '#{@state[:id]}' If (Test-Path $sourceLocation) { $vm = Get-VM -ID $vmId $service = 'Guest Service Interface' If ((Get-VMIntegrationService -Name $service -VM $vm).Enabled -ne $true) { Enable-VMIntegrationService -Name $service -VM $vm Start-Sleep -Seconds 3 } If ((Get-Item $sourceLocation) -is [System.IO.DirectoryInfo]) { ForEach ($item in (Get-ChildItem -Path $sourceLocation -File)) { $destFullPath = (Join-Path $destinationLocation $item.Name) CopyFile $vm $item.FullName $destFullPath } } Else { CopyFile $vm $sourceLocation $destinationLocation } } else { Write-Error "Source file path does not exist: $sourceLocation" } FILECOPY end |
#delete_vm_ps ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/kitchen/driver/powershell.rb', line 118 def delete_vm_ps <<-REMOVE $null = Get-VM -ID "#{@state[:id]}" | Stop-VM -Force -TurnOff -PassThru | Remove-VM -Force REMOVE 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_ps ⇒ Object
83 84 85 86 87 88 |
# File 'lib/kitchen/driver/powershell.rb', line 83 def ensure_vm_running_ps <<-RUNNING Assert-VmRunning -ID "#{@state[:id]}" | ConvertTo-Json RUNNING end |
#execute_command(cmd, options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize
66 67 68 69 70 71 72 73 |
# File 'lib/kitchen/driver/powershell.rb', line 66 def execute_command(cmd, = {}) debug("#Local Command BEGIN (#{cmd})") sh = Mixlib::ShellOut.new(cmd, ) 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 |
#is_64bit? ⇒ Boolean
30 31 32 33 |
# File 'lib/kitchen/driver/powershell.rb', line 30 def is_64bit? os_arch = ENV['PROCESSOR_ARCHITEW6432'] || ENV['PROCESSOR_ARCHITECTURE'] os_arch == 'AMD64' end |
#mount_vm_iso ⇒ Object
142 143 144 145 146 |
# File 'lib/kitchen/driver/powershell.rb', line 142 def mount_vm_iso <<-MOUNTISO mount-vmiso -id "#{@state[:id]}" -Path #{config[:iso_path]} MOUNTISO end |
#new_differencing_disk_ps ⇒ Object
rubocop:enable Metrics/AbcSize
76 77 78 79 80 81 |
# File 'lib/kitchen/driver/powershell.rb', line 76 def new_differencing_disk_ps <<-DIFF New-DifferencingDisk -Path "#{differencing_disk_path}" -ParentPath "#{parent_vhd_path}" DIFF end |
#new_vm_ps ⇒ Object
rubocop:disable Metrics/MethodLength
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/kitchen/driver/powershell.rb', line 91 def new_vm_ps <<-NEWVM $NewVMParams = @{ Generation = #{config[:vm_generation]} MemoryStartupBytes = #{config[:memory_startup_bytes]} Name = "#{instance.name}" Path = "#{kitchen_vm_path}" VHDPath = "#{differencing_disk_path}" SwitchName = "#{config[:vm_switch]}" ProcessorCount = #{config[:processor_count]} UseDynamicMemory = "#{config[:dynamic_memory]}" DynamicMemoryMinBytes = #{config[:dynamic_memory_min_bytes]} DynamicMemoryMaxBytes = #{config[:dynamic_memory_max_bytes]} } New-KitchenVM @NewVMParams | ConvertTo-Json NEWVM end |
#powershell_64_bit ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/kitchen/driver/powershell.rb', line 35 def powershell_64_bit if is_64bit? 'c:\windows\system32\windowspowershell\v1.0\powershell.exe' else 'c:\windows\sysnative\windowspowershell\v1.0\powershell.exe' end 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.
57 58 59 60 61 62 63 |
# File 'lib/kitchen/driver/powershell.rb', line 57 def run_ps(cmd, = {}) cmd = "echo #{cmd}" if config[:dry_run] debug('Preparing to run: ') debug(" #{cmd}") wrapped_command = wrap_command cmd execute_command wrapped_command, end |
#set_vm_ipaddress_ps ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/kitchen/driver/powershell.rb', line 127 def set_vm_ipaddress_ps <<-VMIP (Get-VM -id "#{@state[:id]}").NetworkAdapters | Set-VMNetworkConfiguration -ipaddress "#{config[:ip_address]}" -subnet 255.255.255.0 | ConvertTo-Json VMIP end |
#vm_default_switch_ps ⇒ Object
136 137 138 139 140 |
# File 'lib/kitchen/driver/powershell.rb', line 136 def vm_default_switch_ps <<-VMSWITCH Get-DefaultVMSwitch | ConvertTo-Json VMSWITCH end |
#vm_details_ps ⇒ Object
rubocop:enable Metrics/MethodLength
111 112 113 114 115 116 |
# File 'lib/kitchen/driver/powershell.rb', line 111 def vm_details_ps <<-DETAILS Get-VmDetail -id "#{@state[:id]}" | ConvertTo-Json DETAILS end |
#wrap_command(script) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/kitchen/driver/powershell.rb', line 43 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 |