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

  • :availability_zone (String)

    Sets the availability zone in which the server is located (AUTO, ZONE_1, ZONE_2). If set to AUTO or left empty, servers will be created in a random zone.

  • :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)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/profitbricks/server.rb', line 78

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].to_i < 256 or (options[:ram].to_i % 256) > 0
  raise ArgumentError.new(":availability_zone has to be either 'AUTO', 'ZONE_1', or 'ZONE_2'") if options[:availability_zone] and !['AUTO', 'ZONE_1', 'ZONE_2'].include? options[:availability_zone]
  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, :data_center_id, :availability_zone]
  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



95
96
97
98
99
100
101
102
103
104
# File 'lib/profitbricks/server.rb', line 95

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

#create_nic(options) ⇒ Object

Creates a Nic for the current Server, automatically sets the :server_id

See Also:

  • Nic#create


58
59
60
# File 'lib/profitbricks/server.rb', line 58

def create_nic(options)
  Nic.create(options.merge(:server_id => self.id))
end

#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

#running?Boolean

Checks if the Server is running

Returns:

  • (Boolean)

    true if the Server is running, false otherwise



44
45
46
47
# File 'lib/profitbricks/server.rb', line 44

def running?
  self.reload
  self.virtual_machine_state == "RUNNING"
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.

  • :ram (Fixnum)

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

  • :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.

  • :availability_zone (String)

    Sets the availability zone in which the server is located (AUTO, ZONE_1, ZONE_2). If set to AUTO servers will be placed in a random zone.

  • :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)


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

def update(options = {})
  return false if options.empty?
  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(":availability_zone has to be either 'AUTO', 'ZONE_1', or 'ZONE_2'") if options[:availability_zone] and !['AUTO', 'ZONE_1', 'ZONE_2'].include? options[:availability_zone]
  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, :availability_zone, :os_type]
  xml += "</arg0>"
  response = Profitbricks.request :update_server, xml
  return true if response.to_hash[:update_server_response][:return]
end

#wait_for_runningObject

Blocks until the Server is running



50
51
52
53
54
# File 'lib/profitbricks/server.rb', line 50

def wait_for_running
  while !self.running?
    sleep Profitbricks::Config.polling_interval
  end
end