Class: Deltacloud::Drivers::Mock::MockDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/mock/mock_driver.rb

Constant Summary

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS

Instance Method Summary collapse

Methods inherited from BaseDriver

#blob, #bucket, #catched_exceptions_list, declare_feature, define_hardware_profile, define_instance_states, feature, feature_decl_for, feature_decls, #features, features, #features_for_operation, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, #hardware_profiles, hardware_profiles, #has_capability?, #has_collection?, #image, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #safely, #storage_snapshot, #storage_volume

Constructor Details

#initializeMockDriver

Returns a new instance of MockDriver.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 86

def initialize
  if ENV["DELTACLOUD_MOCK_STORAGE"]
    @storage_root = ENV["DELTACLOUD_MOCK_STORAGE"]
  elsif ENV["USER"]
    @storage_root = File::join("/var/tmp", "deltacloud-mock-#{ENV["USER"]}")
  else
    raise "Please set either the DELTACLOUD_MOCK_STORAGE or USER environment variable"
  end
  if ! File::directory?(@storage_root)
    FileUtils::rm_rf(@storage_root)
    FileUtils::mkdir_p(@storage_root)
    data = Dir::glob(File::join(File::dirname(__FILE__), "data", "*"))
    FileUtils::cp_r(data, @storage_root)
  end
end

Instance Method Details

#blob_data(credentials, bucket_id, blob_id, opts = nil) ⇒ Object

– Blob content –



373
374
375
376
377
378
379
380
381
382
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 373

def blob_data(credentials, bucket_id, blob_id, opts = nil)
  check_credentials(credentials)
  blob=nil
  Dir[ "#{@storage_root}/buckets/blobs/*.yml" ].each do |blob_file|
    if File.basename(blob_file, ".yml") == blob_id
      blob = YAML.load(File.read(blob_file))
      blob[:content].each {|part| yield part}
    end
  end
end

#blobs(credentials, opts = nil) ⇒ Object

– Blobs –



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 357

def blobs(credentials, opts = nil)
  check_credentials(credentials)
  blobs=[]
  Dir[ "#{@storage_root}/buckets/blobs/*.yml" ].each do |blob_file|
    blob = YAML.load( File.read( blob_file ) )
    blob[:id] = File.basename( blob_file, ".yml" )
    blob[:name] = blob[:id]
    blobs << Blob.new( blob )
  end
  blobs = filter_on( blobs, :id, opts )
  blobs
end

#buckets(credentials, opts = nil) ⇒ Object

– Buckets –



314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 314

def buckets(credentials, opts=nil)
  check_credentials(credentials)
  buckets=[]
   Dir[ "#{@storage_root}/buckets/*.yml" ].each do |bucket_file|
    bucket = YAML.load( File.read( bucket_file ) )
    bucket[:id] = File.basename( bucket_file, ".yml" )
    bucket[:name] = bucket[:id]
    buckets << Bucket.new( bucket )
  end
  buckets = filter_on( buckets, :id, opts )
  buckets
end

#create_bucket(credentials, name, opts = nil) ⇒ Object

– Create bucket –



330
331
332
333
334
335
336
337
338
339
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 330

def create_bucket(credentials, name, opts=nil)
  check_credentials(credentials)
  bucket = {
    :name=>name,
    :size=>'0',
    :blob_list=>[]
  }
  File.open( "#{@storage_root}/buckets/#{name}.yml", 'w' ) {|b| YAML.dump( bucket, b )}
  Bucket.new(bucket)
end

#create_instance(credentials, image_id, opts) ⇒ Object



161
162
163
164
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
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 161

def create_instance(credentials, image_id, opts)
  check_credentials( credentials )
  ids = Dir[ "#{@storage_root}/instances/*.yml" ].collect{|e| File.basename( e, ".yml" )}

  count = 0
  while true
    next_id = "inst" + count.to_s
    if not ids.include?(next_id)
      break
    end
    count = count + 1
  end

  realm_id = opts[:realm_id]
  if ( realm_id.nil? )
    realm = realms(credentials).first
    ( realm_id = realm.id ) if realm
  end

  hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id)

  name = opts[:name] || "i-#{Time.now.to_i}"

  instance = {
    :name=>name,
    :state=>'RUNNING',
    :image_id=>image_id,
    :owner_id=>credentials.user,
    :public_addresses=>["#{image_id}.#{next_id}.public.com"],
    :private_addresses=>["#{image_id}.#{next_id}.private.com"],
    :instance_profile => InstanceProfile.new(hwp.name, opts),
    :realm_id=>realm_id,
    :actions=>instance_actions_for( 'RUNNING' )
  }
  File.open( "#{@storage_root}/instances/#{next_id}.yml", 'w' ) {|f|
    YAML.dump( instance, f )
  }
  instance[:id] = next_id
  Instance.new( instance )
end

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



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 283

def create_key(credentials, opts={})
  check_credentials(credentials)
  key_hash = {
    :id => opts[:key_name],
    :credential_type => :key,
    :fingerprint => Key::generate_mock_fingerprint,
    :pem_rsa_key => Key::generate_mock_pem
  }
  key_dir = File.join(@storage_root, 'keys')
  if File.exists?(key_dir + "/#{key_hash[:id]}.yml")
   raise Deltacloud::BackendError.new(403, self.class.to_s, "key-exists",
                                        ["Key with same name already exists"])
  end
  FileUtils.mkdir_p(key_dir) unless File.directory?(key_dir)
  File.open(key_dir + "/#{key_hash[:id]}.yml", 'w') do |f|
    f.puts(YAML::dump(key_hash))
  end
  return Key.new(key_hash)
end

#delete_bucket(credentials, name, opts = nil) ⇒ Object

– Delete bucket –



344
345
346
347
348
349
350
351
352
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 344

def delete_bucket(credentials, name, opts=nil)
  bucket = bucket(credentials, {:id => name})
  unless (bucket.size == "0")
   raise Deltacloud::BackendError.new(403, self.class.to_s, "bucket-not-empty", "delete operation not valid for non-empty bucket")
  end
  safely do
    File.delete("#{@storage_root}/buckets/#{name}.yml")
  end
end

#destroy_instance(credentials, id) ⇒ Object



227
228
229
230
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 227

def destroy_instance(credentials, id)
  check_credentials( credentials )
  FileUtils.rm( "#{@storage_root}/instances/#{id}.yml" )
end

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



303
304
305
306
307
308
309
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 303

def destroy_key(credentials, opts={})
  key = key(credentials, opts)
  safely do
    key_dir = File.join(@storage_root, 'keys')
    File.delete(key_dir + "/#{key.id}.yml")
  end
end

#images(credentials, opts = nil) ⇒ Object

Images



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 112

def images(credentials, opts=nil )
  check_credentials( credentials )
  images = []
  Dir[ "#{@storage_root}/images/*.yml" ].each do |image_file|
    image = YAML.load( File.read( image_file ) )
    image[:id] = File.basename( image_file, ".yml" )
    image[:name] = image[:description]
    images << Image.new( image )
  end
  images = filter_on( images, :id, opts )
  images = filter_on( images, :architecture, opts )
  if ( opts && opts[:owner_id] == 'self' )
    images = images.select{|e| e.owner_id == credentials.user }
  else
    images = filter_on( images, :owner_id, opts )
  end
  images.sort_by{|e| [e.owner_id,e.description]}
end

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

Instances



135
136
137
138
139
140
141
142
143
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 135

def instance(credentials, opts={})
  check_credentials( credentials )
  instance_filename = File.join(@storage_root, 'instances', "#{opts[:id]}.yml")
  return nil unless File.exists?(instance_filename)
  instance = YAML::load_file(instance_filename)
  instance[:actions] = instance_actions_for( instance[:state] )
  instance[:id] = File::basename(instance_filename, ".yml")
  Instance.new(instance)
end

#instances(credentials, opts = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 145

def instances(credentials, opts=nil)
  check_credentials( credentials )
  instances = []
  Dir[ "#{@storage_root}/instances/*.yml" ].each do |instance_file|
    instance = YAML::load_file(instance_file)
    if ( instance[:owner_id] == credentials.user )
      instance[:id] = File.basename( instance_file, ".yml" )
      instance[:actions] = instance_actions_for( instance[:state] )
      instances << Instance.new( instance )
    end
  end
  instances = filter_on( instances, :id, opts )
  instances = filter_on( instances, :state, opts )
  instances
end

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



279
280
281
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 279

def key(credentials, opts={})
  keys(credentials, opts).first
end

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



268
269
270
271
272
273
274
275
276
277
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 268

def keys(credentials, opts={})
  check_credentials(credentials)
  result = []
  key_dir = File.join(@storage_root, 'keys')
  Dir[key_dir + '/*.yml'].each do |key_file|
    result << Key.new(YAML::load(File.read(key_file)))
  end
  result = filter_on( result, :id, opts )
  result
end

#realms(credentials, opts = nil) ⇒ Object



102
103
104
105
106
107
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 102

def realms(credentials, opts=nil)
  return REALMS if ( opts.nil? )
  results = REALMS
  results = filter_on( results, :id, opts )
  results
end

#reboot_instance(credentials, id) ⇒ Object



218
219
220
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 218

def reboot_instance(credentials, id)
  update_instance_state(credentials, id, 'RUNNING')
end

#start_instance(credentials, id) ⇒ Object



214
215
216
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 214

def start_instance(credentials, id)
  update_instance_state(credentials, id, 'RUNNING')
end

#stop_instance(credentials, id) ⇒ Object



222
223
224
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 222

def stop_instance(credentials, id)
  update_instance_state(credentials, id, 'STOPPED')
end

#storage_snapshots(credentials, opts = nil) ⇒ Object

Storage Snapshots



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 254

def storage_snapshots(credentials, opts=nil)
  check_credentials( credentials )
  snapshots = []
  Dir[ "#{@storage_root}/storage_snapshots/*.yml" ].each do |storage_snapshot_file|
    storage_snapshot = YAML.load( File.read( storage_snapshot_file ) )
    if ( storage_snapshot[:owner_id] == credentials.user )
      storage_snapshot[:id] = File.basename( storage_snapshot_file, ".yml" )
      snapshots << StorageSnapshot.new( storage_snapshot )
    end
  end
  snapshots = filter_on( snapshots, :id, opts )
  snapshots
end

#storage_volumes(credentials, opts = nil) ⇒ Object

Storage Volumes



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 236

def storage_volumes(credentials, opts=nil)
  check_credentials( credentials )
  volumes = []
  Dir[ "#{@storage_root}/storage_volumes/*.yml" ].each do |storage_volume_file|
    storage_volume = YAML.load( File.read( storage_volume_file ) )
    if ( storage_volume[:owner_id] == credentials.user )
      storage_volume[:id] = File.basename( storage_volume_file, ".yml" )
      volumes << StorageVolume.new( storage_volume )
    end
  end
  volumes = filter_on( volumes, :id, opts )
  volumes
end

#supported_collectionsObject



28
29
30
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 28

def supported_collections
  DEFAULT_COLLECTIONS + [ :buckets, :keys]
end

#update_instance_state(credentials, id, state) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 202

def update_instance_state(credentials, id, state)
  instance_file = "#{@storage_root}/instances/#{id}.yml"
  instance_yml  = YAML.load( File.read( instance_file ) )
  instance_yml[:id] = id
  instance_yml[:state] = state
  instance_yml[:actions] = instance_actions_for( instance_yml[:state] )
  File.open( instance_file, 'w' ) do |f|
    f << YAML.dump( instance_yml )
  end
  Instance.new( instance_yml )
end

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


384
385
386
387
388
389
390
391
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 384

def valid_credentials?(credentials)
  begin
    check_credentials(credentials)
    return true
  rescue Deltacloud::AuthException
  end
  return false
end