Class: DoSnapshot::Command

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/do_snapshot/command.rb

Overview

Our commands live here :)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#logger, #mailer

Constructor Details

#initialize(*args) ⇒ Command

Returns a new instance of Command.



10
11
12
# File 'lib/do_snapshot/command.rb', line 10

def initialize(*args)
  load_options(*args)
end

Instance Attribute Details

#apiObject

API launcher



94
95
96
# File 'lib/do_snapshot/command.rb', line 94

def api
  @api ||= DoSnapshot::Adapter.api(protocol, delay: delay, timeout: timeout, stop_by: stop_by)
end

Instance Method Details

#create_snapshot(droplet) ⇒ Object

Trying to create a snapshot.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/do_snapshot/command.rb', line 54

def create_snapshot(droplet) # rubocop:disable MethodLength,Metrics/AbcSize
  fail DropletPowerError.new(droplet.id), droplet.name unless api.inactive?(droplet.id)

  logger.info "Start creating snapshot for droplet id: #{droplet.id} name: #{droplet.name}."

  today         = DateTime.now
  name          = "#{droplet.name}_#{today.strftime('%Y_%m_%d')}"
  # noinspection RubyResolve
  snapshot_size = api.snapshots(droplet).size

  logger.debug 'Wait until snapshot will be created.'

  api.create_snapshot droplet.id, name

  snapshot_size += 1

  logger.info "Snapshot name: #{name} created successfully."
  logger.info "Droplet id: #{droplet.id} name: #{droplet.name} snapshots: #{snapshot_size}."

  # Cleanup snapshots.
  cleanup_snapshots droplet, snapshot_size if clean
rescue => e
  case e.class.to_s
  when 'DoSnapshot::SnapshotCleanupError'
    raise e.class, e.message, e.backtrace
  when 'DoSnapshot::DropletPowerError'
    return
  else
    raise SnapshotCreateError.new(droplet.id), e.message, e.backtrace
  end
end

#fail_power_off(e) ⇒ Object



23
24
25
26
27
28
# File 'lib/do_snapshot/command.rb', line 23

def fail_power_off(e)
  return unless e && e.id
  api.start_droplet(e.id)
rescue
  raise DropletFindError, e.message, e.backtrace
end

#load_options(options = {}, skip = %w()) ⇒ Object



30
31
32
33
34
35
# File 'lib/do_snapshot/command.rb', line 30

def load_options(options = {}, skip = %w())
  reset_options
  options.each_pair do |key, option|
    send("#{key}=", option) unless skip.include?(key)
  end if options
end

#power_on_failed_dropletsObject



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

def power_on_failed_droplets
  processed_droplet_ids
    .select { |id| api.inactive?(id) }
    .each   { |id| api.power_on(id) }
end

#processed_droplet_idsObject

Processed droplets



100
101
102
# File 'lib/do_snapshot/command.rb', line 100

def processed_droplet_ids
  @droplet_ids ||= %w()
end

#reset_optionsObject



37
38
39
40
41
# File 'lib/do_snapshot/command.rb', line 37

def reset_options
  [:droplets, :exclude, :only, :keep, :quiet, :stop, :clean, :timeout, :delay, :protocol, :threads, :api].each do |key|
    send("#{key}=", nil)
  end
end

#snapObject



14
15
16
17
18
19
20
21
# File 'lib/do_snapshot/command.rb', line 14

def snap
  logger.info 'Start performing operations'
  work_with_droplets
  power_on_failed_droplets
  logger.info 'All operations has been finished.'

  mailer.notify if mailer && notify && !quiet
end

#stop_droplet(droplet) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/do_snapshot/command.rb', line 43

def stop_droplet(droplet)
  logger.debug 'Shutting down droplet.'
  api.stop_droplet(droplet.id) unless droplet.status.include? 'off'
  true
rescue => e
  logger.error e.message
  false
end