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"


26
27
28
29
30
31
32
33
34
35
# File 'lib/openstack/compute/server.rb', line 26

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

#addressesObject (readonly)

Returns the value of attribute addresses.



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

def addresses
  @addresses
end

#adminPassObject

Returns the value of attribute adminPass.



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

def adminPass
  @adminPass
end

#flavorIdObject (readonly)

Returns the value of attribute flavorId.



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

def flavorId
  @flavorId
end

#hostIdObject (readonly)

Returns the value of attribute hostId.



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

def hostId
  @hostId
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/openstack/compute/server.rb', line 5

def id
  @id
end

#imageIdObject (readonly)

Returns the value of attribute imageId.



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

def imageId
  @imageId
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#progressObject (readonly)

Returns the value of attribute progress.



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

def progress
  @progress
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#backup_scheduleObject

Provides information about the backup schedule for this server. Returns a hash of the form => state, “daily” => state, “enabled” => boolean

>> server.backup_schedule
=> {"weekly"=>"THURSDAY", "daily"=>"H_0400_0600", "enabled"=>true}


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

def backup_schedule
  response = @connection.csreq("GET",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(@id.to_s)}/backup_schedule",@svrmgmtport,@svrmgmtscheme)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  JSON.parse(response.body)['backupSchedule']
end

#backup_schedule=(options) ⇒ Object

Updates the backup schedule for the server. Takes a hash of the form: => state, :daily => state, :enabled => boolean as an argument. All three keys (:weekly, :daily, :enabled) must be provided or an exception will get raised.

>> server.backup_schedule=({:weekly=>"THURSDAY", :daily=>"H_0400_0600", :enabled=>true})
=> {:weekly=>"THURSDAY", :daily=>"H_0400_0600", :enabled=>true}


235
236
237
238
239
240
# File 'lib/openstack/compute/server.rb', line 235

def backup_schedule=(options)
  data = JSON.generate('backupSchedule' => options)
  response = @connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/backup_schedule",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
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


194
195
196
197
198
199
200
201
# File 'lib/openstack/compute/server.rb', line 194

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.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  true
end

#create_image(name) ⇒ 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("My Rails Server")
=>


166
167
168
169
170
171
# File 'lib/openstack/compute/server.rb', line 166

def create_image(name)
  data = JSON.generate(:image => {:serverId => self.id, :name => name})
  response = @connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/images",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  OpenStack::Compute::Image.new(@connection,JSON.parse(response.body)['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


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

def delete!
  response = @connection.csreq("DELETE",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}",@svrmgmtport,@svrmgmtscheme)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#disable_backup_schedule!Object

Removes the existing backup schedule for the server, setting the backups to disabled.

Returns true if the API call succeeds.

>> server.disable_backup_schedule!
=> true


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

def disable_backup_schedule!
  response = @connection.csreq("DELETE",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/backup_schedule",@svrmgmtport,@svrmgmtscheme)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#flavorObject

Returns a new OpenStack::Compute::Flavor object for the flavor assigned to this server.

>> flavor = server.flavor
=> #<OpenStack::Compute::Flavor:0x1014aac20 @name="256 server", @disk=10, @id=1, @ram=256>
>> flavor.name
=> "256 server"


69
70
71
# File 'lib/openstack/compute/server.rb', line 69

def flavor
  OpenStack::Compute::Flavor.new(@connection,self.flavorId)
end

#imageObject

Returns a new OpenStack::Compute::Image object for the image assigned to this server.

>> image = server.image
=> #<OpenStack::Compute::Image:0x10149a960 ...>
>> image.name
=> "Ubuntu 8.04.2 LTS (hardy)"


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

def image
  OpenStack::Compute::Image.new(@connection,self.imageId)
end

#populateObject 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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openstack/compute/server.rb', line 45

def populate
  response = @connection.csreq("GET",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(@id.to_s)}",@svrmgmtport,@svrmgmtscheme)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  data = JSON.parse(response.body)["server"]
  @id        = data["id"]
  @name      = data["name"]
  @status    = data["status"]
  @progress  = data["progress"]
  @addresses = OpenStack::Compute.symbolize_keys(data["addresses"])
    = data["metadata"]
  @hostId    = data["hostId"]
  @imageId   = data["imageId"]
  @flavorId  = data["flavorId"]
    = data["metadata"]
  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


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

def reboot(type="SOFT")
  data = JSON.generate(:reboot => {:type => type})
  response = @connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},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


103
104
105
# File 'lib/openstack/compute/server.rb', line 103

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

#rebuild!(imageId = self.imageId) ⇒ Object

Takes the existing server and rebuilds it with the image identified by the imageId argument. If no imageId is provided, the current image will be used.

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


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

def rebuild!(imageId = self.imageId)
  data = JSON.generate(:rebuild => {:imageId => imageId})
  response = @connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  true
end

#resize!(flavorId) ⇒ Object

Resizes the server to the size contained in the server flavor found at ID flavorId. 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


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

def resize!(flavorId)
  data = JSON.generate(:resize => {:flavorId => flavorId})
  response = @connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  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


210
211
212
213
214
215
216
217
# File 'lib/openstack/compute/server.rb', line 210

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.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  self.populate
  true
end

#share_ip(options) ⇒ Object

Share IP between servers in Shared IP group. Takes a hash of the form: => “1234”, :ipAddress => “67.23.10.132”, :configureServer => false as an argument. The :sharedIpGroupId key is required. The :ipAddress key is required. The :configureServer key is optional and defaults to false.

>> server.share_ip(:sharedIpGroupId => 100, :ipAddress => "67.23.10.132")
=> true


262
263
264
265
266
267
268
269
270
# File 'lib/openstack/compute/server.rb', line 262

def share_ip(options)
  raise OpenStack::Compute::Exception::MissingArgument, "Shared IP Group ID must be supplied" unless options[:sharedIpGroupId]
  raise OpenStack::Compute::Exception::MissingArgument, "Ip Address must be supplied" unless options[:ipAddress]
  options[:configureServer] = false if options[:configureServer].nil?
  data = JSON.generate(:shareIp => {:sharedIpGroupId => options[:sharedIpGroupId], :configureServer => options[:configureServer]})
  response = @connection.csreq("PUT",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/ips/public/#{options[:ipAddress]}",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  true
end

#unshare_ip(options) ⇒ Object

Unshare an IP address. Takes a hash of the form: => “67.23.10.132” as an argument. The :ipAddress key is required.

>> server.unshare_ip(:ipAddress => "67.23.10.132")
=> true


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

def unshare_ip(options)
  raise OpenStack::Compute::Exception::MissingArgument, "Ip Address must be supplied" unless options[:ipAddress]
  response = @connection.csreq("DELETE",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/ips/public/#{options[:ipAddress]}",@svrmgmtport,@svrmgmtscheme)
  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"


117
118
119
120
121
122
123
124
# File 'lib/openstack/compute/server.rb', line 117

def update(options)
  data = JSON.generate(:server => options)
  response = @connection.csreq("PUT",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},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