deltacloud-core 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +502 -0
- data/Rakefile +108 -0
- data/bin/deltacloudd +88 -0
- data/config.ru +5 -0
- data/deltacloud.rb +14 -0
- data/lib/converters/xml_converter.rb +133 -0
- data/lib/deltacloud/base_driver.rb +19 -0
- data/lib/deltacloud/base_driver/base_driver.rb +189 -0
- data/lib/deltacloud/base_driver/features.rb +144 -0
- data/lib/deltacloud/drivers/ec2/ec2_driver.rb +318 -0
- data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +170 -0
- data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +45 -0
- data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +239 -0
- data/lib/deltacloud/drivers/mock/mock_driver.rb +275 -0
- data/lib/deltacloud/drivers/opennebula/cloud_client.rb +116 -0
- data/lib/deltacloud/drivers/opennebula/occi_client.rb +204 -0
- data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +241 -0
- data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +129 -0
- data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +150 -0
- data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +254 -0
- data/lib/deltacloud/drivers/rimu/rimu_hosting_client.rb +87 -0
- data/lib/deltacloud/drivers/rimu/rimu_hosting_driver.rb +143 -0
- data/lib/deltacloud/hardware_profile.rb +131 -0
- data/lib/deltacloud/helpers.rb +5 -0
- data/lib/deltacloud/helpers/application_helper.rb +38 -0
- data/lib/deltacloud/helpers/conversion_helper.rb +39 -0
- data/lib/deltacloud/helpers/hardware_profiles_helper.rb +35 -0
- data/lib/deltacloud/models/base_model.rb +58 -0
- data/lib/deltacloud/models/image.rb +26 -0
- data/lib/deltacloud/models/instance.rb +37 -0
- data/lib/deltacloud/models/instance_profile.rb +47 -0
- data/lib/deltacloud/models/realm.rb +25 -0
- data/lib/deltacloud/models/storage_snapshot.rb +26 -0
- data/lib/deltacloud/models/storage_volume.rb +27 -0
- data/lib/deltacloud/state_machine.rb +84 -0
- data/lib/deltacloud/validation.rb +70 -0
- data/lib/drivers.rb +37 -0
- data/lib/sinatra/lazy_auth.rb +56 -0
- data/lib/sinatra/rabbit.rb +272 -0
- data/lib/sinatra/respond_to.rb +262 -0
- data/lib/sinatra/static_assets.rb +83 -0
- data/lib/sinatra/url_for.rb +44 -0
- data/public/favicon.ico +0 -0
- data/public/images/grid.png +0 -0
- data/public/images/logo-wide.png +0 -0
- data/public/images/rails.png +0 -0
- data/public/images/topbar-bg.png +0 -0
- data/public/javascripts/application.js +2 -0
- data/public/javascripts/controls.js +963 -0
- data/public/javascripts/dragdrop.js +973 -0
- data/public/javascripts/effects.js +1128 -0
- data/public/javascripts/prototype.js +4320 -0
- data/public/stylesheets/compiled/application.css +613 -0
- data/public/stylesheets/compiled/ie.css +31 -0
- data/public/stylesheets/compiled/print.css +27 -0
- data/public/stylesheets/compiled/screen.css +456 -0
- data/server.rb +340 -0
- data/tests/deltacloud_test.rb +60 -0
- data/tests/images_test.rb +94 -0
- data/tests/instances_test.rb +136 -0
- data/tests/realms_test.rb +56 -0
- data/tests/storage_snapshots_test.rb +48 -0
- data/tests/storage_volumes_test.rb +48 -0
- data/views/accounts/index.html.haml +11 -0
- data/views/accounts/show.html.haml +30 -0
- data/views/api/show.html.haml +15 -0
- data/views/api/show.xml.haml +5 -0
- data/views/docs/collection.html.haml +37 -0
- data/views/docs/collection.xml.haml +14 -0
- data/views/docs/index.html.haml +15 -0
- data/views/docs/index.xml.haml +5 -0
- data/views/docs/operation.html.haml +31 -0
- data/views/docs/operation.xml.haml +10 -0
- data/views/errors/auth_exception.html.haml +8 -0
- data/views/errors/auth_exception.xml.haml +2 -0
- data/views/errors/backend_error.html.haml +17 -0
- data/views/errors/backend_error.xml.haml +8 -0
- data/views/errors/validation_failure.html.haml +11 -0
- data/views/errors/validation_failure.xml.haml +7 -0
- data/views/hardware_profiles/index.html.haml +25 -0
- data/views/hardware_profiles/index.xml.haml +4 -0
- data/views/hardware_profiles/show.html.haml +19 -0
- data/views/hardware_profiles/show.xml.haml +17 -0
- data/views/images/index.html.haml +30 -0
- data/views/images/index.xml.haml +7 -0
- data/views/images/show.html.haml +21 -0
- data/views/images/show.xml.haml +5 -0
- data/views/instance_states/show.gv.erb +45 -0
- data/views/instance_states/show.html.haml +31 -0
- data/views/instance_states/show.xml.haml +8 -0
- data/views/instances/index.html.haml +29 -0
- data/views/instances/index.xml.haml +23 -0
- data/views/instances/new.html.haml +49 -0
- data/views/instances/show.html.haml +42 -0
- data/views/instances/show.xml.haml +28 -0
- data/views/layout.html.haml +23 -0
- data/views/realms/index.html.haml +29 -0
- data/views/realms/index.xml.haml +12 -0
- data/views/realms/show.html.haml +15 -0
- data/views/realms/show.xml.haml +10 -0
- data/views/root/index.html.haml +4 -0
- data/views/storage_snapshots/index.html.haml +20 -0
- data/views/storage_snapshots/index.xml.haml +11 -0
- data/views/storage_snapshots/show.html.haml +14 -0
- data/views/storage_snapshots/show.xml.haml +9 -0
- data/views/storage_volumes/index.html.haml +21 -0
- data/views/storage_volumes/index.xml.haml +13 -0
- data/views/storage_volumes/show.html.haml +20 -0
- data/views/storage_volumes/show.xml.haml +13 -0
- metadata +311 -0
@@ -0,0 +1,275 @@
|
|
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
|
+
|
21
|
+
module Deltacloud
|
22
|
+
module Drivers
|
23
|
+
module Mock
|
24
|
+
class MockDriver < Deltacloud::BaseDriver
|
25
|
+
|
26
|
+
( REALMS = [
|
27
|
+
Realm.new({
|
28
|
+
:id=>'us',
|
29
|
+
:name=>'United States',
|
30
|
+
:limit=>:unlimited,
|
31
|
+
:state=>'AVAILABLE',
|
32
|
+
}),
|
33
|
+
Realm.new({
|
34
|
+
:id=>'eu',
|
35
|
+
:name=>'Europe',
|
36
|
+
:limit=>:unlimited,
|
37
|
+
:state=>'AVAILABLE',
|
38
|
+
}),
|
39
|
+
] ) unless defined?( REALMS )
|
40
|
+
|
41
|
+
define_hardware_profile('m1-small') do
|
42
|
+
cpu 1
|
43
|
+
memory 1.7 * 1024
|
44
|
+
storage 160
|
45
|
+
architecture 'i386'
|
46
|
+
end
|
47
|
+
|
48
|
+
define_hardware_profile('m1-large') do
|
49
|
+
cpu 2
|
50
|
+
memory (7.5*1024 .. 15*1024), :default => 10 * 1024
|
51
|
+
storage [ 850, 1024 ]
|
52
|
+
architecture 'x86_64'
|
53
|
+
end
|
54
|
+
|
55
|
+
define_hardware_profile('m1-xlarge') do
|
56
|
+
cpu 4
|
57
|
+
memory (12*1024 .. 32*1024)
|
58
|
+
storage [ 1024, 2048, 4096 ]
|
59
|
+
architecture 'x86_64'
|
60
|
+
end
|
61
|
+
|
62
|
+
# Some clouds tell us nothing about hardware profiles (e.g., OpenNebula)
|
63
|
+
define_hardware_profile 'opaque'
|
64
|
+
|
65
|
+
define_instance_states do
|
66
|
+
start.to( :pending ) .on( :create )
|
67
|
+
|
68
|
+
pending.to( :running ) .automatically
|
69
|
+
|
70
|
+
running.to( :running ) .on( :reboot )
|
71
|
+
running.to( :stopped ) .on( :stop )
|
72
|
+
|
73
|
+
stopped.to( :running ) .on( :start )
|
74
|
+
stopped.to( :finish ) .on( :destroy )
|
75
|
+
end
|
76
|
+
|
77
|
+
feature :instances, :user_name
|
78
|
+
|
79
|
+
def initialize
|
80
|
+
if ENV["DELTACLOUD_MOCK_STORAGE"]
|
81
|
+
@storage_root = ENV["DELTACLOUD_MOCK_STORAGE"]
|
82
|
+
elsif ENV["USER"]
|
83
|
+
@storage_root = File::join("/var/tmp", "deltacloud-mock-#{ENV["USER"]}")
|
84
|
+
else
|
85
|
+
raise "Please set either the DELTACLOUD_MOCK_STORAGE or USER environment variable"
|
86
|
+
end
|
87
|
+
if ! File::directory?(@storage_root)
|
88
|
+
FileUtils::rm_rf(@storage_root)
|
89
|
+
FileUtils::mkdir_p(@storage_root)
|
90
|
+
data = Dir::glob(File::join(File::dirname(__FILE__), "data", "*"))
|
91
|
+
FileUtils::cp_r(data, @storage_root)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def realms(credentials, opts=nil)
|
96
|
+
return REALMS if ( opts.nil? )
|
97
|
+
results = REALMS
|
98
|
+
results = filter_on( results, :id, opts )
|
99
|
+
results
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# Images
|
104
|
+
#
|
105
|
+
|
106
|
+
def images(credentials, opts=nil )
|
107
|
+
check_credentials( credentials )
|
108
|
+
images = []
|
109
|
+
Dir[ "#{@storage_root}/images/*.yml" ].each do |image_file|
|
110
|
+
image = YAML.load( File.read( image_file ) )
|
111
|
+
image[:id] = File.basename( image_file, ".yml" )
|
112
|
+
image[:name] = image[:description]
|
113
|
+
images << Image.new( image )
|
114
|
+
end
|
115
|
+
images = filter_on( images, :id, opts )
|
116
|
+
images = filter_on( images, :architecture, opts )
|
117
|
+
if ( opts && opts[:owner_id] == 'self' )
|
118
|
+
images = images.select{|e| e.owner_id == credentials.user }
|
119
|
+
else
|
120
|
+
images = filter_on( images, :owner_id, opts )
|
121
|
+
end
|
122
|
+
images.sort_by{|e| [e.owner_id,e.description]}
|
123
|
+
end
|
124
|
+
|
125
|
+
#
|
126
|
+
# Instances
|
127
|
+
#
|
128
|
+
|
129
|
+
def instances(credentials, opts=nil)
|
130
|
+
check_credentials( credentials )
|
131
|
+
instances = []
|
132
|
+
Dir[ "#{@storage_root}/instances/*.yml" ].each do |instance_file|
|
133
|
+
instance = YAML.load( File.read( instance_file ) )
|
134
|
+
if ( instance[:owner_id] == credentials.user )
|
135
|
+
instance[:id] = File.basename( instance_file, ".yml" )
|
136
|
+
instance[:actions] = instance_actions_for( instance[:state] )
|
137
|
+
instances << Instance.new( instance )
|
138
|
+
end
|
139
|
+
end
|
140
|
+
instances = filter_on( instances, :id, opts )
|
141
|
+
instances = filter_on( instances, :state, opts )
|
142
|
+
instances
|
143
|
+
end
|
144
|
+
|
145
|
+
def create_instance(credentials, image_id, opts)
|
146
|
+
check_credentials( credentials )
|
147
|
+
ids = Dir[ "#{@storage_root}/instances/*.yml" ].collect{|e| File.basename( e, ".yml" )}
|
148
|
+
|
149
|
+
count = 0
|
150
|
+
while true
|
151
|
+
next_id = "inst" + count.to_s
|
152
|
+
if not ids.include?(next_id)
|
153
|
+
break
|
154
|
+
end
|
155
|
+
count = count + 1
|
156
|
+
end
|
157
|
+
|
158
|
+
realm_id = opts[:realm_id]
|
159
|
+
if ( realm_id.nil? )
|
160
|
+
realm = realms(credentials).first
|
161
|
+
( realm_id = realm.id ) if realm
|
162
|
+
end
|
163
|
+
|
164
|
+
hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id)
|
165
|
+
|
166
|
+
name = opts[:name] || "i-#{Time.now.to_i}"
|
167
|
+
|
168
|
+
instance = {
|
169
|
+
:name=>name,
|
170
|
+
:state=>'RUNNING',
|
171
|
+
:image_id=>image_id,
|
172
|
+
:owner_id=>credentials.user,
|
173
|
+
:public_addresses=>["#{image_id}.#{next_id}.public.com"],
|
174
|
+
:private_addresses=>["#{image_id}.#{next_id}.private.com"],
|
175
|
+
:instance_profile => InstanceProfile.new(hwp.name, opts),
|
176
|
+
:realm_id=>realm_id,
|
177
|
+
:actions=>instance_actions_for( 'RUNNING' )
|
178
|
+
}
|
179
|
+
File.open( "#{@storage_root}/instances/#{next_id}.yml", 'w' ) {|f|
|
180
|
+
YAML.dump( instance, f )
|
181
|
+
}
|
182
|
+
instance[:id] = next_id
|
183
|
+
Instance.new( instance )
|
184
|
+
end
|
185
|
+
|
186
|
+
def start_instance(credentials, id)
|
187
|
+
instance_file = "#{@storage_root}/instances/#{id}.yml"
|
188
|
+
instance_yml = YAML.load( File.read( instance_file ) )
|
189
|
+
instance_yml[:state] = 'RUNNING'
|
190
|
+
File.open( instance_file, 'w' ) do |f|
|
191
|
+
f << YAML.dump( instance_yml )
|
192
|
+
end
|
193
|
+
Instance.new( instance_yml )
|
194
|
+
end
|
195
|
+
|
196
|
+
def reboot_instance(credentials, id)
|
197
|
+
instance_file = "#{@storage_root}/instances/#{id}.yml"
|
198
|
+
instance_yml = YAML.load( File.read( instance_file ) )
|
199
|
+
instance_yml[:state] = 'RUNNING'
|
200
|
+
File.open( instance_file, 'w' ) do |f|
|
201
|
+
f << YAML.dump( instance_yml )
|
202
|
+
end
|
203
|
+
Instance.new( instance_yml )
|
204
|
+
end
|
205
|
+
|
206
|
+
def stop_instance(credentials, id)
|
207
|
+
instance_file = "#{@storage_root}/instances/#{id}.yml"
|
208
|
+
instance_yml = YAML.load( File.read( instance_file ) )
|
209
|
+
instance_yml[:state] = 'STOPPED'
|
210
|
+
File.open( instance_file, 'w' ) do |f|
|
211
|
+
f << YAML.dump( instance_yml )
|
212
|
+
end
|
213
|
+
Instance.new( instance_yml )
|
214
|
+
end
|
215
|
+
|
216
|
+
|
217
|
+
def destroy_instance(credentials, id)
|
218
|
+
check_credentials( credentials )
|
219
|
+
FileUtils.rm( "#{@storage_root}/instances/#{id}.yml" )
|
220
|
+
end
|
221
|
+
|
222
|
+
#
|
223
|
+
# Storage Volumes
|
224
|
+
#
|
225
|
+
|
226
|
+
def storage_volumes(credentials, opts=nil)
|
227
|
+
check_credentials( credentials )
|
228
|
+
volumes = []
|
229
|
+
Dir[ "#{@storage_root}/storage_volumes/*.yml" ].each do |storage_volume_file|
|
230
|
+
storage_volume = YAML.load( File.read( storage_volume_file ) )
|
231
|
+
if ( storage_volume[:owner_id] == credentials.user )
|
232
|
+
storage_volume[:id] = File.basename( storage_volume_file, ".yml" )
|
233
|
+
volumes << StorageVolume.new( storage_volume )
|
234
|
+
end
|
235
|
+
end
|
236
|
+
volumes = filter_on( volumes, :id, opts )
|
237
|
+
volumes
|
238
|
+
end
|
239
|
+
|
240
|
+
#
|
241
|
+
# Storage Snapshots
|
242
|
+
#
|
243
|
+
|
244
|
+
def storage_snapshots(credentials, opts=nil)
|
245
|
+
check_credentials( credentials )
|
246
|
+
snapshots = []
|
247
|
+
Dir[ "#{@storage_root}/storage_snapshots/*.yml" ].each do |storage_snapshot_file|
|
248
|
+
storage_snapshot = YAML.load( File.read( storage_snapshot_file ) )
|
249
|
+
if ( storage_snapshot[:owner_id] == credentials.user )
|
250
|
+
storage_snapshot[:id] = File.basename( storage_snapshot_file, ".yml" )
|
251
|
+
snapshots << StorageSnapshot.new( storage_snapshot )
|
252
|
+
end
|
253
|
+
end
|
254
|
+
snapshots = filter_on( snapshots, :id, opts )
|
255
|
+
snapshots
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
|
260
|
+
def check_credentials(credentials)
|
261
|
+
if ( credentials.user != 'mockuser' )
|
262
|
+
raise Deltacloud::AuthException.new
|
263
|
+
end
|
264
|
+
|
265
|
+
if ( credentials.password != 'mockpassword' )
|
266
|
+
raise Deltacloud::AuthException.new
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
#--------------------------------------------------------------------------- #
|
2
|
+
# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad
|
3
|
+
# Complutense de Madrid (dsa-research.org)
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
#--------------------------------------------------------------------------- #
|
19
|
+
|
20
|
+
require 'rubygems'
|
21
|
+
require 'uri'
|
22
|
+
|
23
|
+
require 'net/https'
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'net/http/post/multipart'
|
27
|
+
rescue LoadError
|
28
|
+
end
|
29
|
+
|
30
|
+
###############################################################################
|
31
|
+
# The CloudClient module contains general functionality to implement a
|
32
|
+
# Cloud Client
|
33
|
+
###############################################################################
|
34
|
+
module CloudClient
|
35
|
+
# #########################################################################
|
36
|
+
# Default location for the authentication file
|
37
|
+
# #########################################################################
|
38
|
+
DEFAULT_AUTH_FILE = ENV["HOME"]+"/.one/one_auth"
|
39
|
+
|
40
|
+
# #########################################################################
|
41
|
+
# Gets authorization credentials from ONE_AUTH or default
|
42
|
+
# auth file.
|
43
|
+
#
|
44
|
+
# Raises an error if authorization is not found
|
45
|
+
# #########################################################################
|
46
|
+
def self.get_one_auth
|
47
|
+
if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and
|
48
|
+
File.file?(ENV["ONE_AUTH"])
|
49
|
+
one_auth=File.read(ENV["ONE_AUTH"]).strip.split(':')
|
50
|
+
elsif File.file?(DEFAULT_AUTH_FILE)
|
51
|
+
one_auth=File.read(DEFAULT_AUTH_FILE).strip.split(':')
|
52
|
+
else
|
53
|
+
raise "No authorization data present"
|
54
|
+
end
|
55
|
+
|
56
|
+
raise "Authorization data malformed" if one_auth.length < 2
|
57
|
+
|
58
|
+
one_auth
|
59
|
+
end
|
60
|
+
|
61
|
+
# #########################################################################
|
62
|
+
# Starts an http connection and calls the block provided. SSL flag
|
63
|
+
# is set if needed.
|
64
|
+
# #########################################################################
|
65
|
+
def self.http_start(url, &block)
|
66
|
+
http = Net::HTTP.new(url.host, url.port)
|
67
|
+
if url.scheme=='https'
|
68
|
+
http.use_ssl = true
|
69
|
+
http.verify_mode=OpenSSL::SSL::VERIFY_NONE
|
70
|
+
end
|
71
|
+
|
72
|
+
begin
|
73
|
+
http.start do |connection|
|
74
|
+
block.call(connection)
|
75
|
+
end
|
76
|
+
rescue Errno::ECONNREFUSED => e
|
77
|
+
str = "Error connecting to server (#{e.to_s})."
|
78
|
+
str << "Server: #{url.host}:#{url.port}"
|
79
|
+
|
80
|
+
return CloudClient::Error.new(str)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# #########################################################################
|
85
|
+
# The Error Class represents a generic error in the Cloud Client
|
86
|
+
# library. It contains a readable representation of the error.
|
87
|
+
# #########################################################################
|
88
|
+
class Error
|
89
|
+
attr_reader :message
|
90
|
+
|
91
|
+
# +message+ a description of the error
|
92
|
+
def initialize(message=nil)
|
93
|
+
@message=message
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_s()
|
97
|
+
@message
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# #########################################################################
|
102
|
+
# Returns true if the object returned by a method of the OpenNebula
|
103
|
+
# library is an Error
|
104
|
+
# #########################################################################
|
105
|
+
def self.is_error?(value)
|
106
|
+
value.class==CloudClient::Error
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Command line help functions
|
111
|
+
module CloudCLI
|
112
|
+
# Returns the command name
|
113
|
+
def cmd_name
|
114
|
+
File.basename($0)
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
#--------------------------------------------------------------------------- #
|
2
|
+
# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad
|
3
|
+
# Complutense de Madrid (dsa-research.org)
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
#--------------------------------------------------------------------------- #
|
19
|
+
|
20
|
+
require 'rubygems'
|
21
|
+
require 'uri'
|
22
|
+
require 'rexml/document'
|
23
|
+
|
24
|
+
require 'deltacloud/drivers/opennebula/cloud_client'
|
25
|
+
|
26
|
+
|
27
|
+
module OCCIClient
|
28
|
+
|
29
|
+
#####################################################################
|
30
|
+
# Client Library to interface with the OpenNebula OCCI Service
|
31
|
+
#####################################################################
|
32
|
+
class Client
|
33
|
+
|
34
|
+
######################################################################
|
35
|
+
# Initialize client library
|
36
|
+
######################################################################
|
37
|
+
def initialize(endpoint_str=nil, user=nil, pass=nil, debug_flag=true)
|
38
|
+
@debug = debug_flag
|
39
|
+
|
40
|
+
# Server location
|
41
|
+
if endpoint_str
|
42
|
+
@endpoint = endpoint_str
|
43
|
+
elsif ENV["OCCI_URL"]
|
44
|
+
@endpoint = ENV["OCCI_URL"]
|
45
|
+
else
|
46
|
+
@endpoint = "http://localhost:4567"
|
47
|
+
end
|
48
|
+
|
49
|
+
# Autentication
|
50
|
+
if user && pass
|
51
|
+
@occiauth = [user, pass]
|
52
|
+
else
|
53
|
+
@occiauth = CloudClient::get_one_auth
|
54
|
+
end
|
55
|
+
|
56
|
+
if !@occiauth
|
57
|
+
raise "No authorization data present"
|
58
|
+
end
|
59
|
+
|
60
|
+
@occiauth[1] = Digest::SHA1.hexdigest(@occiauth[1])
|
61
|
+
end
|
62
|
+
|
63
|
+
#################################
|
64
|
+
# Pool Resource Request Methods #
|
65
|
+
#################################
|
66
|
+
|
67
|
+
######################################################################
|
68
|
+
# Post a new VM to the VM Pool
|
69
|
+
# :instance_type
|
70
|
+
# :xmlfile
|
71
|
+
######################################################################
|
72
|
+
def post_vms(xmlfile)
|
73
|
+
xml=File.read(xmlfile)
|
74
|
+
|
75
|
+
url = URI.parse(@endpoint+"/compute")
|
76
|
+
|
77
|
+
req = Net::HTTP::Post.new(url.path)
|
78
|
+
req.body=xml
|
79
|
+
|
80
|
+
req.basic_auth @occiauth[0], @occiauth[1]
|
81
|
+
|
82
|
+
res = CloudClient::http_start(url) do |http|
|
83
|
+
http.request(req)
|
84
|
+
end
|
85
|
+
|
86
|
+
if CloudClient::is_error?(res)
|
87
|
+
return res
|
88
|
+
else
|
89
|
+
return res.body
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
######################################################################
|
94
|
+
# Retieves the pool of Virtual Machines
|
95
|
+
######################################################################
|
96
|
+
def get_vms
|
97
|
+
url = URI.parse(@endpoint+"/compute")
|
98
|
+
req = Net::HTTP::Get.new(url.path)
|
99
|
+
|
100
|
+
req.basic_auth @occiauth[0], @occiauth[1]
|
101
|
+
|
102
|
+
res = CloudClient::http_start(url) {|http|
|
103
|
+
http.request(req)
|
104
|
+
}
|
105
|
+
|
106
|
+
if CloudClient::is_error?(res)
|
107
|
+
return res
|
108
|
+
else
|
109
|
+
return res.body
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
######################################################################
|
114
|
+
# Retieves the pool of Images owned by the user
|
115
|
+
######################################################################
|
116
|
+
def get_images
|
117
|
+
url = URI.parse(@endpoint+"/storage")
|
118
|
+
req = Net::HTTP::Get.new(url.path)
|
119
|
+
|
120
|
+
req.basic_auth @occiauth[0], @occiauth[1]
|
121
|
+
|
122
|
+
res = CloudClient::http_start(url) {|http|
|
123
|
+
http.request(req)
|
124
|
+
}
|
125
|
+
|
126
|
+
if CloudClient::is_error?(res)
|
127
|
+
return res
|
128
|
+
else
|
129
|
+
return res.body
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
####################################
|
134
|
+
# Entity Resource Request Methods #
|
135
|
+
####################################
|
136
|
+
|
137
|
+
######################################################################
|
138
|
+
# :id VM identifier
|
139
|
+
######################################################################
|
140
|
+
def get_vm(id)
|
141
|
+
url = URI.parse(@endpoint+"/compute/" + id.to_s)
|
142
|
+
req = Net::HTTP::Get.new(url.path)
|
143
|
+
|
144
|
+
req.basic_auth @occiauth[0], @occiauth[1]
|
145
|
+
|
146
|
+
res = CloudClient::http_start(url) {|http|
|
147
|
+
http.request(req)
|
148
|
+
}
|
149
|
+
|
150
|
+
if CloudClient::is_error?(res)
|
151
|
+
return res
|
152
|
+
else
|
153
|
+
return res.body
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
######################################################################
|
158
|
+
# Puts a new Compute representation in order to change its state
|
159
|
+
# :xmlfile Compute OCCI xml representation
|
160
|
+
######################################################################
|
161
|
+
def put_vm(xmlfile)
|
162
|
+
xml=File.read(xmlfile)
|
163
|
+
vm_info=REXML::Document.new(xml).root.elements
|
164
|
+
|
165
|
+
url = URI.parse(@endpoint+'/compute/' + vm_info['ID'].text)
|
166
|
+
|
167
|
+
req = Net::HTTP::Put.new(url.path)
|
168
|
+
req.body = xml
|
169
|
+
|
170
|
+
req.basic_auth @occiauth[0], @occiauth[1]
|
171
|
+
|
172
|
+
res = CloudClient::http_start(url) do |http|
|
173
|
+
http.request(req)
|
174
|
+
end
|
175
|
+
|
176
|
+
if CloudClient::is_error?(res)
|
177
|
+
return res
|
178
|
+
else
|
179
|
+
return res.body
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
#######################################################################
|
184
|
+
# Retieves an Image
|
185
|
+
# :image_uuid Image identifier
|
186
|
+
######################################################################
|
187
|
+
def get_image(image_uuid)
|
188
|
+
url = URI.parse(@endpoint+"/storage/"+image_uuid)
|
189
|
+
req = Net::HTTP::Get.new(url.path)
|
190
|
+
|
191
|
+
req.basic_auth @occiauth[0], @occiauth[1]
|
192
|
+
|
193
|
+
res = CloudClient::http_start(url) {|http|
|
194
|
+
http.request(req)
|
195
|
+
}
|
196
|
+
|
197
|
+
if CloudClient::is_error?(res)
|
198
|
+
return res
|
199
|
+
else
|
200
|
+
return res.body
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|