Class: Profitbricks::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/profitbricks/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#attributes, belongs_to, get_xml_and_update_attributes, #get_xml_and_update_attributes, has_many, #initialize, #reload

Constructor Details

This class inherits a constructor from Profitbricks::Model

Class Method Details

.create(options = {}) ⇒ Server

Creates a Virtual Server within an existing data center. Parameters can be specified to set up a boot device and connect the server to an existing LAN or the Internet.

Parameters:

  • options (Hash) (defaults to: {})

    parameters for the new server

Options Hash (options):

  • :cores (Fixnum)

    Number of cores to be assigned to the specified server (required)

  • :ram (Fixnum)

    Number of RAM memory (in MiB) to be assigned to the server. Must be at least 256 and a multiple of it. (required)

  • :name (String)

    Name of the server to be created

  • :data_center_id (String)

    Defines the data center wherein the server is to be created. If left empty, the server will be created in a new data center.

  • :boot_from_image_id (String)

    Defines an existing CD-ROM/DVD image ID to be set as boot device of the server. A virtual CD-ROM/DVD drive with the mounted image will be connected to the server.

  • :boot_from_storage_id (String)

    Defines an existing storage device ID to be set as boot device of the server. The storage will be connected to the server implicitly.

  • :lan_id (Fixnum)

    Connects the server to the specified LAN ID > 0. If the respective LAN does not exist, it is going to be created.

  • :internet_access (Boolean)

    Set to true to connect the server to the internet via the specified LAN ID. If the LAN is not specified, it is going to be created in the next available LAN ID, starting with LAN ID 1

  • :os_type (String)

    Sets the OS type of the server. (WINDOWS, OTHER) If left empty, the server will inherit the OS Type of its selected boot image / storage.

Returns:

  • (Server)

    The created virtual server

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/profitbricks/server.rb', line 55

def create(options = {})
  raise ArgumentError.new("You must provide :cores and :ram") if options[:ram].nil? and options[:cores].nil?
  raise ArgumentError.new(":ram has to be at least 256MiB and a multiple of it") if options[:ram] < 256 or (options[:ram] % 256) > 0
  raise ArgumentError.new(":os_type has to be either 'WINDOWS' or 'OTHER'") if options[:os_type] and !['WINDOWS', 'OTHER'].include? options[:os_type]
  xml = "<arg0>"
  xml += get_xml_and_update_attributes options, 
              [:cores, :ram, :name, :boot_from_storage_id, :boot_from_image_id, :os_type, :internet_access, :lan_id]
  xml += "</arg0>"
  response = Profitbricks.request :create_server, xml
  self.find(:id => response.to_hash[:create_server_return][:return][:server_id])
end

.find(options = {}) ⇒ Object

Finds a virtual server

Parameters:

  • options (Hash) (defaults to: {})

    currently just :id is supported

Options Hash (options):

  • :id (String)

    The id of the server to locate



71
72
73
74
75
76
77
78
79
80
# File 'lib/profitbricks/server.rb', line 71

def find(options = {})
  # FIXME
  #if options[:name]
  #  dc = PB::Server.all().select { |d| d.name == options[:name] }.first
  #  options[:id] = dc.id if dc
  #end
  raise "Unable to locate the server named '#{options[:name]}'" unless options[:id]
  response = Profitbricks.request :get_server, "<serverId>#{options[:id]}</serverId>"
  PB::Server.new(response.to_hash[:get_server_response][:return])
end

Instance Method Details

#deleteBoolean

Deletes the virtual Server.

Returns:

  • (Boolean)

    true on success, false otherwise



7
8
9
10
# File 'lib/profitbricks/server.rb', line 7

def delete
  response = Profitbricks.request :delete_server, "<serverId>#{self.id}</serverId>"
  return true if response.to_hash[:delete_server_response][:return]
end

#rebootBoolean

Reboots the virtual Server (POWER CYCLE).

Returns:

  • (Boolean)

    true on success, false otherwise



14
15
16
17
# File 'lib/profitbricks/server.rb', line 14

def reboot
  response = Profitbricks.request :reboot_server, "<serverId>#{self.id}</serverId>"
  return true if response.to_hash[:reboot_server_response][:return]
end

#update(options = {}) ⇒ Boolean

Updates parameters of an existing virtual Server device.

Parameters:

  • options (Hash) (defaults to: {})

    parameters for the new server

Options Hash (options):

  • :cores (Fixnum)

    Number of cores to be assigned to the specified server (required)

  • :ram (Fixnum)

    Number of RAM memory (in MiB) to be assigned to the server. Must be at least 256 and a multiple of it. (required)

  • :name (String)

    Name of the server to be created

  • :boot_from_image_id (String)

    Defines an existing CD-ROM/DVD image ID to be set as boot device of the server. A virtual CD-ROM/DVD drive with the mounted image will be connected to the server.

  • :boot_from_storage_id (String)

    Defines an existing storage device ID to be set as boot device of the server. The storage will be connected to the server implicitly.

  • :os_type (String)

    Sets the OS type of the server. (WINDOWS, OTHER) If left empty, the server will inherit the OS Type of its selected boot image / storage.

Returns:

  • (Boolean)

    true on success, false otherwise

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/profitbricks/server.rb', line 28

def update(options = {})
  return false if options.empty?
  raise ArgumentError.new(":ram and :cores are mandatory options.") unless options[:ram] or options[:core]
  raise ArgumentError.new(":ram has to be at least 256MiB and a multiple of it") if options[:ram] < 256 or (options[:ram] % 256) > 0
  raise ArgumentError.new(":os_type has to be either 'WINDOWS' or 'OTHER'") if options[:os_type] and !['WINDOWS', 'OTHER'].include? options[:os_type]
  xml = "<arg0><serverId>#{self.id}</serverId>"
  xml += get_xml_and_update_attributes options, [:cores, :ram, :name, :boot_from_storage_id, :boot_from_image_id, :os_type]
  xml += "</arg0>"
  response = Profitbricks.request :update_server, xml
  return true if response.to_hash[:update_server_response][:return]
end