Class: Bosh::Director::Jobs::Helpers::StemcellsToDeletePicker

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/jobs/helpers/stemcells_to_delete_picker.rb

Instance Method Summary collapse

Constructor Details

#initialize(stemcell_manager) ⇒ StemcellsToDeletePicker

Returns a new instance of StemcellsToDeletePicker.



4
5
6
# File 'lib/bosh/director/jobs/helpers/stemcells_to_delete_picker.rb', line 4

def initialize(stemcell_manager)
  @stemcell_manager = stemcell_manager
end

Instance Method Details

#pick(stemcells_to_keep) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bosh/director/jobs/helpers/stemcells_to_delete_picker.rb', line 8

def pick(stemcells_to_keep)
  unused_stemcell_names_and_versions = @stemcell_manager
                                         .find_all_stemcells
                                         .select { |stemcell| stemcell['deployments'].empty? }

  unused_stemcells_grouped_by_name = unused_stemcell_names_and_versions.inject({}) do |h, stemcell|
    h[stemcell['name']] ||= []
    h[stemcell['name']] << stemcell
    h
  end

  stemcells_to_versions_to_delete = unused_stemcells_grouped_by_name.each_pair do |_, versions|
    versions.sort! do |sc1, sc2|
      Bosh::Common::Version::StemcellVersion.parse(sc1['version']) <=> Bosh::Common::Version::StemcellVersion.parse(sc2['version'])
    end
    versions.pop(stemcells_to_keep)
  end

  stemcells_to_versions_to_delete.values.flatten
end