Class: Train::Transports::VMware::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/train/transports/vmware.rb

Overview

rubocop:disable ClassLength

Constant Summary collapse

POWERSHELL_PROMPT_REGEX =
/PS\s.*> $/

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/train/transports/vmware.rb', line 23

def initialize(options)
  super(options)

  options[:viserver] = options[:viserver] || options[:host]
  options[:username] = options[:username] || options[:user]

  @username = options[:username]
  @viserver = options[:viserver]
  @session = nil
  @stdout_buffer = ''
  @stderr_buffer = ''

  @powershell_binary = detect_powershell_binary

  if @powershell_binary == :powershell
    require 'train/transports/local'
    @powershell = Train::Transports::Local::Connection.new(options)
  end

  if options[:insecure] == true
    run_command_via_connection('Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$False')
  end

  @platform_details = {
    release: "vmware-powercli-#{powercli_version}",
  }

  connect
end

Instance Method Details

#connectObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/train/transports/vmware.rb', line 53

def connect
   = "Connect-VIServer #{options[:viserver]} -User #{options[:username]} -Password #{options[:password]} | Out-Null"
  result = run_command_via_connection()

  if result.exit_status != 0
    message = "Unable to connect to VIServer at #{options[:viserver]}. "
    case result.stderr
    when /Invalid server certificate/
      message += 'Certification verification failed. Please use `--insecure` or set `Set-PowerCLIConfiguration -InvalidCertificateAction Ignore` in PowerShell'
    when /incorrect user name or password/
      message += 'Incorrect username or password'
    else
      message += result.stderr.gsub(/-Password .*\s/, '-Password REDACTED')
    end

    raise message
  end
end

#local?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/train/transports/vmware.rb', line 72

def local?
  true
end

#platformObject



76
77
78
# File 'lib/train/transports/vmware.rb', line 76

def platform
  force_platform!('vmware', @platform_details)
end

#run_command_via_connection(cmd) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/train/transports/vmware.rb', line 80

def run_command_via_connection(cmd)
  if @powershell_binary == :pwsh
    result = parse_pwsh_output(cmd)

    # Attach exit status to result
    exit_status = parse_pwsh_output('echo $?').stdout.chomp
    result.exit_status = exit_status == 'True' ? 0 : 1

    result
  else
    @powershell.run_command(cmd)
  end
end

#unique_identifierObject



94
95
96
97
# File 'lib/train/transports/vmware.rb', line 94

def unique_identifier
  uuid_command = '(Get-VMHost | Get-View).hardware.systeminfo.uuid'
  run_command_via_connection(uuid_command).stdout.chomp
end

#uriObject



99
100
101
# File 'lib/train/transports/vmware.rb', line 99

def uri
  "vmware://#{@username}@#{@viserver}"
end