Class: DoSnapshot::Adapter::Digitalocean

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

Overview

API for CLI commands Operating with Digital Ocean.

Instance Attribute Summary

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 Method Details

#check_keysObject



95
96
97
98
99
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 95

def check_keys
  logger.debug 'Checking DigitalOcean Id\'s.'
  errors = %w( DIGITAL_OCEAN_CLIENT_ID DIGITAL_OCEAN_API_KEY ).map { |key| key if ENV[key].blank? }.compact
  fail DoSnapshot::NoKeysError, "You must have #{errors.join(', ')} in environment or set it via options." if errors.size > 0
end

#cleanup_snapshots(instance, size) ⇒ Object

Cleanup our snapshots.



85
86
87
88
89
90
91
92
93
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 85

def cleanup_snapshots(instance, size)
  (0..size).each do |i|
    # noinspection RubyResolve
    snapshot = instance.snapshots[i]
    event = ::DigitaloceanC::Image.destroy(snapshot.id)

    after_cleanup(instance.id, instance.name, snapshot, event)
  end
end

#create_snapshot(id, name) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 61

def create_snapshot(id, name)
  # noinspection RubyResolve,RubyResolve
  event = ::DigitaloceanC::Droplet.snapshot(id, name: name)

  if !event
    fail DoSnapshot::SnapshotCreateError.new(id), 'Something wrong with DigitalOcean or with your connection :)'
  elsif event && !event.status.include?('OK')
    fail DoSnapshot::SnapshotCreateError.new(id), event.message
  end

  # noinspection RubyResolve
  wait_event(event.event_id)
end

#droplet(id) ⇒ Object

Get single droplet from DigitalOcean



12
13
14
15
16
17
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 12

def droplet(id)
  # noinspection RubyResolve
  response = ::DigitaloceanC::Droplet.find(id)
  fail DropletFindError.new(id), response.message unless response.status.include? 'OK'
  response.droplet
end

#dropletsObject

Get droplets list from DigitalOcean



21
22
23
24
25
26
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 21

def droplets
  # noinspection RubyResolve
  response = ::DigitaloceanC::Droplet.all
  fail DropletListError, response.message unless response.status.include? 'OK'
  response.droplets
end

#inactive?(id) ⇒ Boolean

Checking if droplet is powered off.

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 77

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

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

#power_on(id) ⇒ Object

Request Power On for droplet



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

def power_on(id)
  # noinspection RubyResolve
  event = ::DigitaloceanC::Droplet.power_on(id)
  case event && event.status
  when 'OK'
    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

#snapshots(instance) ⇒ Object



28
29
30
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 28

def snapshots(instance)
  instance.snapshots
end

#stop_droplet(id) ⇒ Object

Power Off request for Droplet



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

def stop_droplet(id)
  # noinspection RubyResolve,RubyResolve
  event = ::DigitaloceanC::Droplet.power_off(id)

  fail event.message unless event.status.include? 'OK'

  # noinspection RubyResolve
  wait_shutdown(id, event.event_id)
rescue => e
  raise DropletShutdownError.new(id), e.message, e.backtrace
end