class VCenterDriver::VIClient
Class VIClient
Attributes
ccr_ref[RW]
rp[RW]
vc_name[RW]
vim[RW]
Public Class Methods
decrypt(msg, token)
click to toggle source
# File lib/vi_client.rb, line 229 def self.decrypt(msg, token) begin cipher = OpenSSL::Cipher.new('aes-256-cbc') cipher.decrypt # Truncate for Ruby 2.4 (in previous versions this was being # automatically truncated) cipher.key = token[0..31] msg = cipher.update(Base64.decode64(msg)) msg << cipher.final rescue StandardError raise 'Error decrypting secret.' end end
get_entities(folder, type, entities = [])
click to toggle source
@return RbVmomi::VIM::
# File lib/vi_client.rb, line 122
def self.get_entities(folder, type, entities = [])
if folder == []
# rubocop:disable Style/ReturnNil
return nil
# rubocop:enable Style/ReturnNil
end
folder.childEntity.each do |child|
the_name, _junk = child.to_s.split('(')
case the_name
when 'Folder'
get_entities(child, type, entities)
when type
entities.push(child)
end
end
entities
end
in_silence() { || ... }
click to toggle source
# File lib/vi_client.rb, line 246 def self.in_silence begin orig_stderr = $stderr.clone orig_stdout = $stdout.clone $stderr.reopen File.new('/dev/null', 'w') $stdout.reopen File.new('/dev/null', 'w') retval = yield rescue StandardError => e $stdout.reopen orig_stdout $stderr.reopen orig_stderr raise e ensure $stdout.reopen orig_stdout $stderr.reopen orig_stderr end retval end
in_stderr_silence() { || ... }
click to toggle source
# File lib/vi_client.rb, line 264 def self.in_stderr_silence begin orig_stderr = $stderr.clone $stderr.reopen File.new('/dev/null', 'w') retval = yield rescue StandardError => e $stderr.reopen orig_stderr raise e ensure $stderr.reopen orig_stderr end retval end
new(opts, host_id = -1)
click to toggle source
# File lib/vi_client.rb, line 34 def initialize(opts, host_id = -1) opts = { :insecure => true }.merge(opts) @host_id = host_id @vim = RbVmomi::VIM.connect(opts) @vc_name = opts[:host] if opts[:host] # Get ccr and get rp @ccr_ref = opts.delete(:ccr) return unless @ccr_ref ccr = RbVmomi::VIM::ClusterComputeResource.new(@vim, @ccr_ref) # Get ref for rp return unless ccr rp = opts.delete(:rp) return unless rp rp_list = get_resource_pools(ccr) rp_ref = rp_list .select {|r| r[:name] == rp }.first[:ref] rescue nil @rp = RbVmomi::VIM::ResourcePool(@vim, rp_ref) if rp_ref end
new_from_datastore(datastore_id)
click to toggle source
# File lib/vi_client.rb, line 169 def self.new_from_datastore(datastore_id) begin client = OpenNebula::Client.new datastore = OpenNebula::Datastore .new_with_id( datastore_id, client ) rc = datastore.info if OpenNebula.is_error?(rc) raise "Could not get datastore info \ for ID: #{datastore_id} - #{rc.message}" end vcenter_id = datastore['TEMPLATE/VCENTER_INSTANCE_ID'] host_pool = OpenNebula::HostPool.new(client) rc = host_pool.info if OpenNebula.is_error?(rc) raise "Could not get hosts information - #{rc.message}" end user = '' password = '' port = 0 host_pool.each do |host| vc_instance_id = host['TEMPLATE/VCENTER_INSTANCE_ID'] next unless vc_instance_id == vcenter_id host_decrypted = OpenNebula::Host .new_with_id( host['ID'], client ) host_decrypted.info(true) user = host_decrypted['TEMPLATE/VCENTER_USER'] password = host_decrypted['TEMPLATE/VCENTER_PASSWORD'] port = host_decrypted['TEMPLATE/VCENTER_PORT'] end if password.empty? || user.empty? raise "Error getting \ credentials for datastore #{datastore_id}" end connection = { :host => datastore['TEMPLATE/VCENTER_HOST'], :user => user, :password => password } connection[:port] = port unless port.nil? new(connection) rescue StandardError => e raise e end end
new_from_host(host_id, client = nil)
click to toggle source
# File lib/vi_client.rb, line 142 def self.new_from_host(host_id, client = nil) begin client = OpenNebula::Client.new if client.nil? host = OpenNebula::Host.new_with_id(host_id, client) rc = host.info(true) if OpenNebula.is_error?(rc) raise "Could not get host info for \ ID: #{host_id} - #{rc.message}" end connection = { :host => host['TEMPLATE/VCENTER_HOST'], :user => host['TEMPLATE/VCENTER_USER'], :rp => host['TEMPLATE/VCENTER_RESOURCE_POOL'], :ccr => host['TEMPLATE/VCENTER_CCR_REF'], :password => host['TEMPLATE/VCENTER_PASSWORD'] } vc_port = host['TEMPLATE/VCENTER_PORT'] connection[:port] = vc_port unless vc_port.nil? new(connection, host_id) rescue StandardError => e raise e end end
Public Instance Methods
close_connection()
click to toggle source
# File lib/vi_client.rb, line 117 def close_connection @vim.close end
get_resource_pools(ccr, rp = nil, parent_prefix = '', rp_array = [])
click to toggle source
# File lib/vi_client.rb, line 86 def get_resource_pools(ccr, rp = nil, parent_prefix = '', rp_array = []) current_rp = '' if !rp rp = ccr.resourcePool else if !parent_prefix.empty? current_rp << parent_prefix current_rp << '/' end current_rp << rp.name end if rp.resourcePool.empty? rp_info = {} rp_info[:name] = current_rp rp_info[:ref] = rp._ref rp_array << rp_info else rp.resourcePool.each do |child_rp| get_resource_pools(ccr, child_rp, current_rp, rp_array) end rp_info = {} rp_info[:name] = current_rp rp_info[:ref] = rp._ref rp_array << rp_info unless current_rp.empty? end rp_array end
host_credentials(one_client)
click to toggle source
# File lib/vi_client.rb, line 66 def host_credentials(one_client) raise 'no host id defined!' if @host_id == -1 host = OpenNebula::Host .new_with_id( @host_id, one_client ) rc = host.info if OpenNebula.is_error?(rc) raise "Could not get host info \ for ID: #{@host_id} - #{rc.message}" end { :pass => host['TEMPLATE/VCENTER_PASSWORD'], :user => host['TEMPLATE/VCENTER_USER'], :host => @vc_name } end
rp_confined?()
click to toggle source
# File lib/vi_client.rb, line 62 def rp_confined? !!@rp end