Class: RightScale::CloudFactory
- Includes:
- RightSupport::Ruby::EasySingleton
- Defined in:
- lib/clouds/cloud_factory.rb
Overview
Singleton for registering and instantiating clouds.
Defined Under Namespace
Classes: UnknownCloud
Constant Summary collapse
- UNKNOWN_CLOUD_NAME =
the unknown cloud is used to automatically detect current instance’s cloud
:unknown
Class Method Summary collapse
-
.normalize_cloud_name(cloud_name) ⇒ Object
Normalizes a cloud name to ensure all variants are resolvable.
Instance Method Summary collapse
-
#create(cloud_name, options) ⇒ Object
Factory method for dynamic metadata types.
-
#default_cloud_name(value = nil) ⇒ Object
Setter/getter for the default cloud name.
-
#detect_cloud(options) ⇒ Object
Attempts to detect the current instance’s cloud by instantiating the various known clouds and running their detection methods.
-
#register(cloud_names, cloud_script_path) ⇒ Object
Registry method for a dynamic metadata type.
-
#registered_script_path(cloud_name) ⇒ Object
Gets the path to the script describing a cloud.
-
#reset_registry ⇒ Object
Resets the global cloud registry (to ensure a clean reload of cloud names).
Class Method Details
.normalize_cloud_name(cloud_name) ⇒ Object
Normalizes a cloud name to ensure all variants are resolvable.
Parameters
- cloud_name(String)
-
cloud name
Return
- result(String)
-
normalized cloud name
165 166 167 |
# File 'lib/clouds/cloud_factory.rb', line 165 def self.normalize_cloud_name(cloud_name) return cloud_name.to_s.strip.downcase end |
Instance Method Details
#create(cloud_name, options) ⇒ Object
Factory method for dynamic metadata types.
Parameters
- cloud(String)
-
a registered_type cloud name
- options(Hash)
-
options for creation
Return
- result(Object)
-
new instance of registered_type metadata type
Raise
- UnknownCloud
-
on error
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/clouds/cloud_factory.rb', line 89 def create(cloud_name, ) raise ArgumentError.new("cloud_name is required") if cloud_name.to_s.empty? raise ArgumentError.new("options[:logger] is required") unless logger = [:logger] raise UnknownCloud.new("No cloud definitions available.") unless @names_to_script_paths cloud_name = cloud_name.to_sym cloud_name = default_cloud_name if UNKNOWN_CLOUD_NAME == cloud_name if UNKNOWN_CLOUD_NAME == cloud_name # persist default cloud name after successful detection. cloud = detect_cloud() raise UnknownCloud.new("Unable to determine a default cloud") unless cloud default_cloud_name(cloud.name) return cloud end cloud_script_path = registered_script_path(cloud_name) = .dup [:name] ||= cloud_name.to_s [:script_path] = cloud_script_path cloud = Cloud.new() text = File.read(cloud_script_path) cloud.instance_eval(text) cloud.abbreviation(cloud_name) unless cloud.abbreviation extend_cloud_by_scripts(cloud, logger) # finalize defaults only after all cloud definitions have been evaluated # by the new cloud object. cloud. return cloud end |
#default_cloud_name(value = nil) ⇒ Object
Setter/getter for the default cloud name. This currently relies on a ‘cloud file’ which must be present in an expected RightScale location.
Parameters
- value(String|Token)
-
default cloud name or nil
Return
- result(String)
-
default cloud name or nil
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/clouds/cloud_factory.rb', line 126 def default_cloud_name(value = nil) cloud_file_path = RightScale::AgentConfig.cloud_file_path if value parent_dir = File.dirname(cloud_file_path) FileUtils.mkdir_p(parent_dir) unless File.directory?(parent_dir) File.open(cloud_file_path, "w") { |f| f.write(value.to_s) } else value = File.read(cloud_file_path).strip if File.file?(cloud_file_path) end value.to_s.empty? ? UNKNOWN_CLOUD_NAME : value end |
#detect_cloud(options) ⇒ Object
Attempts to detect the current instance’s cloud by instantiating the various known clouds and running their detection methods.
Parameters
- options(Hash)
-
options for creation or empty
Return
- cloud(Cloud)
-
detected cloud or nil
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/clouds/cloud_factory.rb', line 146 def detect_cloud() @names_to_script_paths.each_key do |cloud_name| begin cloud = create(cloud_name, ) return cloud if cloud.is_current_cloud? rescue Exception # ignore failures and proceed to detecting next cloud, if any. end end nil end |
#register(cloud_names, cloud_script_path) ⇒ Object
Registry method for a dynamic metadata type.
Parameters
- cloud_names(Array|String)
-
name of one or more clouds (which may include DEFAULT_CLOUD) that use the given type
- cloud_script_path(String)
-
path to script used to describe cloud on creation
Return
always true
45 46 47 48 49 50 |
# File 'lib/clouds/cloud_factory.rb', line 45 def register(cloud_names, cloud_script_path) # relies on each to split on newlines for strings and otherwise do each for collections. cloud_script_path = File.normalize_path(cloud_script_path) cloud_names.each { |cloud_name| registered_type(cloud_name, cloud_script_path) } true end |
#registered_script_path(cloud_name) ⇒ Object
Gets the path to the script describing a cloud.
Parameters
- cloud_name(String)
-
a registered_type cloud name
Return
- cloud_script_path(String)
-
path to script used to describe cloud on creation
Raise
- UnknownCloud
-
on error
72 73 74 75 76 |
# File 'lib/clouds/cloud_factory.rb', line 72 def registered_script_path(cloud_name) cloud_script_path = registered_type(cloud_name) raise UnknownCloud.new("Unknown cloud: #{cloud_name}") unless cloud_script_path return cloud_script_path end |
#reset_registry ⇒ Object
Resets the global cloud registry (to ensure a clean reload of cloud names).
Return
- result(Hash)
-
Hash of cloud names to script paths before reset
56 57 58 59 60 |
# File 'lib/clouds/cloud_factory.rb', line 56 def reset_registry result = @names_to_script_paths @names_to_script_paths = nil result end |