Sha256: 66f15e02b16fe7ca09f92609f7426da9f516f43b459f507273a84acf4a33ff23

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

module Fog
  module Brightbox
    class Compute
      #
      # This selects the preferred image to use based on a number of
      # conditions
      #
      class ImageSelector
        # Prepares a selector with the API output
        #
        # @param [Array<Hash>] images hash matching API output for {Fog::Brightbox::Compute#list_images}
        #
        def initialize(images)
          @images = images
        end

        # Returns current identifier of the latest version of Ubuntu
        #
        # The order of preference is:
        # * Only Official Brightbox images
        # * Only Ubuntu images
        # * Latest by name (alphanumeric sort)
        # * Latest by creation date
        #
        # @return [String] if image matches containing the identifier
        # @return [NilClass] if no image matches
        #
        def latest_ubuntu
          @images.select do |img|
            img["official"] == true &&
              img["arch"] == "i686" &&
              img["name"] =~ /ubuntu/i
          end.sort do |a, b|
            # Reverse sort so "raring" > "precise" and "13.10" > "13.04"
            b["name"].downcase <=> a["name"].downcase
          end.first["id"]
        rescue StandardError
          nil
        end

        # Returns current identifier of the smallest official image
        #
        # @return [String] if image matches containing the identifier
        # @return [NilClass] if no image matches
        #
        def official_minimal
          @images.select do |img|
            img["official"] == true &&
            img["virtual_size"] != 0
          end.sort_by do |img|
            img["disk_size"]
          end.first["id"]
        rescue StandardError
          nil
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fog-brightbox-1.0.0 lib/fog/brightbox/compute/image_selector.rb
fog-brightbox-1.0.0.rc2 lib/fog/brightbox/compute/image_selector.rb