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

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

Instance Attribute Summary collapse

Attributes inherited from Server

#private_key, #private_key_path, #public_key, #public_key_path, #ssh_port, #username

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #sshable?

Methods inherited from Model

#inspect, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/openstack/models/compute/server.rb', line 46

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.os_scheduler_hints = attributes.delete(:os_scheduler_hints)

  super
end

Instance Attribute Details

#flavor_refObject



134
135
136
# File 'lib/fog/openstack/models/compute/server.rb', line 134

def flavor_ref
  @flavor_ref
end

#image_refObject



126
127
128
# File 'lib/fog/openstack/models/compute/server.rb', line 126

def image_ref
  @image_ref
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.



43
44
45
# File 'lib/fog/openstack/models/compute/server.rb', line 43

def os_scheduler_hints=(value)
  @os_scheduler_hints = value
end

#passwordObject (readonly)

Returns the value of attribute password.



42
43
44
# File 'lib/fog/openstack/models/compute/server.rb', line 42

def password
  @password
end

Instance Method Details

#all_addressesObject



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

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.
  @all_addresses ||= service.list_all_addresses.body["floating_ips"].select{|data| data['instance_id'] == id}
end

#associate_address(floating_ip) ⇒ Object



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

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

#change_password(admin_password) ⇒ Object



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

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

#confirm_resizeObject



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

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

#console(log_length = nil) ⇒ Object



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

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

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



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

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

#destroyObject



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

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

#disassociate_address(floating_ip) ⇒ Object



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

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

#floating_ip_addressObject Also known as: public_ip_address



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

def floating_ip_address
  floating_ip_addresses.first
end

#floating_ip_addressesObject Also known as: public_ip_addresses



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

def floating_ip_addresses
  all_addresses.map{|addr| addr["ip"]}
end

#imagesObject



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

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)



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

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

#live_migrate(host, block_migration, disk_over_commit) ⇒ Object



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

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



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

def max_count=(new_max_count)
  @max_count = new_max_count
end

#metadataObject



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

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

#metadata=(new_metadata = {}) ⇒ Object



67
68
69
70
71
72
# File 'lib/fog/openstack/models/compute/server.rb', line 67

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

#migrateObject



208
209
210
211
# File 'lib/fog/openstack/models/compute/server.rb', line 208

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

#min_count=(new_min_count) ⇒ Object



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

def min_count=(new_min_count)
  @min_count = new_min_count
end

#networksObject



241
242
243
# File 'lib/fog/openstack/models/compute/server.rb', line 241

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

#private_ip_addressObject



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

def private_ip_address
  private_ip_addresses.first
end

#private_ip_addressesObject



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

def private_ip_addresses
  ip_addresses - floating_ip_addresses
end

#ready?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/fog/openstack/models/compute/server.rb', line 142

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

#reboot(type = 'SOFT') ⇒ Object



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

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



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

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



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

def reload
  @all_addresses = nil
  super
end

#reset_vm_state(vm_state) ⇒ Object



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

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

#resize(flavor_ref) ⇒ Object



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

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

#revert_resizeObject



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

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

#saveObject

TODO: Implement /os-volumes-boot support with ‘block_device_mapping’

Raises:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/fog/openstack/models/compute/server.rb', line 246

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
  requires :flavor_ref, :image_ref, :name
  options = {
    'personality' => personality,
    'accessIPv4' => accessIPv4,
    'accessIPv6' => accessIPv6,
    'availability_zone' => availability_zone,
    'user_data' => user_data_encoded,
    'key_name'    => key_name,
    'security_groups' => @security_groups,
    'min_count'   => @min_count,
    'max_count'   => @max_count,
    'os:scheduler_hints' => @os_scheduler_hints
  }
  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



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fog/openstack/models/compute/server.rb', line 176

def security_groups
  requires :id

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

  groups.map do |group|
    sg = Fog::Compute::OpenStack::SecurityGroup.new group
    sg.connection = service
    sg
  end
end

#security_groups=(new_security_groups) ⇒ Object



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

def security_groups=(new_security_groups)
  @security_groups = new_security_groups
end

#setup(credentials = {}) ⇒ Object



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

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

#user_data=(ascii_userdata) ⇒ Object



74
75
76
# File 'lib/fog/openstack/models/compute/server.rb', line 74

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