Class: EY::SnapshotMinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ey-flex/snapshot_minder.rb

Defined Under Namespace

Classes: Backend

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SnapshotMinder

Returns a new instance of SnapshotMinder.



104
105
106
# File 'lib/ey-flex/snapshot_minder.rb', line 104

def initialize(opts={})
  @opts = opts
end

Class Method Details

.backendObject



96
97
98
# File 'lib/ey-flex/snapshot_minder.rb', line 96

def self.backend
  @backend ||= Backend::Real.new
end

.enable_mock!Object



100
101
102
# File 'lib/ey-flex/snapshot_minder.rb', line 100

def self.enable_mock!
  @backend = Backend::Mock.new
end

.run(args) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
# File 'lib/ey-flex/snapshot_minder.rb', line 60

def self.run(args)
  options = {:command => :list_snapshots}
  # Build a parser for the command line arguments
  opts = OptionParser.new do |opts|
    opts.version = "0.0.1"

    opts.banner = "Usage: ey-snapshots [-flag] [argument]"
    opts.define_head "ey-snapshots: managing your snapshots..."
    opts.separator '*'*80

    opts.on("-l", "--list-snapshots", "list snapshots") do
      options[:command] = :list_snapshots
    end

    opts.on("-c", "--config CONFIG", "DEPRECATED, does nothing") do |config|
      STDERR.puts "WARNING: --config is DEPRECATED and has no effect"
    end

    opts.on("-i", "--instance-id ID", "DEPRECATED, does nothing") do |iid|
      STDERR.puts "WARNING: --instance-id is DEPRECATED and has no effect"
    end

    opts.on("--snapshot", "request a snapshot of the volumes attached to this instance") do
      options[:command] = :snapshot_volumes
    end

    opts.on("-q", "--quiet", "Supress output to STDOUT") do
      options[:quiet] = true
    end

  end

  opts.parse!(args)
  new(options).send(options[:command])
end

Instance Method Details

#backoff(nth_time) ⇒ Object



126
127
128
# File 'lib/ey-flex/snapshot_minder.rb', line 126

def backoff(nth_time)
  60*nth_time + rand((60 / (nth_time+1)) + 5) #see tests
end

#list_snapshotsObject



130
131
132
133
134
135
136
# File 'lib/ey-flex/snapshot_minder.rb', line 130

def list_snapshots
  snapshots = SnapshotMinder.backend.list
  say "#{snapshots.size} Snapshots available:"
  snapshots.each do |s|
    say "#{s.id} - #{s.volume} #{s.state} #{s.progress} #{s.created_at}"
  end
end

#snapshot_volumesObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ey-flex/snapshot_minder.rb', line 108

def snapshot_volumes
  retry_count = 0
  begin
    SnapshotMinder.backend.snapshot
    say "Snapshot requested."
  rescue EY::InstanceAPIClient::Connection::UnexpectedStatus => e
    retry_count += 1
    if retry_count <= 10
      retry_in = backoff(retry_count)
      say "failed with #{e.inspect}, retrying in #{retry_in} seconds"
      SnapshotMinder.backend.wait(retry_in)
      retry
    else
      raise e
    end
  end
end