bbrowning-deltacloud-core 0.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/COPYING +502 -0
  2. data/Rakefile +85 -0
  3. data/bin/deltacloudd +88 -0
  4. data/config.ru +5 -0
  5. data/deltacloud.rb +14 -0
  6. data/lib/deltacloud/base_driver.rb +19 -0
  7. data/lib/deltacloud/base_driver/base_driver.rb +189 -0
  8. data/lib/deltacloud/base_driver/features.rb +159 -0
  9. data/lib/deltacloud/base_driver/mock_driver.rb +37 -0
  10. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +340 -0
  11. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +170 -0
  12. data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +45 -0
  13. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +322 -0
  14. data/lib/deltacloud/drivers/mock/mock_driver.rb +275 -0
  15. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +116 -0
  16. data/lib/deltacloud/drivers/opennebula/occi_client.rb +204 -0
  17. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +241 -0
  18. data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +129 -0
  19. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +150 -0
  20. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +254 -0
  21. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +84 -0
  22. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +144 -0
  23. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +261 -0
  24. data/lib/deltacloud/hardware_profile.rb +153 -0
  25. data/lib/deltacloud/helpers.rb +5 -0
  26. data/lib/deltacloud/helpers/application_helper.rb +56 -0
  27. data/lib/deltacloud/helpers/conversion_helper.rb +38 -0
  28. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +35 -0
  29. data/lib/deltacloud/method_serializer.rb +84 -0
  30. data/lib/deltacloud/models/base_model.rb +58 -0
  31. data/lib/deltacloud/models/image.rb +26 -0
  32. data/lib/deltacloud/models/instance.rb +37 -0
  33. data/lib/deltacloud/models/instance_profile.rb +47 -0
  34. data/lib/deltacloud/models/realm.rb +25 -0
  35. data/lib/deltacloud/models/storage_snapshot.rb +26 -0
  36. data/lib/deltacloud/models/storage_volume.rb +27 -0
  37. data/lib/deltacloud/state_machine.rb +84 -0
  38. data/lib/deltacloud/validation.rb +70 -0
  39. data/lib/drivers.rb +38 -0
  40. data/lib/sinatra/accept_media_types.rb +128 -0
  41. data/lib/sinatra/lazy_auth.rb +56 -0
  42. data/lib/sinatra/rabbit.rb +272 -0
  43. data/lib/sinatra/respond_to.rb +269 -0
  44. data/lib/sinatra/static_assets.rb +83 -0
  45. data/lib/sinatra/url_for.rb +51 -0
  46. data/public/favicon.ico +0 -0
  47. data/public/images/grid.png +0 -0
  48. data/public/images/logo-wide.png +0 -0
  49. data/public/images/rails.png +0 -0
  50. data/public/images/topbar-bg.png +0 -0
  51. data/public/javascripts/application.js +32 -0
  52. data/public/javascripts/jquery-1.4.2.min.js +154 -0
  53. data/public/stylesheets/compiled/application.css +613 -0
  54. data/public/stylesheets/compiled/ie.css +31 -0
  55. data/public/stylesheets/compiled/print.css +27 -0
  56. data/public/stylesheets/compiled/screen.css +456 -0
  57. data/server.rb +342 -0
  58. data/support/fedora/deltacloudd +68 -0
  59. data/support/fedora/rubygem-deltacloud-core.spec +91 -0
  60. data/tests/deltacloud_test.rb +60 -0
  61. data/tests/images_test.rb +94 -0
  62. data/tests/instances_test.rb +136 -0
  63. data/tests/realms_test.rb +56 -0
  64. data/tests/storage_snapshots_test.rb +48 -0
  65. data/tests/storage_volumes_test.rb +48 -0
  66. data/torquebox-ec2-config.ru +8 -0
  67. data/views/accounts/index.html.haml +11 -0
  68. data/views/accounts/show.html.haml +30 -0
  69. data/views/api/show.html.haml +15 -0
  70. data/views/api/show.xml.haml +5 -0
  71. data/views/docs/collection.html.haml +37 -0
  72. data/views/docs/collection.xml.haml +14 -0
  73. data/views/docs/index.html.haml +15 -0
  74. data/views/docs/index.xml.haml +5 -0
  75. data/views/docs/operation.html.haml +31 -0
  76. data/views/docs/operation.xml.haml +10 -0
  77. data/views/errors/auth_exception.html.haml +8 -0
  78. data/views/errors/auth_exception.xml.haml +2 -0
  79. data/views/errors/backend_error.html.haml +17 -0
  80. data/views/errors/backend_error.xml.haml +8 -0
  81. data/views/errors/validation_failure.html.haml +11 -0
  82. data/views/errors/validation_failure.xml.haml +7 -0
  83. data/views/hardware_profiles/index.html.haml +25 -0
  84. data/views/hardware_profiles/index.xml.haml +4 -0
  85. data/views/hardware_profiles/show.html.haml +19 -0
  86. data/views/hardware_profiles/show.xml.haml +17 -0
  87. data/views/images/index.html.haml +30 -0
  88. data/views/images/index.xml.haml +7 -0
  89. data/views/images/show.html.haml +21 -0
  90. data/views/images/show.xml.haml +5 -0
  91. data/views/instance_states/show.gv.erb +45 -0
  92. data/views/instance_states/show.html.haml +31 -0
  93. data/views/instance_states/show.xml.haml +8 -0
  94. data/views/instances/index.html.haml +30 -0
  95. data/views/instances/index.xml.haml +23 -0
  96. data/views/instances/new.html.haml +55 -0
  97. data/views/instances/show.html.haml +43 -0
  98. data/views/instances/show.xml.haml +41 -0
  99. data/views/layout.html.haml +26 -0
  100. data/views/realms/index.html.haml +29 -0
  101. data/views/realms/index.xml.haml +12 -0
  102. data/views/realms/show.html.haml +15 -0
  103. data/views/realms/show.xml.haml +10 -0
  104. data/views/root/index.html.haml +4 -0
  105. data/views/storage_snapshots/index.html.haml +20 -0
  106. data/views/storage_snapshots/index.xml.haml +11 -0
  107. data/views/storage_snapshots/show.html.haml +14 -0
  108. data/views/storage_snapshots/show.xml.haml +9 -0
  109. data/views/storage_volumes/index.html.haml +21 -0
  110. data/views/storage_volumes/index.xml.haml +13 -0
  111. data/views/storage_volumes/show.html.haml +20 -0
  112. data/views/storage_volumes/show.xml.haml +13 -0
  113. metadata +361 -0
@@ -0,0 +1,37 @@
1
+ require 'deltacloud/method_serializer'
2
+
3
+ # Create 'mock' version of original driver client/gem:
4
+
5
+ module Mock
6
+ class EC2 < AWS::EC2::Base
7
+
8
+ include MethodSerializer::Cache
9
+
10
+ def self.cached_methods
11
+ [
12
+ :describe_images,
13
+ :describe_availability_zones,
14
+ :run_instances,
15
+ :describe_instances,
16
+ :reboot_instances,
17
+ :terminate_instances
18
+ ]
19
+ end
20
+
21
+ MethodSerializer::Cache::wrap_methods(self, :cache_dir => File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'tests', 'ec2', 'support'))
22
+ end
23
+ end
24
+
25
+
26
+ # Replace original client with mock client
27
+ Deltacloud::Drivers::EC2::EC2Driver.class_eval do
28
+ alias_method :original_new_client, :new_client
29
+
30
+ def new_client(credentials, opts={})
31
+ Mock::EC2.new(
32
+ :access_key_id => credentials.user,
33
+ :secret_access_key => credentials.password
34
+ )
35
+ end
36
+
37
+ end
@@ -0,0 +1,340 @@
1
+ #
2
+ # Copyright (C) 2009 Red Hat, Inc.
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+
19
+ require 'deltacloud/base_driver'
20
+ require 'AWS'
21
+
22
+ class Instance
23
+ attr_accessor :keyname
24
+ attr_accessor :authn_error
25
+
26
+ def authn_feature_failed?
27
+ return true unless authn_error.nil?
28
+ end
29
+
30
+ end
31
+
32
+ module Deltacloud
33
+ module Drivers
34
+ module EC2
35
+ class EC2Driver < Deltacloud::BaseDriver
36
+
37
+ feature :instances, :user_data
38
+ feature :instances, :authentication_key
39
+
40
+ define_hardware_profile('m1.small') do
41
+ cpu 1
42
+ memory 1.7 * 1024
43
+ storage 160
44
+ architecture 'i386'
45
+ end
46
+
47
+ define_hardware_profile('m1.large') do
48
+ cpu 4
49
+ memory 7.5 * 1024
50
+ storage 850
51
+ architecture 'x86_64'
52
+ end
53
+
54
+ define_hardware_profile('m1.xlarge') do
55
+ cpu 8
56
+ memory 15 * 1024
57
+ storage 1690
58
+ architecture 'x86_64'
59
+ end
60
+
61
+ define_hardware_profile('c1.medium') do
62
+ cpu 5
63
+ memory 1.7 * 1024
64
+ storage 350
65
+ architecture 'i386'
66
+ end
67
+
68
+ define_hardware_profile('c1.xlarge') do
69
+ cpu 20
70
+ memory 7 * 1024
71
+ storage 1690
72
+ architecture 'x86_64'
73
+ end
74
+
75
+ define_hardware_profile('m2.xlarge') do
76
+ cpu 6.5
77
+ memory 17.1 * 1024
78
+ storage 420
79
+ architecture 'x86_64'
80
+ end
81
+
82
+ define_hardware_profile('m2.2xlarge') do
83
+ cpu 13
84
+ memory 34.2 * 1024
85
+ storage 850
86
+ architecture 'x86_64'
87
+ end
88
+
89
+ define_hardware_profile('m2.4xlarge') do
90
+ cpu 26
91
+ memory 68.4 * 1024
92
+ storage 1690
93
+ architecture 'x86_64'
94
+ end
95
+
96
+ define_instance_states do
97
+ start.to( :pending ) .automatically
98
+ pending.to( :running ) .automatically
99
+ pending.to( :stopping ) .on( :stop )
100
+ pending.to( :stopped ) .automatically
101
+ stopped.to( :running ) .on( :start )
102
+ running.to( :running ) .on( :reboot )
103
+ running.to( :stopping ) .on( :stop )
104
+ shutting_down.to( :stopped ) .automatically
105
+ stopped.to( :finish ) .automatically
106
+ end
107
+
108
+ #
109
+ # Images
110
+ #
111
+
112
+ def images(credentials, opts={} )
113
+ ec2 = new_client(credentials)
114
+ img_arr = []
115
+ # if we know the image_id, we don't want to limit by owner_id, since this
116
+ # will exclude public images
117
+ if (opts and opts[:id])
118
+ config = { :image_id => opts[:id] }
119
+ else
120
+ config = { :owner_id => "amazon" }
121
+ config.merge!({ :owner_id => opts[:owner_id] }) if opts and opts[:owner_id]
122
+ end
123
+ safely do
124
+ ec2.describe_images(config).imagesSet.item.each do |image|
125
+ img_arr << convert_image(image)
126
+ end
127
+ end
128
+ img_arr = filter_on( img_arr, :architecture, opts )
129
+ img_arr.sort_by{|e| [e.owner_id, e.name]}
130
+ end
131
+
132
+ #
133
+ # Realms
134
+ #
135
+
136
+ def realms(credentials, opts=nil)
137
+ ec2 = new_client(credentials)
138
+ realms = []
139
+ safely do
140
+ ec2.describe_availability_zones.availabilityZoneInfo.item.each do |ec2_realm|
141
+ realms << convert_realm( ec2_realm )
142
+ end
143
+ end
144
+ realms
145
+ end
146
+
147
+ #
148
+ # Instances
149
+ #
150
+ def instances(credentials, opts=nil)
151
+ ec2 = new_client(credentials)
152
+ instances = []
153
+ safely do
154
+ param = opts.nil? ? nil : opts[:id]
155
+ ec2_instances = ec2.describe_instances.reservationSet
156
+ return [] unless ec2_instances
157
+ ec2_instances.item.each do |item|
158
+ item.instancesSet.item.each do |ec2_instance|
159
+ instances << convert_instance( ec2_instance, item.ownerId )
160
+ end
161
+ end
162
+ end
163
+ instances = filter_on( instances, :id, opts )
164
+ instances = filter_on( instances, :state, opts )
165
+ instances
166
+ end
167
+
168
+
169
+ def create_instance(credentials, image_id, opts)
170
+ ec2 = new_client( credentials )
171
+ realm_id = opts[:realm_id]
172
+ image = image(credentials, :id => image_id )
173
+ hwp = find_hardware_profile(credentials, opts[:hwp_id], image.id)
174
+ ec2_instances = ec2.run_instances(
175
+ :image_id => image.id,
176
+ :user_data => opts[:user_data],
177
+ :key_name => opts[:keyname],
178
+ :availability_zone => realm_id,
179
+ :monitoring_enabled => true,
180
+ :instance_type => hwp.name,
181
+ :disable_api_termination => false,
182
+ :instance_initiated_shutdown_behavior => 'terminate'
183
+ )
184
+ convert_instance( ec2_instances.instancesSet.item.first, 'pending' )
185
+ end
186
+
187
+ def reboot_instance(credentials, id)
188
+ ec2 = new_client(credentials)
189
+ safely do
190
+ ec2.reboot_instances( :instance_id => id )
191
+ end
192
+ end
193
+
194
+ def stop_instance(credentials, id)
195
+ ec2 = new_client(credentials)
196
+ safely do
197
+ ec2.terminate_instances( :instance_id => id )
198
+ end
199
+ end
200
+
201
+ def destroy_instance(credentials, id)
202
+ ec2 = new_client(credentials)
203
+ safely do
204
+ ec2.terminate_instances( :instance_id => id )
205
+ end
206
+ end
207
+
208
+ #
209
+ # Storage Volumes
210
+ #
211
+ def storage_volumes(credentials, opts=nil)
212
+ ec2 = new_client( credentials )
213
+ volumes = []
214
+ safely do
215
+ if (opts)
216
+ ec2.describe_volumes(:volume_id => opts[:id]).volumeSet.item.each do |ec2_volume|
217
+ volumes << convert_volume( ec2_volume )
218
+ end
219
+ else
220
+ ec2_volumes = ec2.describe_volumes.volumeSet
221
+ return [] unless ec2_volumes
222
+ ec2_volumes.item.each do |ec2_volume|
223
+ volumes << convert_volume( ec2_volume )
224
+ end
225
+ end
226
+ end
227
+ volumes
228
+ end
229
+
230
+ #
231
+ # Storage Snapshots
232
+ #
233
+
234
+ def storage_snapshots(credentials, opts=nil)
235
+ ec2 = new_client( credentials )
236
+ snapshots = []
237
+ safely do
238
+ if (opts)
239
+ ec2.describe_snapshots(:owner => 'self', :snapshot_id => opts[:id]).snapshotSet.item.each do |ec2_snapshot|
240
+ snapshots << convert_snapshot( ec2_snapshot )
241
+ end
242
+ else
243
+ ec2_snapshots = ec2.describe_snapshots(:owner => 'self').snapshotSet
244
+ return [] unless ec2_snapshots
245
+ ec2_snapshots.item.each do |ec2_snapshot|
246
+ snapshots << convert_snapshot( ec2_snapshot )
247
+ end
248
+ end
249
+ end
250
+ snapshots
251
+ end
252
+
253
+ private
254
+
255
+ def new_client(credentials)
256
+ opts = {
257
+ :access_key_id => credentials.user,
258
+ :secret_access_key => credentials.password
259
+ }
260
+ opts[:server] = ENV['DCLOUD_EC2_URL'] if ENV['DCLOUD_EC2_URL']
261
+ AWS::EC2::Base.new(opts)
262
+ end
263
+
264
+ def convert_image(ec2_image)
265
+ Image.new( {
266
+ :id=>ec2_image['imageId'],
267
+ :name=>ec2_image['name'] || ec2_image['imageId'],
268
+ :description=>ec2_image['description'] || ec2_image['imageLocation'] || '',
269
+ :owner_id=>ec2_image['imageOwnerId'],
270
+ :architecture=>ec2_image['architecture'],
271
+ } )
272
+ end
273
+
274
+ def convert_realm(ec2_realm)
275
+ Realm.new( {
276
+ :id=>ec2_realm['zoneName'],
277
+ :name=>ec2_realm['regionName'],
278
+ :limit=>ec2_realm['zoneState'].eql?('available') ? :unlimited : 0,
279
+ :state=>ec2_realm['zoneState'].upcase,
280
+ } )
281
+ end
282
+
283
+ def convert_instance(ec2_instance, owner_id)
284
+ state = ec2_instance['instanceState']['name'].upcase
285
+ state_key = state.downcase.underscore.to_sym
286
+ realm_id = ec2_instance['placement']['availabilityZone']
287
+ (realm_id = nil ) if ( realm_id == '' )
288
+ hwp_name = ec2_instance['instanceType']
289
+ instance = Instance.new( {
290
+ :id=>ec2_instance['instanceId'],
291
+ :name => ec2_instance['imageId'],
292
+ :state=>state,
293
+ :image_id=>ec2_instance['imageId'],
294
+ :owner_id=>owner_id,
295
+ :realm_id=>realm_id,
296
+ :public_addresses=>( ec2_instance['dnsName'] == '' ? [] : [ec2_instance['dnsName']] ),
297
+ :private_addresses=>( ec2_instance['privateDnsName'] == '' ? [] : [ec2_instance['privateDnsName']] ),
298
+ :instance_profile =>InstanceProfile.new(hwp_name),
299
+ :actions=>instance_actions_for( state ),
300
+ :keyname => ec2_instance['keyName']
301
+ } )
302
+ instance.authn_error = "Key not set for instance" unless ec2_instance['keyName']
303
+ return instance
304
+ end
305
+
306
+ def convert_volume(ec2_volume)
307
+ StorageVolume.new( {
308
+ :id=>ec2_volume['volumeId'],
309
+ :created=>ec2_volume['createTime'],
310
+ :state=>ec2_volume['status'].upcase,
311
+ :capacity=>ec2_volume['size'],
312
+ :instance_id=>ec2_volume['snapshotId'],
313
+ :device=>ec2_volume['attachmentSet'],
314
+ } )
315
+ end
316
+
317
+ def convert_snapshot(ec2_snapshot)
318
+ StorageSnapshot.new( {
319
+ :id=>ec2_snapshot['snapshotId'],
320
+ :state=>ec2_snapshot['status'].upcase,
321
+ :storage_volume_id=>ec2_snapshot['volumeId'],
322
+ :created=>ec2_snapshot['startTime'],
323
+ } )
324
+ end
325
+
326
+ def safely(&block)
327
+ begin
328
+ block.call
329
+ rescue AWS::AuthFailure => e
330
+ raise Deltacloud::AuthException.new
331
+ rescue Exception => e
332
+ puts "ERROR: #{e.message}\n#{e.backtrace.join("\n")}"
333
+ end
334
+ end
335
+
336
+ end
337
+
338
+ end
339
+ end
340
+ end
@@ -0,0 +1,170 @@
1
+ module RightAws
2
+ class MockEc2
3
+
4
+ def initialize(opts={})
5
+ end
6
+
7
+ def describe_images(id)
8
+ load_fixtures_for(:images).select { |i| i[:aws_id].eql?(id) }
9
+ end
10
+
11
+ def describe_images_by_owner(id)
12
+ load_fixtures_for(:images).select { |i| i[:aws_owner].eql?(id) }
13
+ end
14
+
15
+ def describe_images(opts={})
16
+ load_fixtures_for(:images)
17
+ end
18
+
19
+ def describe_availability_zones(opts={})
20
+ load_fixtures_for(:realms)
21
+ end
22
+
23
+ def describe_instances(opts={})
24
+ instances = load_fixtures_for(:instances)
25
+ instances.each_with_index do |instance, i|
26
+ instances[i] = update_delayed_state(instance)
27
+ return [instance] if opts and opts[:id] and instance[:aws_instance_id].eql?(opts[:id])
28
+ end
29
+ update_fixtures_for(:instances, instances)
30
+ instances
31
+ end
32
+
33
+ def run_instances(image_id, min_count, max_count, group_ids, key_name, user_data='', addressing_type = nil, instance_type = nil, kernel_id = nil, ramdisk_id = nil, availability_zone = nil, block_device_mappings = nil)
34
+
35
+ instances = load_fixtures_for(:instances)
36
+ image = load_fixtures_for(:images).select { |img| img[:aws_id].eql?(image_id) }.first
37
+
38
+ if availability_zone
39
+ realm = load_fixtures_for(:realms).select { |realm| realm[:zone_name].eql?(availability_zone) }.first
40
+ else
41
+ realm = load_fixtures_for(:realms).first
42
+ end
43
+
44
+ raise Exception unless image
45
+ raise Exception unless realm
46
+
47
+ instance = { }
48
+ instance[:aws_image_id] = image[:aws_id]
49
+ instance[:aws_availability_zone] = realm[:zone_name]
50
+ instance[:aws_instance_type] = instance_type
51
+ instance[:aws_owner] = user_data
52
+ instance[:aws_state] = 'pending'
53
+ instance[:aws_reason] = ''
54
+ instance[:dns_name] = "#{random_dns}-01-C9.usma2.compute.amazonaws.com"
55
+ instance[:private_dns_name] = "#{random_dns}-02-P9.usma2.compute.amazonaws.com"
56
+ instance[:aws_state_code] = "0"
57
+ instance[:aws_key_name] = "staging"
58
+ instance[:aws_kernel_id] = "aki-be3adfd7"
59
+ instance[:aws_ramdisk_id] = "ari-ce34gad7"
60
+ instance[:aws_groups] = ["default"]
61
+ instance[:aws_instance_id] = random_instance_id
62
+ instance[:aws_reservation_id] = "r-aabbccdd"
63
+ instance[:aws_launch_time] = instance_time_format
64
+
65
+ instances << instance
66
+
67
+ update_fixtures_for(:instances, instances)
68
+
69
+ return [instance]
70
+ end
71
+
72
+
73
+ def terminate_instances(id)
74
+ update_instance_state(id, 'stopping', '80')
75
+ end
76
+
77
+ def reboot_instances(id)
78
+ update_instance_state(id, 'pending', '0')
79
+ end
80
+
81
+ alias :destroy_instance :terminate_instances
82
+
83
+ def describe_snapshots(opts={})
84
+ load_fixtures_for(:storage_snapshot)
85
+ end
86
+
87
+ def describe_volumes(opts={})
88
+ load_fixtures_for(:storage_volume)
89
+ end
90
+
91
+ private
92
+
93
+ def driver_dir
94
+ File::expand_path(File::join(File::dirname(__FILE__), '../../../../features/support/ec2'))
95
+ end
96
+
97
+ def fixtures_path
98
+ File::expand_path(File::join(driver_dir, 'fixtures'))
99
+ end
100
+
101
+ def load_fixtures_for(collection)
102
+ YAML.load_file(File::join(fixtures_path, "#{collection}.yaml"))
103
+ end
104
+
105
+ def update_fixtures_for(collection, new_data)
106
+ File.open(File::join(fixtures_path, "#{collection}.yaml"), 'w' ) do |out|
107
+ YAML.dump(new_data, out)
108
+ end
109
+ return new_data
110
+ end
111
+
112
+ def instance_time_format
113
+ DateTime.now.to_s.gsub(/\+(.+)$/, '.000Z')
114
+ end
115
+
116
+ def random_instance_id
117
+ id_1 = ("%.4s" % Time.now.to_i.to_s.reverse).reverse
118
+ id_2 = ("%.3s" % Time.now.to_i.to_s.reverse)
119
+ "i-#{id_1}f#{id_2}"
120
+ end
121
+
122
+ def random_dns
123
+ "domU-#{rand(90)+10}-#{rand(90)+10}-#{rand(90)+10}-#{rand(90)+10}"
124
+ end
125
+
126
+ def update_delayed_state(instance)
127
+ time = DateTime.now - DateTime.parse(instance[:aws_launch_time])
128
+ hours, minutes, seconds, frac = Date.day_fraction_to_time(time)
129
+
130
+ if (minutes>(rand(2)+1) or hours>0) and instance[:aws_state].eql?('pending')
131
+ instance[:aws_state], instance[:aws_state_code] = 'running', '16'
132
+ end
133
+
134
+ if (minutes>(rand(1)+1) or hours>0) and instance[:aws_state].eql?('stopping')
135
+ instance[:aws_state], instance[:aws_state_code] = 'stopped', '80'
136
+ end
137
+
138
+ return instance
139
+ end
140
+
141
+ def update_instance_state(id, state, state_code)
142
+ instance = describe_instances(:id => id).first
143
+ if instance
144
+ instance[:aws_state], instance[:aws_state_code] = state, state_code
145
+ instance[:aws_launch_time] = instance_time_format
146
+ instances = load_fixtures_for(:instances)
147
+ instances.each_with_index do |inst, i|
148
+ instances[i] = instance if inst[:aws_instance_id].eql?(id)
149
+ end
150
+ update_fixtures_for(:instances, instances)
151
+ return instance
152
+ else
153
+ raise Exception
154
+ end
155
+ end
156
+
157
+ end
158
+ end
159
+
160
+ Deltacloud::Drivers::EC2::EC2Driver.class_eval do
161
+ alias_method :original_new_client, :new_client
162
+
163
+ def new_client(credentials, opts={})
164
+ if credentials.user != 'mockuser' and credentials.password != 'mockpassword'
165
+ raise Deltacloud::AuthException.new
166
+ end
167
+ RightAws::MockEc2.new
168
+ end
169
+
170
+ end