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

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

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Server

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

Methods inherited from Model

#inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

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

Methods included from Attributes::InstanceMethods

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

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



44
45
46
47
48
49
50
51
52
53
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 44

def initialize(attributes={})
  @connection = attributes[:connection]
  attributes[:metadata] = {}

  self.security_groups = attributes.delete(:security_groups)
  self.min_count = attributes.delete(:min_count)
  self.max_count = attributes.delete(:max_count)

  super
end

Instance Attribute Details

#flavor_refObject



131
132
133
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 131

def flavor_ref
  @flavor_ref
end

#image_refObject



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

def image_ref
  @image_ref
end

#passwordObject (readonly)

Returns the value of attribute password.



40
41
42
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 40

def password
  @password
end

#private_keyObject



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

def private_key
  @private_key ||= private_key_path && File.read(private_key_path)
end

#private_key_pathObject



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

def private_key_path
  @private_key_path ||= Fog.credentials[:private_key_path]
  @private_key_path &&= File.expand_path(@private_key_path)
end

#public_keyObject



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

def public_key
  @public_key ||= public_key_path && File.read(public_key_path)
end

#public_key_pathObject



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

def public_key_path
  @public_key_path ||= Fog.credentials[:public_key_path]
  @public_key_path &&= File.expand_path(@public_key_path)
end

#usernameObject



263
264
265
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 263

def username
  @username ||= 'root'
end

Instance Method Details

#associate_address(floating_ip) ⇒ Object



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

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

#change_password(admin_password) ⇒ Object



143
144
145
146
147
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 143

def change_password(admin_password)
  requires :id
  connection.change_password_server(id, admin_password)
  true
end

#confirm_resizeObject



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

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

#console(log_length = nil) ⇒ Object



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

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

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



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

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

#destroyObject



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

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

#disassociate_address(floating_ip) ⇒ Object



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

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

#imagesObject



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

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

#live_migrate(host, block_migration, disk_over_commit) ⇒ Object



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

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

#max_count=(new_max_count) ⇒ Object



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

def max_count=(new_max_count)
  @max_count = new_max_count
end

#metadataObject



55
56
57
58
59
60
61
62
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 55

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

#metadata=(new_metadata = {}) ⇒ Object



64
65
66
67
68
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 64

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

#migrateObject



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

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

#min_count=(new_min_count) ⇒ Object



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

def min_count=(new_min_count)
  @min_count = new_min_count
end

#networksObject



221
222
223
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 221

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

#private_ip_addressObject



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

def private_ip_address
  if addresses['private']
    #assume only a single private
    return addresses['private'].first
  elsif addresses['internet']
    #assume no private IP means private cloud
    return addresses['internet'].first
  end
end

#public_ip_addressObject



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

def public_ip_address
  if addresses['public']
    #assume last is either original or assigned from floating IPs
    return addresses['public'].last
  elsif addresses['internet']
    #assume no public IP means private cloud
    return addresses['internet'].first
  end
end

#ready?Boolean

Returns:

  • (Boolean)


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

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

#reboot(type = 'SOFT') ⇒ Object



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

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

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



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

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

#resize(flavor_ref) ⇒ Object



155
156
157
158
159
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 155

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

#revert_resizeObject



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

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

#saveObject

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

Raises:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 226

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_ref, :image_ref, :name
  meta_hash = {}
  .each { |meta| meta_hash.store(meta.key, meta.value) }
  options = {
    'metadata'    => meta_hash,
    '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,
  }
  options = options.reject {|key, value| value.nil?}
  data = connection.create_server(name, image_ref, flavor_ref, options)
  merge_attributes(data.body['server'])
  true
end

#security_groups=(new_security_groups) ⇒ Object



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

def security_groups=(new_security_groups)
  @security_groups = new_security_groups
end

#setup(credentials = {}) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 249

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



70
71
72
# File 'lib/rackspace-fog/openstack/models/compute/server.rb', line 70

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