From e5dd74f749e76693a94562fd42a58389e568bff5 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Wed, 18 Nov 2020 10:59:07 -0500 Subject: [PATCH 1/6] Added powercli::hosts_status function --- CHANGELOG.md | 10 ++++- lib/puppet/functions/powercli/hosts_status.rb | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 lib/puppet/functions/powercli/hosts_status.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1dbe9..3f08bfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,20 @@ # Changelog All notable changes to this project will be documented in this file. + +## Development + +- Added new function `powercli::hosts_status` to retrieve the current connection status + for ESXi hosts in vCenter. + + Contributed by Nick Maludy (@nmaludy) + ## v0.1.2 (2020-11-13) - Simplified dvs_add_hosts resource. It will no longer take into consideration defaults and other complexities. These complexities are to be handed within the puppet profile which calls this resource. Contributed by Greg Perry (@gperry2011) -## Development - - Converted from Travis to GitHub Actions Contributed by Nick Maludy (@nmaludy) diff --git a/lib/puppet/functions/powercli/hosts_status.rb b/lib/puppet/functions/powercli/hosts_status.rb new file mode 100644 index 0000000..f5da266 --- /dev/null +++ b/lib/puppet/functions/powercli/hosts_status.rb @@ -0,0 +1,38 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'puppet_x', 'encore', 'powercli', 'helper')) + +# Returns a hash of hosts along with their connection status +# Note: this uses PowerCLI under the hood, so it should be run as a deferred function +# +# +Puppet::Functions.create_function(:'powercli::hosts_status') do + # @param hosts Explicit list of hosts to retrieve status for. + # If not specified all hosts that exist in the vCenter will be returned + # @return Hash where the key is the ESXi hostname, the value is the connection status for + # that host, either 'online' or 'offline'. + # Hosts that have a connection status of 'Disconnected' or 'NotResponding' will be + # marked as 'offline', otherwise they will be returned as 'online'. + dispatch :hosts_status do + required_param 'Powercli::Connection' :connection + optional_param 'Array[String]', :hosts + return_type "Hash[String, Enum['online', 'offline']" + end + + def hosts_status(connection, hosts = nil) + cmd = <<-EOF + $hosts_list = Get-VMHost + $results = @{} + foreach ($host in $hosts_list) { + $results[$host.Name] = $host.ConnectionState + } + $results | ConvertTo-Json + EOF + resp = PuppetX::PowerCLI::Helper.instance.powercli_connect_exec(connection, cmd) + hosts_hash = JSON.parse(resp[:stdout]) + if hosts + # only return the hosts that the user asked for + hosts_hash.select { |k, v| hosts.include?(k) } + else + hosts_hash + end + end +end From a005f1de6507744faf08ef3acce2fc532f345229 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Wed, 18 Nov 2020 11:42:41 -0500 Subject: [PATCH 2/6] Fix missing comma --- lib/puppet/functions/powercli/hosts_status.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet/functions/powercli/hosts_status.rb b/lib/puppet/functions/powercli/hosts_status.rb index f5da266..7e62655 100644 --- a/lib/puppet/functions/powercli/hosts_status.rb +++ b/lib/puppet/functions/powercli/hosts_status.rb @@ -12,7 +12,7 @@ # Hosts that have a connection status of 'Disconnected' or 'NotResponding' will be # marked as 'offline', otherwise they will be returned as 'online'. dispatch :hosts_status do - required_param 'Powercli::Connection' :connection + required_param 'Powercli::Connection', :connection optional_param 'Array[String]', :hosts return_type "Hash[String, Enum['online', 'offline']" end From 517eb7690faab8e240dfa48d645601f032020933 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Wed, 18 Nov 2020 11:44:54 -0500 Subject: [PATCH 3/6] Fix rubocop --- lib/puppet/functions/powercli/hosts_status.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/puppet/functions/powercli/hosts_status.rb b/lib/puppet/functions/powercli/hosts_status.rb index 7e62655..2506c5d 100644 --- a/lib/puppet/functions/powercli/hosts_status.rb +++ b/lib/puppet/functions/powercli/hosts_status.rb @@ -2,8 +2,6 @@ # Returns a hash of hosts along with their connection status # Note: this uses PowerCLI under the hood, so it should be run as a deferred function -# -# Puppet::Functions.create_function(:'powercli::hosts_status') do # @param hosts Explicit list of hosts to retrieve status for. # If not specified all hosts that exist in the vCenter will be returned @@ -30,7 +28,7 @@ def hosts_status(connection, hosts = nil) hosts_hash = JSON.parse(resp[:stdout]) if hosts # only return the hosts that the user asked for - hosts_hash.select { |k, v| hosts.include?(k) } + hosts_hash.select { |k, _v| hosts.include?(k) } else hosts_hash end From 6175b269b3090027aaec0b8c6a49954afdf2e1d2 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Wed, 18 Nov 2020 11:58:48 -0500 Subject: [PATCH 4/6] Fix type definiton for return of hosts_status --- lib/puppet/functions/powercli/hosts_status.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet/functions/powercli/hosts_status.rb b/lib/puppet/functions/powercli/hosts_status.rb index 2506c5d..43e6323 100644 --- a/lib/puppet/functions/powercli/hosts_status.rb +++ b/lib/puppet/functions/powercli/hosts_status.rb @@ -12,7 +12,7 @@ dispatch :hosts_status do required_param 'Powercli::Connection', :connection optional_param 'Array[String]', :hosts - return_type "Hash[String, Enum['online', 'offline']" + return_type "Hash[String, Enum['online', 'offline']]" end def hosts_status(connection, hosts = nil) From 6084b2716bff2bcb1dc10ca907c15bd9b055a699 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Wed, 18 Nov 2020 12:04:50 -0500 Subject: [PATCH 5/6] Moved ruby-pwsh require into the helper class where it belongs --- lib/puppet/provider/powercli_esx_ntp/powercli.rb | 1 - lib/puppet/provider/powercli_esx_syslog/powercli.rb | 1 - lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb | 1 - lib/puppet_x/encore/powercli/helper.rb | 1 + 4 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/puppet/provider/powercli_esx_ntp/powercli.rb b/lib/puppet/provider/powercli_esx_ntp/powercli.rb index b6f756d..7802667 100644 --- a/lib/puppet/provider/powercli_esx_ntp/powercli.rb +++ b/lib/puppet/provider/powercli_esx_ntp/powercli.rb @@ -1,5 +1,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'powercli')) -require 'ruby-pwsh' Puppet::Type.type(:powercli_esx_ntp).provide(:api, parent: Puppet::Provider::PowerCLI) do commands powershell: 'powershell.exe' diff --git a/lib/puppet/provider/powercli_esx_syslog/powercli.rb b/lib/puppet/provider/powercli_esx_syslog/powercli.rb index 0291f10..414e8da 100644 --- a/lib/puppet/provider/powercli_esx_syslog/powercli.rb +++ b/lib/puppet/provider/powercli_esx_syslog/powercli.rb @@ -1,5 +1,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'powercli')) -require 'ruby-pwsh' require 'uri' Puppet::Type.type(:powercli_esx_syslog).provide(:api, parent: Puppet::Provider::PowerCLI) do diff --git a/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb b/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb index 60857fa..05c1f9b 100644 --- a/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb +++ b/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb @@ -1,5 +1,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'powercli')) -require 'ruby-pwsh' Puppet::Type.type(:powercli_esx_vs_portgroup).provide(:api, parent: Puppet::Provider::PowerCLI) do commands powershell: 'powershell.exe' diff --git a/lib/puppet_x/encore/powercli/helper.rb b/lib/puppet_x/encore/powercli/helper.rb index bd99169..ea48afa 100644 --- a/lib/puppet_x/encore/powercli/helper.rb +++ b/lib/puppet_x/encore/powercli/helper.rb @@ -1,4 +1,5 @@ require 'puppet_x' +require 'ruby-pwsh' require 'singleton' module PuppetX::PowerCLI From 0097294351d493efdead3ca1f3ce8a71e5cf81c7 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Wed, 18 Nov 2020 16:22:00 -0500 Subject: [PATCH 6/6] Converting hosts_status over to rbvmomi --- lib/puppet/functions/powercli/hosts_status.rb | 37 +++++++++++-------- manifests/init.pp | 1 - 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/puppet/functions/powercli/hosts_status.rb b/lib/puppet/functions/powercli/hosts_status.rb index 43e6323..cfd2f47 100644 --- a/lib/puppet/functions/powercli/hosts_status.rb +++ b/lib/puppet/functions/powercli/hosts_status.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'puppet_x', 'encore', 'powercli', 'helper')) +require 'rbvmomi' # Returns a hash of hosts along with their connection status # Note: this uses PowerCLI under the hood, so it should be run as a deferred function @@ -16,21 +16,26 @@ end def hosts_status(connection, hosts = nil) - cmd = <<-EOF - $hosts_list = Get-VMHost - $results = @{} - foreach ($host in $hosts_list) { - $results[$host.Name] = $host.ConnectionState - } - $results | ConvertTo-Json - EOF - resp = PuppetX::PowerCLI::Helper.instance.powercli_connect_exec(connection, cmd) - hosts_hash = JSON.parse(resp[:stdout]) - if hosts - # only return the hosts that the user asked for - hosts_hash.select { |k, _v| hosts.include?(k) } - else - hosts_hash + credentials = { + host: connection['server'], + user: connection['username'], + password: connection['password'], + insecure: true + } + vim = RbVmomi::VIM.connect(credentials) + + # recursive find/grep to find all ESX hosts + # Type list: https://code.vmware.com/apis/196/vsphere + query = { + container: vim.rootFolder, + type: ['HostSystem'], + recursive: true + } + host_list = vim.serviceContent.viewManager.CreateContainerView(query).view + results = {} + host_list.map do |host| + results[host.name] = host.runtime.connectionState end + results end end diff --git a/manifests/init.pp b/manifests/init.pp index 3fef8e4..d0d3596 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -32,7 +32,6 @@ Optional[Powercli::Connection] $vcenter_connection = undef, Optional[Hash] $config = $powercli::params::config, ) inherits powercli::params { - pspackageprovider { 'Nuget': ensure => 'present', }