Class: Deltacloud::Drivers::Gogrid::GogridDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/gogrid/gogrid_driver.rb

Constant Summary

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS

Instance Method Summary collapse

Methods inherited from BaseDriver

#blob, #bucket, #catched_exceptions_list, declare_feature, define_hardware_profile, define_instance_states, feature, feature_decl_for, feature_decls, #features, features, #features_for_operation, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #has_capability?, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #storage_snapshot, #storage_volume

Instance Method Details

#create_instance(credentials, image_id, opts = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 90

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
       = (client, instance[:id])
      if ['username'] and ['password']
        instance['username'] = ['username']
        instance['password'] = ['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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 193

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



175
176
177
178
179
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 175

def destroy_instance(credentials, id)
  safely do
    new_client(credentials).request('grid/server/delete', { 'name' => id})
  end
end

#destroy_load_balancer(credentials, id) ⇒ Object



220
221
222
223
224
225
226
227
228
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 220

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

#hardware_profiles(credentials, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 40

def hardware_profiles(credentials, opts={})
  client = new_client(credentials)
  safely do
    server_types = client.request('common/lookup/list', { 'lookup' => 'server.type' })
    server_rams = client.request('common/lookup/list', { 'lookup' => 'server.ram' })
    @hardware_profiles = []
    server_types['list'].each do |type|
      memory_values = server_rams['list'].collect do |r| 
          r['name'] =~ /MB$/ ? r['name'].gsub(/MB$/, '').to_i : (r['name'].gsub(/(\w{2})$/, '')).to_i*1024
      end
      @hardware_profiles << ::Deltacloud::HardwareProfile.new(type['name'].tr(' ', '-').downcase) do
        cpu 2
        memory memory_values
        storage 25
      end
    end
  end
  @hardware_profiles
end

#images(credentials, opts = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 65

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 136

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
       = (client, instance['id'])
      if ['username'] and ['password']
        instance['username'] = ['username']
        instance['password'] = ['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"
        # in the case of a VM that we just made, the grid/server/get method
        # throws a "400 Bad Request error".  In this case we try again by
        # getting a full listing a filtering on the id.  This could
        # potentially take a long time, but I don't see another way to get
        # information about a newly created instance
        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



275
276
277
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 275

def key(credentials, opts=nil)
  keys(credentials, opts).first
end

#keys(credentials, opts = nil) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 279

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



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 254

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



268
269
270
271
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 268

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



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 124

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



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 241

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



230
231
232
233
234
235
236
237
238
239
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 230

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



82
83
84
85
86
87
88
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 82

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



169
170
171
172
173
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 169

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



187
188
189
190
191
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 187

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



181
182
183
184
185
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 181

def stop_instance(credentials, id)
  safely do
    new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
  end
end

#supported_collectionsObject



60
61
62
63
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 60

def supported_collections
  DEFAULT_COLLECTIONS.reject! { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
  DEFAULT_COLLECTIONS + [ :keys, :load_balancers ]
end

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


290
291
292
293
294
295
296
297
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 290

def valid_credentials?(credentials)
  client = new_client(credentials)
  # FIXME: We need to do this call to determine if
  #        GoGrid is working with given credentials. There is no
  #        other way to check, if given credentials are valid or not.
  return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
  true
end