Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --group-by parameter to specify a unique tag value shared by a raid group #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions bin/ec2-rotate-volume-snapshots
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ $opts = {
:pattern => nil,
:by_tags => nil,
:dry_run => false,
:backoff_limit => 0
:backoff_limit => 0,
:group_by => nil
}

$time_periods = {
Expand All @@ -37,12 +38,16 @@ end
def rotate_em(snapshots)
# poor man's way to get a deep copy of our time_periods definition hash
periods = Marshal.load(Marshal.dump($time_periods))
if $opts[:group_by]
group_bys = []
group_by_key = $opts[:group_by]
end

snapshots.each do |snapshot|
time = Time.parse(snapshot[:aws_started_at])
snapshot_id = snapshot[:aws_id]
description = snapshot[:aws_description]
keep_reason = nil
snapshot[:keep_reason] = nil

if $opts[:pattern] && description !~ /#{$opts[:pattern]}/
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Skipping snapshot with description #{description}"
Expand All @@ -57,19 +62,33 @@ def rotate_em(snapshots)
time_string = time.strftime period_info[:format]
if Time.now - time < keep * period_info[:seconds]
if !keeping.key?(time_string) && keeping.length < keep
keep_reason = period
snapshot[:keep_reason] = period
keeping[time_string] = snapshot
if group_by_key
group_bys.push(snapshot[:tags][group_by_key])
end
end
break
end
end
end

snapshots.each do |snapshot|
if group_by_key && snapshot[:keep_reason].nil? && group_bys.include?(snapshot[:tags][group_by_key])
snapshot[:keep_reason] = "volume is associated with others by #{group_by_key}=#{snapshot[:tags][group_by_key]}"
end

if keep_reason.nil? && snapshot == snapshots.last && $opts[:keep_last]
keep_reason = 'last snapshot'
if snapshot[:keep_reason].nil? && snapshot == snapshots.last && $opts[:keep_last]
snapshot[:keep_reason] = 'last snapshot'
end
end

if !keep_reason.nil?
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Keeping for #{keep_reason}"
snapshots.each do |snapshot|
snapshot_id = snapshot[:aws_id]
description = snapshot[:aws_description]
time = Time.parse(snapshot[:aws_started_at])
if !snapshot[:keep_reason].nil?
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Keeping for #{snapshot[:keep_reason]}"
else
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Deleting"
begin
Expand Down Expand Up @@ -147,6 +166,10 @@ OptionParser.new do |o|
o.on("--dry-run", "Shows what would happen without doing anything") do |v|
$opts[:dry_run] = true
end

o.on("--group-by TAG", "Saves any volumes that have the same value for this tag key") do |v|
$opts[:group_by] = v
end
end.parse!

if $opts[:aws_access_key].nil? || $opts[:aws_secret_access_key].nil?
Expand Down