Module: Builderator::Control::Clean

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

Overview

Control logic for cleanup tasks

Class Method Summary collapse

Class Method Details

.aborted? ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/builderator/control/clean.rb', line 104

def aborted?
  @commit && @abort
end

.commit? ⇒ Boolean

Returns:

  • (Boolean)


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

def commit?
  @commit && !@abort
end

.configs! ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/builderator/control/clean.rb', line 24

def configs!
  resources = Model.launch_configs.unused(options)

  limit!(Model::LaunchConfigs, 'Cleanup Launch Configurations', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.each do |l, _|
    yield :remove, "Launch Configuration #{ l }", :red
    Model.launch_configs.resources.delete(l)

    next unless commit?
    Util.asg.delete_launch_configuration(:launch_configuration_name => l)
  end
rescue Aws::AutoScaling::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanup Launch Configurations', e)
  yield(*exceptions.last.status)
end

.exceptions ⇒ Object



108
109
110
# File 'lib/builderator/control/clean.rb', line 108

def exceptions
  @exceptions ||= []
end

.images! ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/builderator/control/clean.rb', line 42

def images!
  resources = Model.images.unused(options)

  limit!(Model::Images, 'Cleanup Images', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.each do |i, image|
    yield :remove, "Image #{ i } (#{ image[:properties]['name'] })", :red
    Model.images.resources.delete(i)

    next unless commit?
    Util.ec2.deregister_image(:image_id => i)
  end
rescue Aws::EC2::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanup Images', e)
  yield(*exceptions.last.status)
end

.limit? ⇒ Boolean

Returns:

  • (Boolean)


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

def limit?
  @limit
end

.options(arg = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/builderator/control/clean.rb', line 12

def options(arg = nil)
  return @options unless arg.is_a?(Hash)

  @options = arg.clone

  Util.region(@options.delete('region'))
  @commit = @options.delete('commit') { false }
  @limit = @options.delete('limit') { true }

  @options
end

.snapshots! ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/builderator/control/clean.rb', line 60

def snapshots!
  resources = Model.snapshots.unused

  limit!(Model::Snapshots, 'Cleanup Snapshots', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.each do |s, _|
    yield :remove, "Snapshot #{ s }", :red
    Model.snapshots.resources.delete(s)

    next unless commit?
    Util.ec2.delete_snapshot(:snapshot_id => s)
  end
rescue Aws::EC2::Errors::ServiceError => e
  exceptions << Util::AwsException.new('Cleanup Snapshots', e)
  yield(*exceptions.last.status)
end

.volumes! ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/builderator/control/clean.rb', line 78

def volumes!
  resources = Model.volumes.unused

  limit!(Model::Volumes, 'Cleanup Volumes', resources, &Proc.new)
  aborted!(&Proc.new)

  resources.each do |v, _|
    yield :remove, "Volume #{ v }", :red
    Model.volumes.resources.delete(v)

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