Class: Kitchen::Driver::Aws::StandardPlatform

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/driver/aws/standard_platform.rb,
lib/kitchen/driver/aws/standard_platform/alma.rb,
lib/kitchen/driver/aws/standard_platform/rhel.rb,
lib/kitchen/driver/aws/standard_platform/macos.rb,
lib/kitchen/driver/aws/standard_platform/rocky.rb,
lib/kitchen/driver/aws/standard_platform/amazon.rb,
lib/kitchen/driver/aws/standard_platform/centos.rb,
lib/kitchen/driver/aws/standard_platform/debian.rb,
lib/kitchen/driver/aws/standard_platform/fedora.rb,
lib/kitchen/driver/aws/standard_platform/ubuntu.rb,
lib/kitchen/driver/aws/standard_platform/amazon2.rb,
lib/kitchen/driver/aws/standard_platform/freebsd.rb,
lib/kitchen/driver/aws/standard_platform/windows.rb,
lib/kitchen/driver/aws/standard_platform/amazon2023.rb

Overview

Lets you grab StandardPlatform objects that help search for official AMIs in your region and tell you useful tidbits like usernames.

To use these, set your platform name to a supported platform name like:

centos rhel fedora freebsd macos ubuntu windows

The implementation will select the latest matching version and AMI.

You can specify a version and optional architecture as well:

windows-2012r2-i386 centos-7

Useful reference for platform AMIs: https://alestic.com/2014/01/ec2-ssh-username/

Defined Under Namespace

Classes: Alma, Amazon, Amazon2, Amazon2023, Centos, Debian, El, Fedora, Freebsd, MacOS, Rocky, Ubuntu, Windows

Constant Summary collapse

SUPPORTED_ARCHITECTURES =

The list of supported architectures

%w{x86_64 i386 arm64}.freeze
EBS_VOLUME_TYPES =

The list of supported ebs volume types

%w{gp3 gp2}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver, name, version, architecture) ⇒ StandardPlatform

Create a new StandardPlatform object.

Parameters:

  • driver (Kitchen::Driver::Ec2)

    The driver.

  • name (String)

    The name of the platform (rhel, centos, etc.)

  • version (String)

    The version of the platform (7.1, 2008sp1, etc.)

  • architecture (String)

    The architecture (i386, x86_64, arm64)



52
53
54
55
56
57
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 52

def initialize(driver, name, version, architecture)
  @driver = driver
  @name = name
  @version = version
  @architecture = architecture
end

Instance Attribute Details

#architectureString (readonly)

The architecture of the platform, e.g. i386, x86_64

Returns:

  • (String)

See Also:



87
88
89
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 87

def architecture
  @architecture
end

#driverKitchen::Driver::Ec2 (readonly)

The driver.



64
65
66
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 64

def driver
  @driver
end

#nameString (readonly)

The name of the platform (e.g. rhel, centos, etc.)

Returns:

  • (String)


71
72
73
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 71

def name
  @name
end

#versionString (readonly)

The version of the platform (e.g. 7.1, 2008sp1, etc.)

Returns:

  • (String)


78
79
80
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 78

def version
  @version
end

Class Method Details

.from_image(driver, image) ⇒ Kitchen::Driver::Aws::StandardPlatform

Detect platform from an image.

Parameters:

Returns:



183
184
185
186
187
188
189
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 183

def self.from_image(driver, image)
  platforms.each_value do |platform|
    result = platform.from_image(driver, image)
    return result if result
  end
  nil
end

.from_platform_string(driver, platform_string) ⇒ Kitchen::Driver::Aws::StandardPlatform

Instantiate a platform from a platform name.

Parameters:

  • driver (Kitchen::Driver::Ec2)

    The driver.

  • platform_string (String)

    The platform string, e.g. "windows", "ubuntu-7.1", "centos-7-i386"

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 154

def self.from_platform_string(driver, platform_string)
  platform, version, architecture = parse_platform_string(platform_string)

  # A few platform names end in what looks like a version but is
  # really part of the distribution's name: "amazon2023" and "amazon2"
  # are Amazon Linux 2023 and Amazon Linux 2, not releases 2023 and 2
  # of "amazon" (which is the long-EOL Amazon Linux 1). Written with a
  # dash -- the spelling the documentation uses -- the split above
  # lands on the "amazon" platform searching for a release that never
  # existed, and no image matches. Fold the release back into the name
  # whenever the two together name a platform that is registered.
  if version && platforms["#{platform}#{version}"]
    platform = "#{platform}#{version}"
    version = nil
  end

  return unless platform && platforms[platform]

  platforms[platform].new(driver, platform, version, architecture)
end

.parse_platform_string(platform_string) ⇒ Array(String, String, String)

Split a platform string into its parts.

The trailing segment is only treated as an architecture when it is one of SUPPORTED_ARCHITECTURES; anything else stays part of the version, so that a typo surfaces as an unmatched version rather than being silently discarded.

Parameters:

  • platform_string (String)

    e.g. "centos-9-x86_64"

Returns:

  • (Array(String, String, String))

    the platform name, version and architecture, any of which except the name may be nil



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 201

def self.parse_platform_string(platform_string)
  platform, version = platform_string.split("-", 2)

  # If the right side is a valid architecture, use it as such
  # i.e. debian-i386 or windows-server-2012r2-i386
  if version && SUPPORTED_ARCHITECTURES.include?(version.split("-")[-1])
    # server-2012r2-i386 -> server-2012r2, -, i386
    version, _dash, architecture = version.rpartition("-")
    version = nil if version == ""
  end

  [platform, version, architecture]
end

.platformsArray<Kitchen::Driver::Aws::StandardPlatform>

The list of StandardPlatform objects. StandardPlatforms register themselves with this.



131
132
133
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 131

def self.platforms
  @platforms ||= {}
end

Instance Method Details

#find_image(image_search) ⇒ String?

Find the best matching image for the given image search.

The search hash is converted into EC2's filter format, and the results are ranked by #sort_images before the best match is taken.

Parameters:

  • image_search (Hash{String => String, Array<String>})

    EC2 image filters, keyed by filter name

Returns:

  • (String, nil)

    the image ID (e.g. "ami-213984723"), or nil when the search matched nothing



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 109

def find_image(image_search)
  driver.debug("Searching for images matching #{image_search} ...")
  # Convert to ec2 search format (pairs of name+values)
  filters = image_search.map do |key, value|
    { name: key.to_s, values: Array(value).map(&:to_s) }
  end

  # We prefer most recent first
  images = driver.ec2.resource.images(filters:)
  images = sort_images(images)
  show_returned_images(images)

  # Grab the best match
  images.first&.id
end

#to_sString

A human-readable description of this platform.

Version and architecture are omitted when unknown, so an unqualified platform reads as just "ubuntu" rather than "ubuntu ".

Returns:

  • (String)

    e.g. "ubuntu 24.04 x86_64"



141
142
143
# File 'lib/kitchen/driver/aws/standard_platform.rb', line 141

def to_s
  "#{name}#{version ? " #{version}" : ""}#{architecture ? " #{architecture}" : ""}"
end