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

+ (Array <Server>) all

Returns a list of all Servers created by the user.



134
135
136
# File 'lib/profitbricks/server.rb', line 134

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

+ (Server) create(options = {})

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.

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.

Raises:

  • (ArgumentError)


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

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

+ (Object) find(options = {})

Finds a virtual server

Options Hash (options):

  • :id (String)

    The id of the server to locate



167
168
169
170
171
172
173
174
175
176
# File 'lib/profitbricks/server.rb', line 167

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

- (Object) create_nic(options)

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

See Also:

  • Nic#create


111
112
113
# File 'lib/profitbricks/server.rb', line 111

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

- (Boolean) delete

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

- (Boolean) power_off

Stops an existing virtual server (HARD power off)



39
40
41
42
43
# File 'lib/profitbricks/server.rb', line 39

def power_off
  @virtual_machine_state = 'SHUTOFF'
  Profitbricks.request :power_off_server, server_id: self.id
  return true
end

- (Array <String>) private_ips

Helper method to get a list of all private IP adresses



125
126
127
# File 'lib/profitbricks/server.rb', line 125

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

- (Boolean) provisioned?

Checks if the Server was successfully provisioned



93
94
95
96
97
98
99
100
# File 'lib/profitbricks/server.rb', line 93

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

- (Array <String>) public_ips

Helper method to get a list of all public IP adresses



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

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

- (Boolean) reboot

Reboots an existing virtual server (SOFT REBOOT).



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

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

- (Boolean) reset

Resets an existing virtual server (POWER CYCLE).



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

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

- (Boolean) running?

Checks if the Server is running



78
79
80
81
# File 'lib/profitbricks/server.rb', line 78

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

- (Boolean) shutdown

Stops an existing virtual server gracefully (SOFT stop)



47
48
49
50
51
# File 'lib/profitbricks/server.rb', line 47

def shutdown
  @virtual_machine_state = 'SHUTDOWN'
  Profitbricks.request :shutdown_server, server_id: self.id
  return true
end

- (Boolean) start

Starts an existing virtual server



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

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

- (Boolean) update(options = {})

Updates parameters of an existing virtual Server device.

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.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/profitbricks/server.rb', line 63

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

- (Object) wait_for_provisioning

Blocks until the Server is provisioned



103
104
105
106
107
# File 'lib/profitbricks/server.rb', line 103

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

- (Object) wait_for_running

Blocks until the Server is running



84
85
86
87
88
# File 'lib/profitbricks/server.rb', line 84

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