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

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



106
107
108
109
110
111
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 106

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.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 94

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



74
75
76
77
78
79
80
81
82
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 74

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.



86
87
88
89
90
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 86

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

  instance.status.include?('off')
end

#power_on(id) ⇒ Object

Request Power On for droplet



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 47

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.



115
116
117
118
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 115

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

#start_droplet(id) ⇒ Object

Power On request for Droplet



36
37
38
39
40
41
42
43
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 36

def start_droplet(id)
  # noinspection RubyResolve
  instance = droplet(id)

  return power_on(id) unless instance.status.include?('active')

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

#stop_droplet(id) ⇒ Object

Power Off request for Droplet



62
63
64
65
66
67
68
69
70
# File 'lib/do_snapshot/adapter/digitalocean_v2.rb', line 62

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_event(response.action.id)
end