Class: Deltacloud::Drivers::Gogrid::GogridDriver
- Inherits:
-
BaseDriver
- Object
- BaseDriver
- Deltacloud::Drivers::Gogrid::GogridDriver
show all
- Defined in:
- lib/deltacloud/drivers/gogrid/gogrid_driver.rb
Instance Method Summary
collapse
-
#create_instance(credentials, image_id, opts = nil) ⇒ Object
-
#create_load_balancer(credentials, opts = {}) ⇒ Object
-
#destroy_instance(credentials, id) ⇒ Object
-
#destroy_load_balancer(credentials, id) ⇒ Object
-
#images(credentials, opts = nil) ⇒ Object
-
#instances(credentials, opts = nil) ⇒ Object
-
#key(credentials, opts = nil) ⇒ Object
-
#keys(credentials, opts = nil) ⇒ Object
-
#lb_register_instance(credentials, opts = {}) ⇒ Object
-
#lb_unregister_instance(credentials, opts = {}) ⇒ Object
Move this to capabilities.
-
#list_instances(credentials, id) ⇒ Object
-
#load_balancer(credentials, opts = {}) ⇒ Object
-
#load_balancers(credentials, opts = {}) ⇒ Object
-
#realms(credentials, opts = nil) ⇒ Object
-
#reboot_instance(credentials, id) ⇒ Object
-
#start_instance(credentials, id) ⇒ Object
-
#stop_instance(credentials, id) ⇒ Object
-
#supported_collections ⇒ Object
-
#valid_credentials?(credentials) ⇒ Boolean
Methods inherited from BaseDriver
#blob, #blob_data, #blobs, #bucket, #buckets, #catched_exceptions_list, #create_blob, #create_bucket, declare_feature, define_hardware_profile, define_instance_states, #delete_blob, #delete_bucket, feature, feature_decl_for, feature_decls, #features, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #hardware_profiles, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #storage_snapshot, #storage_snapshots, #storage_volume, #storage_volumes
Instance Method Details
#create_instance(credentials, image_id, opts = nil) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 76
def create_instance(credentials, image_id, opts=nil)
server_ram = nil
if opts[:hwp_memory]
mem = opts[:hwp_memory].to_i
server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
else
server_ram = "512MB"
end
client = new_client(credentials)
name = (opts[:name] && opts[:name]!='') ? opts[:name] : get_random_instance_name
safely do
instance = client.request('grid/server/add', {
'name' => name,
'image' => image_id,
'server.ram' => server_ram,
'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
})['list'].first
if instance
login_data = get_login_data(client, instance[:id])
if login_data['username'] and login_data['password']
instance['username'] = login_data['username']
instance['password'] = login_data['password']
inst = convert_instance(instance, credentials.user)
else
inst = convert_instance(instance, credentials.user)
inst.authn_error = "Unable to fetch password"
end
return inst
else
return nil
end
end
end
|
#create_load_balancer(credentials, opts = {}) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 179
def create_load_balancer(credentials, opts={})
gogrid = new_client(credentials)
balancer, l_instance = nil, nil
safely do
virtip = get_free_ip_from_realm(credentials, opts['realm_id'])
if opts['instance_id']
l_instance = instance(credentials, :id => opts['instance_id'])
real_ip = {
'realiplist.0.port' => opts['listener_inst_port'],
'realiplist.0.ip' => l_instance ? l_instance.public_addresses.first : ""
}
else
real_ip = false
end
request = {
'name' => opts['name'],
'virtualip.ip' => virtip,
'virtualip.port' => opts['listener_lbr_port'],
}
request.merge!(real_ip) if real_ip
balancer = gogrid.request('grid/loadbalancer/add', request)['list'].first
end
balancer = convert_load_balancer(credentials, balancer)
balancer.instances = [l_instance] if l_instance
balancer
end
|
#destroy_instance(credentials, id) ⇒ Object
161
162
163
164
165
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 161
def destroy_instance(credentials, id)
safely do
new_client(credentials).request('grid/server/delete', { 'name' => id})
end
end
|
#destroy_load_balancer(credentials, id) ⇒ Object
206
207
208
209
210
211
212
213
214
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 206
def destroy_load_balancer(credentials, id)
gogrid = new_client(credentials)
balancer = nil
safely do
balancer = gogrid.request('grid/loadbalancer/delete', { 'name' => id })
balancer = load_balancer(credentials, :id => id) unless balancer
end
convert_load_balancer(credentials, balancer)
end
|
#images(credentials, opts = nil) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 51
def images(credentials, opts=nil)
imgs = []
if opts and opts[:id]
safely do
imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
end
else
safely do
imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
convert_image(image, credentials.user)
end
end
end
imgs = filter_on( imgs, :architecture, opts )
imgs.sort_by{|e| [e.owner_id, e.description]}
end
|
#instances(credentials, opts = nil) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 122
def instances(credentials, opts=nil)
instances = []
if opts and opts[:id]
begin
client = new_client(credentials)
instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
login_data = get_login_data(client, instance['id'])
if login_data['username'] and login_data['password']
instance['username'] = login_data['username']
instance['password'] = login_data['password']
inst = convert_instance(instance, credentials.user)
else
inst = convert_instance(instance, credentials.user)
inst.authn_error = "Unable to fetch password"
end
instances = [inst]
rescue Exception => e
if e.message == "400 Bad Request"
instances = list_instances(credentials, opts[:id])
end
end
else
instances = list_instances(credentials, nil)
end
instances = filter_on( instances, :state, opts )
instances
end
|
#key(credentials, opts = nil) ⇒ Object
261
262
263
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 261
def key(credentials, opts=nil)
keys(credentials, opts).first
end
|
#keys(credentials, opts = nil) ⇒ Object
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 265
def keys(credentials, opts=nil)
gogrid = new_client( credentials )
creds = []
safely do
gogrid.request('support/password/list')['list'].each do |password|
creds << convert_key(password)
end
end
return creds
end
|
#lb_register_instance(credentials, opts = {}) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 240
def lb_register_instance(credentials, opts={})
client = new_client(credentials)
instance = instance(credentials, :id => opts[:instance_id])
balancer = client.request('grid/loadbalancer/get', { 'name' => opts[:id]})['list'].first
safely do
convert_load_balancer(credentials, client.request('grid/loadbalancer/edit', {
"id" => balancer['id'],
"realiplist.#{balancer['realiplist'].size}.ip" => instance.public_addresses.first,
"realiplist.#{balancer['realiplist'].size}.port" => balancer['virtualip']['port']
}))
end
end
|
#lb_unregister_instance(credentials, opts = {}) ⇒ Object
Move this to capabilities
254
255
256
257
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 254
def lb_unregister_instance(credentials, opts={})
raise Deltacloud::BackendFeatureUnsupported.new('501',
'Unregistering instances from load balancer is not supported in GoGrid')
end
|
#list_instances(credentials, id) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 110
def list_instances(credentials, id)
instances = []
safely do
new_client(credentials).request('grid/server/list')['list'].collect do |instance|
if id.nil? or instance['name'] == id
instances << convert_instance(instance, credentials.user)
end
end
end
instances
end
|
#load_balancer(credentials, opts = {}) ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 227
def load_balancer(credentials, opts={})
gogrid = new_client(credentials)
balancer = nil
begin
balancer = gogrid.request('grid/loadbalancer/get', { 'name' => opts[:id] })['list'].first
balancer['instances'] = instances(credentials)
return convert_load_balancer(credentials, balancer)
rescue OpenURI::HTTPError
balancer = load_balancers(credentials, :id => opts[:id]).first
end
end
|
#load_balancers(credentials, opts = {}) ⇒ Object
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 216
def load_balancers(credentials, opts={})
gogrid = new_client(credentials)
balancers = []
safely do
balancer = gogrid.request('grid/loadbalancer/list', opts || {})['list'].each do |balancer|
balancers << balancer
end
end
balancers.collect { |b| convert_load_balancer(credentials, b) }
end
|
#realms(credentials, opts = nil) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 68
def realms(credentials, opts=nil)
safely do
new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
convert_realm(realm)
end
end
end
|
#reboot_instance(credentials, id) ⇒ Object
155
156
157
158
159
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 155
def reboot_instance(credentials, id)
safely do
new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
end
end
|
#start_instance(credentials, id) ⇒ Object
173
174
175
176
177
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 173
def start_instance(credentials, id)
safely do
new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
end
end
|
#stop_instance(credentials, id) ⇒ Object
167
168
169
170
171
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 167
def stop_instance(credentials, id)
safely do
new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
end
end
|
#supported_collections ⇒ Object
46
47
48
49
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 46
def supported_collections
DEFAULT_COLLECTIONS.reject! { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
DEFAULT_COLLECTIONS + [ :keys, :load_balancers ]
end
|
#valid_credentials?(credentials) ⇒ Boolean
276
277
278
279
280
281
282
283
|
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 276
def valid_credentials?(credentials)
client = new_client(credentials)
return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
true
end
|