class DoSnapshot::Adapter::Abstract

API for CLI commands Operating with Digital Ocean.

Attributes

delay[RW]
stop_by[W]
timeout[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/do_snapshot/adapter/abstract.rb, line 14
def initialize(options = {})
  check_keys
  set_id
  options.each_pair do |key, option|
    send("#{key}=", option)
  end
end

Public Instance Methods

start_droplet(id) click to toggle source

Power On request for Droplet

# File lib/do_snapshot/adapter/abstract.rb, line 24
def start_droplet(id)
  # noinspection RubyResolve
  instance = droplet(id)

  return power_on(id) if instance.respond_to?(:status) && !instance.status.include?('active')

  logger.error "Droplet #{id} is still running. Skipping."
end

Protected Instance Methods

after_cleanup(droplet_id, droplet_name, snapshot, event) click to toggle source
# File lib/do_snapshot/adapter/abstract.rb, line 71
def after_cleanup(droplet_id, droplet_name, snapshot, event)
  if !event
    logger.error "Destroy of snapshot #{snapshot.name} for droplet id: #{droplet_id} name: #{droplet_name} is failed."
  elsif event && !event.status.include?('OK')
    logger.error event.message
  else
    logger.debug "Snapshot name: #{snapshot.name} delete requested."
  end
end
check_keys() click to toggle source
# File lib/do_snapshot/adapter/abstract.rb, line 37
def check_keys; end
droplet_timeout?(id, time) click to toggle source

Droplet request timeout

# File lib/do_snapshot/adapter/abstract.rb, line 91
def droplet_timeout?(id, time)
  timeout? id, time, "Droplet id: #{id} shutdown event closed by timeout #{time}"
end
get_event_status(_id, _time) click to toggle source

This is stub for event status

# File lib/do_snapshot/adapter/abstract.rb, line 96
def get_event_status(_id, _time)
  true
end
get_shutdown_status(id, time) click to toggle source

Looking for droplet status. Before snapshot we need to know that machine is powered off.

# File lib/do_snapshot/adapter/abstract.rb, line 103
def get_shutdown_status(id, time)
  fail "Droplet #{id} not responding for shutdown!" if droplet_timeout?(id, time)

  inactive?(id)
end
set_id() click to toggle source
# File lib/do_snapshot/adapter/abstract.rb, line 35
def set_id; end
stop_by() click to toggle source
# File lib/do_snapshot/adapter/abstract.rb, line 39
def stop_by
  @stop_by ||= :event_status
end
timeout?(id, time, message = "Event click to toggle source

Event request timeout.

# File lib/do_snapshot/adapter/abstract.rb, line 83
def timeout?(id, time, message = "Event #{id} finished by timeout #{time}")
  return false unless (Time.now - time) > @timeout
  logger.debug message
  true
end
wait_event(event_id) click to toggle source

Waiting for event exit

# File lib/do_snapshot/adapter/abstract.rb, line 55
def wait_event(event_id)
  wait_wrap(event_id) { |id, time| get_event_status(id, time) }
end
wait_shutdown(droplet_id, event_id) click to toggle source

Waiting for droplet shutdown

# File lib/do_snapshot/adapter/abstract.rb, line 60
def wait_shutdown(droplet_id, event_id)
  case stop_by
  when :power_status
    wait_wrap(droplet_id, "Droplet Id: #{droplet_id} shutting down") { |id, time| get_shutdown_status(id, time) }
  when :event_status
    wait_event(event_id)
  else
    fail 'Please define :stopper method (:droplet_status, :event_status'
  end
end
wait_wrap(id, message = "Event Id: click to toggle source

Waiting wrapper

# File lib/do_snapshot/adapter/abstract.rb, line 44
def wait_wrap(id, message = "Event Id: #{id}", &status_block)
  logger.debug message
  time = Time.now
  sleep(delay) until status_block.call(id, time)
rescue => e
  logger.error e.message
  e.backtrace.each { |t| logger.error t }
  DoSnapshot::EventError.new(id)
end