Class: CloudFlock::Target::Servers::Platform
- Inherits:
-
Object
- Object
- CloudFlock::Target::Servers::Platform
- Defined in:
- lib/cloudflock/target/servers/platform.rb,
lib/cloudflock/error.rb
Overview
Public: Serves as a small class to easily map host specifications to Image and Flavor IDs in Rackspace Cloud.
Examples
# Build platform data for a given CPE object
platform = Platform.new(cpe)
platform.image_id
# => Fixnum
Defined Under Namespace
Classes: MapUndefined, V1, V2, ValueError
Instance Attribute Summary collapse
-
#managed ⇒ Object
Public: Gets/sets whether the target platform will be managed.
-
#rack_connect ⇒ Object
Public: Gets/sets whether the target platform will use Rack Connect.
Instance Method Summary collapse
-
#build_maps ⇒ Object
Internal: Build image and flavor maps.
-
#build_recommendation(args) ⇒ Object
Public: Give a recommendation for a Flavor ID and an Image ID which should suffice for a migration target host.
-
#get_target_by_symbol(symbol, value) ⇒ Object
Public: Iterate through TARGET_LIST until a suitably large flavor_id is found, then return the appropriate index.
-
#image ⇒ Object
Public: Return the Image ID to be used based on whether the account is managed, and the platform used.
-
#initialize(cpe) ⇒ Platform
constructor
Public: Initialize the Platform object.
-
#to_s ⇒ Object
Public: Generate a String of the platform’s name/version suitable for display.
Constructor Details
#initialize(cpe) ⇒ Platform
Public: Initialize the Platform object.
cpe - CPE object from which to generate platform object.
Raises ArgumentError if anything but a CPE object was given. Raises KeyError if the CPE object doesn’t have a vendor or version defined.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cloudflock/target/servers/platform.rb', line 25 def initialize(cpe) raise ArgumentError unless cpe.kind_of?(CPE) raise KeyError if cpe.vendor.nil? or cpe.version.nil? @cpe = cpe @distro = cpe.vendor @product = cpe.product @version = cpe.version @managed = false @rack_connect = false build_maps end |
Instance Attribute Details
#managed ⇒ Object
Public: Gets/sets whether the target platform will be managed.
15 16 17 |
# File 'lib/cloudflock/target/servers/platform.rb', line 15 def managed @managed end |
#rack_connect ⇒ Object
Public: Gets/sets whether the target platform will use Rack Connect.
17 18 19 |
# File 'lib/cloudflock/target/servers/platform.rb', line 17 def rack_connect @rack_connect end |
Instance Method Details
#build_maps ⇒ Object
Internal: Build image and flavor maps
Returns nothing
95 96 97 98 |
# File 'lib/cloudflock/target/servers/platform.rb', line 95 def build_maps build_image_maps build_flavor_maps end |
#build_recommendation(args) ⇒ Object
Public: Give a recommendation for a Flavor ID and an Image ID which should suffice for a migration target host.
args - Hash containing information on which to base the recommendation:
:memory - Hash containing memory information:
:total - Total amount of memory allocated to the host
profiled.
:mem_used - Amount of RAM in use at the time of
profiling.
:swapping? - Boolean denoting whether the host was
swapping at the time of profiling.
:disk - Fixnum containing the amount of disk which appears to be
in use at the time of profiling.
Returns a Hash containing the Flavor ID and a String containing the reasoning for the decision.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cloudflock/target/servers/platform.rb', line 116 def build_recommendation(args) recommendation = {} target_mem = get_target_by_symbol(:mem, args[:memory][:mem_used]) target_mem += 1 if args[:memory][:swapping?] target_disk = get_target_by_symbol(:hdd, args[:disk]) if target_mem >= target_disk recommendation[:flavor] = target_mem recommendation[:flavor_reason] = "RAM usage" else recommendation[:flavor] = target_disk recommendation[:flavor_reason] = "Disk usage" end recommendation end |
#get_target_by_symbol(symbol, value) ⇒ Object
Public: Iterate through TARGET_LIST until a suitably large flavor_id is found, then return the appropriate index. If no entry can be found, raise ValueError.
symbol - A Symbol referencing the search target in TARGET_LIST. value - A Fixnum containing the amount of memory or disk space required.
Returns a Fixnum referencing the TARGET_LIST index. Raises ValueError if no symbol describes an appropriate target.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cloudflock/target/servers/platform.rb', line 78 def get_target_by_symbol(symbol, value) unless self.class.const_defined?(:FLAVOR_LIST) raise MapUndefined, "FLAVOR_LIST is undefined; maps appear unbuild." end self.class::FLAVOR_LIST.each_with_index do |target, idx| if target[symbol] > value return idx end end raise ValueError, "Unable to find a flavor matching #{symbol} #{value}" end |
#image ⇒ Object
Public: Return the Image ID to be used based on whether the account is managed, and the platform used
Returns the Image ID corresponding to the Platform object as a String
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cloudflock/target/servers/platform.rb', line 51 def image [:MANAGED_MAP, :UNMANAGED_MAP].each do |map| unless self.class.const_defined?(map) raise MapUndefined, "Const #{map} is undefined; maps appear unbuilt" end end map = @managed ? self.class::MANAGED_MAP : self.class::UNMANAGED_MAP distro = @distro.downcase.to_sym unless map[distro].nil? return map[distro][@version] unless map[distro][@version].nil? return map[distro]["*"] unless map[distro]["*"].nil? end nil end |
#to_s ⇒ Object
Public: Generate a String of the platform’s name/version suitable for display
Returns a String describing the Platform
43 44 45 |
# File 'lib/cloudflock/target/servers/platform.rb', line 43 def to_s "#{@distro.capitalize} #{@product.gsub(/_/, ' ').capitalize} #{@version}" end |