Class: DoSnapshot::Adapter::DigitaloceanV2
- Defined in:
- lib/do_snapshot/adapter/digitalocean_v2.rb
Overview
API for CLI commands Operating with Digital Ocean.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Attributes inherited from Abstract
Instance Method Summary collapse
- #check_keys ⇒ Object
-
#cleanup_snapshots(instance, size) ⇒ Object
Cleanup our snapshots.
-
#create_snapshot(id, name) ⇒ Object
Sending event to create snapshot via DigitalOcean API and wait for success.
-
#droplet(id) ⇒ Object
Get single droplet from DigitalOcean.
-
#droplets ⇒ Object
Get droplets list from DigitalOcean.
-
#inactive?(id) ⇒ Boolean
Checking if droplet is powered off.
-
#power_on(id) ⇒ Object
Request Power On for droplet.
-
#set_id ⇒ Object
Set id’s of Digital Ocean API.
- #snapshots(instance) ⇒ Object
-
#stop_droplet(id) ⇒ Object
Power Off request for Droplet.
Methods inherited from Abstract
Methods included from Helpers
Constructor Details
This class inherits a constructor from DoSnapshot::Adapter::Abstract
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 10 def client @client end |
Instance Method Details
#check_keys ⇒ Object
95 96 97 98 99 100 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 95 def check_keys logger.debug 'Checking DigitalOcean Access Token.' %w( DIGITAL_OCEAN_ACCESS_TOKEN ).each do |key| fail DoSnapshot::NoTokenError, "You must have #{key} in environment or set it via options." if ENV[key].blank? end end |
#cleanup_snapshots(instance, size) ⇒ Object
Cleanup our snapshots.
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 83 def cleanup_snapshots(instance, size) (0..size).each do |i| # noinspection RubyResolve snapshot = instance.snapshot_ids[i] action = client.image.destroy(snapshot) logger.debug action unless action.success? after_cleanup(instance.id, instance.name, snapshot, action) end end |
#create_snapshot(id, name) ⇒ Object
Sending event to create snapshot via DigitalOcean API and wait for success
63 64 65 66 67 68 69 70 71 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 63 def create_snapshot(id, name) # noinspection RubyResolve,RubyResolve response = client.droplet.snapshot(id, name: name) fail DoSnapshot::SnapshotCreateError.new(id), response. unless response.respond_to?(:action) # noinspection RubyResolve wait_event(response.action.id) end |
#droplet(id) ⇒ Object
Get single droplet from DigitalOcean
14 15 16 17 18 19 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 14 def droplet(id) # noinspection RubyResolve response = client.droplet.show(id) fail DropletFindError.new(id), response. unless response.respond_to?(:droplet) response.droplet end |
#droplets ⇒ Object
Get droplets list from DigitalOcean
23 24 25 26 27 28 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 23 def droplets # noinspection RubyResolve response = client.droplet.all fail DropletListError, response. unless response.respond_to?(:droplets) response.droplets end |
#inactive?(id) ⇒ Boolean
Checking if droplet is powered off.
75 76 77 78 79 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 75 def inactive?(id) instance = droplet(id) instance.status.include?('off') end |
#power_on(id) ⇒ Object
Request Power On for droplet
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 36 def power_on(id) # noinspection RubyResolve response = client.droplet.power_on(id) fail DoSnapshot::EventError.new(id), response. unless response.respond_to?(:action) if response.action.status.include?('in-progress') logger.info "Droplet id: #{id} is requested for Power On." else logger.error "Droplet id: #{id} is failed to request for Power On." end end |
#set_id ⇒ Object
Set id’s of Digital Ocean API.
104 105 106 107 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 104 def set_id logger.debug 'Setting DigitalOcean Access Token.' @client = ::Barge::Client.new(access_token: ENV['DIGITAL_OCEAN_ACCESS_TOKEN'], timeout: 15, open_timeout: 15) end |
#snapshots(instance) ⇒ Object
30 31 32 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 30 def snapshots(instance) instance.snapshot_ids end |
#stop_droplet(id) ⇒ Object
Power Off request for Droplet
51 52 53 54 55 56 57 58 59 |
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 51 def stop_droplet(id) # noinspection RubyResolve,RubyResolve response = client.droplet.power_off(id) fail DropletShutdownError.new(id), response. unless response.respond_to?(:action) # noinspection RubyResolve wait_shutdown(id, response.action.id) end |