Class: VCenterDriver::VIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/vi_client.rb

Overview

Class VIClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, host_id = -1)) ⇒ VIClient

Returns a new instance of VIClient.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

Instance Attribute Details

#ccr_refObject

Returns the value of attribute ccr_ref.



32
33
34
# File 'lib/vi_client.rb', line 32

def ccr_ref
  @ccr_ref
end

#rpObject

Returns the value of attribute rp.



30
31
32
# File 'lib/vi_client.rb', line 30

def rp
  @rp
end

#vc_nameObject

Returns the value of attribute vc_name.



31
32
33
# File 'lib/vi_client.rb', line 31

def vc_name
  @vc_name
end

#vimObject

Returns the value of attribute vim.



29
30
31
# File 'lib/vi_client.rb', line 29

def vim
  @vim
end

Class Method Details

.decrypt(msg, token) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/vi_client.rb', line 221

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 = []) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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_silenceObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/vi_client.rb', line 238

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_silenceObject



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/vi_client.rb', line 256

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_from_datastore(datastore_id) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/vi_client.rb', line 165

def self.new_from_datastore(datastore_id)
    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)
end

.new_from_host(host_id, client = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vi_client.rb', line 142

def self.new_from_host(host_id, client = nil)
    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)
end

Instance Method Details

#close_connectionObject



117
118
119
# File 'lib/vi_client.rb', line 117

def close_connection
    @vim.close
end

#get_resource_pools(ccr, rp = nil, parent_prefix = '', rp_array = []) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/vi_client.rb', line 62

def rp_confined?
    !!@rp
end