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

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

Constant Summary collapse

USER_NAME_MAX =
constraints(:collection => :instances, :feature => :user_name)[:max_length]

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS, BaseDriver::STATE_MACHINE_OPTS

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, #configured_providers, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

#create_instance(credentials, image_id, opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 104

def create_instance(credentials, image_id, opts={})
  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

  name = opts[:name]
  if not name
    name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
  end

  if name.length > USER_NAME_MAX
    raise "Parameter name must be #{USER_NAME_MAX} characters or less"
  end

  client = new_client(credentials)
  params = {
    'name' => name,
    'image' => image_id,
    'server.ram' => server_ram,
    'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
  }
  params.merge!('isSandbox' => 'true') if opts[:sandbox]
  safely do
    instance = client.request('grid/server/add', params)['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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 231

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_instance_port'].to_i,
        '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_balancer_port'].to_i,
    }
    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



213
214
215
216
217
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 213

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

#destroy_load_balancer(credentials, id) ⇒ Object



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

def destroy_load_balancer(credentials, id)
  gogrid = new_client(credentials)
  balancer = nil
  safely do
    balancer = gogrid.request('grid/loadbalancer/delete', { 'name' => id })
  end
end

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



67
68
69
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 67

def hardware_profile(credentials, opts={})
  hardware_profiles(credentials, :id => opts[:id]).first
end

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 46

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
  opts[:name] ||= opts[:id]
  filter_on( @hardware_profiles, :name, opts )
end

#images(credentials, opts = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 71

def images(credentials, opts=nil)
  imgs = []
  if opts and opts[:id]
    safely do
      begin
        imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
      rescue => e
        raise e unless e.message =~ /Bad Request/
        imgs = []
      end
    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



174
175
176
177
178
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
205
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 174

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



309
310
311
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 309

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

#keys(credentials, opts = nil) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 313

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



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

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

Raises:

  • (Deltacloud::BackendFeatureUnsupported)


304
305
306
307
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 304

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



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 162

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



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

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



266
267
268
269
270
271
272
273
274
275
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 266

def load_balancers(credentials, opts={})
  gogrid = new_client(credentials)
  balancers = []
  safely do
    gogrid.request('grid/loadbalancer/list', opts || {})['list'].each do |b|
      balancers << b
    end
  end
  balancers.collect { |b| convert_load_balancer(credentials, b) }
end

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



93
94
95
96
97
98
99
100
101
102
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 93

def realms(credentials, opts={})
  realms = []
  client = new_client(credentials)
  safely do
    client.request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
      realms << convert_realm(realm)
    end
  end
  realms = filter_on(realms, :id, opts)
end

#reboot_instance(credentials, id) ⇒ Object



207
208
209
210
211
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 207

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

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 149

def run_on_instance(credentials, opts={})
  target = instance(credentials, opts[:id])
  param = {}
  param[:credentials] = {
    :username => target.username,
    :password => target.password,
  }
  param[:credentials].merge!({ :password => opts[:password]}) if opts[:password].length>0
  param[:port] = opts[:port] || '22'
  param[:ip] = opts[:ip] || target.public_addresses.first.address
  Deltacloud::Runner.execute(opts[:cmd], param)
end

#start_instance(credentials, id) ⇒ Object



225
226
227
228
229
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 225

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



219
220
221
222
223
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 219

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

#valid_credentials?(credentials) ⇒ Boolean



324
325
326
327
328
329
330
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 324

def valid_credentials?(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