Class: Backends::Opennebula::Compute

Inherits:
Base
  • Object
show all
Includes:
Helpers::ComputeActionHelper, Helpers::ComputeCreateHelper, Helpers::ComputeParseHelper
Defined in:
lib/backends/opennebula/compute.rb

Constant Summary collapse

COMPUTE_NINTF_REGEXP =
/compute_(?<compute_id>\d+)_nic_(?<compute_nic_id>\d+)/
/compute_(?<compute_id>\d+)_disk_(?<compute_disk_id>\d+)/
OS_TPL_TERM_PREFIX =
'uuid'

Constants included from Helpers::ComputeCreateHelper

Helpers::ComputeCreateHelper::COMPUTE_BASE64_REGEXP, Helpers::ComputeCreateHelper::COMPUTE_DN_BASED_AUTHS, Helpers::ComputeCreateHelper::COMPUTE_SSH_REGEXP, Helpers::ComputeCreateHelper::COMPUTE_USER_DATA_SIZE_LIMIT

Constants included from Helpers::ComputeParseHelper

Helpers::ComputeParseHelper::CONTEXTUALIZATION_ATTR_KEY, Helpers::ComputeParseHelper::CONTEXTUALIZATION_ATTR_UD, Helpers::ComputeParseHelper::CONTEXTUALIZATION_MIXIN_KEY, Helpers::ComputeParseHelper::CONTEXTUALIZATION_MIXIN_UD, Helpers::ComputeParseHelper::NETWORK_GENERATED_PREFIX, Helpers::ComputeParseHelper::STORAGE_GENERATED_PREFIX

Constants inherited from Base

Base::API_VERSION

Instance Method Summary collapse

Methods included from Helpers::ComputeActionHelper

#trigger_action_prolog, #trigger_action_restart, #trigger_action_start, #trigger_action_state_check, #trigger_action_stop, #trigger_action_suspend

Methods included from Helpers::ComputeCreateHelper

#create_with_links, #create_with_os_tpl

Methods included from Helpers::ComputeParseHelper

#parse_backend_obj, #parse_basic_attrs, #parse_context_attrs, #parse_links, #parse_links_network, #parse_links_storage, #parse_one_attrs, #parse_state

Methods inherited from Base

#add_other_backend, #initialize

Methods included from Helpers::ExtensionsHelper

#read_extensions

Methods included from Helpers::JsonCollectionHelper

#read_from_json

Constructor Details

This class inherits a constructor from Backends::Opennebula::Base

Instance Method Details

#attach_network(networkinterface) ⇒ String

Attaches a network to an existing compute instance, compute instance and network instance in question are identified by occi.core.source, occi.core.target attributes. If the requested instance cannot be linked, an error describing the problem must be raised, @see Backends::Errors.

Examples:

networkinterface = ::Occi::Infrastructure::Networkinterface.new
attach_network(networkinterface) #=> "65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf"

Parameters:

  • networkinterface (::Occi::Infrastructure::Networkinterface)

    NI instance containing necessary attributes

Returns:

  • (String)

    final identifier of the new network interface


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/backends/opennebula/compute.rb', line 233

def attach_network(networkinterface)
  compute_id = networkinterface.attributes['occi.core.source'].split('/').last

  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(compute_id), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  fail Backends::Errors::ResourceStateError, 'Given compute instance is not running!' unless virtual_machine.lcm_state_str == 'RUNNING'

  template_location = File.join(@options.templates_dir, 'compute_nic.erb')
  template = Erubis::Eruby.new(File.read(template_location)).evaluate(networkinterface: networkinterface)

  rc = virtual_machine.nic_attach(template)
  check_retval(rc, Backends::Errors::ResourceActionError)

  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  "compute_#{virtual_machine['ID']}_nic_#{virtual_machine['TEMPLATE/NIC[last()]/NIC_ID']}"
end

#attach_storage(storagelink) ⇒ String

Attaches a storage to an existing compute instance, compute instance and storage instance in question are identified by occi.core.source, occi.core.target attributes. If the requested instance cannot be linked, an error describing the problem must be raised, @see Backends::Errors.

Examples:

storagelink = ::Occi::Infrastructure::Storagelink.new
attach_storage(storagelink) #=> "65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf"

Parameters:

  • storagelink (::Occi::Infrastructure::Storagelink)

    SL instance containing necessary attributes

Returns:

  • (String)

    final identifier of the new storage link


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/backends/opennebula/compute.rb', line 265

def attach_storage(storagelink)
  compute_id = storagelink.attributes['occi.core.source'].split('/').last

  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(compute_id), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  fail Backends::Errors::ResourceStateError, 'Given compute instance is not running!' unless virtual_machine.lcm_state_str == 'RUNNING'

  template_location = File.join(@options.templates_dir, 'compute_disk.erb')
  template = Erubis::Eruby.new(File.read(template_location)).evaluate(storagelink: storagelink)

  rc = virtual_machine.disk_attach(template)
  check_retval(rc, Backends::Errors::ResourceActionError)

  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  "compute_#{virtual_machine['ID']}_disk_#{virtual_machine['TEMPLATE/DISK[last()]/DISK_ID']}"
end

#create(compute) ⇒ String

Instantiates a new compute instance from ::Occi::Infrastructure::Compute. ID given in the occi.core.id attribute is optional and can be changed inside this method. Final occi.core.id must be returned as a String. If the requested instance cannot be created, an error describing the problem must be raised, @see Backends::Errors.

Examples:

compute = ::Occi::Infrastructure::Compute.new
compute_id = create(compute)
    #=> "65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf"

Parameters:

  • compute (::Occi::Infrastructure::Compute)

    compute instance containing necessary attributes

Returns:

  • (String)

    final identifier of the new compute instance


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/backends/opennebula/compute.rb', line 94

def create(compute)
  compute_id = compute.id

  os_tpl_mixins = compute.mixins.get_related_to(::Occi::Infrastructure::OsTpl.mixin.type_identifier)
  if !os_tpl_mixins.empty?
    compute_id = create_with_os_tpl(compute)
  elsif !compute.links.empty?
    compute_id = create_with_links(compute)
  else
    fail Backends::Errors::ResourceNotValidError,
         "Given instance contains neither an os_tpl " \
         "mixin or links necessary to create a virtual machine!"
  end

  compute_id
end

#delete(compute_id) ⇒ true, false

Deletes a specific compute instance, instance to be deleted is specified by an ID, this ID must match the occi.core.id attribute of the deleted instance. If the requested instance cannot be deleted, an error describing the problem must be raised, @see Backends::Errors.

Examples:

delete("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf") #=> true

Parameters:

  • compute_id (String)

    an identifier of a compute instance to be deleted

Returns:

  • (true, false)

    result of the operation


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/backends/opennebula/compute.rb', line 154

def delete(compute_id)
  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(compute_id), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  if virtual_machine.lcm_state_str == 'RUNNING'
    rc = virtual_machine.shutdown
  else
    rc = virtual_machine.delete
  end

  check_retval(rc, Backends::Errors::ResourceActionError)

  true
end

#delete_all(mixins = nil) ⇒ true, false

Deletes all compute instances, instances to be deleted must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance. If the requested instances cannot be deleted, an error describing the problem must be raised, @see Backends::Errors.

Examples:

delete_all #=> true

mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
delete_all(mixins)  #=> true

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (true, false)

    result of the operation


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/backends/opennebula/compute.rb', line 124

def delete_all(mixins = nil)
  # TODO: impl filtering with mixins
  backend_compute_pool = ::OpenNebula::VirtualMachinePool.new(@client)
  rc = backend_compute_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  backend_compute_pool.each do |backend_compute|
    if backend_compute.lcm_state_str == 'RUNNING'
      rc = backend_compute.shutdown
    else
      rc = backend_compute.delete
    end

    check_retval(rc, Backends::Errors::ResourceActionError)
  end

  true
end

#detach_network(networkinterface_id) ⇒ true, false

Detaches a network from an existing compute instance, the compute instance in question must be identifiable using the networkinterface ID passed as an argument. If the requested link instance cannot be detached, an error describing the problem must be raised, @see Backends::Errors.

Examples:

detach_network("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf") #=> true

Parameters:

  • networkinterface_id (String)

    network interface identifier

Returns:

  • (true, false)

    result of the operation


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/backends/opennebula/compute.rb', line 296

def detach_network(networkinterface_id)
  matched = COMPUTE_NINTF_REGEXP.match(networkinterface_id)
  fail Backends::Errors::IdentifierNotValidError, 'ID of the given networkinterface is not valid!' unless matched

  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(matched[:compute_id]), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  fail Backends::Errors::ResourceStateError, 'Given compute instance is not running!' unless virtual_machine.lcm_state_str == 'RUNNING'

  rc = virtual_machine.nic_detach(matched[:compute_nic_id].to_i)
  check_retval(rc, Backends::Errors::ResourceActionError)

  true
end

#detach_storage(storagelink_id) ⇒ true, false

Detaches a storage from an existing compute instance, the compute instance in question must be identifiable using the storagelink ID passed as an argument. If the requested link instance cannot be detached, an error describing the problem must be raised, @see Backends::Errors.

Examples:

detach_storage("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf") #=> true

Parameters:

  • storagelink_id (String)

    storage link identifier

Returns:

  • (true, false)

    result of the operation


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/backends/opennebula/compute.rb', line 322

def detach_storage(storagelink_id)
  matched = COMPUTE_SLINK_REGEXP.match(storagelink_id)
  fail Backends::Errors::IdentifierNotValidError, 'ID of the given storagelink is not valid!' unless matched

  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(matched[:compute_id]), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  fail Backends::Errors::ResourceStateError, 'Given compute instance is not running!' unless virtual_machine.lcm_state_str == 'RUNNING'

  rc = virtual_machine.disk_detach(matched[:compute_disk_id].to_i)
  check_retval(rc, Backends::Errors::ResourceActionError)

  true
end

#get(compute_id) ⇒ ::Occi::Infrastructure::Compute?

Gets a specific compute instance as ::Occi::Infrastructure::Compute. ID given as an argument must match the occi.core.id attribute inside the returned ::Occi::Infrastructure::Compute instance, however it is possible to implement internal mapping to a platform-specific identifier.

Examples:

compute = get('65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf')
    #=> #<::Occi::Infrastructure::Compute>

Parameters:

  • compute_id (String)

    OCCI identifier of the requested compute instance

Returns:

  • (::Occi::Infrastructure::Compute, nil)

    a compute instance or `nil`


72
73
74
75
76
77
78
79
# File 'lib/backends/opennebula/compute.rb', line 72

def get(compute_id)
  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(compute_id), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  fail Backends::Errors::ResourceNotFoundError, "Instance with ID #{compute_id} does not exist!" if virtual_machine.state_str == 'DONE'
  parse_backend_obj(virtual_machine)
end

#get_extensions::Occi::Collection

Returns a collection of custom mixins introduced (and specific for) the enabled backend. Only mixins and actions are allowed.

Returns:

  • (::Occi::Collection)

    collection of extensions (custom mixins and/or actions)


435
436
437
# File 'lib/backends/opennebula/compute.rb', line 435

def get_extensions
  read_extensions 'compute', @options.model_extensions_dir
end

#get_network(networkinterface_id) ⇒ ::Occi::Infrastructure::Networkinterface

Gets a network from an existing compute instance, the compute instance in question must be identifiable using the networkinterface ID passed as an argument. If the requested link instance cannot be found, an error describing the problem must be raised, @see Backends::Errors.

Examples:

get_network("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf")
    #=> #<::Occi::Infrastructure::Networkinterface>

Parameters:

  • networkinterface_id (String)

    network interface identifier

Returns:

  • (::Occi::Infrastructure::Networkinterface)

    instance of the found networkinterface


349
350
351
352
353
354
355
356
357
# File 'lib/backends/opennebula/compute.rb', line 349

def get_network(networkinterface_id)
  matched = COMPUTE_NINTF_REGEXP.match(networkinterface_id)
  fail Backends::Errors::IdentifierNotValidError, 'ID of the given networkinterface is not valid!' unless matched

  intf = get(matched[:compute_id]).links.to_a.select { |l| l.id == networkinterface_id }
  fail Backends::Errors::ResourceNotFoundError, 'Networkinterface with the given ID does not exist!' if intf.blank?

  intf.first
end

#get_os_tpl(term) ⇒ ::Occi::Core::Mixin?

Gets a specific os_tpl mixin instance as ::Occi::Core::Mixin. Term given as an argument must match the term inside the returned ::Occi::Core::Mixin instance.

Examples:

os_tpl = get_os_tpl('65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf')
    #=> #<::Occi::Core::Mixin>

Parameters:

  • term (String)

    OCCI term of the requested os_tpl mixin instance

Returns:

  • (::Occi::Core::Mixin, nil)

    a mixin instance or `nil`


477
478
479
480
# File 'lib/backends/opennebula/compute.rb', line 477

def get_os_tpl(term)
  # TODO: make it more efficient!
  list_os_tpl.to_a.select { |m| m.term == term }.first
end

#get_resource_tpl(term) ⇒ ::Occi::Core::Mixin?

Gets a specific resource_tpl mixin instance as ::Occi::Core::Mixin. Term given as an argument must match the term inside the returned ::Occi::Core::Mixin instance.

Examples:

resource_tpl = get_resource_tpl('65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf')
    #=> #<::Occi::Core::Mixin>

Parameters:

  • term (String)

    OCCI term of the requested resource_tpl mixin instance

Returns:

  • (::Occi::Core::Mixin, nil)

    a mixin instance or `nil`


504
505
506
# File 'lib/backends/opennebula/compute.rb', line 504

def get_resource_tpl(term)
  list_resource_tpl.to_a.select { |m| m.term == term }.first
end

#get_storage(storagelink_id) ⇒ ::Occi::Infrastructure::Storagelink

Gets a storage from an existing compute instance, the compute instance in question must be identifiable using the storagelink ID passed as an argument. If the requested link instance cannot be detached, an error describing the problem must be raised, @see Backends::Errors.

Examples:

get_storage("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf")
    #=> #<::Occi::Infrastructure::Storagelink>

Parameters:

  • storagelink_id (String)

    storage link identifier

Returns:

  • (::Occi::Infrastructure::Storagelink)

    instance of the found storagelink


370
371
372
373
374
375
376
377
378
# File 'lib/backends/opennebula/compute.rb', line 370

def get_storage(storagelink_id)
  matched = COMPUTE_SLINK_REGEXP.match(storagelink_id)
  fail Backends::Errors::IdentifierNotValidError, 'ID of the given storagelink is not valid!' unless matched

  link = get(matched[:compute_id]).links.to_a.select { |l| l.id == storagelink_id }
  fail Backends::Errors::ResourceNotFoundError, 'Storagelink with the given ID does not exist!' if link.blank?

  link.first
end

#list(mixins = nil) ⇒ ::Occi::Core::Resources

Gets all compute instances, instances must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance. Returned collection must contain ::Occi::Infrastructure::Compute instances wrapped in ::Occi::Core::Resources.

Examples:

computes = list #=> #<::Occi::Core::Resources>
computes.first #=> #<::Occi::Infrastructure::Compute>

mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
computes = list(mixins) #=> #<::Occi::Core::Resources>

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (::Occi::Core::Resources)

    a collection of compute instances


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/backends/opennebula/compute.rb', line 47

def list(mixins = nil)
  # TODO: impl filtering with mixins
  compute = ::Occi::Core::Resources.new
  backend_compute_pool = ::OpenNebula::VirtualMachinePool.new(@client)
  rc = backend_compute_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  backend_compute_pool.each do |backend_compute|
    compute << parse_backend_obj(backend_compute)
  end

  compute
end

#list_ids(mixins = nil) ⇒ Array<String>

Gets all compute instance IDs, no details, no duplicates. Returned identifiers must correspond to those found in the occi.core.id attribute of ::Occi::Infrastructure::Compute instances.

Examples:

list_ids #=> []
list_ids #=> ["65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf",
                         "ggf4f65adfadf-adgg4ad-daggad-fydd4fadyfdfd"]

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (Array<String>)

    IDs for all available compute instances


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/backends/opennebula/compute.rb', line 19

def list_ids(mixins = nil)
  # TODO: impl filtering with mixins
  backend_compute_pool = ::OpenNebula::VirtualMachinePool.new(@client)
  rc = backend_compute_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  compute = []
  backend_compute_pool.each do |backend_compute|
    compute << backend_compute['ID']
  end

  compute
end

#list_os_tpl::Occi::Core::Mixins

Gets backend-specific `os_tpl` mixins which should be merged into ::Occi::Model of the server.

Examples:

mixins = list_os_tpl #=> #<::Occi::Core::Mixins>
mixins.first #=> #<::Occi::Core::Mixin>

Returns:

  • (::Occi::Core::Mixins)

    a collection of mixins


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/backends/opennebula/compute.rb', line 447

def list_os_tpl
  os_tpl = ::Occi::Core::Mixins.new
  backend_tpl_pool = ::OpenNebula::TemplatePool.new(@client)
  rc = backend_tpl_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  backend_tpl_pool.each do |backend_tpl|
    depends = %w|http://schemas.ogf.org/occi/infrastructure#os_tpl|
    term = tpl_to_term(backend_tpl)
    scheme = "#{@options.backend_scheme}/occi/infrastructure/os_tpl#"
    title = backend_tpl['NAME']
    location = "/mixin/os_tpl/#{term}/"
    applies = %w|http://schemas.ogf.org/occi/infrastructure#compute|

    os_tpl << ::Occi::Core::Mixin.new(scheme, term, title, nil, depends, nil, location, applies)
  end

  os_tpl
end

#list_resource_tpl::Occi::Core::Mixins

Gets platform- or backend-specific `resource_tpl` mixins which should be merged into ::Occi::Model of the server.

Examples:

mixins = list_resource_tpl #=> #<::Occi::Core::Mixins>
mixins.first  #=> #<::Occi::Core::Mixin>

Returns:

  • (::Occi::Core::Mixins)

    a collection of mixins


490
491
492
# File 'lib/backends/opennebula/compute.rb', line 490

def list_resource_tpl
  @resource_tpl
end

#partial_update(compute_id, attributes = nil, mixins = nil, links = nil) ⇒ true, false

Partially updates an existing compute instance, instance to be updated is specified by compute_id. If the requested instance cannot be updated, an error describing the problem must be raised, @see Backends::Errors.

Examples:

attributes = ::Occi::Core::Attributes.new
mixins = ::Occi::Core::Mixins.new
links = ::Occi::Core::Links.new
partial_update(compute_id, attributes, mixins, links) #=> true

Parameters:

  • compute_id (String)

    unique identifier of a compute instance to be updated

  • attributes (::Occi::Core::Attributes) (defaults to: nil)

    a collection of attributes to be updated

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a collection of mixins to be added

  • links (::Occi::Core::Links) (defaults to: nil)

    a collection of links to be added

Returns:

  • (true, false)

    result of the operation


186
187
188
189
# File 'lib/backends/opennebula/compute.rb', line 186

def partial_update(compute_id, attributes = nil, mixins = nil, links = nil)
  # TODO: impl
  fail Backends::Errors::MethodNotImplementedError, 'Partial updates are currently not supported!'
end

#trigger_action(compute_id, action_instance) ⇒ true, false

Triggers an action on an existing compute instance, the compute instance in question is identified by a compute instance ID, action is identified by the action.term attribute of the action instance passed as an argument. If the requested action cannot be triggered, an error describing the problem must be raised, @see Backends::Errors.

Examples:

action_instance = ::Occi::Core::ActionInstance.new
trigger_action("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf", action_instance)
  #=> true

Parameters:

  • compute_id (String)

    compute instance identifier

  • action_instance (::Occi::Core::ActionInstance)

    action to be triggered

Returns:

  • (true, false)

    result of the operation


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/backends/opennebula/compute.rb', line 413

def trigger_action(compute_id, action_instance)
  case action_instance.action.type_identifier
  when 'http://schemas.ogf.org/occi/infrastructure/compute/action#stop'
    trigger_action_stop(compute_id, action_instance.attributes)
  when 'http://schemas.ogf.org/occi/infrastructure/compute/action#start'
    trigger_action_start(compute_id, action_instance.attributes)
  when 'http://schemas.ogf.org/occi/infrastructure/compute/action#restart'
    trigger_action_restart(compute_id, action_instance.attributes)
  when 'http://schemas.ogf.org/occi/infrastructure/compute/action#suspend'
    trigger_action_suspend(compute_id, action_instance.attributes)
  else
    fail Backends::Errors::ActionNotImplementedError,
         "Action #{action_instance.action.type_identifier.inspect} is not implemented!"
  end

  true
end

#trigger_action_on_all(action_instance, mixins = nil) ⇒ true, false

Triggers an action on all existing compute instance, instances must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance, action is identified by the action.term attribute of the action instance passed as an argument. If the requested action cannot be triggered, an error describing the problem must be raised, @see Backends::Errors.

Examples:

action_instance = ::Occi::Core::ActionInstance.new
mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
trigger_action_on_all(action_instance, mixin) #=> true

Parameters:

  • action_instance (::Occi::Core::ActionInstance)

    action to be triggered

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (true, false)

    result of the operation


394
395
396
397
# File 'lib/backends/opennebula/compute.rb', line 394

def trigger_action_on_all(action_instance, mixins = nil)
  list_ids(mixins).each { |cmpt| trigger_action(cmpt, action_instance) }
  true
end

#update(compute) ⇒ true, false

Updates an existing compute instance, instance to be updated is specified using the occi.core.id attribute of the instance passed as an argument. If the requested instance cannot be updated, an error describing the problem must be raised, @see Backends::Errors.

Examples:

compute = ::Occi::Infrastructure::Compute.new
update(compute) #=> true

Parameters:

  • compute (::Occi::Infrastructure::Compute)

    instance containing updated information

Returns:

  • (true, false)

    result of the operation


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/backends/opennebula/compute.rb', line 202

def update(compute)
  virtual_machine = ::OpenNebula::VirtualMachine.new(::OpenNebula::VirtualMachine.build_xml(compute.id), @client)
  rc = virtual_machine.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  fail Backends::Errors::ResourceStateError, 'Given compute instance is not powered off!' unless virtual_machine.state_str == 'POWEROFF'

  resize_template = ''
  resize_template << "VCPU = #{compute.cores.to_i}" if compute.cores
  resize_template << "CPU = #{compute.speed.to_f * (compute.cores || virtual_machine['TEMPLATE/VCPU']).to_i}" if compute.speed
  resize_template << "MEMORY = #{(compute.memory.to_f * 1024).to_i}" if compute.memory

  return false if resize_template.blank?

  rc = virtual_machine.resize(resize_template, true)
  check_retval(rc, Backends::Errors::ResourceActionError)

  true
end