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

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

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, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #hardware_profiles, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #safely, #storage_snapshot, #storage_volume

Constructor Details

#initializeMockDriver

Returns a new instance of MockDriver.



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

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 –



319
320
321
322
323
324
325
326
327
328
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 319

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 –



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

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 –



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 260

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 –



276
277
278
279
280
281
282
283
284
285
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 276

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



150
151
152
153
154
155
156
157
158
159
160
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
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 150

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

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

– Delete bucket –



290
291
292
293
294
295
296
297
298
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 290

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



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

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

#images(credentials, opts = nil) ⇒ Object

Images



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

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

#instances(credentials, opts = nil) ⇒ Object

Instances



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 134

def instances(credentials, opts=nil)
  check_credentials( credentials )
  instances = []
  Dir[ "#{@storage_root}/instances/*.yml" ].each do |instance_file|
    instance = YAML.load( File.read( 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

#realms(credentials, opts = nil) ⇒ Object



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

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



207
208
209
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 207

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

#start_instance(credentials, id) ⇒ Object



203
204
205
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 203

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

#stop_instance(credentials, id) ⇒ Object



211
212
213
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 211

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

#storage_snapshots(credentials, opts = nil) ⇒ Object

Storage Snapshots



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 243

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



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 225

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 ]
end

#update_instance_state(credentials, id, state) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 191

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)


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

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