Class: Fog::OpenStack::Compute::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/openstack/compute/models/server.rb', line 60

def initialize(attributes = {})
  # Old 'connection' is renamed as service and should be used instead
  prepare_service_value(attributes)

  self.security_groups = attributes.delete(:security_groups)
  self.min_count = attributes.delete(:min_count)
  self.max_count = attributes.delete(:max_count)
  self.nics = attributes.delete(:nics)
  self.os_scheduler_hints = attributes.delete(:os_scheduler_hints)
  self.block_device_mapping = attributes.delete(:block_device_mapping)
  self.block_device_mapping_v2 = attributes.delete(:block_device_mapping_v2)

  super
end

Instance Attribute Details

#block_device_mappingObject

Returns the value of attribute block_device_mapping.



54
55
56
# File 'lib/fog/openstack/compute/models/server.rb', line 54

def block_device_mapping
  @block_device_mapping
end

#block_device_mapping_v2Object

Returns the value of attribute block_device_mapping_v2.



54
55
56
# File 'lib/fog/openstack/compute/models/server.rb', line 54

def block_device_mapping_v2
  @block_device_mapping_v2
end

#flavor_refObject

Returns the value of attribute flavor_ref.



187
188
189
# File 'lib/fog/openstack/compute/models/server.rb', line 187

def flavor_ref
  @flavor_ref
end

#image_refObject

Returns the value of attribute image_ref.



183
184
185
# File 'lib/fog/openstack/compute/models/server.rb', line 183

def image_ref
  @image_ref
end

#max_count=(value) ⇒ Object (writeonly)

Sets the attribute max_count

Parameters:

  • value

    the value to set the attribute max_count to.



336
337
338
# File 'lib/fog/openstack/compute/models/server.rb', line 336

def max_count=(value)
  @max_count = value
end

#min_count=(value) ⇒ Object (writeonly)

Sets the attribute min_count

Parameters:

  • value

    the value to set the attribute min_count to.



334
335
336
# File 'lib/fog/openstack/compute/models/server.rb', line 334

def min_count=(value)
  @min_count = value
end

#nics=(value) ⇒ Object (writeonly)

Sets the attribute nics

Parameters:

  • value

    the value to set the attribute nics to.



53
54
55
# File 'lib/fog/openstack/compute/models/server.rb', line 53

def nics=(value)
  @nics = value
end

#os_scheduler_hints=(value) ⇒ Object (writeonly)

Sets the attribute os_scheduler_hints

Parameters:

  • value

    the value to set the attribute os_scheduler_hints to.



53
54
55
# File 'lib/fog/openstack/compute/models/server.rb', line 53

def os_scheduler_hints=(value)
  @os_scheduler_hints = value
end

#passwordObject (readonly)

Returns the value of attribute password.



52
53
54
# File 'lib/fog/openstack/compute/models/server.rb', line 52

def password
  @password
end

#personalityHash

Note:

This attribute is only used for server creation. This field will be nil on subsequent retrievals.

Returns Hash containing data to inject into the file system of the cloud server instance during server creation.

Examples:

To inject fog.txt into file system

:personality => [{ :path => '/root/fog.txt',
                   :contents => Base64.encode64('Fog was here!')
                }]

Returns:

  • (Hash)

    Hash containing data to inject into the file system of the cloud server instance during server creation.

See Also:



29
# File 'lib/fog/openstack/compute/models/server.rb', line 29

attribute :personality

#projectObject

In some cases it’s handy to be able to store the project for the record, e.g. swift doesn’t contain project info in the result, so we can track it in this attribute based on what project was used in the request



58
59
60
# File 'lib/fog/openstack/compute/models/server.rb', line 58

def project
  @project
end

#security_groupsObject



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/fog/openstack/compute/models/server.rb', line 229

def security_groups
  if id
    requires :id

    groups = service.list_security_groups(:server_id => id).body['security_groups']

    groups.map do |group|
      Fog::OpenStack::Compute::SecurityGroup.new group.merge(:service => service)
    end
  else
    service.security_groups.all
  end
end

Instance Method Details

#all_addressesObject



104
105
106
107
108
109
# File 'lib/fog/openstack/compute/models/server.rb', line 104

def all_addresses
  # currently openstack API does not tell us what is a floating ip vs a fixed ip for the vm listing,
  # we fall back to get all addresses and filter sadly.
  # Only includes manually-assigned addresses, not auto-assigned
  @all_addresses ||= service.list_all_addresses.body["floating_ips"].select { |data| data['instance_id'] == id }
end

#associate_address(floating_ip) ⇒ Object



319
320
321
322
# File 'lib/fog/openstack/compute/models/server.rb', line 319

def associate_address(floating_ip)
  requires :id
  service.associate_address id, floating_ip
end

#attach_volume(volume_id, device_name) ⇒ Object



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

def attach_volume(volume_id, device_name)
  requires :id
  service.attach_volume(volume_id, id, device_name)
  true
end

#change_password(admin_password) ⇒ Object



199
200
201
202
203
# File 'lib/fog/openstack/compute/models/server.rb', line 199

def change_password(admin_password)
  requires :id
  service.change_server_password(id, admin_password)
  true
end

#confirm_resizeObject



223
224
225
226
227
# File 'lib/fog/openstack/compute/models/server.rb', line 223

def confirm_resize
  requires :id
  service.confirm_resize_server(id)
  true
end

#console(log_length = nil) ⇒ Object



299
300
301
302
# File 'lib/fog/openstack/compute/models/server.rb', line 299

def console(log_length = nil)
  requires :id
  service.get_console_output(id, log_length)
end

#create_image(name, metadata = {}) ⇒ Object



294
295
296
297
# File 'lib/fog/openstack/compute/models/server.rb', line 294

def create_image(name,  = {})
  requires :id
  service.create_image(id, name, )
end

#destroyObject



93
94
95
96
97
# File 'lib/fog/openstack/compute/models/server.rb', line 93

def destroy
  requires :id
  service.delete_server(id)
  true
end

#detach_volume(volume_id) ⇒ Object



360
361
362
363
364
# File 'lib/fog/openstack/compute/models/server.rb', line 360

def detach_volume(volume_id)
  requires :id
  service.detach_volume(id, volume_id)
  true
end

#disassociate_address(floating_ip) ⇒ Object



324
325
326
327
# File 'lib/fog/openstack/compute/models/server.rb', line 324

def disassociate_address(floating_ip)
  requires :id
  service.disassociate_address id, floating_ip
end

#evacuate(host = nil, on_shared_storage = nil, admin_password = nil) ⇒ Object



314
315
316
317
# File 'lib/fog/openstack/compute/models/server.rb', line 314

def evacuate(host = nil, on_shared_storage = nil, admin_password = nil)
  requires :id
  service.evacuate_server(id, host, on_shared_storage, admin_password)
end

#failed?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/fog/openstack/compute/models/server.rb', line 195

def failed?
  state == 'ERROR'
end

#floating_ip_addressObject



165
166
167
# File 'lib/fog/openstack/compute/models/server.rb', line 165

def floating_ip_address
  floating_ip_addresses.first
end

#floating_ip_addressesObject



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
# File 'lib/fog/openstack/compute/models/server.rb', line 127

def floating_ip_addresses
  all_floating = if addresses
                   flattened_values = addresses.values.flatten
                   flattened_values.select { |d| d["OS-EXT-IPS:type"] == "floating" }.collect { |a| a["addr"] }
                 else
                   []
                 end

  # Return them all, leading with manually assigned addresses
  manual = all_addresses.collect { |addr| addr["ip"] }

  all_floating.sort do |a, b|
    a_manual = manual.include? a
    b_manual = manual.include? b

    if a_manual && !b_manual
      -1
    elsif !a_manual && b_manual
      1
    else
      0
    end
  end
  all_floating.empty? ? manual : all_floating
end

#imagesObject



99
100
101
102
# File 'lib/fog/openstack/compute/models/server.rb', line 99

def images
  requires :id
  service.images(:server => self)
end

#ip_addressesObject

returns all ip_addresses for a given instance this includes both the fixed ip(s) and the floating ip(s)



123
124
125
# File 'lib/fog/openstack/compute/models/server.rb', line 123

def ip_addresses
  addresses ? addresses.values.flatten.collect { |x| x['addr'] } : []
end

#live_migrate(host, block_migration, disk_over_commit) ⇒ Object



309
310
311
312
# File 'lib/fog/openstack/compute/models/server.rb', line 309

def live_migrate(host, block_migration, disk_over_commit)
  requires :id
  service.live_migrate_server(id, host, block_migration, disk_over_commit)
end

#metadataObject



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

def 
  @metadata ||= begin
    Fog::OpenStack::Compute::Metadata.new(:service => service,
                                          :parent  => self)
  end
end

#metadata=(new_metadata = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/fog/openstack/compute/models/server.rb', line 82

def metadata=( = {})
  return unless 
  metas = []
  .each { |k, v| metas << {"key" => k, "value" => v} }
  @metadata = .load(metas)
end

#migrateObject



304
305
306
307
# File 'lib/fog/openstack/compute/models/server.rb', line 304

def migrate
  requires :id
  service.migrate_server(id)
end

#networksObject



338
339
340
# File 'lib/fog/openstack/compute/models/server.rb', line 338

def networks
  service.networks(:server => self)
end

#os_interfacesObject



111
112
113
114
# File 'lib/fog/openstack/compute/models/server.rb', line 111

def os_interfaces
  requires :id
  service.os_interfaces(:server => self)
end

#pauseObject



256
257
258
259
# File 'lib/fog/openstack/compute/models/server.rb', line 256

def pause
  requires :id
  service.pause_server(id)
end

#private_ip_addressObject



179
180
181
# File 'lib/fog/openstack/compute/models/server.rb', line 179

def private_ip_address
  private_ip_addresses.first
end

#private_ip_addressesObject



173
174
175
176
177
# File 'lib/fog/openstack/compute/models/server.rb', line 173

def private_ip_addresses
  rfc1918_regexp = /(^10\.|^172\.1[6-9]\.|^172\.2[0-9]\.|^172.3[0-1]\.|^192\.168\.)/
  almost_private = ip_addresses - public_ip_addresses - floating_ip_addresses
  almost_private.select { |ip| rfc1918_regexp.match ip }
end

#public_ip_addressObject



169
170
171
# File 'lib/fog/openstack/compute/models/server.rb', line 169

def public_ip_address
  public_ip_addresses.first
end

#public_ip_addressesObject



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fog/openstack/compute/models/server.rb', line 153

def public_ip_addresses
  if floating_ip_addresses.empty?
    if addresses
      addresses.select { |s| s[0] =~ /public/i }.collect { |a| a[1][0]['addr'] }
    else
      []
    end
  else
    floating_ip_addresses
  end
end

#ready?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/fog/openstack/compute/models/server.rb', line 191

def ready?
  state == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



245
246
247
248
249
# File 'lib/fog/openstack/compute/models/server.rb', line 245

def reboot(type = 'SOFT')
  requires :id
  service.reboot_server(id, type)
  true
end

#rebuild(image_ref, name, admin_pass = nil, metadata = nil, personality = nil) ⇒ Object



205
206
207
208
209
# File 'lib/fog/openstack/compute/models/server.rb', line 205

def rebuild(image_ref, name, admin_pass = nil,  = nil, personality = nil)
  requires :id
  service.rebuild_server(id, image_ref, name, admin_pass, , personality)
  true
end

#reloadObject



116
117
118
119
# File 'lib/fog/openstack/compute/models/server.rb', line 116

def reload
  @all_addresses = nil
  super
end

#reset_vm_state(vm_state) ⇒ Object



329
330
331
332
# File 'lib/fog/openstack/compute/models/server.rb', line 329

def reset_vm_state(vm_state)
  requires :id
  service.reset_server_state id, vm_state
end

#resize(flavor_ref) ⇒ Object



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

def resize(flavor_ref)
  requires :id
  service.resize_server(id, flavor_ref)
  true
end

#revert_resizeObject



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

def revert_resize
  requires :id
  service.revert_resize_server(id)
  true
end

#saveObject

Raises:

  • (Fog::Errors::Error)


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/fog/openstack/compute/models/server.rb', line 366

def save
  raise Fog::Errors::Error, 'Resaving an existing object may create a duplicate' if persisted?
  requires :flavor_ref, :name
  requires_one :image_ref, :block_device_mapping, :block_device_mapping_v2
  options = {
    'personality'             => personality,
    'accessIPv4'              => accessIPv4,
    'accessIPv6'              => accessIPv6,
    'availability_zone'       => availability_zone,
    'user_data'               => user_data_encoded,
    'key_name'                => key_name,
    'config_drive'            => config_drive,
    'security_groups'         => @security_groups,
    'min_count'               => @min_count,
    'max_count'               => @max_count,
    'nics'                    => @nics,
    'os:scheduler_hints'      => @os_scheduler_hints,
    'block_device_mapping'    => @block_device_mapping,
    'block_device_mapping_v2' => @block_device_mapping_v2,
  }
  options['metadata'] = .to_hash unless @metadata.nil?
  options = options.reject { |_key, value| value.nil? }
  data = service.create_server(name, image_ref, flavor_ref, options)
  merge_attributes(data.body['server'])
  true
end

#setup(credentials = {}) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/fog/openstack/compute/models/server.rb', line 393

def setup(credentials = {})
  requires :ssh_ip_address, :identity, :public_key, :username
  ssh = Fog::SSH.new(ssh_ip_address, username, credentials)
  ssh.run([
            %(mkdir .ssh),
            %(echo "#{public_key}" >> ~/.ssh/authorized_keys),
            %(passwd -l #{username}),
            %(echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json),
            %(echo "#{Fog::JSON.encode()}" >> ~/metadata.json)
          ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#shelveObject



279
280
281
282
# File 'lib/fog/openstack/compute/models/server.rb', line 279

def shelve
  requires :id
  service.shelve_server(id)
end

#shelve_offloadObject



289
290
291
292
# File 'lib/fog/openstack/compute/models/server.rb', line 289

def shelve_offload
  requires :id
  service.shelve_offload_server(id)
end

#startObject



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/fog/openstack/compute/models/server.rb', line 266

def start
  requires :id

  case state.downcase
  when 'paused'
    service.unpause_server(id)
  when 'suspended'
    service.resume_server(id)
  else
    service.start_server(id)
  end
end

#stopObject



251
252
253
254
# File 'lib/fog/openstack/compute/models/server.rb', line 251

def stop
  requires :id
  service.stop_server(id)
end

#suspendObject



261
262
263
264
# File 'lib/fog/openstack/compute/models/server.rb', line 261

def suspend
  requires :id
  service.suspend_server(id)
end

#unshelveObject



284
285
286
287
# File 'lib/fog/openstack/compute/models/server.rb', line 284

def unshelve
  requires :id
  service.unshelve_server(id)
end

#user_data=(ascii_userdata) ⇒ Object



89
90
91
# File 'lib/fog/openstack/compute/models/server.rb', line 89

def user_data=(ascii_userdata)
  self.user_data_encoded = [ascii_userdata].pack('m') if ascii_userdata
end

#volume_attachmentsObject



349
350
351
352
# File 'lib/fog/openstack/compute/models/server.rb', line 349

def volume_attachments
  requires :id
  service.get_server_volumes(id).body['volumeAttachments']
end

#volumesObject



342
343
344
345
346
347
# File 'lib/fog/openstack/compute/models/server.rb', line 342

def volumes
  requires :id
  service.volumes.select do |vol|
    vol.attachments.find { |attachment| attachment["serverId"] == id }
  end
end