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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/openstack/models/compute/server.rb', line 55

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.



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

def block_device_mapping
  @block_device_mapping
end

#block_device_mapping_v2Object

Returns the value of attribute block_device_mapping_v2.



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

def block_device_mapping_v2
  @block_device_mapping_v2
end

#flavor_refObject



162
163
164
# File 'lib/fog/openstack/models/compute/server.rb', line 162

def flavor_ref
  @flavor_ref
end

#image_refObject



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

def image_ref
  @image_ref
end

#nics=(value) ⇒ Object (writeonly)

Sets the attribute nics

Parameters:

  • value

    the value to set the attribute nics to.



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

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.



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

def os_scheduler_hints=(value)
  @os_scheduler_hints = value
end

#passwordObject (readonly)

Returns the value of attribute password.



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

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:



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

attribute :personality

Instance Method Details

#all_addressesObject



101
102
103
104
105
106
# File 'lib/fog/openstack/models/compute/server.rb', line 101

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



276
277
278
279
# File 'lib/fog/openstack/models/compute/server.rb', line 276

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

#attach_volume(volume_id, device_name) ⇒ Object



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

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

#change_password(admin_password) ⇒ Object



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

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

#confirm_resizeObject



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

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

#console(log_length = nil) ⇒ Object



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

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

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



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

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

#destroyObject



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

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

#detach_volume(volume_id) ⇒ Object



321
322
323
324
325
# File 'lib/fog/openstack/models/compute/server.rb', line 321

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

#disassociate_address(floating_ip) ⇒ Object



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

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

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  self.state == 'ERROR'
end

#floating_ip_addressObject Also known as: public_ip_address



140
141
142
# File 'lib/fog/openstack/models/compute/server.rb', line 140

def floating_ip_address
  floating_ip_addresses.first
end

#floating_ip_addressesObject Also known as: public_ip_addresses



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fog/openstack/models/compute/server.rb', line 119

def floating_ip_addresses
  all_floating=addresses.values.flatten.select{ |data| data["OS-EXT-IPS:type"]=="floating" }.map{|addr| addr["addr"] }

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

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

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

#imagesObject



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

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)



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

def ip_addresses
  addresses.values.flatten.map{|x| x['addr']}
end

#live_migrate(host, block_migration, disk_over_commit) ⇒ Object



271
272
273
274
# File 'lib/fog/openstack/models/compute/server.rb', line 271

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

#max_count=(new_max_count) ⇒ Object



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

def max_count=(new_max_count)
  @max_count = new_max_count
end

#metadataObject



70
71
72
73
74
75
76
77
# File 'lib/fog/openstack/models/compute/server.rb', line 70

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

#metadata=(new_metadata = {}) ⇒ Object



79
80
81
82
83
84
# File 'lib/fog/openstack/models/compute/server.rb', line 79

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

#migrateObject



266
267
268
269
# File 'lib/fog/openstack/models/compute/server.rb', line 266

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

#min_count=(new_min_count) ⇒ Object



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

def min_count=(new_min_count)
  @min_count = new_min_count
end

#networksObject



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

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

#pauseObject



233
234
235
236
# File 'lib/fog/openstack/models/compute/server.rb', line 233

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

#private_ip_addressObject



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

def private_ip_address
  private_ip_addresses.first
end

#private_ip_addressesObject



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

def private_ip_addresses
  ip_addresses - floating_ip_addresses
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  self.state == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



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

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



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

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



108
109
110
111
# File 'lib/fog/openstack/models/compute/server.rb', line 108

def reload
  @all_addresses = nil
  super
end

#reset_vm_state(vm_state) ⇒ Object



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

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

#resize(flavor_ref) ⇒ Object



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

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

#revert_resizeObject



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

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

#saveObject

Raises:

  • (Fog::Errors::Error)


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/fog/openstack/models/compute/server.rb', line 327

def save
  raise Fog::Errors::Error.new('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

#security_groupsObject



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

def security_groups
  requires :id

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

  groups.map do |group|
    Fog::Compute::OpenStack::SecurityGroup.new group.merge({:service => service})
  end
end

#security_groups=(new_security_groups) ⇒ Object



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

def security_groups=(new_security_groups)
  @security_groups = new_security_groups
end

#setup(credentials = {}) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/fog/openstack/models/compute/server.rb', line 354

def setup(credentials = {})
  requires :ssh_ip_address, :identity, :public_key, :username
  Fog::SSH.new(ssh_ip_address, username, credentials).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

#startObject



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fog/openstack/models/compute/server.rb', line 243

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



228
229
230
231
# File 'lib/fog/openstack/models/compute/server.rb', line 228

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

#suspendObject



238
239
240
241
# File 'lib/fog/openstack/models/compute/server.rb', line 238

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

#user_data=(ascii_userdata) ⇒ Object



86
87
88
# File 'lib/fog/openstack/models/compute/server.rb', line 86

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

#volume_attachmentsObject



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

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

#volumesObject



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

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