Class: DoSnapshot::Adapter::DigitaloceanV2

Inherits:
Abstract
  • Object
show all
Defined in:
lib/do_snapshot/adapter/digitalocean_v2.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

#initialize, #start_droplet

Methods included from Helpers

#logger, #mailer

Constructor Details

This class inherits a constructor from DoSnapshot::Adapter::Abstract

Instance Attribute Details

#clientObject (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_keysObject



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].nil? || ENV[key].empty?
  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.message 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.message unless response.respond_to?(:droplet)
  response.droplet
end

#dropletsObject

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.message unless response.respond_to?(:droplets)
  response.droplets
end

#inactive?(id) ⇒ Boolean

Checking if droplet is powered off.

Returns:

  • (Boolean)


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.message 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_idObject

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.message unless response.respond_to?(:action)

  # noinspection RubyResolve
  wait_shutdown(id, response.action.id)
end