Class: Profitbricks::Server
Class Method Summary collapse
-
.all ⇒ Array <Server>
Returns a list of all Servers created by the user.
-
.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.
-
#private_ips ⇒ Array <String>
Helper method to get a list of all private IP adresses.
-
#provisioned? ⇒ Boolean
Checks if the Server was successfully provisioned.
-
#public_ips ⇒ Array <String>
Helper method to get a list of all public IP adresses.
-
#reset ⇒ Boolean
Resets an existing virtual server (POWER CYCLE).
-
#running? ⇒ Boolean
Checks if the Server is running.
-
#start ⇒ Boolean
Starts an existing virtual server.
-
#stop ⇒ Boolean
Stops an existing virtual server (HARD power off).
-
#update(options = {}) ⇒ Boolean
Updates parameters of an existing virtual Server device.
-
#wait_for_provisioning ⇒ Object
Blocks until the Server is provisioned.
-
#wait_for_running ⇒ Object
Blocks until the Server is running.
Methods inherited from Model
#attributes, belongs_to, has_many, #initialize, #reload
Constructor Details
This class inherits a constructor from Profitbricks::Model
Class Method Details
.all ⇒ Array <Server>
Returns a list of all Servers created by the user.
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.
137 138 139 140 141 142 143 144 145 |
# File 'lib/profitbricks/server.rb', line 137 def create( = {}) raise ArgumentError.new("You must provide :cores and :ram") if [:ram].nil? or [: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] [:server_name] = .delete :name if [:name] response = Profitbricks.request :create_server, self.find(:id => response[:server_id]) end |
.find(options = {}) ⇒ Object
Finds a virtual server
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/profitbricks/server.rb', line 151 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, server_id: [: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
95 96 97 |
# File 'lib/profitbricks/server.rb', line 95 def create_nic() Nic.create(.merge(:server_id => self.id)) end |
#delete ⇒ Boolean
Deletes the virtual Server.
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_ips ⇒ Array <String>
Helper method to get a list 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
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_ips ⇒ Array <String>
Helper method to get a list 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 |
#reset ⇒ Boolean
Resets an existing virtual server (POWER CYCLE).
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
62 63 64 65 |
# File 'lib/profitbricks/server.rb', line 62 def running? self.reload self.virtual_machine_state == "RUNNING" end |
#start ⇒ Boolean
Starts an existing virtual server
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 |
#stop ⇒ Boolean
Stops an existing virtual server (HARD power off)
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.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/profitbricks/server.rb', line 47 def update( = {}) return false if .empty? raise ArgumentError.new(":ram has to be at least 256MiB and a multiple of it") if [:ram] and ([: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] update_attributes_from_hash [:server_name] = .delete :name if [:name] [:server_id] = self.id response = Profitbricks.request :update_server, return true end |
#wait_for_provisioning ⇒ Object
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_running ⇒ Object
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 |