Class: TrainPlugins::VMware::Connection
- Inherits:
-
Train::Plugins::Transport::BaseConnection
- Object
- Train::Plugins::Transport::BaseConnection
- TrainPlugins::VMware::Connection
- Defined in:
- lib/train-vmware/connection.rb
Overview
You must inherit from BaseConnection.
Constant Summary collapse
- POWERSHELL_PROMPT_REGEX =
/PS\s.*> $/.freeze
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #platform ⇒ Object
- #run_command_via_connection(cmd, &_data_handler) ⇒ Object
- #unique_identifier ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/train-vmware/connection.rb', line 14 def initialize() super() [:viserver] = [:viserver] || [:host] [:username] = [:username] || [:user] @username = [:username] @viserver = [: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() end if [: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
#connect ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/train-vmware/connection.rb', line 44 def connect login_command = "Connect-VIServer #{[:viserver]} -User #{[:username]} -Password #{[:password]} | Out-Null" result = run_command_via_connection(login_command) if result.exit_status != 0 = "Unable to connect to VIServer at #{[:viserver]}. " case result.stderr when /Invalid server certificate/ += "Certification verification failed. Please use `--insecure` or set `Set-PowerCLIConfiguration -InvalidCertificateAction Ignore` in PowerShell" when /incorrect user name or password/ += "Incorrect username or password" else += result.stderr.gsub(/-Password .*\s/, "-Password REDACTED") end raise end end |
#platform ⇒ Object
63 64 65 |
# File 'lib/train-vmware/connection.rb', line 63 def platform force_platform!("vmware", @platform_details) end |
#run_command_via_connection(cmd, &_data_handler) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/train-vmware/connection.rb', line 67 def run_command_via_connection(cmd, &_data_handler) 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_identifier ⇒ Object
81 82 83 84 |
# File 'lib/train-vmware/connection.rb', line 81 def unique_identifier uuid_command = "(Get-VMHost | Get-View).hardware.systeminfo.uuid" run_command_via_connection(uuid_command).stdout.chomp end |
#uri ⇒ Object
86 87 88 |
# File 'lib/train-vmware/connection.rb', line 86 def uri "vmware://#{@username}@#{@viserver}" end |