Module: Builderator::Control::Cleaner

Defined in:
lib/builderator/control/cleaner.rb

Overview

Control logic for cleanup tasks

Class Method Summary collapse

Class Method Details

.aborted?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/builderator/control/cleaner.rb', line 96

def aborted?
  Config.cleaner.commit && @abort
end

.commit?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/builderator/control/cleaner.rb', line 92

def commit?
  Config.cleaner.commit && !@abort
end

.configs!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

.exceptionsObject



100
101
102
# File 'lib/builderator/control/cleaner.rb', line 100

def exceptions
  @exceptions ||= []
end

.images!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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