module Kitchen::Driver::PowerShellScripts

Public Instance Methods

additional_disks() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 140
      def additional_disks
        return if config[:additional_disks].nil?

        <<-EOH
        AdditionalDisks = @("#{@additional_disk_objects.join('","')}")
        EOH
      end
copy_vm_file_ps(source, dest) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 205
      def copy_vm_file_ps(source, dest)
        <<-FILECOPY
          Function CopyFile ($VM, [string]$SourcePath, [string]$DestPath) {
              $p = @{ CreateFullPath = $true ; FileSource = 'Host'; Force = $true }
              $VM |
                Copy-VMFile -SourcePath $SourcePath -DestinationPath $DestPath @p
          }

          $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() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 156
      def delete_vm_ps
        <<-REMOVE

          $null = Get-VM -ID "#{@state[:id]}" |
            Stop-VM -Force -TurnOff -PassThru |
            Remove-VM -Force
        REMOVE
      end
encode_command(script) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 26
def encode_command(script)
  encoded_script = script.encode("UTF-16LE", "UTF-8")
  Base64.strict_encode64(encoded_script)
end
ensure_vm_running_ps() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 108
      def ensure_vm_running_ps
        <<-RUNNING

          Assert-VmRunning -ID "#{@state[:id]}" | ConvertTo-Json
        RUNNING
      end
execute_command(cmd, options = {}) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 75
def execute_command(cmd, options = {})
  debug("#Command BEGIN (#{cmd})")

  sh = nil
  bm = Benchmark.measure do
    sh = connection.run_command(cmd, options)
  end

  debug("Command END #{Util.duration(bm.total)}")
  raise "Failed: #{sh.stderr}" if sh.exit_status != 0

  stdout = sanitize_stdout(sh.stdout)
  JSON.parse(stdout) if stdout.length > 2
end
is_32bit?() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 39
def is_32bit?
  os_arch = ENV["PROCESSOR_ARCHITEW6432"] || ENV["PROCESSOR_ARCHITECTURE"]
  ruby_arch = ["foo"].pack("p").size == 4 ? 32 : 64
  os_arch != "AMD64" && ruby_arch == 32
end
is_64bit?() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 31
def is_64bit?
  return true if remote_hyperv

  os_arch = ENV["PROCESSOR_ARCHITEW6432"] || ENV["PROCESSOR_ARCHITECTURE"]
  ruby_arch = ["foo"].pack("p").size == 4 ? 32 : 64
  os_arch == "AMD64" && ruby_arch == 64
end
mount_vm_iso() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 187
      def mount_vm_iso
        <<-MOUNTISO
          mount-vmiso -id "#{@state[:id]}" -Path #{config[:iso_path]}
        MOUNTISO
      end
new_additional_disk_ps(disk_path, disk_size) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 101
      def new_additional_disk_ps(disk_path, disk_size)
        <<-ADDDISK

          New-VHD -Path "#{disk_path}" -SizeBytes #{disk_size}GB | Out-Null
        ADDDISK
      end
new_differencing_disk_ps() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 94
      def new_differencing_disk_ps
        <<-DIFF

          New-DifferencingDisk -Path "#{differencing_disk_path}" -ParentPath "#{parent_vhd_path}"
        DIFF
      end
new_vm_ps() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 115
      def new_vm_ps
        <<-NEWVM

          $NewVMParams = @{
            Generation = #{config[:vm_generation]}
            DisableSecureBoot = "#{config[:disable_secureboot]}"
            MemoryStartupBytes = #{config[:memory_startup_bytes]}
            StaticMacAddress = "#{config[:static_mac_address]}"
            Name = "#{instance.name}"
            Path = "#{kitchen_vm_path}"
            VHDPath = "#{differencing_disk_path}"
            SwitchName = "#{config[:vm_switch]}"
            VlanId = #{config[:vm_vlan_id] || "$null"}
            ProcessorCount = #{config[:processor_count]}
            UseDynamicMemory = "#{config[:dynamic_memory]}"
            DynamicMemoryMinBytes = #{config[:dynamic_memory_min_bytes]}
            DynamicMemoryMaxBytes = #{config[:dynamic_memory_max_bytes]}
            boot_iso_path = "#{boot_iso_path}"
            EnableGuestServices = "#{config[:enable_guest_services]}"
            #{additional_disks}
          }
          New-KitchenVM @NewVMParams | ConvertTo-Json
        NEWVM
      end
powershell_64_bit() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 45
def powershell_64_bit
  if is_64bit? || is_32bit?
    'c:\windows\system32\windowspowershell\v1.0\powershell.exe'
  else
    'c:\windows\sysnative\windowspowershell\v1.0\powershell.exe'
  end
end
resize_vhd() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 193
      def resize_vhd
        <<-VMNOTE
          Resize-VHD -Path "#{parent_vhd_path}" -SizeBytes #{config[:resize_vhd]}
        VMNOTE
      end
run_ps(cmd, options = {}) click to toggle source

Convenience method to run a powershell command locally.

@param cmd [String] command to run locally @param options [Hash] options hash @see Kitchen::ShellOut.run_command @api private

# File lib/kitchen/driver/powershell.rb, line 67
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
sanitize_stdout(stdout) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 90
def sanitize_stdout(stdout)
  stdout.split("\n").select { |s| !s.start_with?("PS") }.join("\n")
end
set_vm_ipaddress_ps() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 165
      def set_vm_ipaddress_ps
        <<-VMIP

          while ((Get-VM -id "#{@state[:id]}").NetworkAdapters[0].Status -ne 'Ok'){
            start-sleep 10
          }

          (Get-VM -id "#{@state[:id]}").NetworkAdapters |
            Set-VMNetworkConfiguration -ipaddress "#{config[:ip_address]}" `
              -subnet "#{config[:subnet]}" `
              -gateway "#{config[:gateway]}" `
              -dnsservers #{ruby_array_to_ps_array(config[:dns_servers])} |
            ConvertTo-Json
        VMIP
      end
set_vm_note() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 199
      def set_vm_note
        <<-VMNOTE
          Set-VM -Name (Get-VM | Where-Object{ $_.ID -eq "#{@state[:id]}"}).Name -Note "#{config[:vm_note]}"
        VMNOTE
      end
vm_default_switch_ps() click to toggle source
# File lib/kitchen/driver/powershell.rb, line 181
      def vm_default_switch_ps
        <<-VMSWITCH
          Get-DefaultVMSwitch "#{config[:vm_switch]}" | ConvertTo-Json
        VMSWITCH
      end
vm_details_ps() click to toggle source

TODO: Report if VM has no IP address instead of silently waiting forever

# File lib/kitchen/driver/powershell.rb, line 149
      def vm_details_ps
        <<-DETAILS

          Get-VmDetail -id "#{@state[:id]}" | ConvertTo-Json
        DETAILS
      end
wrap_command(script) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 53
def wrap_command(script)
  debug("Loading functions from #{base_script_path}")
  new_script = [ ". #{base_script_path}", "#{script}" ].join(";\n")
  debug("Wrapped script: #{new_script}")
  "#{powershell_64_bit} -noprofile -executionpolicy bypass" \
  " -encodedcommand #{encode_command new_script} -outputformat Text"
end

Private Instance Methods

ruby_array_to_ps_array(list) click to toggle source
# File lib/kitchen/driver/powershell.rb, line 243
def ruby_array_to_ps_array(list)
  return "@()" if list.nil? || list.empty?

  list.to_s.tr("[]", "()").prepend("@")
end