Class: CIMI::Service::Machine

Inherits:
Base
  • Object
show all
Defined in:
lib/cimi/service/machine.rb

Overview

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Constant Summary

Constants inherited from Base

Base::METADATA_TYPES

Constants included from Deltacloud::Helpers::Database

Deltacloud::Helpers::Database::DATABASE_COLLECTIONS

Instance Attribute Summary

Attributes inherited from Base

#context, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, collection_name, create_url, #destroy, #extract_properties!, inherited, #initialize, list, metadata, #model_class, model_class, model_name, parse, #ref_id, #resolve, resolve, resource_attributes, resource_capabilities, #save, #select_attributes, #to_json, #to_xml

Methods included from Deltacloud::Helpers::Database

#current_db, #current_provider, #provides?

Methods included from Deltacloud::Helpers::Drivers

#driver, #driver_class, #driver_class_name, #driver_name, #driver_source_name, #driver_symbol, included, #provider_name

Constructor Details

This class inherits a constructor from CIMI::Service::Base

Class Method Details

.attach_volume(volume, location, context) ⇒ Object

returns the newly attach machine_volume



59
60
61
62
63
# File 'lib/cimi/service/machine.rb', line 59

def self.attach_volume(volume, location, context)
  context.driver.attach_storage_volume(context.credentials,
   {:id=>volume, :instance_id=>context.params[:id], :device=>location})
  CIMI::Service::MachineVolume.find(context.params[:id], context, volume)
end

.convert_instance_actions(instance, context) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/cimi/service/machine.rb', line 137

def self.convert_instance_actions(instance, context)
  actions = instance.actions.collect do |action|
    action = :restart if action == :reboot
    name = action
    name = :delete if action == :destroy # In CIMI destroy operation become delete
    { :href => context.send(:"#{action}_machine_url", instance.id), :rel => "http://schemas.dmtf.org/cimi/1/action/#{name}" }
  end
  actions <<  { :href => context.send(:"machine_images_url"), :rel => "http://schemas.dmtf.org/cimi/1/action/capture" } if instance.can_create_image?
  actions
end

.convert_instance_addresses(instance) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cimi/service/machine.rb', line 124

def self.convert_instance_addresses(instance)
  (instance.public_addresses + instance.private_addresses).collect do |address|
    {
      :hostname => address.is_hostname? ? address : nil,
      :mac_address => address.is_mac? ? address : nil,
      :state => 'Active',
      :protocol => 'IPv4',
      :address => address.is_ipv4? ? address : nil,
      :allocation => 'Static'
    }
  end
end

.convert_instance_cpu(profile, context) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/cimi/service/machine.rb', line 109

def self.convert_instance_cpu(profile, context)
  cpu_override = profile.overrides.find { |p, v| p == :cpu }
  if cpu_override.nil?
    CIMI::Service::MachineConfiguration.find(profile.id, context).cpu
  else
    cpu_override[1]
  end
end

.convert_instance_memory(profile, context) ⇒ Object



118
119
120
121
122
# File 'lib/cimi/service/machine.rb', line 118

def self.convert_instance_memory(profile, context)
  machine_conf = CIMI::Service::MachineConfiguration.find(profile.name, context)
  memory_override = profile.overrides.find { |p, v| p == :memory }
  memory_override.nil? ? machine_conf.memory.to_i : context.to_kibibyte(memory_override[1].to_i,"MB")
end

.convert_instance_state(state) ⇒ Object

FIXME: This will convert ‘RUNNING’ state to ‘STARTED’ which is defined in CIMI (p65)



101
102
103
104
105
106
107
# File 'lib/cimi/service/machine.rb', line 101

def self.convert_instance_state(state)
  case state
    when "RUNNING" then "STARTED"
    when "PENDING" then "CREATING" #aruba only exception... could be "STARTING" here
    else state
  end
end

.convert_storage_volumes(instance, context) ⇒ Object



148
149
150
151
152
# File 'lib/cimi/service/machine.rb', line 148

def self.convert_storage_volumes(instance, context)
  instance.storage_volumes ||= [] #deal with nilpointers
  instance.storage_volumes.map{|vol| {:href=>context.volume_url(vol.keys.first),
                                     :initial_location=>vol.values.first} }
end

.delete!(id, context) ⇒ Object



53
54
55
56
# File 'lib/cimi/service/machine.rb', line 53

def self.delete!(id, context)
  context.driver.destroy_instance(context.credentials, id)
  new(context, :values => { :id => id }).destroy
end

.detach_volume(volume, location, context) ⇒ Object

returns the machine_volume_collection for the given machine



66
67
68
69
70
# File 'lib/cimi/service/machine.rb', line 66

def self.detach_volume(volume, location, context)
  context.driver.detach_storage_volume(context.credentials,
   {:id=>volume, :instance_id=>context.params[:id], :device=>location})
  CIMI::Service::MachineVolume.collection_for_instance(context.params[:id], context)
end

.find(id, context) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cimi/service/machine.rb', line 25

def self.find(id, context)
  instances = []
  if id == :all
    instances = context.driver.instances(context.credentials)
    instances.map { |instance| from_instance(instance, context) }.compact
  else
    instance = context.driver.instance(context.credentials, :id => id)
    raise CIMI::Model::NotFound unless instance
    from_instance(instance, context)
  end
end

.from_instance(instance, context) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cimi/service/machine.rb', line 72

def self.from_instance(instance, context)
  cpu =  memory = (instance.instance_profile.id == "opaque")? "n/a" : nil
  machine_conf = CIMI::Service::MachineConfiguration.find(instance.instance_profile.name, context)
  machine_spec = {
    :name => instance.name,
    :created => instance.launch_time.nil? ? Time.now.xmlschema : Time.parse(instance.launch_time.to_s).xmlschema,
    :description => "No description set for Machine #{instance.name}",
    :id => context.machine_url(instance.id),
    :state => convert_instance_state(instance.state),
    :cpu => cpu || convert_instance_cpu(instance.instance_profile, context),
    :memory => memory || convert_instance_memory(instance.instance_profile, context),
    :disks => { :href => context.machine_url(instance.id)+"/disks"},
    :volumes => { :href=>context.machine_url(instance.id)+"/volumes"},
    :operations => convert_instance_actions(instance, context)
  }
  if context.expand? :disks
    machine_spec[:disks] = CIMI::Service::Disk.find(instance, machine_conf, context, :all)
  end
  if context.expand? :volumes
    machine_spec[:volumes] = CIMI::Service::MachineVolume.find(instance.id, context, :all)
  end
  machine_spec[:realm] = instance.realm_id if instance.realm_id
  machine_spec[:machine_image] = { :href => context.machine_image_url(instance.image_id) } if instance.image_id
  self.new(context, :values => machine_spec)
end

Instance Method Details

#perform(action, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cimi/service/machine.rb', line 37

def perform(action, &block)
  begin
    op = action.operation
    op = :reboot if op == :restart
    puts "PERFORM #{op.inspect} on #{ref_id(id)}"
    if context.driver.send(:"#{op}_instance", context.credentials, ref_id(id))
      block.callback :success
    else
      raise "Operation #{op} failed to execute on given Machine #{ref_id(id)}"
    end
  rescue => e
    raise
    block.callback :failure, e.message
  end
end