Class: Fog::Compute::Softlayer::Server

Inherits:
Fog::Compute::Server
  • Object
show all
Defined in:
lib/fog/softlayer/models/compute/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.

Raises:

  • (Exception)


51
52
53
54
55
56
# File 'lib/fog/softlayer/models/compute/server.rb', line 51

def initialize(attributes = {})
  # Forces every request inject bare_metal parameter
  raise Exception if attributes[:collection].nil? and attributes['bare_metal'].nil?
  super(attributes)
  set_defaults
end

Instance Method Details

#add_tags(tags) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
# File 'lib/fog/softlayer/models/compute/server.rb', line 58

def add_tags(tags)
  requires :id
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  tags.each do |tag|
    service.tags.new(:resource_id => self.id, :name => tag).save
  end
  self.reload
  true
end

#bare_metalObject



72
73
74
# File 'lib/fog/softlayer/models/compute/server.rb', line 72

def bare_metal
  @bare_metal
end

#bare_metal?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/fog/softlayer/models/compute/server.rb', line 68

def bare_metal?
  bare_metal
end

#datacenterObject



81
82
83
# File 'lib/fog/softlayer/models/compute/server.rb', line 81

def datacenter
  attributes[:datacenter][:name] unless attributes[:datacenter].nil?
end

#datacenter=(name) ⇒ Object



76
77
78
79
# File 'lib/fog/softlayer/models/compute/server.rb', line 76

def datacenter=(name)
  name = name['name'] if name.is_a?(Hash)
  attributes[:datacenter] = { :name => name }
end

#delete_tags(tags) ⇒ Object

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
# File 'lib/fog/softlayer/models/compute/server.rb', line 85

def delete_tags(tags)
  requires :id
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  tags.each do |tag|
    service.tags.new(:resource_id => self.id, :name => tag).destroy
  end
  self.reload
  true
end

#destroyObject



95
96
97
98
99
100
# File 'lib/fog/softlayer/models/compute/server.rb', line 95

def destroy
  requires :id
  request = bare_metal? ? :delete_bare_metal_server : :delete_vm
  response = service.send(request, self.id)
  response.body
end

#dns_nameObject



102
103
104
# File 'lib/fog/softlayer/models/compute/server.rb', line 102

def dns_name
  fqdn
end

#generate_order_templateObject



382
383
384
385
386
387
# File 'lib/fog/softlayer/models/compute/server.rb', line 382

def generate_order_template
  copy = self.dup
  copy.pre_save
  return service.generate_bare_metal_order_template(copy.attributes).body if bare_metal?
  service.generate_virtual_guest_order_template(copy.attributes).body
end

#get_active_ticketsObject



359
360
361
362
# File 'lib/fog/softlayer/models/compute/server.rb', line 359

def get_active_tickets
  return service.get_bare_metal_active_tickets(id).body if bare_metal?
  service.get_virtual_guest_active_tickets(id).body
end

#get_upgrade_optionsObject



369
370
371
372
# File 'lib/fog/softlayer/models/compute/server.rb', line 369

def get_upgrade_options
  return service.get_bare_metal_upgrade_item_prices(id).body if bare_metal?
  service.get_virtual_guest_upgrade_item_prices(id).body
end

#get_usersObject



364
365
366
367
# File 'lib/fog/softlayer/models/compute/server.rb', line 364

def get_users
  return service.get_bare_metal_users(id).body if bare_metal?
  service.get_virtual_guest_users(id).body
end

#image_idObject



110
111
112
# File 'lib/fog/softlayer/models/compute/server.rb', line 110

def image_id
  attributes[:image_id][:globalIdentifier] unless attributes[:image_id].nil?
end

#image_id=(uuid) ⇒ Object



106
107
108
# File 'lib/fog/softlayer/models/compute/server.rb', line 106

def image_id=(uuid)
  attributes[:image_id] = {:globalIdentifier => uuid}
end

#key_pairsObject



181
182
183
# File 'lib/fog/softlayer/models/compute/server.rb', line 181

def key_pairs
  attributes[:key_pairs]
end

#key_pairs=(keys) ⇒ Object

Raises:

  • (ArgumentError)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fog/softlayer/models/compute/server.rb', line 185

def key_pairs=(keys)
  raise ArgumentError, "Argument #{local_variables.first.to_s} for #{self.class.name}##{__method__} must be Array." unless keys.is_a?(Array)
  attributes[:key_pairs] = []
  keys.map do |key|
    key = self.symbolize_keys(key) if key.is_a?(Hash)
    unless key.is_a?(Fog::Compute::Softlayer::KeyPair) or (key.is_a?(Hash) and key[:id])
      raise ArgumentError, "Elements of keys array for #{self.class.name}##{__method__} must be a Hash with key 'id', or Fog::Compute::Softlayer::KeyPair"
    end
    key = service.key_pairs.get(key[:id]) unless key.is_a?(Fog::Compute::Softlayer::KeyPair)
    attributes[:key_pairs] << key
  end
end

#nameObject



118
119
120
# File 'lib/fog/softlayer/models/compute/server.rb', line 118

def name
  attributes[:hostname]
end

#name=(set) ⇒ Object



114
115
116
# File 'lib/fog/softlayer/models/compute/server.rb', line 114

def name=(set)
  attributes[:hostname] = set
end

#network_componentsObject



234
235
236
237
238
239
240
# File 'lib/fog/softlayer/models/compute/server.rb', line 234

def network_components
  if id
    (public_network_components << private_network_components).flatten
  else
    attributes[:network_components]
  end
end

#os_codeObject



154
155
156
# File 'lib/fog/softlayer/models/compute/server.rb', line 154

def os_code
  attributes['operatingSystem']['softwareLicense']['softwareDescription']['referenceCode'] if attributes['operatingSystem']
end

#pre_saveObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fog/softlayer/models/compute/server.rb', line 122

def pre_save
  extract_flavor
  validate_attributes
  if self.vlan
    attributes[:vlan] = { :networkVlan => { :id => self.vlan.id } }
  end
  if self.private_vlan
    attributes[:private_vlan] = { :networkVlan => { :id => self.private_vlan.id } }
  end
  if self.key_pairs
    attributes[:key_pairs].map! { |key| { :id => key.id } }
  end
  if self.network_components
    self.network_components = self.network_components.map do |component|
      component[:maxSpeed] = component.delete(:speed) if component[:speed]
      component[:maxSpeed] = component.delete(:max_speed) if component[:max_speed]
      component
    end
  end

  remap_attributes(attributes, attributes_mapping)
  clean_attributes
end

#private_ipObject

maintain backward compatibility with <0.3.13



146
147
148
# File 'lib/fog/softlayer/models/compute/server.rb', line 146

def private_ip # maintain backward compatibility with <0.3.13
  private_ip_address
end

#private_network_componentsObject



250
251
252
253
254
255
256
# File 'lib/fog/softlayer/models/compute/server.rb', line 250

def private_network_components
  if attributes['backendNetworkComponents']
    attributes['backendNetworkComponents'].map { |n| Fog::Compute::Softlayer::NetworkComponent.new(n) }
  else
    []
  end
end

#private_vlanObject



158
159
160
# File 'lib/fog/softlayer/models/compute/server.rb', line 158

def private_vlan
  attributes[:private_vlan] ||= _get_private_vlan
end

#private_vlan=(value) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/fog/softlayer/models/compute/server.rb', line 162

def private_vlan=(value)
  unless value.is_a?(Integer) or value.is_a?(Fog::Network::Softlayer::Network)
    raise ArgumentError, "vlan argument for #{self.class.name}##{__method__} must be Integer or Fog::Network::Softlayer::Network."
  end
  value = network_connection.networks.get(value) if value.is_a?(Integer)
  attributes[:private_vlan] = value
end

#provision_scriptObject



230
231
232
# File 'lib/fog/softlayer/models/compute/server.rb', line 230

def provision_script
  attributes[:provision_script]
end

#provision_script=(value) ⇒ Object



226
227
228
# File 'lib/fog/softlayer/models/compute/server.rb', line 226

def provision_script=(value)
  attributes[:provision_script] = value
end

#public_ipObject

maintain backward compatibility with <0.3.13



150
151
152
# File 'lib/fog/softlayer/models/compute/server.rb', line 150

def public_ip # maintain backward compatibility with <0.3.13
  public_ip_address
end

#public_network_componentsObject



242
243
244
245
246
247
248
# File 'lib/fog/softlayer/models/compute/server.rb', line 242

def public_network_components
  if attributes['frontendNetworkComponents']
    attributes['frontendNetworkComponents'].map { |n| Fog::Compute::Softlayer::NetworkComponent.new(n) }
  else
    []
  end
end

#ram=(set) ⇒ Object



210
211
212
213
214
215
# File 'lib/fog/softlayer/models/compute/server.rb', line 210

def ram=(set)
  if set.is_a?(Array) and set.first['hardwareComponentModel']
    set = 1024 * set.first['hardwareComponentModel']['capacity'].to_i
  end
  attributes[:ram] = set
end

#ready?Boolean

Returns:

  • (Boolean)


258
259
260
261
262
263
264
265
266
267
268
# File 'lib/fog/softlayer/models/compute/server.rb', line 258

def ready?
  begin
    if bare_metal?
      state == "on"
    else
      state == "Running"
    end
  rescue Excon::Errors::InternalServerError => e
    false
  end
end

#reboot(use_hard_reboot = true) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'lib/fog/softlayer/models/compute/server.rb', line 270

def reboot(use_hard_reboot = true)
  # requires :id # TODO: debug why this breaks the tests on bare metal and uncomment this
  if bare_metal?
    service.reboot_bare_metal_server(id, use_hard_reboot)
  else
    service.reboot_vm(id, use_hard_reboot)
  end
  true
end

#relaunch!Object

reload the OS on a server (method name reload was already taken)



171
172
173
174
175
176
177
178
179
# File 'lib/fog/softlayer/models/compute/server.rb', line 171

def relaunch!
  requires :id
  body = [ "FORCE", {}]
  body[1][:sshKeyIds] = key_pairs.map {|kp| kp.id} unless key_pairs.empty?
  type = bare_metal? ? :hardware_server : :virtual_guest
  status = service.request(type, "#{id}/reloadOperatingSystem", :body => body, :http_method => :post).status
  wait_for { not ready? } # block until the relaunch has begun
  [200, 201].include?(status)
end

#saveObject

Note:

You should use servers.create to create servers instead calling this method directly

Creates server

  • requires attributes: :name, :domain, and :flavor_id OR (:cpu_count && :ram && :disks)

  • State Transitions

    • BUILD -> ACTIVE

    • BUILD -> ERROR (on error)

Raises:

  • (Fog::Errors::Error)


339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/fog/softlayer/models/compute/server.rb', line 339

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
  copy = self.dup
  copy.pre_save

  data = if bare_metal?
    service.create_bare_metal_server(copy.attributes).body
  else
    service.create_vm(copy.attributes).body.first
  end

  data.delete("bare_metal")
  merge_attributes(data)
  true
end

#shutdownObject

Soft power off



313
314
315
316
317
318
319
320
321
# File 'lib/fog/softlayer/models/compute/server.rb', line 313

def shutdown
  # requires :id # TODO: debug why this breaks the tests on bare metal and uncomment this
  if bare_metal?
    raise Fog::Errors::Error.new('Shutdown not supported on baremetal servers. Use #stop.')
  else
    service.power_off_vm(id, false)
  end
  true
end

#snapshotObject



287
288
289
# File 'lib/fog/softlayer/models/compute/server.rb', line 287

def snapshot
  # TODO: implement
end

#ssh_passwordObject



280
281
282
283
284
285
# File 'lib/fog/softlayer/models/compute/server.rb', line 280

def ssh_password
  requires :id
  service_path = bare_metal? ? :hardware_server : :virtual_guest
  @sshpass ||= service.request(service_path, id, :query => 'objectMask=mask[id,operatingSystem.passwords[password]]').body
  @sshpass['operatingSystem']['passwords'][0]['password'] unless @sshpass['operatingSystem'].nil? or @sshpass['operatingSystem']['passwords'].empty?
end

#startObject



291
292
293
294
295
296
297
298
299
# File 'lib/fog/softlayer/models/compute/server.rb', line 291

def start
  # requires :id # TODO: debug why this breaks the tests on bare metal and uncomment this
  if bare_metal?
    service.power_on_bare_metal_server(id)
  else
    service.power_on_vm(id)
  end
  true
end

#stateObject



323
324
325
326
327
328
329
# File 'lib/fog/softlayer/models/compute/server.rb', line 323

def state
  if bare_metal?
    service.request(:hardware_server, "#{id}/getServerPowerState").body
  else
    service.request(:virtual_guest, "#{id}/getPowerState").body['name']
  end
end

#stopObject

Hard power off



302
303
304
305
306
307
308
309
310
# File 'lib/fog/softlayer/models/compute/server.rb', line 302

def stop
  # requires :id # TODO: debug why this breaks the tests on bare metal and uncomment this
  if bare_metal?
    service.power_off_bare_metal_server(id)
  else
    service.power_off_vm(id, true)
  end
  true
end

#tagsObject



355
356
357
# File 'lib/fog/softlayer/models/compute/server.rb', line 355

def tags
  attributes[:tags].map { |i| i['tag']['name'] if i['tag'] }.compact if attributes[:tags]
end

#update(update_attributes) ⇒ Object

Raises:

  • (ArgumentError)


374
375
376
377
378
379
380
# File 'lib/fog/softlayer/models/compute/server.rb', line 374

def update(update_attributes)
  raise ArgumentError if update_attributes.nil?
  product_connection
  prices = get_item_prices_id(update_attributes)
  order = generate_upgrade_order(prices, update_attributes[:time] || update_attributes[:maintenance_window])
  @product_conn.place_order(order).body
end

#user_dataObject



222
223
224
# File 'lib/fog/softlayer/models/compute/server.rb', line 222

def user_data
  attributes[:user_data]
end

#user_data=(value) ⇒ Object



218
219
220
# File 'lib/fog/softlayer/models/compute/server.rb', line 218

def user_data=(value)
  attributes[:user_data] = [{'value' => value}]
end

#vlanObject



198
199
200
# File 'lib/fog/softlayer/models/compute/server.rb', line 198

def vlan
  attributes[:vlan] ||= _get_vlan
end

#vlan=(value) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/fog/softlayer/models/compute/server.rb', line 202

def vlan=(value)
  unless value.is_a?(Integer) or value.is_a?(Fog::Network::Softlayer::Network)
    raise ArgumentError, "vlan argument for #{self.class.name}##{__method__} must be Integer or Fog::Network::Softlayer::Network."
  end
  value = network_connection.networks.get(value) if value.is_a?(Integer)
  attributes[:vlan] = value
end