Skip to content

Commit

Permalink
Don't assume ubuntu if there is no platform name
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeiser committed Feb 12, 2016
1 parent a5804f1 commit 113195b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

# Specify your gem"s dependencies in kitchen-ec2.gemspec
gemspec
gem "test-kitchen", :path => "../test-kitchen"
gem "test-kitchen"
gem "winrm-transport"
gem "winrm-fs"

Expand Down
16 changes: 16 additions & 0 deletions lib/kitchen/driver/aws/standard_platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ def sort_images(images)
# P1: We prefer the latest version over anything else
sort_by_version(images)
end

def show_returned_images(images)
if images.empty?
driver.error("Search returned 0 images.")
else
driver.debug("Search returned #{images.size} images:")
images.each do |image|
platform = self.class.from_image(driver, image)
if platform
driver.debug("- #{image.name}: Detected #{platform}. #{driver.image_info(image)}")
else
driver.debug("- #{image.name}: No platform detected. #{driver.image_info(image)}")
end
end
end
end
end
end
end
Expand Down
56 changes: 29 additions & 27 deletions lib/kitchen/driver/aws/standard_platform/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,7 @@ def self.from_image(driver, image)
end
end

private

def windows_name_filter
major, revision, service_pack = windows_version_parts

case revision
when nil
revision_strings = ["", "R*_"]
when 0
revision_strings = [""]
else
revision_strings = ["R#{revision}_"]
end

case service_pack
when nil
revision_strings = revision_strings.flat_map { |r| ["#{r}RTM", "#{r}SP*"] }
when 0
revision_strings = revision_strings.map { |r| "#{r}RTM" }
else
revision_strings = revision_strings.map { |r| "#{r}SP#{service_pack}" }
end

revision_strings.map do |r|
"Windows_Server-#{major || "*"}-#{r}-English-*-Base-*"
end
end
protected

# Turn windows version into [ major, revision, service_pack ]
#
Expand Down Expand Up @@ -129,6 +103,34 @@ def windows_version_parts

[major, revision, service_pack]
end

private

def windows_name_filter
major, revision, service_pack = windows_version_parts

case revision
when nil
revision_strings = ["", "R*_"]
when 0
revision_strings = [""]
else
revision_strings = ["R#{revision}_"]
end

case service_pack
when nil
revision_strings = revision_strings.flat_map { |r| ["#{r}RTM", "#{r}SP*"] }
when 0
revision_strings = revision_strings.map { |r| "#{r}RTM" }
else
revision_strings = revision_strings.map { |r| "#{r}SP#{service_pack}" }
end

revision_strings.map do |r|
"Windows_Server-#{major || "*"}-#{r}-English-*-Base-*"
end
end
end
end
end
Expand Down
57 changes: 30 additions & 27 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class Ec2 < Kitchen::Driver::Base # rubocop:disable Metrics/ClassLength
default_config :price, nil
default_config :retryable_tries, 60
default_config :retryable_sleep, 5
default_config :aws_access_key_id, ENV["AWS_SSH_KEY_ID"]
default_config :aws_access_key_id, nil
default_config :aws_secret_access_key, nil
default_config :aws_session_token, nil
default_config :aws_ssh_key_id, nil
default_config :aws_ssh_key_id, ENV["AWS_SSH_KEY_ID"]
default_config :image_id do |driver|
driver.default_ami
end
Expand Down Expand Up @@ -253,10 +253,16 @@ def image
end

def default_instance_type
@instance_type ||= begin
@instance_type ||= config[:flavor_id] || begin
# We default to the free tier (t2.micro for hvm, t1.micro for paravirtual)
config[:flavor_id] ||
((image && image.virtualization_type == "hvm") ? "t2.micro" : "t1.micro")
if image && image.virtualization_type == "hvm"
info("instance_type not specified. Using free tier t2.micro instance ...")
"t2.micro"
else
info("instance_type not specified. Using free tier t1.micro instance since" \
" image is paravirtual (pick an hvm image to use the superior t2.micro!) ...")
"t1.micro"
end
end
end

Expand All @@ -271,24 +277,17 @@ def desired_platform
if platform
debug("platform name #{instance.platform.name} appears to be a standard platform." \
" Searching for #{platform} ...")
else
debug("platform name #{instance.platform.name} does not have a valid os." \
" Searching for latest stable ubuntu ...")
platform = Aws::StandardPlatform.from_platform_string(self, "ubuntu")
end
platform
end
end

def default_ami
@default_ami ||= begin
image_search = config[:image_search] || desired_platform.image_search
image = desired_platform.find_image(image_search)
if !image
error("No image found for #{instance.name} search criteria #{image_search}")
raise
end
image
search_platform = desired_platform ||
Aws::StandardPlatform.from_platform_string(self, "ubuntu")
image_search = config[:image_search] || search_platform.image_search
search_platform.find_image(image_search)
end
end

Expand Down Expand Up @@ -539,23 +538,27 @@ def default_windows_user_data

def show_chosen_image
# Print some debug stuff
root_device = image.block_device_mappings.
find { |b| b.device_name == image.root_device_name }
debug("Image for #{instance.name}: #{image.name}." \
" Architecture: #{image.architecture}," \
" Virtualization: #{image.virtualization_type}," \
" Storage: #{image.root_device_type}" \
"#{root_device && root_device.ebs ? " #{root_device.ebs.volume_type}" : ""}," \
" Created: #{image.creation_date}")
debug("Image for #{instance.name}: #{image.name}. #{image_info(image)}")
if actual_platform
debug("Detected platform: #{actual_platform.name} version #{actual_platform.version}" \
" on #{actual_platform.architecture}. Instance Type: #{config[:instance_type]}," \
" Username: #{config[:username] || actual_platform.username}.")
info("Detected platform: #{actual_platform.name} version #{actual_platform.version}" \
" on #{actual_platform.architecture}. Instance Type: #{config[:instance_type]}." \
" Username: #{config[:username] || "#{actual_platform.username} (default)"}.")
else
debug("No platform detected for #{image.name}.")
end
end

def image_info(image)
root_device = image.block_device_mappings.
find { |b| b.device_name == image.root_device_name }
volume_type = " #{root_device.ebs.volume_type}" if root_device && root_device.ebs

" Architecture: #{image.architecture}," \
" Virtualization: #{image.virtualization_type}," \
" Storage: #{image.root_device_type}#{volume_type}," \
" Created: #{image.creation_date}"
end

end
end
end
2 changes: 1 addition & 1 deletion spec/kitchen/driver/ec2/image_selection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def new_instance(platform_name: "blarghle")

context "and platform.name is not a well known platform name" do
let(:image) { FakeImage.new(:name => "ubuntu") }
it "searches for an image id without using the standard filters" do
it "does not search for (or find) an image, and informs the user they need to set image_id" do
expect(driver.ec2.resource).
to receive(:images).
with(:filters => [{ :name => "name", :values => %w[SuperImage] }]).
Expand Down

0 comments on commit 113195b

Please sign in to comment.