Class: Fog::Compute::DigitalOcean::Server
- Inherits:
-
Server
- Object
- Server
- Fog::Compute::DigitalOcean::Server
- Defined in:
- lib/fog/digitalocean/models/compute/server.rb
Overview
A DigitalOcean Droplet
Instance Attribute Summary collapse
-
#ssh_keys ⇒ Object
writeonly
Sets the attribute ssh_keys.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy the server, freeing up the resources.
-
#flavor ⇒ Object
Helper method to get the flavor name.
-
#image ⇒ Object
Helper method to get the image name.
-
#ip_address ⇒ Object
Deprecated: Use public_ip_address instead.
-
#ip_addresses ⇒ Object
Helper method to get an array with all available IP addresses.
-
#power_cycle ⇒ Object
Reboot the server (hard reboot).
-
#ready? ⇒ Boolean
Checks whether the server status is ‘active’.
-
#reboot ⇒ Object
Reboot the server (soft reboot).
-
#region ⇒ Object
Helper method to get the region name.
-
#save ⇒ Boolean
Creates the server (not to be called directly).
- #setup(credentials = {}) ⇒ Object
-
#shutdown ⇒ Object
Shutdown the server.
-
#start ⇒ Object
Power on the server.
-
#stop ⇒ Object
Power off the server.
-
#update ⇒ Object
DigitalOcean API does not support updating server state.
Instance Attribute Details
#ssh_keys=(value) ⇒ Object (writeonly)
Sets the attribute ssh_keys
21 22 23 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 21 def ssh_keys=(value) @ssh_keys = value end |
Instance Method Details
#destroy ⇒ Object
Destroy the server, freeing up the resources.
DigitalOcean will stop charging you for the resources the server was using.
Once the server has been destroyed, there’s no way to recover it so the data is irrecoverably lost.
IMPORTANT: As of 2013/01/31, you should wait some time to destroy the server after creating it. If you try to destroy the server too fast, the destroy event may be lost and the server will remain running and consuming resources, so DigitalOcean will keep charging you. Double checked this with DigitalOcean staff and confirmed that it’s the way it works right now.
Double check the server has been destroyed!
155 156 157 158 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 155 def destroy requires :id service.destroy_server id end |
#flavor ⇒ Object
Helper method to get the flavor name
178 179 180 181 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 178 def flavor requires :flavor_id @flavor ||= service.flavors.get(flavor_id.to_i) end |
#image ⇒ Object
Helper method to get the image name
184 185 186 187 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 184 def image requires :image_id @image ||= service.images.get(image_id.to_i) end |
#ip_address ⇒ Object
Deprecated: Use public_ip_address instead.
24 25 26 27 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 24 def ip_address Fog::Logger.warning("ip_address has been deprecated. Use public_ip_address instead") public_ip_address end |
#ip_addresses ⇒ Object
Helper method to get an array with all available IP addresses
196 197 198 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 196 def ip_addresses [public_ip_address, private_ip_address].flatten.select(&:present?) end |
#power_cycle ⇒ Object
Reboot the server (hard reboot).
Powers the server off and then powers it on again.
40 41 42 43 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 40 def power_cycle requires :id service.power_cycle_server self.id end |
#ready? ⇒ Boolean
Checks whether the server status is ‘active’.
The server transitions from ‘new’ to ‘active’ sixty to ninety seconds after creating it (time varies and may take more than 90 secs).
167 168 169 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 167 def ready? state == 'active' end |
#reboot ⇒ Object
Reboot the server (soft reboot).
The preferred method of rebooting a server.
32 33 34 35 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 32 def reboot requires :id service.reboot_server self.id end |
#region ⇒ Object
Helper method to get the region name
190 191 192 193 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 190 def region requires :region_id @region ||= service.regions.get(region_id.to_i) end |
#save ⇒ Boolean
Creates the server (not to be called directly).
Usually called by Fog::Collection#create
docean = Fog::Compute.new({
:provider => 'DigitalOcean',
:digitalocean_api_key => 'key-here', # your API key here
:digitalocean_client_id => 'client-id-here' # your client key here
})
docean.servers.create :name => 'foobar',
:image_id => image_id_here,
:flavor_id => flavor_id_here,
:region_id => region_id_here
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 115 def save raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted? requires :name, :flavor_id, :image_id, :region_id = {} if attributes[:ssh_key_ids] [:ssh_key_ids] = attributes[:ssh_key_ids] elsif @ssh_keys [:ssh_key_ids] = @ssh_keys.map(&:id) end [:private_networking] = private_networking [:backups_active] = backups_active data = service.create_server name, flavor_id, image_id, region_id, merge_attributes(data.body['droplet']) true end |
#setup(credentials = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 82 def setup(credentials = {}) requires :ssh_ip_address commands = [ %{mkdir .ssh}, %{passwd -l #{username}}, %{echo "#{Fog::JSON.encode(Fog::JSON.sanitize(attributes))}" >> ~/attributes.json} ] if public_key commands << %{echo "#{public_key}" >> ~/.ssh/authorized_keys} end # wait for DigitalOcean to be ready wait_for { sshable?(credentials) } Fog::SSH.new(ssh_ip_address, username, credentials).run(commands) end |
#shutdown ⇒ Object
Shutdown the server
Sends a shutdown signal to the operating system. The server consumes resources while powered off so you are still charged.
52 53 54 55 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 52 def shutdown requires :id service.shutdown_server self.id end |
#start ⇒ Object
Power on the server.
The server consumes resources while powered on so you will be charged.
Each time a server is spun up, even if for a few seconds, it is charged for an hour.
77 78 79 80 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 77 def start requires :id service.power_on_server self.id end |
#stop ⇒ Object
Power off the server
Works as a power switch. The server consumes resources while powered off so you are still charged.
64 65 66 67 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 64 def stop requires :id service.power_off_server self.id end |
#update ⇒ Object
DigitalOcean API does not support updating server state
172 173 174 175 |
# File 'lib/fog/digitalocean/models/compute/server.rb', line 172 def update msg = 'DigitalOcean servers do not support updates' raise NotImplementedError.new(msg) end |