module Builderator::Control::Cleaner

Control logic for cleanup tasks

Public Class Methods

aborted?() click to toggle source
# File lib/builderator/control/cleaner.rb, line 96
def aborted?
  Config.cleaner.commit && @abort
end
commit?() click to toggle source
# File lib/builderator/control/cleaner.rb, line 92
def commit?
  Config.cleaner.commit && !@abort
end
configs!() { |:launch_configs, "Found #{length} Launch Configurations to remove"| ... } click to toggle source
# File lib/builderator/control/cleaner.rb, line 11
def configs!
  resources = Model::Cleaner.launch_configs.unused

  yield :launch_configs, "Found #{resources.length} Launch Configurations to remove"
  verify!(:launch_configs, 'Cleanup Launch Configurations', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.keys.sort.each do |id|
    yield :remove, "Launch Configuration #{id}", :red
    Model::Cleaner.launch_configs.resources.delete(id)

    next unless commit?
    Util.asg.delete_launch_configuration(:launch_configuration_name => id)
  end
rescue Aws::AutoScaling::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanerup Launch Configurations', e)
  yield(*exceptions.last.status)
end
exceptions() click to toggle source
# File lib/builderator/control/cleaner.rb, line 100
def exceptions
  @exceptions ||= []
end
images!() { |:images, "Found #{length} Images to remove"| ... } click to toggle source
# File lib/builderator/control/cleaner.rb, line 30
def images!
  resources = Model::Cleaner.images.unused

  yield :images, "Found #{resources.length} Images to remove"
  yield :grouping, "Groupd images by #{Config.cleaner.group_by}" if Config.cleaner.group_by
  yield :keep, "Keeping #{Config.cleaner.keep} images in each group"
  verify!(:images, 'Cleanup Images', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.values
    .sort { |a, b| a[:properties]['name'] <=> b[:properties]['name'] }
    .each do |image|
      yield :remove, "Image #{image[:id]} (#{image[:properties]['name']})", :red
      Model::Cleaner.images.resources.delete(image[:id])

      next unless commit?
      Util.ec2.deregister_image(:image_id => image[:id])
    end

rescue Aws::EC2::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanerup Images', e)
  yield(*exceptions.last.status)
end
snapshots!() { |:snapshots, "Found #{length} Snapshots to remove"| ... } click to toggle source
# File lib/builderator/control/cleaner.rb, line 54
def snapshots!
  resources = Model::Cleaner.snapshots.unused

  yield :snapshots, "Found #{resources.length} Snapshots to remove"
  verify!(:snapshots, 'Cleanup Snapshots', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.keys.sort.each do |id|
    yield :remove, "Snapshot #{id}", :red
    Model::Cleaner.snapshots.resources.delete(id)

    next unless commit?
    Util.ec2.delete_snapshot(:snapshot_id => id)
  end
rescue Aws::EC2::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanerup Snapshots', e)
  yield(*exceptions.last.status)
end
volumes!() { |:volumes, "Found #{length} Volumes to remove"| ... } click to toggle source
# File lib/builderator/control/cleaner.rb, line 73
def volumes!
  resources = Model::Cleaner.volumes.unused

  yield :volumes, "Found #{resources.length} Volumes to remove"
  verify!(:volumes, 'Cleanup Volumes', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.keys.sort.each do |id|
    yield :remove, "Volume #{id}", :red
    Model::Cleaner.volumes.resources.delete(id)

    next unless commit?
    Util.ec2.delete_volume(:volume_id => id)
  end
rescue Aws::EC2::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanerup Volumes', e)
  yield(*exceptions.last.status)
end

Private Class Methods

aborted!() { |:aborted, 'The following resources will NOT be removed because'\ ' safty constraints have not been met!', :yellow| ... } click to toggle source
# File lib/builderator/control/cleaner.rb, line 106
def aborted!
  yield :aborted, 'The following resources will NOT be removed because'\
    ' safty constraints have not been met!', :yellow if aborted?
end
verify!(resource_name, task, resources) { |:commit, 'This is not a dry-run. Press CTL-C to stop! '\ '(continuing in 5 seconds)', :red| ... } click to toggle source
# File lib/builderator/control/cleaner.rb, line 111
def verify!(resource_name, task, resources)
  if Config.cleaner.commit
    yield :commit, 'This is not a dry-run. Press CTL-C to stop! '\
                   '(continuing in 5 seconds)', :red

    sleep(5) ## Give $USER a few seconds to stop
  end

  return unless resources.size >= Config.cleaner.limits[resource_name]

  ex = Util::LimitException.new(resource_name, task, resources)
  yield(*ex.status)

  if Config.cleaner.force
    yield :force, 'Limits will be ignored. Press CTL-C to stop! '\
                  '(continuing in 5 seconds)', :red
    sleep(5) ## Give $USER a few seconds to stop

    return
  end

  exceptions << ex
  @abort = true
end