Class: DoSnapshot::Adapter::DropletKit

Inherits:
Abstract
  • Object
show all
Defined in:
lib/do_snapshot/adapter/droplet_kit.rb

Overview

API for CLI commands Operating with Digital Ocean.

Instance Attribute Summary collapse

Attributes inherited from Abstract

#delay, #timeout

Instance Method Summary collapse

Methods inherited from Abstract

#start_droplet

Methods included from Helpers

#logger, #mailer

Constructor Details

#initialize(*args) ⇒ DropletKit

Returns a new instance of DropletKit.



15
16
17
18
19
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 15

def initialize(*args)
  @per_page = 1000
  @page = 1
  super(*args)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 13

def client
  @client
end

#pageObject (readonly)

Returns the value of attribute page.



13
14
15
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 13

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



13
14
15
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 13

def per_page
  @per_page
end

Instance Method Details

#check_keysObject



104
105
106
107
108
109
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 104

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) ⇒ Object

Cleanup our snapshots.



96
97
98
99
100
101
102
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 96

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) ⇒ Object

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



76
77
78
79
80
81
82
83
84
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 76

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) ⇒ Object

Get single droplet from DigitalOcean



23
24
25
26
27
28
29
30
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 23

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

#dropletsObject

Get droplets list from DigitalOcean



34
35
36
37
38
39
40
41
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 34

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) ⇒ Boolean

Checking if droplet is powered off.

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 88

def inactive?(id)
  instance = droplet(id)

  instance.status.include?('off') if instance.respond_to?(:status)
end

#power_on(id) ⇒ Object

Request Power On for droplet



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 49

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_idObject

Set id’s of Digital Ocean API.



113
114
115
116
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 113

def set_id
  logger.debug 'Setting DigitalOcean Access Token.'
  @client = ::DropletKit::Client.new(access_token: ENV['DIGITAL_OCEAN_ACCESS_TOKEN'])
end

#snapshots(instance) ⇒ Object



43
44
45
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 43

def snapshots(instance)
  instance.snapshot_ids if instance.respond_to?(:snapshot_ids)
end

#stop_droplet(id) ⇒ Object

Power Off request for Droplet



64
65
66
67
68
69
70
71
72
# File 'lib/do_snapshot/adapter/droplet_kit.rb', line 64

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