deltacloud-core 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (294) hide show
  1. data/NOTICE +1 -1
  2. data/Rakefile +63 -21
  3. data/bin/deltacloudd +10 -6
  4. data/config.ru +62 -6
  5. data/config/drivers/ec2.yaml +8 -0
  6. data/config/drivers/fgcp.yaml +13 -0
  7. data/deltacloud-core.gemspec +10 -2
  8. data/lib/cimi/collections.rb +58 -0
  9. data/lib/cimi/collections/address_templates.rb +49 -0
  10. data/lib/cimi/collections/addresses.rb +74 -0
  11. data/lib/cimi/collections/cloud_entry_point.rb +37 -0
  12. data/lib/{deltacloud/helpers/conversion_helper.rb → cimi/collections/entity_metadata.rb} +24 -16
  13. data/lib/cimi/collections/machine_admins.rb +74 -0
  14. data/lib/cimi/collections/machine_configurations.rb +49 -0
  15. data/lib/cimi/collections/machine_images.rb +50 -0
  16. data/lib/cimi/collections/machines.rb +157 -0
  17. data/lib/cimi/collections/network_configurations.rb +47 -0
  18. data/lib/cimi/collections/network_templates.rb +48 -0
  19. data/lib/cimi/collections/networks.rb +125 -0
  20. data/lib/cimi/collections/routing_group_templates.rb +47 -0
  21. data/lib/cimi/collections/routing_groups.rb +48 -0
  22. data/lib/cimi/collections/volume_configurations.rb +48 -0
  23. data/lib/cimi/collections/volume_images.rb +50 -0
  24. data/lib/cimi/collections/volumes.rb +80 -0
  25. data/lib/cimi/collections/vsp_configurations.rb +48 -0
  26. data/lib/cimi/collections/vsp_templates.rb +50 -0
  27. data/lib/cimi/collections/vsps.rb +108 -0
  28. data/lib/cimi/dependencies.rb +1 -0
  29. data/lib/cimi/helpers.rb +116 -0
  30. data/lib/cimi/helpers/cimi_helper.rb +6 -2
  31. data/lib/cimi/models.rb +72 -0
  32. data/lib/cimi/{model → models}/action.rb +1 -1
  33. data/lib/cimi/models/address.rb +72 -0
  34. data/lib/cimi/models/address_collection.rb +34 -0
  35. data/lib/cimi/models/address_template.rb +54 -0
  36. data/lib/cimi/models/address_template_collection.rb +34 -0
  37. data/lib/cimi/{model → models}/base.rb +2 -2
  38. data/lib/cimi/{model → models}/cloud_entry_point.rb +3 -5
  39. data/lib/cimi/{model → models}/entity_metadata.rb +7 -6
  40. data/lib/cimi/{model → models}/entity_metadata_collection.rb +2 -2
  41. data/lib/cimi/{model → models}/errors.rb +8 -0
  42. data/lib/cimi/{model → models}/machine.rb +10 -10
  43. data/lib/cimi/{model → models}/machine_admin.rb +1 -1
  44. data/lib/cimi/{model → models}/machine_admin_collection.rb +2 -2
  45. data/lib/cimi/{model → models}/machine_collection.rb +2 -2
  46. data/lib/cimi/{model → models}/machine_configuration.rb +9 -6
  47. data/lib/cimi/{model → models}/machine_configuration_collection.rb +2 -2
  48. data/lib/cimi/{model → models}/machine_image.rb +1 -1
  49. data/lib/cimi/{model → models}/machine_image_collection.rb +2 -2
  50. data/lib/cimi/{model → models}/machine_template.rb +0 -0
  51. data/lib/cimi/{model → models}/machine_template_collection.rb +2 -2
  52. data/lib/cimi/models/network.rb +109 -0
  53. data/lib/cimi/{model → models}/network_collection.rb +2 -2
  54. data/lib/cimi/{model → models}/network_configuration.rb +3 -4
  55. data/lib/cimi/{model → models}/network_configuration_collection.rb +2 -2
  56. data/lib/cimi/models/network_template.rb +36 -0
  57. data/lib/cimi/models/network_template_collection.rb +35 -0
  58. data/lib/cimi/models/routing_group.rb +34 -0
  59. data/lib/cimi/models/routing_group_collection.rb +34 -0
  60. data/lib/cimi/models/routing_group_template.rb +34 -0
  61. data/lib/cimi/models/routing_group_template_collection.rb +35 -0
  62. data/lib/cimi/{model → models}/schema.rb +0 -0
  63. data/lib/cimi/{model → models}/volume.rb +4 -4
  64. data/lib/cimi/{model → models}/volume_collection.rb +2 -2
  65. data/lib/cimi/{model → models}/volume_configuration.rb +1 -1
  66. data/lib/cimi/{model → models}/volume_configuration_collection.rb +2 -2
  67. data/lib/cimi/{model → models}/volume_image.rb +1 -1
  68. data/lib/cimi/{model → models}/volume_image_collection.rb +2 -2
  69. data/lib/cimi/{model → models}/volume_template.rb +0 -0
  70. data/lib/cimi/{model → models}/volume_template_collection.rb +2 -2
  71. data/lib/cimi/models/vsp.rb +102 -0
  72. data/lib/cimi/models/vsp_collection.rb +34 -0
  73. data/lib/cimi/models/vsp_configuration.rb +40 -0
  74. data/lib/cimi/models/vsp_configuration_collection.rb +34 -0
  75. data/lib/cimi/models/vsp_template.rb +34 -0
  76. data/lib/cimi/models/vsp_template_collection.rb +34 -0
  77. data/lib/cimi/server.rb +27 -549
  78. data/lib/deltacloud/api.rb +79 -0
  79. data/lib/deltacloud/collections.rb +54 -0
  80. data/lib/deltacloud/collections/addresses.rb +91 -0
  81. data/lib/deltacloud/collections/buckets.rb +273 -0
  82. data/lib/deltacloud/collections/drivers.rb +51 -0
  83. data/lib/deltacloud/collections/firewalls.rb +116 -0
  84. data/lib/deltacloud/collections/hardware_profiles.rb +29 -0
  85. data/lib/deltacloud/collections/images.rb +73 -0
  86. data/lib/deltacloud/collections/instance_states.rb +59 -0
  87. data/lib/deltacloud/collections/instances.rb +113 -0
  88. data/lib/deltacloud/collections/keys.rb +61 -0
  89. data/lib/deltacloud/collections/load_balancers.rb +102 -0
  90. data/lib/deltacloud/collections/metrics.rb +28 -0
  91. data/lib/deltacloud/collections/realms.rb +28 -0
  92. data/lib/deltacloud/collections/storage_snapshots.rb +51 -0
  93. data/lib/deltacloud/collections/storage_volumes.rb +99 -0
  94. data/lib/deltacloud/core_ext.rb +14 -6
  95. data/lib/deltacloud/core_ext/string.rb +17 -5
  96. data/lib/deltacloud/drivers.rb +6 -42
  97. data/lib/deltacloud/drivers/azure/azure_driver.rb +0 -4
  98. data/lib/deltacloud/{base_driver → drivers}/base_driver.rb +56 -18
  99. data/lib/deltacloud/drivers/condor/condor_driver.rb +1 -8
  100. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +142 -33
  101. data/lib/deltacloud/drivers/eucalyptus/eucalyptus_driver.rb +2 -6
  102. data/lib/deltacloud/{base_driver → drivers}/exceptions.rb +25 -2
  103. data/lib/deltacloud/drivers/features.rb +154 -0
  104. data/lib/deltacloud/drivers/fgcp/fgcp_client.rb +387 -0
  105. data/lib/deltacloud/drivers/fgcp/fgcp_driver.rb +1435 -0
  106. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +8 -11
  107. data/lib/deltacloud/drivers/google/google_driver.rb +2 -5
  108. data/lib/deltacloud/drivers/mock/data/keys/test-key.yml +28 -0
  109. data/lib/deltacloud/drivers/mock/mock_client.rb +2 -2
  110. data/lib/deltacloud/drivers/mock/mock_driver.rb +58 -40
  111. data/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb +145 -0
  112. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +107 -73
  113. data/lib/deltacloud/drivers/opennebula/occi_client.rb +285 -145
  114. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +189 -126
  115. data/lib/deltacloud/drivers/openstack/openstack_driver.rb +427 -8
  116. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +7 -9
  117. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +48 -66
  118. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +44 -51
  119. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +7 -8
  120. data/lib/deltacloud/drivers/sbc/sbc_client.rb +1 -1
  121. data/lib/deltacloud/drivers/sbc/sbc_driver.rb +3 -3
  122. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +8 -8
  123. data/lib/deltacloud/drivers/vsphere/vsphere_driver.rb +39 -13
  124. data/lib/deltacloud/drivers/vsphere/vsphere_filemanager.rb +1 -0
  125. data/lib/deltacloud/helpers.rb +79 -7
  126. data/lib/{sinatra/lazy_auth.rb → deltacloud/helpers/auth_helper.rb} +5 -9
  127. data/lib/deltacloud/helpers/{blob_stream.rb → blob_stream_helper.rb} +7 -7
  128. data/lib/deltacloud/helpers/deltacloud_helper.rb +286 -0
  129. data/lib/deltacloud/helpers/driver_helper.rb +58 -0
  130. data/lib/deltacloud/helpers/rabbit_helper.rb +34 -0
  131. data/lib/{sinatra/url_for.rb → deltacloud/helpers/url_helper.rb} +31 -9
  132. data/lib/deltacloud/models.rb +19 -17
  133. data/lib/deltacloud/models/bucket.rb +4 -0
  134. data/lib/deltacloud/{hardware_profile.rb → models/hardware_profile.rb} +4 -2
  135. data/lib/deltacloud/models/image.rb +1 -0
  136. data/lib/deltacloud/models/instance.rb +4 -2
  137. data/lib/deltacloud/models/instance_address.rb +4 -0
  138. data/lib/deltacloud/models/key.rb +4 -0
  139. data/lib/deltacloud/models/metric.rb +40 -0
  140. data/lib/deltacloud/{state_machine.rb → models/state_machine.rb} +18 -1
  141. data/lib/deltacloud/server.rb +41 -1222
  142. data/lib/deltacloud_rack.rb +79 -0
  143. data/lib/{cimi/model/network_template.rb → ec2/helpers.rb} +7 -10
  144. data/lib/{deltacloud/backend_capability.rb → ec2/helpers/errors.rb} +24 -29
  145. data/lib/ec2/helpers/result.rb +31 -0
  146. data/lib/ec2/query_parser.rb +152 -0
  147. data/lib/ec2/server.rb +70 -0
  148. data/lib/{deltacloud.rb → sinatra.rb} +7 -12
  149. data/lib/sinatra/rack_accept.rb +13 -7
  150. data/lib/sinatra/rack_driver_select.rb +1 -1
  151. data/lib/sinatra/rack_matrix_params.rb +1 -3
  152. data/public/images/metric.png +0 -0
  153. data/public/javascripts/jquery.mobile-1.0.1.min.js +177 -0
  154. data/public/stylesheets/jquery.mobile-1.0.1.min.css +2 -0
  155. data/public/stylesheets/new.css +10 -0
  156. data/support/fedora/deltacloud-core.spec +1 -1
  157. data/tests/api/common.rb +1 -0
  158. data/tests/api/driver_test.rb +79 -0
  159. data/tests/api/library_test.rb +48 -0
  160. data/tests/cimi/features/step_definitions/common_steps.rb +2 -2
  161. data/tests/cimi/features/step_definitions/machines_steps.rb +5 -4
  162. data/tests/cimi/features/step_definitions/volumes_steps.rb +8 -8
  163. data/tests/cimi/features/support/env.rb +33 -11
  164. data/tests/common.rb +7 -8
  165. data/tests/drivers/ec2/api_test.rb +19 -0
  166. data/tests/drivers/ec2/common.rb +23 -0
  167. data/tests/drivers/ec2/drivers_test.rb +120 -0
  168. data/tests/drivers/ec2/hardware_profiles_test.rb +224 -0
  169. data/tests/drivers/ec2/images_test.rb +230 -0
  170. data/tests/drivers/ec2/instances_test.rb +356 -0
  171. data/tests/drivers/ec2/keys_test.rb +181 -0
  172. data/tests/drivers/ec2/realms_test.rb +146 -0
  173. data/tests/drivers/fgcp/api_test.rb +47 -0
  174. data/tests/drivers/fgcp/hardware_profiles_test.rb +54 -0
  175. data/tests/drivers/fgcp/realms_test.rb +42 -0
  176. data/tests/drivers/{rackspace → fgcp}/setup.rb +5 -6
  177. data/tests/drivers/google/api_test.rb +10 -26
  178. data/tests/drivers/google/buckets_test.rb +79 -95
  179. data/tests/drivers/google/common.rb +54 -0
  180. data/tests/drivers/mock/api_test.rb +4 -127
  181. data/tests/drivers/mock/buckets_test.rb +195 -0
  182. data/tests/drivers/mock/common.rb +7 -0
  183. data/tests/drivers/mock/drivers_test.rb +123 -0
  184. data/tests/drivers/mock/hardware_profiles_test.rb +190 -100
  185. data/tests/drivers/mock/images_test.rb +162 -103
  186. data/tests/drivers/mock/instances_test.rb +310 -220
  187. data/tests/drivers/mock/keys_test.rb +161 -0
  188. data/tests/drivers/mock/realms_test.rb +109 -70
  189. data/tests/drivers/mock/storage_snapshots_test.rb +114 -0
  190. data/tests/drivers/mock/storage_volumes_test.rb +122 -0
  191. data/tests/drivers/openstack/api_test.rb +8 -3
  192. data/tests/drivers/openstack/common.rb +21 -0
  193. data/tests/drivers/openstack/hardware_profiles_test.rb +20 -9
  194. data/tests/drivers/openstack/images_test.rb +11 -5
  195. data/tests/drivers/openstack/instances_test.rb +61 -16
  196. data/tests/drivers/openstack/realms_test.rb +11 -7
  197. data/tests/drivers/rackspace/api_test.rb +7 -2
  198. data/tests/drivers/rackspace/buckets_test.rb +7 -2
  199. data/tests/drivers/rackspace/common.rb +16 -0
  200. data/tests/drivers/rackspace/hardware_profiles_test.rb +7 -2
  201. data/tests/drivers/rackspace/images_test.rb +7 -2
  202. data/tests/drivers/rackspace/instances_test.rb +10 -5
  203. data/tests/drivers/rackspace/realms_test.rb +7 -2
  204. data/tests/drivers/rhevm/api_test.rb +12 -6
  205. data/tests/drivers/rhevm/{setup.rb → common.rb} +8 -1
  206. data/tests/drivers/rhevm/hardware_profiles_test.rb +7 -2
  207. data/tests/drivers/rhevm/images_test.rb +8 -2
  208. data/tests/drivers/rhevm/instances_test.rb +7 -2
  209. data/tests/drivers/rhevm/realms_test.rb +7 -2
  210. data/tests/minitest_common.rb +58 -0
  211. data/tests/minitest_common_api_test.rb +115 -0
  212. data/views/addresses/associate.html.haml +1 -1
  213. data/views/addresses/index.html.haml +1 -1
  214. data/views/addresses/show.html.haml +2 -3
  215. data/views/api/show.html.haml +22 -9
  216. data/views/api/show.xml.haml +8 -7
  217. data/views/blobs/show.xml.haml +7 -4
  218. data/views/buckets/new.html.haml +2 -2
  219. data/views/cimi/errors/401.xml.haml +1 -1
  220. data/views/error.html.haml +3 -3
  221. data/views/errors/400.html.haml +1 -1
  222. data/views/errors/400.xml.haml +1 -2
  223. data/views/errors/401.html.haml +8 -7
  224. data/views/errors/401.xml.haml +1 -1
  225. data/views/errors/404.xml.haml +1 -0
  226. data/views/errors/500.xml.haml +4 -2
  227. data/views/{cimi/errors/403.html.haml → errors/501.html.haml} +4 -2
  228. data/views/errors/501.xml.haml +1 -0
  229. data/views/errors/502.xml.haml +1 -7
  230. data/views/{cimi/errors/500.html.haml → errors/504.html.haml} +0 -0
  231. data/views/errors/504.xml.haml +1 -0
  232. data/views/firewalls/new.html.haml +2 -2
  233. data/views/firewalls/new_rule.html.haml +1 -1
  234. data/views/firewalls/show.html.haml +1 -1
  235. data/views/hardware_profiles/index.html.haml +3 -1
  236. data/views/hardware_profiles/index.xml.haml +2 -2
  237. data/views/hardware_profiles/show.html.haml +3 -3
  238. data/views/hardware_profiles/show.xml.haml +3 -3
  239. data/views/images/show.html.haml +5 -0
  240. data/views/images/show.xml.haml +6 -1
  241. data/views/instance_states/show.html.haml +1 -1
  242. data/views/instances/index.html.haml +1 -1
  243. data/views/instances/new.html.haml +54 -39
  244. data/views/instances/run_command.html.haml +24 -15
  245. data/views/instances/show.html.haml +7 -3
  246. data/views/instances/show.xml.haml +2 -2
  247. data/views/keys/show.xml.haml +1 -0
  248. data/views/layout.html.haml +5 -9
  249. data/views/load_balancers/show.html.haml +12 -6
  250. data/views/metrics/index.html.haml +13 -0
  251. data/views/metrics/index.xml.haml +5 -0
  252. data/views/metrics/show.html.haml +23 -0
  253. data/views/metrics/show.xml.haml +9 -0
  254. data/views/realms/show.xml.haml +2 -1
  255. data/views/root/index.html.haml +1 -1
  256. data/views/storage_snapshots/show.html.haml +1 -1
  257. data/views/storage_snapshots/show.xml.haml +1 -0
  258. data/views/storage_volumes/attach.html.haml +1 -2
  259. data/views/storage_volumes/index.html.haml +1 -1
  260. data/views/storage_volumes/new.html.haml +22 -16
  261. data/views/storage_volumes/show.html.haml +10 -4
  262. data/views/storage_volumes/show.xml.haml +3 -4
  263. metadata +547 -519
  264. data/DISCLAIMER +0 -8
  265. data/lib/cimi/model.rb +0 -52
  266. data/lib/cimi/model/network.rb +0 -69
  267. data/lib/deltacloud/base_driver.rb +0 -18
  268. data/lib/deltacloud/base_driver/features.rb +0 -262
  269. data/lib/deltacloud/base_driver/mock_driver.rb +0 -78
  270. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +0 -186
  271. data/lib/deltacloud/drivers/rhevm/rhevm_client.rb +0 -521
  272. data/lib/deltacloud/helpers/application_helper.rb +0 -267
  273. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +0 -50
  274. data/lib/deltacloud/helpers/json_helper.rb +0 -31
  275. data/lib/deltacloud/method_serializer.rb +0 -83
  276. data/lib/deltacloud/validation.rb +0 -107
  277. data/lib/sinatra/rabbit.rb +0 -441
  278. data/lib/sinatra/rack_runtime.rb +0 -47
  279. data/lib/sinatra/rack_syslog.rb +0 -86
  280. data/lib/sinatra/sinatra_verbose.rb +0 -73
  281. data/lib/sinatra/static_assets.rb +0 -99
  282. data/public/javascripts/jquery.mobile-1.0rc1.min.js +0 -170
  283. data/public/stylesheets/jquery.mobile-1.0rc1.min.css +0 -12
  284. data/tests/drivers/google/setup.rb +0 -38
  285. data/tests/drivers/mock/instance_states_test.rb +0 -71
  286. data/tests/drivers/mock/setup.rb +0 -3
  287. data/tests/drivers/mock/url_for_test.rb +0 -67
  288. data/tests/drivers/openstack/setup.rb +0 -20
  289. data/views/cimi/errors/400.html.haml +0 -41
  290. data/views/cimi/errors/401.html.haml +0 -41
  291. data/views/cimi/errors/404.html.haml +0 -29
  292. data/views/cimi/errors/405.html.haml +0 -29
  293. data/views/cimi/errors/502.html.haml +0 -43
  294. data/views/cimi/errors/backend_capability_failure.html.haml +0 -29
@@ -15,230 +15,293 @@
15
15
  # under the License.
16
16
  #
17
17
 
18
- require 'deltacloud/base_driver'
18
+ require 'deltacloud/drivers/opennebula/occi_client'
19
19
 
20
20
  require 'erb'
21
+ # TBD Nokogiri support
21
22
  require 'rexml/document'
22
23
 
23
- path = File.dirname(__FILE__)
24
- $: << path
25
-
26
- require 'occi_client'
27
-
28
24
  module Deltacloud
29
25
  module Drivers
30
26
  module Opennebula
31
27
 
32
28
  class OpennebulaDriver < Deltacloud::BaseDriver
33
29
 
34
- feature :instances, :user_name
30
+ define_hardware_profile('default')
35
31
 
36
32
  ######################################################################
37
33
  # Hardware profiles
38
- ######################################################################
39
-
40
- define_hardware_profile 'small'
41
-
42
- define_hardware_profile 'medium'
34
+ #####################################################################
35
+ def hardware_profiles(credentials, opts=nil)
36
+ occi_client = new_client(credentials)
37
+ xml = occi_client.get_instance_types
38
+ if CloudClient.is_error?(xml)
39
+ # OpenNebula 3.0 support
40
+ @hardware_profiles = ['small','medium','large'].map {|name|
41
+ ::Deltacloud::HardwareProfile.new(name)
42
+ }
43
+ else
44
+ # OpenNebula 3.2 support
45
+ @hardware_profiles = REXML::Document.new(xml).root.elements.map {|d|
46
+ elem = d.elements
47
+ ::Deltacloud::HardwareProfile.new(elem['NAME'].text) {
48
+ cpu elem['CPU']
49
+ memory elem['MEMORY']
50
+ storage elem['STORAGE']
51
+ architecture elem['ARCHITECTURE']
52
+ }
53
+ }
54
+ end
43
55
 
44
- define_hardware_profile 'large'
56
+ filter_hardware_profiles(@hardware_profiles, opts)
57
+ end
45
58
 
46
59
  ######################################################################
47
60
  # Realms
48
61
  ######################################################################
49
62
 
50
63
  (REALMS = [
51
- Realm.new( {
52
- :id=>'Any id',
53
- :name=>'Any name',
54
- :limit=>:unlimited,
55
- :state=>'Any state',
56
- } ),
64
+ Realm.new( {
65
+ :id=>'ONE',
66
+ :name=>'Opennebula',
67
+ :limit=>:unlimited,
68
+ :state=>'AVAILABLE',
69
+ } ),
57
70
  ] ) unless defined?( REALMS )
58
71
 
59
72
 
60
73
  def realms(credentials, opts=nil)
61
- return REALMS if ( opts.nil? )
62
- results = REALMS
63
- results = filter_on( results, :id, opts )
64
- results
74
+ return REALMS if ( opts.nil? )
75
+ results = REALMS
76
+ results = filter_on( results, :id, opts )
77
+ results
65
78
  end
66
79
 
67
80
 
68
81
  ######################################################################
69
82
  # Images
70
83
  ######################################################################
71
-
72
84
  def images(credentials, opts=nil)
73
- occi_client = new_client(credentials)
74
-
75
- images = []
76
- imagesxml = occi_client.get_images
85
+ occi_client = new_client(credentials)
77
86
 
78
- storage = REXML::Document.new(imagesxml)
79
- storage.root.elements.each do |d|
80
- id = d.attributes['href'].split("/").last
87
+ xml = treat_response(occi_client.get_images)
81
88
 
82
- diskxml = occi_client.get_image(id)
89
+ # TBD Add extended info in the pool
90
+ images = REXML::Document.new(xml).root.elements.map do |d|
91
+ im_id = d.attributes['href'].split("/").last
92
+ storage = treat_response(occi_client.get_image(im_id))
93
+ convert_image(storage, credentials)
94
+ end
95
+ end
83
96
 
84
- images << convert_image(diskxml.to_s(), credentials)
85
- end
86
- images
97
+ def image(credentials, opts=nil)
98
+ occi_client = new_client(credentials)
99
+ xml = treat_response(occi_client.get_image(opts[:id]))
100
+ convert_image(xml, credentials)
87
101
  end
88
102
 
103
+ def destroy_image(credentials, id)
104
+ occi_client = new_client(credentials)
105
+ treat_response(occi_client.delete_image(opts[:id]))
106
+ end
89
107
 
90
108
  ######################################################################
91
109
  # Instances
92
110
  ######################################################################
93
111
 
94
- define_instance_states do
95
- start.to( :pending ) .on( :create )
96
-
97
- pending.to( :running ) .automatically
98
-
99
- running.to( :stopped ) .on( :stop )
112
+ feature :instances, :user_name
113
+ # TBD Add Context to the VMs
114
+
115
+ OCCI_VM = %q{
116
+ <COMPUTE>
117
+ <% if opts[:name] %>
118
+ <NAME><%=opts[:name]%></NAME>
119
+ <% end %>
120
+ <INSTANCE_TYPE><%= opts[:hwp_id] || 'small' %></INSTANCE_TYPE>
121
+ <DISK>
122
+ <STORAGE href="<%= storage_href %>" />
123
+ </DISK>
124
+ </COMPUTE>
125
+ }
126
+
127
+ OCCI_ACTION = %q{
128
+ <COMPUTE>
129
+ <ID><%= id %></ID>
130
+ <STATE><%= strstate %></STATE>
131
+ </COMPUTE>
132
+ }
133
+
134
+ VM_STATES = {
135
+ "INIT" => "START",
136
+ "PENDING" => "PENDING",
137
+ "HOLD" => "STOPPED",
138
+ "ACTIVE" => "RUNNING",
139
+ "STOPPED" => "STOPPED",
140
+ "SUSPENDED" => "STOPPED",
141
+ "DONE" => "FINISHED",
142
+ "FAILED" => "FINISHED"
143
+ }
100
144
 
101
- stopped.to( :running ) .on( :start )
102
- stopped.to( :finish ) .on( :destroy )
145
+ define_instance_states do
146
+ start.to(:pending) .on( :create )
147
+ pending.to(:running) .automatically
148
+ stopped.to(:running) .on( :start )
149
+ running.to(:running) .on( :reboot )
150
+ running.to(:stopping) .on( :stop )
151
+ stopping.to(:stopped) .automatically
152
+ running.to(:stopping) .on( :destroy )
153
+ stopping.to(:finish) .automatically
103
154
  end
104
155
 
105
-
106
156
  def instances(credentials, opts=nil)
107
- occi_client = new_client(credentials)
157
+ occi_client = new_client(credentials)
108
158
 
109
- instances = []
110
- instancesxml = occi_client.get_vms
159
+ xml = treat_response(occi_client.get_vms)
111
160
 
112
- computes = REXML::Document.new(instancesxml)
113
- computes.root.elements.each do |d|
114
- vm_id = d.attributes['href'].split("/").last
115
-
116
- computexml = occi_client.get_vm(vm_id)
161
+ # TBD Add extended info in the pool
162
+ instances = REXML::Document.new(xml).root.elements.map do |d|
163
+ vm_id = d.attributes['href'].split("/").last
164
+ computexml = treat_response(occi_client.get_vm(vm_id))
165
+ convert_instance(computexml, credentials)
166
+ end
117
167
 
118
- instances << convert_instance(computexml.to_s(), credentials)
119
- end
120
- instances = filter_on( instances, :id, opts )
121
- instances = filter_on( instances, :state, opts )
122
- instances
168
+ instances = filter_on( instances, :state, opts )
123
169
  end
124
170
 
171
+ def instance(credentials, opts=nil)
172
+ occi_client = new_client(credentials)
173
+ xml = treat_response(occi_client.get_vm(opts[:id]))
174
+ convert_instance(xml, credentials)
175
+ end
125
176
 
126
177
  def create_instance(credentials, image_id, opts=nil)
127
- occi_client = new_client(credentials)
178
+ occi_client = new_client(credentials)
128
179
 
129
- hwp_id = opts[:hwp_id] || 'small'
180
+ storage_href = "#{occi_client.endpoint}/storage/#{image_id}"
130
181
 
131
- if not opts[:name]
132
- opts[:name] = "#{Time.now.to_i.to_s}#{rand(9)}"
133
- end
182
+ instancexml = ERB.new(OCCI_VM).result(binding)
183
+ instancefile = "|echo '#{instancexml}'"
134
184
 
135
- instancexml = ERB.new(OCCI_VM).result(binding)
136
- instancefile = "|echo '#{instancexml}'"
185
+ # TBD Specify VNET in the template.
137
186
 
138
- xmlvm = occi_client.post_vms(instancefile)
187
+ xmlvm = treat_response(occi_client.post_vms(instancefile))
139
188
 
140
- convert_instance(xmlvm.to_s(), credentials)
189
+ convert_instance(xmlvm, credentials)
141
190
  end
142
191
 
143
-
144
192
  def start_instance(credentials, id)
145
- occi_action(credentials, id, 'RESUME')
193
+ occi_action(credentials, id, 'RESUME')
146
194
  end
147
195
 
148
196
 
149
197
  def stop_instance(credentials, id)
150
- occi_action(credentials, id, 'STOPPED')
198
+ occi_action(credentials, id, 'STOPPED')
151
199
  end
152
200
 
153
201
 
154
202
  def destroy_instance(credentials, id)
155
- occi_action(credentials, id, 'DONE')
203
+ occi_action(credentials, id, 'DONE')
156
204
  end
157
205
 
158
-
206
+ def reboot_instance(credentials, id)
207
+ begin
208
+ occi_action(credentials, id, 'REBOOT')
209
+ rescue Exception => e
210
+ # TBD Check exception
211
+ # OpenNebula 3.0 support
212
+ raise "Reboot action not supported"
213
+ end
214
+ end
159
215
 
160
216
  private
161
217
 
162
218
  def new_client(credentials)
163
- OCCIClient::Client.new(nil, credentials.user, credentials.password, false)
219
+ OCCIClient::Client.new(api_provider, credentials.user, credentials.password, false)
164
220
  end
165
221
 
166
222
 
167
223
  def convert_image(diskxml, credentials)
168
- disk = REXML::Document.new(diskxml)
169
- diskhash = disk.root.elements
170
-
171
- Image.new( {
172
- :id=>diskhash['ID'].text,
173
- :name=>diskhash['NAME'].text,
174
- :description=>diskhash['NAME'].text,
175
- :owner_id=>credentials.user,
176
- :architecture=>'Any architecture',
177
- } )
224
+ storage = REXML::Document.new(diskxml).root.elements
225
+
226
+ # TBD Add Image STATE, OWNER
227
+ Image.new( {
228
+ :id=>storage['ID'].text,
229
+ :name=>storage['NAME'].text,
230
+ :description=>storage['TYPE'].text,
231
+ :owner_id=>credentials.user,
232
+ :state=>"AVAILABLE",
233
+ :architecture=>storage['ARCH'],
234
+ :hardware_profiles=>hardware_profiles(credentials)
235
+ } )
178
236
  end
179
237
 
180
238
 
181
239
  def convert_instance(computexml, credentials)
182
- compute = REXML::Document.new(computexml)
183
- computehash = compute.root.elements
184
-
185
- imageid = computehash['STORAGE/DISK[@type="disk"]'].attributes['href'].split("/").last
240
+ compute = REXML::Document.new(computexml)
241
+ computehash = compute.root.elements
186
242
 
187
- state = (computehash['STATE'].text == "ACTIVE") ? "RUNNING" : "STOPPED"
243
+ network = []
244
+ computehash.each('NIC/IP') {|ip| network<<InstanceAddress.new(ip.text, :type => :ipv4)}
188
245
 
189
- hwp_name = computehash['INSTANCE_TYPE'] || 'small'
190
-
191
- networks = []
192
- (computehash['NETWORK'].each do |n|
193
- networks << InstanceAddress.new(n.attributes['ip'])
194
- end) unless computehash['NETWORK'].nil?
246
+ image_id = nil
247
+ if computehash['DISK/STORAGE']
248
+ image_id = computehash['DISK/STORAGE'].attributes['href'].split("/").last
249
+ end
195
250
 
196
- Instance.new( {
197
- :id=>computehash['ID'].text,
198
- :owner_id=>credentials.user,
199
- :name=>computehash['NAME'].text,
200
- :image_id=>imageid,
201
- :instance_profile=>InstanceProfile.new(hwp_name),
202
- :realm_id=>'Any realm',
203
- :state=>state,
204
- :public_addreses=>networks,
205
- :private_addreses=>networks,
206
- :actions=> instance_actions_for( state )
207
- } )
251
+ Instance.new( {
252
+ :id=>computehash['ID'].text,
253
+ :owner_id=>credentials.user,
254
+ :name=>computehash['NAME'].text,
255
+ :image_id=>image_id,
256
+ :instance_profile=>InstanceProfile.new(computehash['INSTANCE_TYPE'].text||'small'),
257
+ :realm_id=>'ONE',
258
+ :state=>VM_STATES[computehash['STATE'].text],
259
+ :public_addresses=>network,
260
+ :private_addresses=>[],
261
+ :actions=> instance_actions_for( VM_STATES[computehash['STATE'].text] )
262
+ } )
208
263
  end
209
264
 
210
265
 
211
266
  def occi_action(credentials, id, strstate)
212
- occi_client = new_client(credentials)
267
+ occi_client = new_client(credentials)
213
268
 
214
- actionxml = ERB.new(OCCI_ACTION).result(binding)
215
- actionfile = "|echo '#{actionxml}'"
269
+ actionxml = ERB.new(OCCI_ACTION).result(binding)
270
+ actionfile = "|echo '#{actionxml}'"
216
271
 
217
- xmlvm = occi_client.put_vm(actionfile)
272
+ xmlvm = treat_response(occi_client.put_vm(actionfile))
218
273
 
219
- convert_instance(xmlvm.to_s(), credentials)
274
+ convert_instance(xmlvm, credentials)
220
275
  end
221
276
 
222
277
 
223
- (OCCI_VM = %q{
224
- <COMPUTE>
225
- <NAME><%=opts[:name]%></NAME>
226
- <INSTANCE_TYPE><%= hwp_id %></INSTANCE_TYPE>
227
- <STORAGE>
228
- <DISK image="<%= image_id %>" dev="sda1" />
229
- </STORAGE>
230
- </COMPUTE>
231
- }.gsub(/^ /, '') ) unless defined?( OCCI_VM )
278
+ def treat_response(res)
279
+ safely do
280
+ if CloudClient.is_error?(res)
281
+ raise case res.code
282
+ when "401" then "AuthenticationFailure"
283
+ when "404" then "ObjectNotFound"
284
+ else res.message
285
+ end
286
+ end
287
+ end
288
+ res
289
+ end
232
290
 
291
+ exceptions do
292
+ on /AuthenticationFailure/ do
293
+ status 401
294
+ end
233
295
 
234
- (OCCI_ACTION = %q{
235
- <COMPUTE>
236
- <ID><%= id %></ID>
237
- <STATE><%= strstate %></STATE>
238
- </COMPUTE>
239
- }.gsub(/^ /, '') ) unless defined?( OCCI_ACTION )
296
+ on /ObjectNotFound/ do
297
+ status 404
298
+ end
240
299
 
241
- end
300
+ on // do
301
+ status 502
302
+ end
303
+ end
304
+ end
242
305
 
243
306
  end
244
307
  end
@@ -14,34 +14,453 @@
14
14
  # under the License.
15
15
  #
16
16
 
17
- require 'deltacloud/drivers/rackspace/rackspace_driver.rb'
17
+ require 'openstack'
18
+ require 'tempfile'
19
+
18
20
  module Deltacloud
19
21
  module Drivers
20
22
  module Openstack
21
- class OpenstackDriver < Rackspace::RackspaceDriver
23
+ class OpenstackDriver < Deltacloud::BaseDriver
22
24
 
23
25
  feature :instances, :user_name
24
26
  feature :instances, :authentication_password
25
27
  feature :instances, :user_files
28
+ feature :images, :user_name
26
29
 
27
30
  define_instance_states do
28
31
  start.to( :pending ) .on( :create )
29
32
  pending.to( :running ) .automatically
30
33
  running.to( :running ) .on( :reboot )
31
- running.to( :shutting_down ) .on( :stop )
32
- shutting_down.to( :stopped ) .automatically
34
+ running.to( :stopping ) .on( :stop )
35
+ stopping.to( :stopped ) .automatically
33
36
  stopped.to( :finish ) .automatically
34
37
  end
35
38
 
36
- def new_client(credentials)
39
+ define_hardware_profile('default')
40
+
41
+ def hardware_profiles(credentials, opts = {})
42
+ os = new_client(credentials)
43
+ results = []
44
+ safely do
45
+ if opts[:id]
46
+ flavor = os.flavor(opts[:id])
47
+ results << convert_from_flavor(flavor)
48
+ else
49
+ results = os.flavors.collect do |f|
50
+ convert_from_flavor(f)
51
+ end
52
+ end
53
+ filter_hardware_profiles(results, opts)
54
+ end
55
+ end
56
+
57
+ def images(credentials, opts={})
58
+ os = new_client(credentials)
59
+ results = []
60
+ profiles = hardware_profiles(credentials)
61
+ safely do
62
+ if(opts[:id])
63
+ img = os.get_image(opts[:id])
64
+ results << convert_from_image(img, os.connection.authuser)
65
+ else
66
+ results = os.list_images.collect do |img|
67
+ convert_from_image(img, os.connection.authuser)
68
+ end
69
+ end
70
+ end
71
+ results.each do |img|
72
+ img.hardware_profiles = profiles
73
+ end
74
+ filter_on(results, :owner_id, opts)
75
+ end
76
+
77
+ def create_image(credentials, opts)
78
+ os = new_client(credentials)
79
+ safely do
80
+ server = os.get_server(opts[:id])
81
+ image_name = opts[:name] || "#{server.name}_#{Time.now}"
82
+ img = server.create_image(:name=>image_name)
83
+ convert_from_image(img, os.connection.authuser)
84
+ end
85
+ end
86
+
87
+ def destroy_image(credentials, image_id)
88
+ os = new_client(credentials)
89
+ begin
90
+ image = os.get_image(image_id)
91
+ image.delete!
92
+ rescue => e
93
+ raise Deltacloud::ExceptionHandler::BackendError.new(e, "ERROR: Cannot delete image with ID:#{image_id}.")
94
+ end
95
+ end
96
+
97
+ def realms(credentials, opts={})
98
+ os = new_client(credentials)
99
+ limits = ""
100
+ safely do
101
+ lim = os.limits
102
+ limits << "ABSOLUTE >> Max. Instances: #{lim[:absolute][:maxTotalInstances]} Max. RAM: #{lim[:absolute][:maxTotalRAMSize]} || "
103
+ lim[:rate].each do |rate|
104
+ if rate[:regex] =~ /servers/
105
+ limits << "SERVERS >> Total: #{rate[:limit].first[:value]} Remaining: #{rate[:limit].first[:remaining]} Time Unit: per #{rate[:limit].first[:unit]}"
106
+ end
107
+ end
108
+ end
109
+ [ Realm.new( { :id=>'default',
110
+ :name=>'default',
111
+ :limit => limits,
112
+ :state=>'AVAILABLE' })]
113
+ end
114
+
115
+ def instances(credentials, opts={})
116
+ os = new_client(credentials)
117
+ insts = []
118
+ safely do
119
+ if opts[:id]
120
+ server = os.get_server(opts[:id].to_i)
121
+ insts << convert_from_server(server, os.connection.authuser)
122
+ else
123
+ insts = os.list_servers_detail.collect do |server|
124
+ convert_from_server(server, os.connection.authuser)
125
+ end
126
+ end
127
+ end
128
+ insts = filter_on( insts, :state, opts )
129
+ insts
130
+ end
131
+
132
+ def create_instance(credentials, image_id, opts)
133
+ os = new_client( credentials )
134
+ result = nil
135
+ #opts[:personality]: path1='server_path1'. content1='contents1', path2='server_path2', content2='contents2' etc
136
+ params = {}
137
+ params[:personality] = extract_personality(opts)
138
+ params[:name] = (opts[:name] && opts[:name].length>0)? opts[:name] : Time.now.to_s
139
+ params[:imageRef] = image_id
140
+ params[:flavorRef] = (opts[:hwp_id] && opts[:hwp_id].length>0) ?
141
+ opts[:hwp_id] : hardware_profiles(credentials).first.name
142
+ if opts[:password] && opts[:password].length > 0
143
+ params[:adminPass]=opts[:password]
144
+ end
145
+ safely do
146
+ server = os.create_server(params)
147
+ result = convert_from_server(server, os.connection.authuser)
148
+ end
149
+ result
150
+ end
151
+
152
+ def reboot_instance(credentials, instance_id)
153
+ os = new_client(credentials)
154
+ safely do
155
+ server = os.get_server(instance_id.to_i)
156
+ server.reboot! # sends a hard reboot (power cycle) - could instead server.reboot("SOFT")
157
+ convert_from_server(server, os.connection.authuser)
158
+ end
159
+ end
160
+
161
+ def destroy_instance(credentials, instance_id)
162
+ os = new_client(credentials)
163
+ safely do
164
+ server = os.get_server(instance_id.to_i)
165
+ server.delete!
166
+ convert_from_server(server, os.connection.authuser)
167
+ end
168
+ end
169
+
170
+ alias_method :stop_instance, :destroy_instance
171
+
172
+ def valid_credentials?(credentials)
173
+ begin
174
+ new_client(credentials)
175
+ rescue
176
+ return false
177
+ end
178
+ true
179
+ end
180
+
181
+ def buckets(credentials, opts={})
182
+ os = new_client(credentials, :buckets)
183
+ buckets = []
184
+ safely do
185
+ if opts[:id]
186
+ buckets << convert_bucket(os.container(opts[:id]))
187
+ else
188
+ os.containers.each{|bucket_name| buckets << convert_bucket(os.container(bucket_name))}
189
+ end
190
+ end
191
+ buckets
192
+ end
193
+
194
+ def create_bucket(credentials, name, opts={})
195
+ os = new_client(credentials, :buckets)
196
+ bucket = nil
197
+ safely do
198
+ bucket = os.create_container(name)
199
+ end
200
+ convert_bucket(bucket)
201
+ end
202
+
203
+ def delete_bucket(credentials, name, opts={})
204
+ os = new_client(credentials, :buckets)
205
+ safely do
206
+ os.delete_container(name)
207
+ end
208
+ end
209
+
210
+ def blobs(credentials, opts={})
211
+ os = new_client(credentials, :buckets)
212
+ blobs = []
213
+ safely do
214
+ bucket = os.container(opts['bucket'])
215
+ if(opts[:id])
216
+ blobs << convert_blob(bucket.object(opts[:id]), opts['bucket'])
217
+ else
218
+ bucket.objects_detail.each{|blob| blobs << convert_blob(blob, opts['bucket'])}
219
+ end
220
+ end
221
+ blobs
222
+ end
223
+
224
+ def blob_data(credentials, bucket, blob, opts={})
225
+ os = new_client(credentials, :buckets)
226
+ safely do
227
+ os.container(bucket).object(blob).data_stream do |chunk|
228
+ yield chunk
229
+ end
230
+ end
231
+ end
232
+
233
+ def create_blob(credentials, bucket, blob, data, opts={})
234
+ os = new_client(credentials, :buckets)
37
235
  safely do
38
- CloudServers::Connection.new(:username => credentials.user, :api_key => credentials.password, :auth_url => api_provider)
236
+ BlobHelper.rename_metadata_headers(opts, "X-Object-Meta-")
237
+ os_blob = os.container(bucket).create_object(blob, {:content_type=> data[:type], :metadata=>opts}, data[:tempfile])
238
+ convert_blob(os_blob, bucket)
39
239
  end
40
240
  end
41
241
 
42
- private :new_client
242
+ def delete_blob(credentials, bucket, blob, opts={})
243
+ os = new_client(credentials, :buckets)
244
+ safely do
245
+ os.container(bucket).delete_object(blob)
246
+ end
247
+ end
248
+
249
+ def blob_metadata(credentials, opts={})
250
+ os = new_client(credentials, :buckets)
251
+ safely do
252
+ os.container(opts['bucket']).object(opts[:id]).metadata
253
+ end
254
+ end
255
+
256
+ def update_blob_metadata(credentials, opts={})
257
+ os = new_client(credentials, :buckets)
258
+ safely do
259
+ BlobHelper.rename_metadata_headers(opts["meta_hash"], "")
260
+ blob = os.container(opts['bucket']).object(opts[:id])
261
+ blob.set_metadata(opts['meta_hash'])
262
+ end
263
+ end
264
+
265
+ #params: {:user,:password,:bucket,:blob,:content_type,:content_length,:metadata}
266
+ def blob_stream_connection(params)
267
+ tokens = params[:user].split("+")
268
+ user_name, tenant_name = tokens.first, tokens.last
269
+ #need a client for the auth_token and endpoints
270
+ os = OpenStack::Connection.create(:username => user_name, :api_key => params[:password], :authtenant => tenant_name, :auth_url => api_provider, :service_type => "object-store")
271
+ http = Net::HTTP.new(os.connection.service_host, os.connection.service_port)
272
+ http.use_ssl = true
273
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
274
+ path = os.connection.service_path + URI.encode("/#{params[:bucket]}/#{params[:blob]}")
275
+ request = Net::HTTP::Put.new(path)
276
+ request['X-Auth-Token'] = os.connection.authtoken
277
+ request['X-Storage-Token'] = os.connection.authtoken
278
+ request['Connection'] = "Keep-Alive"
279
+ request['Content-Type'] = params[:content_type]
280
+ request['Content-Length'] = params[:content_length]
281
+ request['Expect'] = "100-continue"
282
+ metadata = params[:metadata] || {}
283
+ BlobHelper::rename_metadata_headers(metadata, 'X-Object-Meta-')
284
+ metadata.each{|k,v| request[k] = v}
285
+ return http, request
286
+ end
287
+
288
+ private
289
+
290
+ #for v2 authentication credentials.name == "username+tenant_name"
291
+ def new_client(credentials, type = :compute)
292
+ tokens = credentials.user.split("+")
293
+ if (tokens.size != 2 && api_v2)
294
+ raise ValidationFailure.new(Exception.new("Error: expected \"username+tenantname\" as username, you provided: #{credentials.user}"))
295
+ else
296
+ user_name, tenant_name = tokens.first, tokens.last
297
+ end
298
+ safely do
299
+ case type
300
+ when :compute
301
+ OpenStack::Connection.create(:username => user_name, :api_key => credentials.password, :authtenant => tenant_name, :auth_url => api_provider)
302
+ when :buckets
303
+ OpenStack::Connection.create(:username => user_name, :api_key => credentials.password, :authtenant => tenant_name, :auth_url => api_provider, :service_type => "object-store")
304
+ else
305
+ raise ValidationFailure.new(Exception.new("Error: tried to initialise Openstack connection using" +
306
+ " an unknown service_type: #{type}"))
307
+ end
308
+ end
309
+ end
310
+
311
+ def cloudfiles_client(credentials)
312
+ safely do
313
+ CloudFiles::Connection.new(:username => credentials.user, :api_key => credentials.password)
314
+ end
315
+ end
316
+
317
+ #NOTE: for the convert_from_foo methods below... openstack-compute
318
+ #gives Hash for 'flavors' but OpenStack::Compute::Flavor for 'flavor'
319
+ #hence the use of 'send' to deal with both cases and save duplication
320
+
321
+ def convert_from_flavor(flavor)
322
+ op = (flavor.class == Hash)? :fetch : :send
323
+ HardwareProfile.new(flavor.send(op, :id).to_s) do
324
+ architecture 'x86_64'
325
+ memory flavor.send(op, :ram).to_i
326
+ storage flavor.send(op, :disk).to_i
327
+ cpu flavor.send(op, :vcpus).to_i
328
+ end
329
+ end
330
+
331
+ def convert_from_image(image, owner)
332
+ op = (image.class == Hash)? :fetch : :send
333
+ Image.new({
334
+ :id => image.send(op, :id),
335
+ :name => image.send(op, :name),
336
+ :description => image.send(op, :name),
337
+ :owner_id => owner,
338
+ :state => image.send(op, :status),
339
+ :architecture => 'x86_64'
340
+ })
341
+ end
342
+
343
+ def convert_from_server(server, owner)
344
+ op = (server.class == Hash)? :fetch : :send
345
+ image = server.send(op, :image)
346
+ flavor = server.send(op, :flavor)
347
+ begin
348
+ password = server.send(op, :adminPass) || ""
349
+ rescue IndexError
350
+ password = ""
351
+ end
352
+ inst = Instance.new(
353
+ :id => server.send(op, :id).to_s,
354
+ :realm_id => 'default',
355
+ :owner_id => owner,
356
+ :description => server.send(op, :name),
357
+ :name => server.send(op, :name),
358
+ :state => convert_instance_state(server.send(op, :status).downcase),
359
+ :architecture => 'x86_64',
360
+ :image_id => image[:id] || image["id"],
361
+ :instance_profile => InstanceProfile::new(flavor[:id] || flavor["id"]),
362
+ :public_addresses => convert_server_addresses(server, :public),
363
+ :private_addresses => convert_server_addresses(server, :private),
364
+ :username => 'root',
365
+ :password => password
366
+ )
367
+ inst.actions = instance_actions_for(inst.state)
368
+ inst.create_image = 'RUNNING'.eql?(inst.state)
369
+ inst
370
+ end
371
+
372
+ def convert_instance_state(openstack_state)
373
+ case openstack_state
374
+ when /.*reboot/
375
+ "PENDING"
376
+ when /.*deleting/
377
+ "STOPPING"
378
+ when /.*deleted/
379
+ "STOPPED"
380
+ when /build.*$/
381
+ "PENDING"
382
+ when /active/
383
+ "RUNNING"
384
+ else
385
+ "UNKOWN"
386
+ end
387
+ end
388
+
389
+ def convert_server_addresses(server, type)
390
+ op, address_label = (server.class == Hash)? [:fetch, :addr] : [:send, :address]
391
+ addresses = (server.send(op, :addresses)[type] || []).collect do |addr|
392
+ type = (addr.send(op, :version) == 4)? :ipv4 : :ipv6
393
+ InstanceAddress.new(addr.send(op, address_label), {:type=>type} )
394
+ end
395
+ end
396
+
397
+ def convert_bucket(bucket)
398
+ Bucket.new({ :id => bucket.name,
399
+ :name => bucket.name,
400
+ :size => bucket.count,
401
+ :blob_list => bucket.objects })
402
+ end
403
+
404
+ def convert_blob(blob, bucket_name)
405
+ op, blob_meta = (blob.class == Hash)? [:fetch, {}] : [:send, blob.metadata]
406
+ Blob.new({ :id => blob.send(op, :name),
407
+ :bucket => bucket_name,
408
+ :content_length => blob.send(op, :bytes),
409
+ :content_type => blob.send(op, :content_type),
410
+ :last_modified => blob.send(op, :last_modified),
411
+ :user_metadata => blob_meta })
412
+ end
413
+
414
+ #IN: path1='server_path1'. content1='contents1', path2='server_path2', content2='contents2' etc
415
+ #OUT:{local_path=>server_path, local_path1=>server_path2 etc}
416
+ def extract_personality(opts)
417
+ personality_hash = opts.inject({}) do |result, (opt_k,opt_v)|
418
+ if (opt_k.to_s =~ /^path([1-5]+)/ and opts[opt_k] != nil and opts[opt_k].length > 0)
419
+ unless opts[:"content#{$1}"].nil?
420
+ case opts[:"content#{$1}"]
421
+ when String
422
+ tempfile = Tempfile.new("os_personality_local_#{$1}")
423
+ tempfile.write(opts[:"content#{$1}"])
424
+ result[tempfile.path]=opts[:"path#{$1}"]
425
+ when Hash
426
+ result[opts[:"content#{$1}"][:tempfile].path]=opts[:"path#{$1}"]
427
+ end
428
+ end
429
+ end
430
+ result
431
+ end
432
+ end
433
+
434
+ def api_v2
435
+ if api_provider =~ /.*v2.0/
436
+ true
437
+ else
438
+ false
439
+ end
440
+ end
441
+
442
+ exceptions do
443
+
444
+ on /(Exception::BadRequest|PersonalityFilePathTooLong|PersonalityFileTooLarge|TooManyPersonalityItems)/ do
445
+ status 400
446
+ end
447
+
448
+ on /OpenStack::Exception::Authentication/ do
449
+ status 401
450
+ end
451
+
452
+ on /OpenStack::Exception::ItemNotFound/ do
453
+ status 404
454
+ end
455
+
456
+ on /Exception::Other/ do
457
+ status 500
458
+ end
459
+
460
+ end
461
+
462
+
43
463
  end
44
464
  end
45
465
  end
46
466
  end
47
-