Class: Fog::Brightbox::Compute::ImageSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/brightbox/compute/image_selector.rb

Overview

This selects the preferred image to use based on a number of conditions

Defined Under Namespace

Classes: NameSorter

Instance Method Summary collapse

Constructor Details

#initialize(images) ⇒ ImageSelector

Prepares a selector with the API output

Parameters:

  • images (Array<Hash>)

    hash matching API output for Fog::Brightbox::Compute#list_images



13
14
15
# File 'lib/fog/brightbox/compute/image_selector.rb', line 13

def initialize(images)
  @images = images
end

Instance Method Details

#latest_ubuntuString, NilClass

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

Returns:

  • (String)

    if image matches containing the identifier

  • (NilClass)

    if no image matches



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/brightbox/compute/image_selector.rb', line 28

def latest_ubuntu
  @images.select do |img|
    img["official"] == true &&
      img["status"] == "available" &&
      img["arch"] == "x86_64" &&
      img["name"] =~ /ubuntu/i
  end.sort do |a, b|
    # Reverse sort so "22.10" > "22.04"
    NameSorter.new(b["name"]).version <=> NameSorter.new(a["name"]).version
  end.first["id"]
rescue StandardError
  nil
end

#official_minimalString, NilClass

Returns current identifier of the smallest official image

Returns:

  • (String)

    if image matches containing the identifier

  • (NilClass)

    if no image matches



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/brightbox/compute/image_selector.rb', line 47

def official_minimal
  @images.select do |img|
    img["official"] == true &&
      img["status"] == "available" &&
      img["virtual_size"] != 0
  end.sort_by do |img|
    img["disk_size"]
  end.first["id"]
rescue StandardError
  nil
end