mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[brightbox|compute] Helper to get default image from API
* First will look for a configuration value brightbox_default_image * Second will query the API for latest Ubuntu (ideally LTS)
This commit is contained in:
parent
977f6e3b8b
commit
b221b97ba6
3 changed files with 61 additions and 1 deletions
|
@ -101,7 +101,24 @@ module Fog
|
||||||
request :update_server_group
|
request :update_server_group
|
||||||
request :update_user
|
request :update_user
|
||||||
|
|
||||||
|
module Shared
|
||||||
|
# Returns an identifier for the default image for use
|
||||||
|
#
|
||||||
|
# Currently tries to find the latest version Ubuntu LTS (i686) widening
|
||||||
|
# up to the latest, official version of Ubuntu available.
|
||||||
|
#
|
||||||
|
# Highly recommended that you actually select the image you want to run
|
||||||
|
# on your servers yourself!
|
||||||
|
#
|
||||||
|
# @return [String, nil]
|
||||||
|
def default_image
|
||||||
|
return @default_image_id unless @default_image_id.nil?
|
||||||
|
@default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Mock
|
class Mock
|
||||||
|
include Shared
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
|
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
|
||||||
|
@ -111,9 +128,15 @@ module Fog
|
||||||
def request(options)
|
def request(options)
|
||||||
raise "Not implemented"
|
raise "Not implemented"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def select_default_image
|
||||||
|
"img-mockd"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Real
|
class Real
|
||||||
|
include Shared
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
# Currently authentication and api endpoints are the same but may change
|
# Currently authentication and api endpoints are the same but may change
|
||||||
|
@ -180,6 +203,34 @@ module Fog
|
||||||
options[:headers] = headers
|
options[:headers] = headers
|
||||||
@connection.request(options)
|
@connection.request(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Queries the API and tries to select the most suitable official Image
|
||||||
|
# to use if the user chooses not to select their own.
|
||||||
|
def select_default_image
|
||||||
|
return @default_image_id unless @default_image_id.nil?
|
||||||
|
|
||||||
|
all_images = Fog::Compute[:brightbox].list_images
|
||||||
|
official_images = all_images.select {|img| img["official"] == true}
|
||||||
|
ubuntu_lts_images = official_images.select {|img| img["name"] =~ /Ubuntu.*LTS/}
|
||||||
|
ubuntu_lts_i686_images = ubuntu_lts_images.select {|img| img["arch"] == "i686"}
|
||||||
|
|
||||||
|
if ubuntu_lts_i686_images.empty?
|
||||||
|
# Accept other architectures
|
||||||
|
if ubuntu_lts_images.empty?
|
||||||
|
# Accept non-LTS versions of Ubuntu
|
||||||
|
unsorted_images = official_images.select {|img| img["name"] =~ /Ubuntu/}
|
||||||
|
else
|
||||||
|
unsorted_images = ubuntu_lts_images
|
||||||
|
end
|
||||||
|
else
|
||||||
|
unsorted_images = ubuntu_lts_i686_images
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get the latest and use it's ID for the default image
|
||||||
|
@default_image_id = unsorted_images.sort {|a,b| a["created_at"] <=> b["created_at"]}.first["id"]
|
||||||
|
rescue
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,7 @@ module Fog
|
||||||
attribute :server_type
|
attribute :server_type
|
||||||
|
|
||||||
def initialize(attributes={})
|
def initialize(attributes={})
|
||||||
self.image_id ||= 'img-wwgbb' # Ubuntu Lucid 10.04 server (i686)
|
self.image_id ||= Fog::Compute[:brightbox].default_image
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
9
tests/brightbox/compute_tests.rb
Normal file
9
tests/brightbox/compute_tests.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
Shindo.tests('Fog::Compute::Brightbox::Real', ['brightbox']) do
|
||||||
|
|
||||||
|
@bb = Fog::Compute::Brightbox::Real.new({})
|
||||||
|
|
||||||
|
tests("#respond_to? :default_image").returns(true) do
|
||||||
|
@bb.respond_to?(:default_image)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue