Class: Profitbricks::Server
Class Method Summary collapse
-
.create(options = {}) ⇒ Server
Creates a Virtual Server within an existing data center.
-
.find(options = {}) ⇒ Object
Finds a virtual server.
Instance Method Summary collapse
-
#create_nic(options) ⇒ Object
Creates a Nic for the current Server, automatically sets the :server_id.
-
#delete ⇒ Boolean
Deletes the virtual Server.
-
#reboot ⇒ Boolean
Reboots the virtual Server (POWER CYCLE).
-
#running? ⇒ Boolean
Checks if the Server is running.
-
#update(options = {}) ⇒ Boolean
Updates parameters of an existing virtual Server device.
-
#wait_for_running ⇒ Object
Blocks until the Server is running.
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.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/profitbricks/server.rb', line 78 def create( = {}) raise ArgumentError.new("You must provide :cores and :ram") if [:ram].nil? and [:cores].nil? raise ArgumentError.new(":ram has to be at least 256MiB and a multiple of it") if [:ram].to_i < 256 or ([:ram].to_i % 256) > 0 raise ArgumentError.new(":availability_zone has to be either 'AUTO', 'ZONE_1', or 'ZONE_2'") if [:availability_zone] and !['AUTO', 'ZONE_1', 'ZONE_2'].include? [:availability_zone] raise ArgumentError.new(":os_type has to be either 'WINDOWS' or 'OTHER'") if [:os_type] and !['WINDOWS', 'OTHER'].include? [:os_type] xml = "<arg0>" xml += get_xml_and_update_attributes , [: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
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/profitbricks/server.rb', line 95 def find( = {}) # 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 '#{[:name]}'" unless [:id] response = Profitbricks.request :get_server, "<serverId>#{[: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
58 59 60 |
# File 'lib/profitbricks/server.rb', line 58 def create_nic() Nic.create(.merge(:server_id => self.id)) end |
#delete ⇒ Boolean
Deletes the virtual Server.
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 |
#reboot ⇒ Boolean
Reboots the virtual Server (POWER CYCLE).
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
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.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/profitbricks/server.rb', line 29 def update( = {}) return false if .empty? raise ArgumentError.new(":ram has to be at least 256MiB and a multiple of it") if [:ram] < 256 or ([:ram] % 256) > 0 raise ArgumentError.new(":availability_zone has to be either 'AUTO', 'ZONE_1', or 'ZONE_2'") if [:availability_zone] and !['AUTO', 'ZONE_1', 'ZONE_2'].include? [:availability_zone] raise ArgumentError.new(":os_type has to be either 'WINDOWS' or 'OTHER'") if [:os_type] and !['WINDOWS', 'OTHER'].include? [:os_type] xml = "<arg0><serverId>#{self.id}</serverId>" xml += get_xml_and_update_attributes , [: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_running ⇒ Object
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 |