Class: DoSnapshot::Command

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

Overview

Our commands live here :)

Constant Summary collapse

RESET_OPTIONS =
[:droplets, :exclude, :only, :keep, :quiet,
:stop, :clean, :timeout, :shutdown, :delay,
:protocol, :threads, :api]

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.



15
16
17
# File 'lib/do_snapshot/command.rb', line 15

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

Instance Attribute Details

#apiObject

API launcher



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

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/do_snapshot/command.rb', line 61

def create_snapshot(droplet) # rubocop:disable MethodLength,Metrics/AbcSize
  fail_if_shutdown(droplet)

  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



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

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

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



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

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



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

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



107
108
109
# File 'lib/do_snapshot/command.rb', line 107

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

#reset_optionsObject



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

def reset_options
  RESET_OPTIONS.each do |key|
    send("#{key}=", nil)
  end
end

#snapObject



19
20
21
22
23
24
25
26
# File 'lib/do_snapshot/command.rb', line 19

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



49
50
51
52
53
54
55
56
57
# File 'lib/do_snapshot/command.rb', line 49

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