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

Methods included from Helpers

#logger, #mailer

Constructor Details

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

Instance Method Details

#check_keysObject



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

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.



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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 72

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)


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

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

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

#power_on(id) ⇒ Object

Request Power On for droplet



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

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

#start_droplet(id) ⇒ Object

Power On request for Droplet



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

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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/do_snapshot/adapter/digitalocean.rb', line 58

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

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

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