Class: Deltacloud::BaseDriver

Inherits:
Object
  • Object
show all
Includes:
Deltacloud, Exceptions
Defined in:
lib/deltacloud/drivers/base_driver.rb

Constant Summary collapse

STATE_MACHINE_OPTS =
{
  :all_states => [:start, :pending, :running, :stopping, :stopped, :finish, :error],
  :all_actions => [:create, :reboot, :stop, :start, :destroy]
}
MEMBER_SHOW_METHODS =
[ :realm, :image, :instance, :storage_volume, :bucket, :blob,
:key, :firewall ]

Constants included from Deltacloud

API_VERSION, CIMI_API_VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deltacloud

[], config, configure, connect, database, default_frontend, drivers, enabled_frontends, frontend_required?, frontends, generate_routes, initialize_database, need_database?, new, require_frontend!

Methods included from CollectionMethods

#collection_exists?, #collection_names, #collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Class Method Details

.constraints(opts = {}) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/deltacloud/drivers/base_driver.rb', line 54

def self.constraints(opts={})
  if opts[:collection] and opts[:feature]
    return [] unless @constraints.has_key? opts[:collection]
    return @constraints[opts[:collection]][opts[:feature]]
  end
  @constraints ||= {}
end

.define_hardware_profile(profile_id, &block) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/deltacloud/drivers/base_driver.rb', line 74

def self.define_hardware_profile(profile_id, &block)
  @hardware_profiles ||= []
  hw_profile = @hardware_profiles.find{|e| e.id == profile_id }
  return if hw_profile
  hw_profile = ::Deltacloud::HardwareProfile.new(profile_id, &block )
  @hardware_profiles << hw_profile
  hw_profile.params
end

.define_instance_states(&block) ⇒ Object



125
126
127
128
# File 'lib/deltacloud/drivers/base_driver.rb', line 125

def self.define_instance_states(&block)
  machine = ::Deltacloud::StateMachine.new(STATE_MACHINE_OPTS, &block)
  @instance_state_machine = machine
end

.driver_nameObject



35
36
37
# File 'lib/deltacloud/drivers/base_driver.rb', line 35

def self.driver_name
  name.split('::').last.gsub('Driver', '').downcase
end

.feature(collection, *feature_list) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/deltacloud/drivers/base_driver.rb', line 43

def self.feature(collection, *feature_list)
  feature_list.each do |feature_name|
    next if has_feature?(collection, feature_name)
    constraints[collection] ||= {}
    constraints[collection][feature_name] ||= {}
    constraints[collection][feature_name].merge!(yield) if block_given?
    features[collection] ||= []
    features[collection] << feature_name
  end
end

.featuresObject



39
40
41
# File 'lib/deltacloud/drivers/base_driver.rb', line 39

def self.features
  @features ||= {}
end

.hardware_profilesObject



83
84
85
86
# File 'lib/deltacloud/drivers/base_driver.rb', line 83

def self.hardware_profiles
  @hardware_profiles ||= []
  @hardware_profiles
end

.has_feature?(collection, feature_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/deltacloud/drivers/base_driver.rb', line 62

def self.has_feature?(collection, feature_name)
  features.has_key?(collection) and features[collection].include?(feature_name)
end

.instance_state_machineObject



130
131
132
# File 'lib/deltacloud/drivers/base_driver.rb', line 130

def self.instance_state_machine
  @instance_state_machine
end

Instance Method Details

#address(credentials, opts = {}) ⇒ Object



258
259
260
# File 'lib/deltacloud/drivers/base_driver.rb', line 258

def address(credentials, opts={})
  addresses(credentials, opts).first if has_capability?(:addresses)
end

#api_providerObject



295
296
297
# File 'lib/deltacloud/drivers/base_driver.rb', line 295

def api_provider
  Thread.current[:provider] || ENV['API_PROVIDER']
end

#blob(credentials, opts = {}) ⇒ Object



246
247
248
# File 'lib/deltacloud/drivers/base_driver.rb', line 246

def blob(credentials, opts = {})
  blobs(credentials, opts).first if has_capability?(:blobs)
end

#bucket(credentials, opts = {}) ⇒ Object



241
242
243
244
# File 'lib/deltacloud/drivers/base_driver.rb', line 241

def bucket(credentials, opts = {})
  #list of objects within bucket
  buckets(credentials, opts).first if has_capability?(:buckets)
end

#catched_exceptions_listObject



291
292
293
# File 'lib/deltacloud/drivers/base_driver.rb', line 291

def catched_exceptions_list
  { :error => [], :auth => [], :glob => [] }
end

#configured_providersObject

Return an array of the providers statically configured in the driver’s YAML file



301
302
303
# File 'lib/deltacloud/drivers/base_driver.rb', line 301

def configured_providers
  []
end

#filter_hardware_profiles(profiles, opts) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/deltacloud/drivers/base_driver.rb', line 98

def filter_hardware_profiles(profiles, opts)
  if opts
    if v = opts[:architecture]
      profiles = profiles.select { |hwp| hwp.include?(:architecture, v) }
    end
    if v = opts[:id]
      profiles = profiles.select { |hwp| hwp.id == v }
    end
  end
  profiles
end

#filter_on(collection, opts, *attributes) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/deltacloud/drivers/base_driver.rb', line 266

def filter_on(collection, opts, *attributes)
  opts, attributes = attributes.pop, [opts] if !opts.nil? and !(opts.is_a?(Hash))
  # FIXME: ^^ This is just to keep backward compatibility until
  # drivers will be fixed to use the new syntax.
  #
  # filter_on instances, :opts, [ :id, :state, :realm_id]
  #
  # instead of old:
  #
  # filter_on instances, :id, opts
  # filter_on instances, :state, opts
  # filter_on instances, :realm_id, opts

  return collection if opts.nil?
  attributes.each do |attribute|
    next unless filter = opts[attribute]
    if ( filter.is_a?( Array ) )
      collection.select! { |e| filter.include?( e.send(attribute) ) }
    else
      collection.select! { |e| filter == e.send(attribute) }
    end
  end
  collection
end

#find_hardware_profile(credentials, profile_id, image_id) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/deltacloud/drivers/base_driver.rb', line 110

def find_hardware_profile(credentials, profile_id, image_id)
  hwp = nil
  if profile_id
    unless hwp = hardware_profile(credentials, profile_id)
      raise Exceptions.exception_from_status(400, "Hardware profile '#{profile_id}' does not exists.")
    end
  else
    unless image = image(credentials, :id=>image_id)
      raise Exceptions.exception_from_status(400, "Image #{image_id} does not exists.")
    end
    hwp = hardware_profiles(credentials, :architecture => image.architecture).first
  end
  return hwp
end

#firewall(credentials, opts = {}) ⇒ Object



254
255
256
# File 'lib/deltacloud/drivers/base_driver.rb', line 254

def firewall(credentials, opts={})
  firewalls(credentials, opts).first if has_capability?(:firewalls)
end

#hardware_profile(credentials, profile_id) ⇒ Object



93
94
95
96
# File 'lib/deltacloud/drivers/base_driver.rb', line 93

def hardware_profile(credentials, profile_id)
  profile_id = profile_id[:id] if profile_id.kind_of? Hash
  hardware_profiles(credentials, :id => profile_id).first
end

#hardware_profiles(credentials, opts = {}) ⇒ Object



88
89
90
91
# File 'lib/deltacloud/drivers/base_driver.rb', line 88

def hardware_profiles(credentials, opts = {})
  results = self.class.hardware_profiles
  filter_hardware_profiles(results, opts)
end

#has_capability?(method) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
# File 'lib/deltacloud/drivers/base_driver.rb', line 149

def has_capability?(method)
  method = (RUBY_VERSION < '1.9') ? method.to_s : method
  # Prevent has_capability fail when driver is inherited from another
  # driver, like Eucalyptus
  superclass_methods = self.class.superclass.name == 'Deltacloud::BaseDriver' ?
    self.class.superclass.instance_methods : []
  (self.class.instance_methods - superclass_methods).include? method
end

#has_feature?(collection, feature_name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/deltacloud/drivers/base_driver.rb', line 66

def has_feature?(collection, feature_name)
  self.class.has_feature?(collection, feature_name)
end

#image(credentials, opts) ⇒ Object



225
226
227
# File 'lib/deltacloud/drivers/base_driver.rb', line 225

def image(credentials, opts)
  images(credentials, opts).first if has_capability?(:images)
end

#instance(credentials, opts) ⇒ Object



229
230
231
# File 'lib/deltacloud/drivers/base_driver.rb', line 229

def instance(credentials, opts)
  instances(credentials, opts).first if has_capability?(:instances)
end

#instance_actions_for(state) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/deltacloud/drivers/base_driver.rb', line 138

def instance_actions_for(state)
  actions = []
  states = instance_state_machine.states()
  current_state = states.find{|e| e.name == state.underscore.to_sym }
  if ( current_state )
    actions = current_state.transitions.collect{|e|e.action}
    actions.reject!{|e| e.nil?}
  end
  actions
end

#instance_state_machineObject



134
135
136
# File 'lib/deltacloud/drivers/base_driver.rb', line 134

def instance_state_machine
  self.class.instance_state_machine
end

#key(credentials, opts = nil) ⇒ Object



250
251
252
# File 'lib/deltacloud/drivers/base_driver.rb', line 250

def key(credentials, opts=nil)
  keys(credentials, opts).first if has_capability?(:keys)
end

#nameObject



70
71
72
# File 'lib/deltacloud/drivers/base_driver.rb', line 70

def name
  self.class.name.split('::').last.gsub('Driver', '').downcase
end

#realm(credentials, opts) ⇒ Object

Capabilities The rabbit dsl supports declaring a capability that is required in the backend driver for the call to succeed. A driver can provide a capability by implementing the method with the same name as the capability. Below is a list of the capabilities as the expected method signatures.

Following the capability list are the resource member show methods. They each require that the corresponding collection method be defined

TODO: standardize all of these to the same signature (credentials, opts)

def realms(credentials, opts=nil)

def images(credentials, ops)

def instances(credentials, ops) def create_instance(credentials, image_id, opts) def start_instance(credentials, id) def stop_instance(credentials, id) def reboot_instance(credentials, id)

def storage_volumes(credentials, ops)

def storage_snapshots(credentials, ops)

def buckets(credentials, opts = nil) def create_bucket(credentials, name, opts=nil) def delete_bucket(credentials, name, opts=nil)

def blobs(credentials, opts = nil) def blob_data(credentials, bucket_id, blob_id, opts) def create_blob(credentials, bucket_id, blob_id, blob_data, opts=nil) def delete_blob(credentials, bucket_id, blob_id, opts=nil)

def keys(credentials, opts) def create_key(credentials, opts) def destroy_key(credentials, opts)

def firewalls(credentials, opts) def create_firewall(credentials, opts) def delete_firewall(credentials, opts) def create_firewall_rule(credentials, opts) def delete_firewall_rule(credentials, opts) def providers(credentials)



221
222
223
# File 'lib/deltacloud/drivers/base_driver.rb', line 221

def realm(credentials, opts)
  realms = realms(credentials, opts).first if has_capability?(:realms)
end

#storage_snapshot(credentials, opts) ⇒ Object



237
238
239
# File 'lib/deltacloud/drivers/base_driver.rb', line 237

def storage_snapshot(credentials, opts)
  storage_snapshots(credentials, opts).first if has_capability?(:storage_snapshots)
end

#storage_volume(credentials, opts) ⇒ Object



233
234
235
# File 'lib/deltacloud/drivers/base_driver.rb', line 233

def storage_volume(credentials, opts)
  storage_volumes(credentials, opts).first if has_capability?(:storage_volumes)
end

#supported_collections(credentials) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/deltacloud/drivers/base_driver.rb', line 158

def supported_collections(credentials)
  collection_arr = []
  Deltacloud::Collections.modules(:deltacloud).each do |m|
    m.collections.each do |c|
      # Get the required capability for the :index operation (like 'realms' or 'instance_state_machine')
      index_operation_capability = c.operation(:index).required_capability
      # Then use this capability to check if the 'capability' lambda defined
      # for the Sinatra::Base class evaluate to 'true'
      next if m.settings.respond_to?(:capability) and !m.settings.capability(index_operation_capability)
      yield c if block_given?
      collection_arr << c
    end
  end
  collection_arr
end

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


305
306
307
308
309
310
311
312
313
314
# File 'lib/deltacloud/drivers/base_driver.rb', line 305

def valid_credentials?(credentials)
  begin
    new_client(credentials)
  rescue  Deltacloud::Exceptions::AuthenticationFailure
    return false
  rescue => e
    safely { raise e }
  end
  true
end