class Profitbricks::Server
Public Class Methods
Returns a list of all Servers created by the user.
@return [Array <Server>] Array of all available Servers
# File lib/profitbricks/server.rb, line 118 def all DataCenter.all.collect(&:servers).flatten.compact end
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.
@param [Hash] options parameters for the new server @option options [Fixnum] :cores Number of cores to be assigned to the specified server (required) @option options [Fixnum] :ram Number of RAM memory (in MiB) to be assigned to the server. Must be at least 256 and a multiple of it. (required) @option options [String] :name Name of the server to be created @option options [String] :data_center_id Defines the data center wherein the server is to be created. If left empty, the server will be created in a new data center. @option options [String] :boot_from_image_id 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. @option options [String] :boot_from_storage_id Defines an existing storage device ID to be set as boot device of the server. The storage will be connected to the server implicitly. @option options [Fixnum] :lan_id Connects the server to the specified LAN ID > 0. If the respective LAN does not exist, it is going to be created. @option options [Boolean] :internet_access 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 @option options [String] :availability_zone 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. @option options [String] :os_type 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. @return [Server] The created virtual server
# 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
Finds a virtual server
@param [Hash] options currently just :id is supported @option options [String] :id The id of the server to locate
# 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
Public Instance Methods
Deletes the virtual Server
. @return [Boolean] true on success, false otherwise
# File lib/profitbricks/server.rb, line 8 def delete Profitbricks.request :delete_server, server_id: self.id return true end
Helper method to get a list of all private IP adresses
@return [Array <String>] Array of all private IP adresses
# File lib/profitbricks/server.rb, line 109 def private_ips filter_nics_and_return_ips {|nic| nic.internet_access == false } end
Helper method to get a list of all public IP adresses
@return [Array <String>] Array of all public IP adresses
# File lib/profitbricks/server.rb, line 102 def public_ips filter_nics_and_return_ips {|nic| nic.internet_access == true } end
Resets an existing virtual server (POWER CYCLE). @return [Boolean] true on success, false otherwise
# File lib/profitbricks/server.rb, line 15 def reset @virtual_machine_state = 'NOSTATE' Profitbricks.request :reset_server, server_id: self.id return true end
Starts an existing virtual server @return [Boolean] true on success, false otherwise
# File lib/profitbricks/server.rb, line 23 def start @virtual_machine_state = 'NOSTATE' Profitbricks.request :start_server, server_id: self.id return true end
Stops an existing virtual server (HARD power off) @return [Boolean] true on success, false otherwise
# File lib/profitbricks/server.rb, line 31 def stop @virtual_machine_state = 'SHUTOFF' Profitbricks.request :stop_server, server_id: self.id return true end
Updates parameters of an existing virtual Server
device. @param [Hash] options parameters for the new server @option options [Fixnum] :cores Number of cores to be assigned to the specified server. @option options [Fixnum] :ram Number of RAM memory (in MiB) to be assigned to the server. Must be at least 256 and a multiple of it. @option options [String] :name Name of the server to be created. @option options [String] :boot_from_image_id 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. @option options [String] :boot_from_storage_id Defines an existing storage device ID to be set as boot device of the server. The storage will be connected to the server implicitly. @option options [String] :availability_zone 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. @option options [String] :os_type 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. @return [Boolean] true on success, false otherwise
# 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
Blocks until the Server
is provisioned
# File lib/profitbricks/server.rb, line 87 def wait_for_provisioning while !self.provisioned? sleep Profitbricks::Config.polling_interval end end
Blocks until the Server
is running
# File lib/profitbricks/server.rb, line 68 def wait_for_running while !self.running? sleep Profitbricks::Config.polling_interval end end
Private Instance Methods
# File lib/profitbricks/server.rb, line 164 def filter_nics_and_return_ips(&block) return [] if self.nics.nil? self.nics.select { |nic| yield nic }.collect(&:ips).flatten end