class DoSnapshot::Adapter::DropletKit

API for CLI commands Operating with Digital Ocean.

Attributes

client[R]
page[R]
per_page[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method DoSnapshot::Adapter::Abstract::new
# File lib/do_snapshot/adapter/droplet_kit.rb, line 14
def initialize(*args)
  @per_page = 1000
  @page = 1
  super(*args)
end

Public Instance Methods

check_keys() click to toggle source
# File lib/do_snapshot/adapter/droplet_kit.rb, line 103
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].nil? || ENV[key].empty?
  end
end
cleanup_snapshots(instance, size) click to toggle source

Cleanup our snapshots.

# File lib/do_snapshot/adapter/droplet_kit.rb, line 95
def cleanup_snapshots(instance, size)
  (0..size).each do |i|
    # noinspection RubyResolve
    snapshot = snapshots(instance)[i]
    delete_image(instance, snapshot)
  end
end
create_snapshot(id, name) click to toggle source

Sending event to create snapshot via DigitalOcean API and wait for success

# File lib/do_snapshot/adapter/droplet_kit.rb, line 75
def create_snapshot(id, name)
  # noinspection RubyResolve,RubyResolve
  response = client.droplet_actions.snapshot(droplet_id: id, name: name)

  # noinspection RubyResolve
  wait_event(response.id)
rescue ::DropletKit::Error => e
  fail DoSnapshot::SnapshotCreateError.new(id), e.message
end
droplet(id) click to toggle source

Get single droplet from DigitalOcean

# File lib/do_snapshot/adapter/droplet_kit.rb, line 22
def droplet(id)
  # noinspection RubyResolve
  result = client.droplets.find(id: id)
  fail DropletFindError, id unless result
  result
rescue ::DropletKit::Error => e
  raise DropletFindError, id unless e.message
end
droplets() click to toggle source

Get droplets list from DigitalOcean

# File lib/do_snapshot/adapter/droplet_kit.rb, line 33
def droplets
  # noinspection RubyResolve
  response = client.droplets.all(page: page, per_page: per_page)
  response.many?
  response
rescue ::NoMethodError => e
  fail DropletListError, e
end
inactive?(id) click to toggle source

Checking if droplet is powered off.

# File lib/do_snapshot/adapter/droplet_kit.rb, line 87
def inactive?(id)
  instance = droplet(id)

  instance.status.include?('off') if instance.respond_to?(:status)
end
power_on(id) click to toggle source

Request Power On for droplet

# File lib/do_snapshot/adapter/droplet_kit.rb, line 48
def power_on(id)
  # noinspection RubyResolve
  response = client.droplet_actions.power_on(droplet_id: id)

  if response.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
rescue ::DropletKit::Error => e
  fail DoSnapshot::EventError.new(id), e.message
end
set_id() click to toggle source

Set id's of Digital Ocean API.

# File lib/do_snapshot/adapter/droplet_kit.rb, line 112
def set_id
  logger.debug 'Setting DigitalOcean Access Token.'
  @client = ::DropletKit::Client.new(access_token: ENV['DIGITAL_OCEAN_ACCESS_TOKEN'])
end
snapshots(instance) click to toggle source
# File lib/do_snapshot/adapter/droplet_kit.rb, line 42
def snapshots(instance)
  instance.snapshot_ids if instance.respond_to?(:snapshot_ids)
end
stop_droplet(id) click to toggle source

Power Off request for Droplet

# File lib/do_snapshot/adapter/droplet_kit.rb, line 63
def stop_droplet(id)
  # noinspection RubyResolve,RubyResolve
  response = client.droplet_actions.power_off(droplet_id: id)

  # noinspection RubyResolve
  wait_shutdown(id, response.id)
rescue ::DropletKit::Error => e
  fail DropletShutdownError.new(id), e.message
end

Protected Instance Methods

after_cleanup(droplet_id, droplet_name, snapshot, action) click to toggle source
# File lib/do_snapshot/adapter/droplet_kit.rb, line 127
def after_cleanup(droplet_id, droplet_name, snapshot, action)
  if !action
    logger.error "Destroy of snapshot #{snapshot} for droplet id: #{droplet_id} name: #{droplet_name} is failed."
  else
    logger.debug "Snapshot: #{snapshot} delete requested."
  end
end
delete_image(instance, snapshot) click to toggle source
# File lib/do_snapshot/adapter/droplet_kit.rb, line 119
def delete_image(instance, snapshot) # rubocop:disable Metrics/AbcSize
  action = client.images.delete(id: snapshot)
  after_cleanup(instance.id, instance.name, snapshot, action)
rescue ::DropletKit::Error => e
  logger.debug "#{snapshot} #{e.message}"
  after_cleanup(instance.id, instance.name, snapshot, false)
end
get_event_status(id, time) click to toggle source

Looking for event status.

# File lib/do_snapshot/adapter/droplet_kit.rb, line 137
def get_event_status(id, time)
  return true if timeout?(id, time)

  response = client.actions.find(id: id)

  fail DoSnapshot::EventError.new(id), response.message unless response.respond_to?(:status)

  # noinspection RubyResolve,RubyResolve
  response.status.include?('completed') ? true : false
end