bbrowning-deltacloud-core 0.0.4-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. data/COPYING +176 -0
  2. data/Rakefile +99 -0
  3. data/bin/deltacloudd +120 -0
  4. data/config.ru +5 -0
  5. data/deltacloud.rb +18 -0
  6. data/lib/deltacloud/base_driver/base_driver.rb +229 -0
  7. data/lib/deltacloud/base_driver/features.rb +166 -0
  8. data/lib/deltacloud/base_driver/mock_driver.rb +40 -0
  9. data/lib/deltacloud/base_driver.rb +20 -0
  10. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +410 -0
  11. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +170 -0
  12. data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +50 -0
  13. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +332 -0
  14. data/lib/deltacloud/drivers/gogrid/test.rb +13 -0
  15. data/lib/deltacloud/drivers/mock/data/images/img1.yml +3 -0
  16. data/lib/deltacloud/drivers/mock/data/images/img2.yml +3 -0
  17. data/lib/deltacloud/drivers/mock/data/images/img3.yml +3 -0
  18. data/lib/deltacloud/drivers/mock/data/instances/inst0.yml +16 -0
  19. data/lib/deltacloud/drivers/mock/data/instances/inst1.yml +9 -0
  20. data/lib/deltacloud/drivers/mock/data/instances/inst2.yml +9 -0
  21. data/lib/deltacloud/drivers/mock/data/storage_snapshots/snap1.yml +4 -0
  22. data/lib/deltacloud/drivers/mock/data/storage_snapshots/snap2.yml +4 -0
  23. data/lib/deltacloud/drivers/mock/data/storage_snapshots/snap3.yml +4 -0
  24. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol1.yml +6 -0
  25. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol2.yml +6 -0
  26. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol3.yml +6 -0
  27. data/lib/deltacloud/drivers/mock/mock_driver.rb +277 -0
  28. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +116 -0
  29. data/lib/deltacloud/drivers/opennebula/occi_client.rb +204 -0
  30. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +241 -0
  31. data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +130 -0
  32. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +182 -0
  33. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +255 -0
  34. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +85 -0
  35. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +166 -0
  36. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +286 -0
  37. data/lib/deltacloud/hardware_profile.rb +153 -0
  38. data/lib/deltacloud/helpers/application_helper.rb +115 -0
  39. data/lib/deltacloud/helpers/conversion_helper.rb +39 -0
  40. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +35 -0
  41. data/lib/deltacloud/helpers.rb +5 -0
  42. data/lib/deltacloud/method_serializer.rb +85 -0
  43. data/lib/deltacloud/models/base_model.rb +59 -0
  44. data/lib/deltacloud/models/image.rb +27 -0
  45. data/lib/deltacloud/models/instance.rb +38 -0
  46. data/lib/deltacloud/models/instance_profile.rb +48 -0
  47. data/lib/deltacloud/models/key.rb +35 -0
  48. data/lib/deltacloud/models/realm.rb +26 -0
  49. data/lib/deltacloud/models/storage_snapshot.rb +27 -0
  50. data/lib/deltacloud/models/storage_volume.rb +28 -0
  51. data/lib/deltacloud/state_machine.rb +84 -0
  52. data/lib/deltacloud/validation.rb +70 -0
  53. data/lib/drivers.rb +50 -0
  54. data/lib/sinatra/accept_media_types.rb +128 -0
  55. data/lib/sinatra/lazy_auth.rb +56 -0
  56. data/lib/sinatra/rabbit.rb +273 -0
  57. data/lib/sinatra/respond_to.rb +272 -0
  58. data/lib/sinatra/static_assets.rb +83 -0
  59. data/lib/sinatra/url_for.rb +53 -0
  60. data/public/favicon.ico +0 -0
  61. data/public/images/grid.png +0 -0
  62. data/public/images/logo-wide.png +0 -0
  63. data/public/images/rails.png +0 -0
  64. data/public/images/topbar-bg.png +0 -0
  65. data/public/javascripts/application.js +32 -0
  66. data/public/javascripts/jquery-1.4.2.min.js +154 -0
  67. data/public/stylesheets/compiled/application.css +613 -0
  68. data/public/stylesheets/compiled/ie.css +31 -0
  69. data/public/stylesheets/compiled/print.css +27 -0
  70. data/public/stylesheets/compiled/screen.css +456 -0
  71. data/server.rb +354 -0
  72. data/support/fedora/deltacloudd +68 -0
  73. data/support/fedora/rubygem-deltacloud-core.spec +91 -0
  74. data/tests/api_test.rb +37 -0
  75. data/tests/hardware_profiles_test.rb +120 -0
  76. data/tests/images_test.rb +111 -0
  77. data/tests/instance_states_test.rb +52 -0
  78. data/tests/instances_test.rb +219 -0
  79. data/tests/realms_test.rb +78 -0
  80. data/tests/url_for_test.rb +50 -0
  81. data/views/accounts/index.html.haml +11 -0
  82. data/views/accounts/show.html.haml +30 -0
  83. data/views/api/show.html.haml +15 -0
  84. data/views/api/show.xml.haml +5 -0
  85. data/views/docs/collection.html.haml +37 -0
  86. data/views/docs/collection.xml.haml +14 -0
  87. data/views/docs/index.html.haml +15 -0
  88. data/views/docs/index.xml.haml +5 -0
  89. data/views/docs/operation.html.haml +31 -0
  90. data/views/docs/operation.xml.haml +10 -0
  91. data/views/errors/auth_exception.html.haml +8 -0
  92. data/views/errors/auth_exception.xml.haml +2 -0
  93. data/views/errors/backend_error.html.haml +19 -0
  94. data/views/errors/backend_error.xml.haml +8 -0
  95. data/views/errors/not_found.html.haml +6 -0
  96. data/views/errors/not_found.xml.haml +2 -0
  97. data/views/errors/validation_failure.html.haml +11 -0
  98. data/views/errors/validation_failure.xml.haml +7 -0
  99. data/views/hardware_profiles/index.html.haml +25 -0
  100. data/views/hardware_profiles/index.xml.haml +4 -0
  101. data/views/hardware_profiles/show.html.haml +19 -0
  102. data/views/hardware_profiles/show.xml.haml +18 -0
  103. data/views/images/index.html.haml +30 -0
  104. data/views/images/index.xml.haml +8 -0
  105. data/views/images/show.html.haml +21 -0
  106. data/views/images/show.xml.haml +5 -0
  107. data/views/instance_states/show.gv.erb +45 -0
  108. data/views/instance_states/show.html.haml +31 -0
  109. data/views/instance_states/show.xml.haml +8 -0
  110. data/views/instances/index.html.haml +30 -0
  111. data/views/instances/index.xml.haml +21 -0
  112. data/views/instances/new.html.haml +55 -0
  113. data/views/instances/show.html.haml +43 -0
  114. data/views/instances/show.xml.haml +49 -0
  115. data/views/keys/index.html.haml +26 -0
  116. data/views/keys/index.xml.haml +4 -0
  117. data/views/keys/new.html.haml +8 -0
  118. data/views/keys/show.html.haml +22 -0
  119. data/views/keys/show.xml.haml +20 -0
  120. data/views/layout.html.haml +26 -0
  121. data/views/realms/index.html.haml +29 -0
  122. data/views/realms/index.xml.haml +10 -0
  123. data/views/realms/show.html.haml +15 -0
  124. data/views/realms/show.xml.haml +9 -0
  125. data/views/root/index.html.haml +4 -0
  126. data/views/storage_snapshots/index.html.haml +20 -0
  127. data/views/storage_snapshots/index.xml.haml +9 -0
  128. data/views/storage_snapshots/show.html.haml +14 -0
  129. data/views/storage_snapshots/show.xml.haml +7 -0
  130. data/views/storage_volumes/index.html.haml +21 -0
  131. data/views/storage_volumes/index.xml.haml +13 -0
  132. data/views/storage_volumes/show.html.haml +20 -0
  133. data/views/storage_volumes/show.xml.haml +11 -0
  134. metadata +334 -0
@@ -0,0 +1,255 @@
1
+ #
2
+ # Copyright (C) 2009 Red Hat, Inc.
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one or more
5
+ # contributor license agreements. See the NOTICE file distributed with
6
+ # this work for additional information regarding copyright ownership. The
7
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance with the
9
+ # License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
+ # License for the specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ require 'deltacloud/base_driver'
20
+ require 'yaml'
21
+
22
+ module Deltacloud
23
+ module Drivers
24
+ module RHEVM
25
+
26
+ class RHEVMDriver < Deltacloud::BaseDriver
27
+
28
+ SCRIPT_DIR = File.dirname(__FILE__) + '/scripts'
29
+ CONFIG = YAML.load_file(File.dirname(__FILE__) + '/../../../../config/rhevm_config.yml')
30
+ SCRIPT_DIR_ARG = '"' + SCRIPT_DIR + '"'
31
+ DELIM_BEGIN="<_OUTPUT>"
32
+ DELIM_END="</_OUTPUT>"
33
+ POWERSHELL="c:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
34
+ NO_OWNER=""
35
+
36
+ feature :instances, :user_name
37
+
38
+ #
39
+ # Execute a Powershell command, and convert the output
40
+ # to YAML in order to get back an array of maps.
41
+ #
42
+ def execute(credentials, command, *args)
43
+ args = args.to_a
44
+ argString = genArgString(credentials, args)
45
+ puts argString
46
+ outputMaps = {}
47
+ output = `#{POWERSHELL} -command "&{#{File.join(SCRIPT_DIR, command)} #{argString}; exit $LASTEXITCODE}`
48
+ exitStatus = $?.exitstatus
49
+ puts(output)
50
+ puts("EXITSTATUS #{exitStatus}")
51
+ st = output.index(DELIM_BEGIN)
52
+ if (st)
53
+ st += DELIM_BEGIN.length
54
+ ed = output.index(DELIM_END)
55
+ output = output.slice(st, (ed-st))
56
+ # Lets make it yaml
57
+ output.strip!
58
+ if (output.length > 0)
59
+ outputMaps = YAML.load(self.toYAML(output))
60
+ end
61
+ end
62
+ outputMaps
63
+ end
64
+
65
+ def genArgString(credentials, args)
66
+ commonArgs = [SCRIPT_DIR_ARG, credentials.user, credentials.password, CONFIG["domain"]]
67
+ commonArgs.concat(args)
68
+ commonArgs.join(" ")
69
+ end
70
+
71
+ def toYAML(output)
72
+ yOutput = "- \n" + output
73
+ yOutput.gsub!(/^(\w*)[ ]*:[ ]*([A-Z0-9a-z._ -:{}]*)/,' \1: "\2"')
74
+ yOutput.gsub!(/^[ ]*$/,"- ")
75
+ puts(yOutput)
76
+ yOutput
77
+ end
78
+
79
+ def statify(state)
80
+ st = state.nil? ? "" : state.upcase()
81
+ return "running" if st == "UP"
82
+ return "stopped" if st == "DOWN"
83
+ return "pending" if st == "POWERING UP"
84
+ st
85
+ end
86
+
87
+ define_hardware_profile 'rhevm'
88
+
89
+ #
90
+ # Realms
91
+ #
92
+
93
+ def realms(credentials, opts=nil)
94
+ domains = execute(credentials, "storageDomains.ps1")
95
+ if (!opts.nil? && opts[:id])
96
+ domains = domains.select{|d| opts[:id] == d["StorageId"]}
97
+ end
98
+
99
+ realms = []
100
+ domains.each do |dom|
101
+ realms << domain_to_realm(dom)
102
+ end
103
+ realms
104
+ end
105
+
106
+ def domain_to_realm(dom)
107
+ Realm.new({
108
+ :id => dom["StorageId"],
109
+ :name => dom["Name"],
110
+ :limit => dom["AvailableDiskSize"]
111
+ })
112
+ end
113
+
114
+
115
+
116
+ #
117
+ # Images
118
+ #
119
+
120
+ def images(credentials, opts=nil )
121
+ templates = []
122
+ if (opts.nil?)
123
+ templates = execute(credentials, "templates.ps1")
124
+ else
125
+ if (opts[:id])
126
+ templates = execute(credentials, "templateById.ps1", opts[:id])
127
+ end
128
+ end
129
+ images = []
130
+ templates.each do |templ|
131
+ images << template_to_image(templ)
132
+ end
133
+ images
134
+ end
135
+
136
+ def template_to_image(templ)
137
+ Image.new({
138
+ :id => templ["TemplateId"],
139
+ :name => templ["Name"],
140
+ :description => templ["Description"],
141
+ :architecture => templ["OperatingSystem"],
142
+ :owner_id => NO_OWNER,
143
+ :mem_size_md => templ["MemSizeMb"],
144
+ :instance_count => templ["ChildCount"],
145
+ :state => templ["Status"],
146
+ :capacity => templ["SizeGB"]
147
+ })
148
+ end
149
+
150
+ #
151
+ # Instances
152
+ #
153
+
154
+ define_instance_states do
155
+ start.to(:stopped) .on( :create )
156
+
157
+ pending.to(:shutting_down) .on( :stop )
158
+ pending.to(:running) .automatically
159
+
160
+ running.to(:pending) .on( :reboot )
161
+ running.to(:shutting_down) .on( :stop )
162
+
163
+ shutting_down.to(:stopped) .automatically
164
+ stopped.to(:pending) .on( :start )
165
+ stopped.to(:finish) .on( :destroy )
166
+ end
167
+
168
+ def instances(credentials, opts=nil)
169
+ vms = []
170
+ if (opts.nil?)
171
+ vms = execute(credentials, "vms.ps1")
172
+ else
173
+ if (opts[:id])
174
+ vms = execute(credentials, "vmById.ps1", opts[:id])
175
+ end
176
+ end
177
+ instances = []
178
+ vms.each do |vm|
179
+ instances << vm_to_instance(vm)
180
+ end
181
+ instances = filter_on( instances, :id, opts )
182
+ instances = filter_on( instances, :state, opts )
183
+ instances
184
+ end
185
+
186
+ def vm_to_instance(vm)
187
+ Instance.new({
188
+ :id => vm["VmId"],
189
+ :description => vm["Description"],
190
+ :name => vm["Name"],
191
+ :architecture => vm["OperatingSystem"],
192
+ :owner_id => NO_OWNER,
193
+ :image_id => vm["TemplateId"],
194
+ :state => statify(vm["Status"]),
195
+ :instance_profile => InstanceProfile.new("rhevm"),
196
+ :actions => instance_actions_for(statify(vm["Status"])),
197
+ })
198
+ end
199
+
200
+ def start_instance(credentials, image_id)
201
+ vm = execute(credentials, "startVm.ps1", image_id)
202
+ vm_to_instance(vm[0])
203
+ end
204
+
205
+ def stop_instance(credentials, image_id)
206
+ vm = execute(credentials, "stopVm.ps1", image_id)
207
+ vm_to_instance(vm[0])
208
+ end
209
+
210
+ def create_instance(credentials, image_id, opts)
211
+ name = opts[:name]
212
+ name = "Inst-#{rand(10000)}" if (name.nil? or name.empty?)
213
+ realm_id = opts[:realm_id]
214
+ if (realm_id.nil?)
215
+ realms = filter_on(realms(credentials, opts), :name, :name => "data")
216
+ puts realms[0]
217
+ realm_id = realms[0].id
218
+ end
219
+ vm = execute(credentials, "addVm.ps1", image_id, name, realm_id)
220
+ vm_to_instance(vm[0])
221
+ end
222
+
223
+ def reboot_instance(credentials, image_id)
224
+ vm = execute(credentials, "rebootVm.ps1", image_id)
225
+ vm_to_instance(vm[0])
226
+ end
227
+
228
+ def destroy_instance(credentials, image_id)
229
+ vm = execute(credentials, "deleteVm.ps1", image_id)
230
+ vm_to_instance(vm[0])
231
+ end
232
+
233
+ #
234
+ # Storage Volumes
235
+ #
236
+
237
+ def storage_volumes(credentials, ids=nil)
238
+ volumes = []
239
+ volumes
240
+ end
241
+
242
+ #
243
+ # Storage Snapshots
244
+ #
245
+
246
+ def storage_snapshots(credentials, ids=nil)
247
+ snapshots = []
248
+ snapshots
249
+ end
250
+
251
+ end
252
+
253
+ end
254
+ end
255
+ end
@@ -0,0 +1,85 @@
1
+ #
2
+ # Copyright (C) 2009 RimuHosting Ltd
3
+ # Author: Ivan Meredith <ivan@ivan.net.nz>
4
+ #
5
+ # Licensed to the Apache Software Foundation (ASF) under one or more
6
+ # contributor license agreements. See the NOTICE file distributed with
7
+ # this work for additional information regarding copyright ownership. The
8
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
9
+ # "License"); you may not use this file except in compliance with the
10
+ # License. You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
+ # License for the specific language governing permissions and limitations
18
+ # under the License.
19
+
20
+ require "net/http"
21
+ require "net/https"
22
+ require "rubygems"
23
+ require "json"
24
+ require "deltacloud/base_driver"
25
+
26
+ module Deltacloud
27
+ module Drivers
28
+ module RimuHosting
29
+
30
+ class RimuHostingClient
31
+ def initialize(credentials ,baseuri = 'https://rimuhosting.com/r')
32
+ @uri = URI.parse(baseuri)
33
+ @service = Net::HTTP.new(@uri.host, @uri.port)
34
+ @service.use_ssl = true
35
+ @auth = "rimuhosting apikey=#{credentials.password}"
36
+ end
37
+
38
+ def request(resource, data='', method='GET')
39
+ headers = {"Accept" => "application/json", "Content-Type" => "application/json"}
40
+ if(!@auth.nil?)
41
+ headers["Authorization"] = @auth
42
+ end
43
+ r = @service.send_request(method, @uri.path + resource, data, headers)
44
+ puts r.body
45
+ res = JSON.parse(r.body)
46
+ res = res[res.keys[0]]
47
+
48
+ if(res['response_type'] == "ERROR" and ( (res['error_info']['error_class'] == "PermissionException") or
49
+ (res['error_info']['error_class'] == "LoginRequired") ))
50
+ raise Deltacloud::AuthException.new
51
+ end
52
+ res
53
+ end
54
+
55
+ def list_images
56
+ request('/distributions')["distro_infos"]
57
+ end
58
+
59
+ def list_plans
60
+ request('/pricing-plans;server-type=VPS')["pricing_plan_infos"]
61
+ end
62
+
63
+ def list_nodes
64
+ request('/orders;include_inactive=N')["about_orders"]
65
+ end
66
+
67
+ def set_server_state(id, state)
68
+ json = {"reboot_request" => {"running_state" => state}}.to_json
69
+ request("/orders/order-#{id}-a/vps/running-state", json, 'PUT')
70
+ end
71
+
72
+ def delete_server(id)
73
+ request("/orders/order-#{id}-a/vps",'', 'DELETE')
74
+ end
75
+
76
+ def create_server(image_id, plan_code, name)
77
+ json = {:new_vps => {:instantiation_options => {:domain_name => name, :distro => image_id},
78
+ :pricing_plan_code => plan_code}}.to_json
79
+ request('/orders/new-vps',json, 'POST')[:about_order]
80
+ end
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,166 @@
1
+ #
2
+ # Copyright (C) 2009 RimuHosting Ltd
3
+ # Author: Ivan Meredith <ivan@ivan.net.nz>
4
+ #
5
+ # Licensed to the Apache Software Foundation (ASF) under one or more
6
+ # contributor license agreements. See the NOTICE file distributed with
7
+ # this work for additional information regarding copyright ownership. The
8
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
9
+ # "License"); you may not use this file except in compliance with the
10
+ # License. You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
+ # License for the specific language governing permissions and limitations
18
+ # under the License.
19
+
20
+ require "deltacloud/base_driver"
21
+ require "deltacloud/drivers/rimuhosting/rimuhosting_client"
22
+
23
+ module Deltacloud
24
+ module Drivers
25
+ module RimuHosting
26
+
27
+ class RimuHostingDriver < Deltacloud::BaseDriver
28
+
29
+ feature :instances, :user_name
30
+
31
+ def images(credentails, opts=nil)
32
+ safely do
33
+ rh = RimuHostingClient.new(credentails)
34
+ images = rh.list_images.map do | image |
35
+ Image.new({
36
+ :id => image["distro_code"].gsub(/\./,"-"),
37
+ :name => image["distro_code"],
38
+ :description => image["distro_description"],
39
+ :owner_id => "root",
40
+ :architecture => "x86"
41
+ })
42
+ end
43
+ end
44
+ images.sort_by{|e| [e.description]}
45
+ images = filter_on( images, :id, opts)
46
+ images
47
+ end
48
+
49
+ def hardware_profiles(credentials, opts = nil)
50
+ safely do
51
+ rh = RimuHostingClient.new(credentials)
52
+ results = rh.list_plans.map do |plan|
53
+ # FIXME: x86 is not a valid architecture; what is Rimu offering ?
54
+ # FIXME: VPS plans offer a range of memory/storage, but that's
55
+ # not contained in hte pricing_plan_infos
56
+ HardwareProfile.new(plan["pricing_plan_code"]) do
57
+ memory plan["minimum_memory_mb"].to_f
58
+ storage plan["minimum_disk_gb"].to_i
59
+ architecture "x86"
60
+ end
61
+ end
62
+ end
63
+ filter_hardware_profiles(results, opts)
64
+ end
65
+
66
+ def realms(credentials, opts=nil)
67
+ [Realm.new( {
68
+ :id=>"rimu",
69
+ :name=>"RimuHosting",
70
+ :state=> "AVAILABLE"
71
+ } )]
72
+ end
73
+
74
+ def instances(credentials, opts=nil)
75
+ safely do
76
+ rh = RimuHostingClient.new(credentials)
77
+ instances = rh.list_nodes.map do | inst |
78
+ convert_srv_to_instance(inst)
79
+ end
80
+ end
81
+ instances = filter_on( instances, :id, opts)
82
+ instances = filter_on( instances, :state, opts )
83
+ instances
84
+ end
85
+
86
+ def reboot_instance(credentials, id)
87
+ safely do
88
+ rh = RimuHostingClient.new(credentials)
89
+ rh.set_server_state(id, :RESTARTING)
90
+ end
91
+ end
92
+
93
+ def start_instance(credentials, id)
94
+ safely do
95
+ rh = RimuHostingClient.new(credentials)
96
+ rh.set_server_state(id, :STARTED)
97
+ end
98
+ end
99
+
100
+ def stop_instance(credentials, id)
101
+ destroy_instance(credentials, id)
102
+ end
103
+
104
+ def destroy_instance(credentials, id)
105
+ safely do
106
+ rh = RimuHostingClient.new(credentials)
107
+ return rh.delete_server(id)
108
+ end
109
+ end
110
+
111
+ def create_instance(credentials, image_id, opts)
112
+ rh = RimuHostingClient.new(credentials)
113
+ # really need to raise an exception here.
114
+ hwp_id = opts[:hwp_id] || 1
115
+ # really bad, but at least its a fqdn
116
+ name = Time.now.to_s + '.com'
117
+ if (opts[:name]) then
118
+ name = opts[:name]
119
+ end
120
+ convert_srv_to_instance(rh.create_server(image_id, hwp_id, name))
121
+
122
+ end
123
+
124
+ def convert_srv_to_instance( inst )
125
+ Instance.new({
126
+ :id => inst["order_oid"].to_s,
127
+ :name => inst["domain_name"],
128
+ :image_id => "lenny",
129
+ :state => "RUNNING",
130
+ :name => inst["domain_name"],
131
+ :realm_id => "RH",
132
+ :owner_id => "root",
133
+ :instance_profile => InstanceProfile.new("none"),
134
+ :actions => instance_actions_for("RUNNING"),
135
+ :public_addresses => inst["allocated_ips"]["primary_ip"],
136
+ :launch_time => inst["billing_info"]["order_date"]["iso_format"]
137
+ })
138
+ end
139
+
140
+ define_instance_states do
141
+ start.to( :pending ) .automatically
142
+
143
+ pending.to( :running ) .automatically
144
+
145
+ running.to( :running ) .on(:reboot)
146
+ running.to( :shutting_down ) .on(:stop)
147
+
148
+ shutting_down.to( :stopped ) .automatically
149
+
150
+ stopped.to( :finish ) .automatically
151
+ end
152
+
153
+ def safely(&block)
154
+ begin
155
+ block.call
156
+ rescue Exception => e
157
+ raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
158
+ end
159
+ end
160
+
161
+
162
+ end
163
+
164
+ end
165
+ end
166
+ end