Class: OpenStack::Compute::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id) ⇒ Server

This class is the representation of a single Server object. The constructor finds the server identified by the specified ID number, accesses the API via the populate method to get information about that server, and returns the object.

Will be called via the get_server or create_server methods on the OpenStack::Compute::Connection object, and will likely not be called directly.

>> server = cs.get_server(110917)
=> #<OpenStack::Compute::Server:0x1014e5438 ....>
>> server.name
=> "RenamedRubyTest"


31
32
33
34
35
36
37
38
39
40
# File 'lib/openstack/compute/server.rb', line 31

def initialize(connection,id)
  @connection    = connection
  @id            = id
  @svrmgmthost   = connection.svrmgmthost
  @svrmgmtpath   = connection.svrmgmtpath
  @svrmgmtport   = connection.svrmgmtport
  @svrmgmtscheme = connection.svrmgmtscheme
  populate
  return self
end

Instance Attribute Details

#accessipv4Object (readonly)

Returns the value of attribute accessipv4.



11
12
13
# File 'lib/openstack/compute/server.rb', line 11

def accessipv4
  @accessipv4
end

#accessipv6Object (readonly)

Returns the value of attribute accessipv6.



12
13
14
# File 'lib/openstack/compute/server.rb', line 12

def accessipv6
  @accessipv6
end

#addressesObject (readonly)

Returns the value of attribute addresses.



13
14
15
# File 'lib/openstack/compute/server.rb', line 13

def addresses
  @addresses
end

#adminPassObject

Returns the value of attribute adminPass.



20
21
22
# File 'lib/openstack/compute/server.rb', line 20

def adminPass
  @adminPass
end

#createdObject (readonly)

Returns the value of attribute created.



18
19
20
# File 'lib/openstack/compute/server.rb', line 18

def created
  @created
end

#flavorObject (readonly)

Returns the value of attribute flavor.



16
17
18
# File 'lib/openstack/compute/server.rb', line 16

def flavor
  @flavor
end

#hostIdObject (readonly)

Returns the value of attribute hostId.



14
15
16
# File 'lib/openstack/compute/server.rb', line 14

def hostId
  @hostId
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/openstack/compute/server.rb', line 7

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



15
16
17
# File 'lib/openstack/compute/server.rb', line 15

def image
  @image
end

#metadataObject (readonly)

Returns the value of attribute metadata.



17
18
19
# File 'lib/openstack/compute/server.rb', line 17

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/openstack/compute/server.rb', line 8

def name
  @name
end

#progressObject (readonly)

Returns the value of attribute progress.



10
11
12
# File 'lib/openstack/compute/server.rb', line 10

def progress
  @progress
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/openstack/compute/server.rb', line 9

def status
  @status
end

#updatedObject (readonly)

Returns the value of attribute updated.



19
20
21
# File 'lib/openstack/compute/server.rb', line 19

def updated
  @updated
end

Instance Method Details

#change_password!(password) ⇒ Object

Changes the admin password. Returns the password if it succeeds.



258
259
260
261
262
# File 'lib/openstack/compute/server.rb', line 258

def change_password!(password)
  json = JSON.generate(:changePassword => { :adminPass => password })
  @connection.req('POST', "/servers/#{@id}/action", :data => json)
  @adminPass = password
end

#confirm_resize!Object

After a server resize is complete, calling this method will confirm the resize with the Openstack API, and discard the fallback/original image.

Returns true if the API call succeeds.

>> server.confirm_resize!
=> true


231
232
233
234
235
236
237
238
# File 'lib/openstack/compute/server.rb', line 231

def confirm_resize!
  # If the resize bug gets figured out, should put a check here to make sure that it's in the proper state for this.
  data = JSON.generate(:confirmResize => nil)
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  true
end

#create_image(options) ⇒ Object

Takes a snapshot of the server and creates a server image from it. That image can then be used to build new servers. The snapshot is saved asynchronously. Check the image status to make sure that it is ACTIVE before attempting to perform operations on it.

A name string for the saved image must be provided. A new OpenStack::Compute::Image object for the saved image is returned.

The image is saved as a backup, of which there are only three available slots. If there are no backup slots available, A OpenStack::Compute::Exception::OpenStackComputeFault will be raised.

>> image = server.create_image(:name => "My Rails Server")
=>


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

def create_image(options)
  data = JSON.generate(:createImage => options)
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  image_id = response["Location"].scan(/.*\/(.*)/).flatten[0]
  OpenStack::Compute::Image.new(@connection, image_id)
end

#delete!Object

Deletes the server from Openstack Compute. The server will be shut down, data deleted, and billing stopped.

Returns true if the API call succeeds.

>> server.delete!
=> true


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

def delete!
  response = @connection.req('DELETE', "/servers/#{@id}")
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#get_addresses(address_info) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/openstack/compute/server.rb', line 264

def get_addresses(address_info)
  address_list = OpenStack::Compute::AddressList.new
  address_info.each do |label, addr|
    addr.each do |address|
      address_list << OpenStack::Compute::Address.new(label,address)
      if address_list.last.version == 4 && (!@accessipv4 || accessipv4 == "") then
        @accessipv4 = address_list.last.address
      end
    end
  end
  address_list
end

#populate(data = nil) ⇒ Object Also known as: refresh

Makes the actual API call to get information about the given server object. If you are attempting to track the status or project of a server object (for example, when rebuilding, creating, or resizing a server), you will likely call this method within a loop until the status becomes “ACTIVE” or other conditions are met.

Returns true if the API call succeeds.

>> server.refresh
=> true


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/openstack/compute/server.rb', line 50

def populate(data=nil)
  path = "/servers/#{URI.encode(@id.to_s)}"
  if data.nil? then
      response = @connection.req("GET", path)
      OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
      data = JSON.parse(response.body)["server"]
  end
  @id        = data["id"]
  @name      = data["name"]
  @status    = data["status"]
  @progress  = data["progress"]
  @addresses = get_addresses(data["addresses"])
  @metadata  = OpenStack::Compute::Metadata.new(@connection, path, data["metadata"])
  @hostId    = data["hostId"]
  @image   = data["image"]
  @flavor  = data["flavor"]
  @created = DateTime.strptime(data["created"]).to_time
  @updated = DateTime.strptime(data["updated"]).to_time
  true
end

#reboot(type = "SOFT") ⇒ Object

Sends an API request to reboot this server. Takes an optional argument for the type of reboot, which can be “SOFT” (graceful shutdown) or “HARD” (power cycle). The hard reboot is also triggered by server.reboot!, so that may be a better way to call it.

Returns true if the API call succeeds.

>> server.reboot
=> true


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

def reboot(type="SOFT")
  data = JSON.generate(:reboot => {:type => type})
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#reboot!Object

Sends an API request to hard-reboot (power cycle) the server. See the reboot method for more information.

Returns true if the API call succeeds.

>> server.reboot!
=> true


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

def reboot!
  self.reboot("HARD")
end

#rebuild!(options) ⇒ Object

The rebuild function removes all data on the server and replaces it with the specified image. The serverRef and all IP addresses will remain the same. If name and metadata are specified, they will replace existing values, otherwise they will not change. A rebuild operation always removes data injected into the file system via server personality. You may reinsert data into the filesystem during the rebuild.

This method expects a hash of the form: {

:imageRef => "https://foo.com/v1.1/images/2",
:name => "newName",
:metadata => { :values => { :foo : "bar" } },
:personality => [
  {
    :path => "/etc/banner.txt",
    :contents => : "ICAgpY2hhcmQgQmFjaA=="
  }
]

}

This will wipe and rebuild the server, but keep the server ID number, name, and IP addresses the same.

Returns true if the API call succeeds.

>> server.rebuild!
=> true


180
181
182
183
184
185
186
187
188
189
# File 'lib/openstack/compute/server.rb', line 180

def rebuild!(options)
  options[:personality] = Personalities.get_personality(options[:personality])
  json = JSON.generate(:rebuild => options)
  response = @connection.req('POST', "/servers/#{@id}/action", :data => json)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  data = JSON.parse(response.body)['server']
  self.populate(data)
  self.adminPass = data['adminPass']
  true
end

#resize!(flavorRef) ⇒ Object

Resizes the server to the size contained in the server flavor found at ID flavorRef. The server name, ID number, and IP addresses will remain the same. After the resize is done, the server.status will be set to “VERIFY_RESIZE” until the resize is confirmed or reverted.

Refreshes the OpenStack::Compute::Server object, and returns true if the API call succeeds.

>> server.resize!(1)
=> true


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

def resize!(flavorRef)
  data = JSON.generate(:resize => {:flavorRef => flavorRef})
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  true
end

#resumeObject

Sends an API request to resume this server.

Returns true if the API call succeeds.

>> server.suspend
=> true


91
92
93
94
95
96
# File 'lib/openstack/compute/server.rb', line 91

def resume
  data = JSON.generate(:resume => nil)
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#revert_resize!Object

After a server resize is complete, calling this method will reject the resized server with the Openstack API, destroying the new image and replacing it with the pre-resize fallback image.

Returns true if the API call succeeds.

>> server.confirm_resize!
=> true


247
248
249
250
251
252
253
254
# File 'lib/openstack/compute/server.rb', line 247

def revert_resize!
  # If the resize bug gets figured out, should put a check here to make sure that it's in the proper state for this.
  data = JSON.generate(:revertResize => nil)
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  true
end

#suspendObject

Sends an API request to suspend this server.

Returns true if the API call succeeds.

>> server.suspend
=> true


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

def suspend
  data = JSON.generate(:suspend => nil)
  response = @connection.req('POST', "/servers/#{@id}/action", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#update(options) ⇒ Object

Updates various parameters about the server. Currently, the only operations supported are changing the server name (not the actual hostname on the server, but simply the label in the Servers API) and the administrator password (note: changing the admin password will trigger a reboot of the server). Other options are ignored. One or both key/value pairs may be provided. Keys are case-sensitive.

Input hash key values are :name and :adminPass. Returns true if the API call succeeds.

>> server.update(:name => "MyServer", :adminPass => "12345")
=> true
>> server.name
=> "MyServer"


132
133
134
135
136
137
138
139
# File 'lib/openstack/compute/server.rb', line 132

def update(options)
  data = JSON.generate(:server => options)
  response = @connection.req('PUT', "/servers/#{@id}", :data => data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  # If we rename the instance, repopulate the object
  self.populate if options[:name]
  true
end