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, has_many, #initialize, #reload

Constructor Details

This class inherits a constructor from Profitbricks::Model

Class Method Details

.allArray <Server>

Returns a list of all Servers created by the user.

Returns:

  • (Array <Server>)

    Array of all available Servers



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

def all
  DataCenter.all.collect(&:servers).flatten.compact
end

.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)


137
138
139
140
141
142
143
144
145
# File 'lib/profitbricks/server.rb', line 137

def create(options = {})
  raise ArgumentError.new("You must provide :cores and :ram") if options[:ram].nil? or 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]
  options[:server_name] = options.delete :name if options[:name]
  response = Profitbricks.request :create_server, options
  self.find(:id => response[: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



151
152
153
154
155
156
157
158
159
160
# File 'lib/profitbricks/server.rb', line 151

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, server_id: options[:id]
  PB::Server.new(response)
end

Instance Method Details

#create_nic(options) ⇒ Object

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

See Also:

  • Nic#create


95
96
97
# File 'lib/profitbricks/server.rb', line 95

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



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

def delete
  Profitbricks.request :delete_server, server_id: self.id
  return true
end

#private_ipsArray <String>

Helper method to get a list of all private IP adresses

Returns:

  • (Array <String>)

    Array of all private IP adresses



109
110
111
# File 'lib/profitbricks/server.rb', line 109

def private_ips
  filter_nics_and_return_ips {|nic| nic.internet_access == false }
end

#provisioned?Boolean

Checks if the Server was successfully provisioned

Returns:

  • (Boolean)

    true if the Server was provisioned, false otherwise



77
78
79
80
81
82
83
84
# File 'lib/profitbricks/server.rb', line 77

def provisioned?
  self.reload
  if @provisioning_state == 'AVAILABLE'
    true
  else
    false
  end
end

#public_ipsArray <String>

Helper method to get a list of all public IP adresses

Returns:

  • (Array <String>)

    Array of all public IP adresses



102
103
104
# File 'lib/profitbricks/server.rb', line 102

def public_ips
  filter_nics_and_return_ips {|nic| nic.internet_access == true }
end

#resetBoolean

Resets an existing virtual server (POWER CYCLE).

Returns:

  • (Boolean)

    true on success, false otherwise



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

def reset
  @virtual_machine_state = 'NOSTATE'
  Profitbricks.request :reset_server, server_id: self.id
  return true
end

#running?Boolean

Checks if the Server is running

Returns:

  • (Boolean)

    true if the Server is running, false otherwise



62
63
64
65
# File 'lib/profitbricks/server.rb', line 62

def running?
  self.reload
  self.virtual_machine_state == "RUNNING"
end

#startBoolean

Starts an existing virtual server

Returns:

  • (Boolean)

    true on success, false otherwise



23
24
25
26
27
# File 'lib/profitbricks/server.rb', line 23

def start
  @virtual_machine_state = 'NOSTATE'
  Profitbricks.request :start_server, server_id: self.id
  return true
end

#stopBoolean

Stops an existing virtual server (HARD power off)

Returns:

  • (Boolean)

    true on success, false otherwise



31
32
33
34
35
# File 'lib/profitbricks/server.rb', line 31

def stop
  @virtual_machine_state = 'SHUTOFF'
  Profitbricks.request :stop_server, server_id: self.id
  return true
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)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/profitbricks/server.rb', line 47

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] and (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]
  update_attributes_from_hash options
  options[:server_name] = options.delete :name if options[:name]
  options[:server_id] = self.id
  response = Profitbricks.request :update_server, options
  return true
end

#wait_for_provisioningObject

Blocks until the Server is provisioned



87
88
89
90
91
# File 'lib/profitbricks/server.rb', line 87

def wait_for_provisioning
  while !self.provisioned?
    sleep Profitbricks::Config.polling_interval
  end
end

#wait_for_runningObject

Blocks until the Server is running



68
69
70
71
72
# File 'lib/profitbricks/server.rb', line 68

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