Class: Awshelper::CLI

Inherits:
Thor
  • Object
show all
Includes:
Ec2, Thor::Actions
Defined in:
lib/awshelper/cli.rb

Instance Method Summary collapse

Methods included from Ec2

#ami_id, #ec2, #find_snapshot_id, #instance_availability_zone, #instance_id, #local_ipv4

Instance Method Details

#ebs_cleanupObject



189
190
191
192
193
194
195
196
# File 'lib/awshelper/cli.rb', line 189

def ebs_cleanup()
 ec2.describe_volumes(:filters => { 'status' => 'available', 'size' => '8' }).each do |r|
   if r[:aws_size] == 8 and  r[:aws_status] == 'available' and r[:tags] == {} and  r[:snapshot_id] != nil and  r[:snapshot_id][0,5] == 'snap-' then
    log("Deleting unused volume #{r[:aws_id]} from snapshot #{r[:snapshot_id]}")
    ec2.delete_volume(r[:aws_id])
   end
 end
end

#snap(device, volume_id = nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/awshelper/cli.rb', line 93

def snap(device, volume_id=nil)
  vol = determine_volume(device, volume_id)
  snap_description = options[:description] if options[:description]
  snap_description = "Created by aws_helper(#{instance_id}/#{local_ipv4}) for #{ami_id} from #{vol[:aws_id]}" if !options[:description]
  snapshot = ec2.create_snapshot(vol[:aws_id],snap_description)
  log("Created snapshot of #{vol[:aws_id]} as #{snapshot[:aws_id]}")
end

#snap_email(to, from, email_server, subject = 'EBS Backups') ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/awshelper/cli.rb', line 150

def snap_email(to, from, email_server, subject='EBS Backups')
  rows = 20
  rows = options[:rows] if options[:rows]
  #owner = {}
  #owner = {:aws_owner => options[:owner]} if options[:owner]
  message = ""
  log("Report on snapshots")
  # ({ Name="start-time", Values="today in YYYY-MM-DD"})
  i = rows
  ec2.describe_snapshots().sort { |a,b| b[:aws_started_at] <=> a[:aws_started_at] }.each do |snapshot|
    if i >0
      if options[:owner].to_s == '' || snapshot[:owner_id].to_s == options[:owner].to_s
        message = message+"#{snapshot[:aws_id]} #{snapshot[:aws_volume_id]} #{snapshot[:aws_started_at]} #{snapshot[:aws_description]} #{snapshot[:aws_status]}\n"
        i = i-1
      end
    end
  end
  opts = {}
  opts[:server] = email_server
  opts[:from] = from
  opts[:from_alias] = 'EBS Backups'
  opts[:subject] = subject
  opts[:body] = message
  send_email(to,opts)
end

#snap_prune(device, volume_id = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/awshelper/cli.rb', line 115

def snap_prune(device, volume_id=nil)
  snapshots_to_keep = options[:snapshots_to_keep]
  vol = determine_volume(device, volume_id)
  old_snapshots = Array.new
  log("Checking for old snapshots")
  ec2.describe_snapshots.sort { |a,b| b[:aws_started_at] <=> a[:aws_started_at] }.each do |snapshot|
    if snapshot[:aws_volume_id] == vol[:aws_id]
      log("Found old snapshot #{snapshot[:aws_id]} (#{snapshot[:aws_volume_id]}) #{snapshot[:aws_started_at]}")
      old_snapshots << snapshot
    end
  end
  if old_snapshots.length > snapshots_to_keep
    old_snapshots[snapshots_to_keep, old_snapshots.length].each do |die|
      log("Deleting old snapshot #{die[:aws_id]}")
      ec2.delete_snapshot(die[:aws_id])
    end
  end
end