Class: Deltacloud::Drivers::Mock::MockDriver
- Inherits:
-
BaseDriver
- Object
- BaseDriver
- Deltacloud::Drivers::Mock::MockDriver
show all
- Defined in:
- lib/deltacloud/drivers/mock/mock_driver.rb
Instance Method Summary
collapse
-
#create_instance(credentials, image_id, opts) ⇒ Object
-
#destroy_instance(credentials, id) ⇒ Object
-
#images(credentials, opts = nil) ⇒ Object
-
#initialize ⇒ MockDriver
constructor
A new instance of MockDriver.
-
#instances(credentials, opts = nil) ⇒ Object
-
#realms(credentials, opts = nil) ⇒ Object
-
#reboot_instance(credentials, id) ⇒ Object
-
#start_instance(credentials, id) ⇒ Object
-
#stop_instance(credentials, id) ⇒ Object
-
#storage_snapshots(credentials, opts = nil) ⇒ Object
-
#storage_volumes(credentials, opts = nil) ⇒ Object
-
#valid_credentials?(credentials) ⇒ Boolean
Methods inherited from BaseDriver
#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, #supported_collections
Constructor Details
Returns a new instance of MockDriver.
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 81
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
#create_instance(credentials, image_id, opts) ⇒ Object
147
148
149
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
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 147
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
|
#destroy_instance(credentials, id) ⇒ Object
219
220
221
222
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 219
def destroy_instance(credentials, id)
check_credentials( credentials )
FileUtils.rm( "#{@storage_root}/instances/#{id}.yml" )
end
|
#images(credentials, opts = nil) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 108
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 131
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
97
98
99
100
101
102
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 97
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
198
199
200
201
202
203
204
205
206
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 198
def reboot_instance(credentials, id)
instance_file = "#{@storage_root}/instances/#{id}.yml"
instance_yml = YAML.load( File.read( instance_file ) )
instance_yml[:state] = 'RUNNING'
File.open( instance_file, 'w' ) do |f|
f << YAML.dump( instance_yml )
end
Instance.new( instance_yml )
end
|
#start_instance(credentials, id) ⇒ Object
188
189
190
191
192
193
194
195
196
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 188
def start_instance(credentials, id)
instance_file = "#{@storage_root}/instances/#{id}.yml"
instance_yml = YAML.load( File.read( instance_file ) )
instance_yml[:state] = 'RUNNING'
File.open( instance_file, 'w' ) do |f|
f << YAML.dump( instance_yml )
end
Instance.new( instance_yml )
end
|
#stop_instance(credentials, id) ⇒ Object
208
209
210
211
212
213
214
215
216
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 208
def stop_instance(credentials, id)
instance_file = "#{@storage_root}/instances/#{id}.yml"
instance_yml = YAML.load( File.read( instance_file ) )
instance_yml[:state] = 'STOPPED'
File.open( instance_file, 'w' ) do |f|
f << YAML.dump( instance_yml )
end
Instance.new( instance_yml )
end
|
#storage_snapshots(credentials, opts = nil) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 246
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
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 228
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
|
#valid_credentials?(credentials) ⇒ Boolean
260
261
262
263
264
265
266
267
|
# File 'lib/deltacloud/drivers/mock/mock_driver.rb', line 260
def valid_credentials?(credentials)
begin
check_credentials(credentials)
return true
rescue Deltacloud::AuthException
end
return false
end
|