Class: Bolt::Inventory::Target
- Inherits:
-
Object
- Object
- Bolt::Inventory::Target
- Defined in:
- lib/bolt/inventory/target.rb
Overview
This class represents the active state of a target within the inventory.
Constant Summary collapse
- ILLEGAL_CHARS =
Illegal characters that are not permitted in target names. These characters are delimiters for target and group names and allowing them would cause unexpected behavior.
/[\s,]/.freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#safe_name ⇒ Object
readonly
Returns the value of attribute safe_name.
-
#target_alias ⇒ Object
readonly
Returns the value of attribute target_alias.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #add_facts(new_facts = {}) ⇒ Object
- #config ⇒ Object
-
#facts ⇒ Object
rubocop:enable Naming/AccessorMethodName.
- #features ⇒ Object
- #group_cache ⇒ Object
- #host ⇒ Object
-
#initialize(target_data, inventory) ⇒ Target
constructor
A new instance of Target.
- #invalidate_config_cache! ⇒ Object
- #invalidate_group_cache! ⇒ Object
- #password ⇒ Object
- #plugin_hooks ⇒ Object
- #port ⇒ Object
-
#protocol ⇒ Object
For remote targets, protocol is the value of the URI scheme.
- #remote? ⇒ Boolean
- #resource(type, title) ⇒ Object
- #set_config(key_or_key_path, value) ⇒ Object
- #set_feature(feature, value = true) ⇒ Object
- #set_local_defaults ⇒ Object
-
#set_resource(resource) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
-
#set_var(var_hash) ⇒ Object
This method isn’t actually an accessor and we want the name to correspond to the Puppet function rubocop:disable Naming/AccessorMethodName.
- #to_s ⇒ Object
-
#transport ⇒ Object
For remote targets, the transport is always ‘remote’.
-
#transport_config ⇒ Object
We only want to look up transport config keys for the configured transport.
- #user ⇒ Object
-
#validate ⇒ Object
Validate the target.
-
#vars ⇒ Object
rubocop:enable Naming/AccessorMethodName.
Constructor Details
#initialize(target_data, inventory) ⇒ Target
Returns a new instance of Target.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bolt/inventory/target.rb', line 16 def initialize(target_data, inventory) unless target_data['name'] || target_data['uri'] raise Bolt::Inventory::ValidationError.new("Target must have either a name or uri", nil) end @logger = Bolt::Logger.logger(inventory) # If the target isn't mentioned by any groups, it won't have a uri or # name and we will use the target_name as both @uri = target_data['uri'] @uri_obj = self.class.parse_uri(@uri) # If the target has a name, use that as the safe name. Otherwise, turn # the uri into a safe name by omitting the password. if target_data['name'] @name = target_data['name'] @safe_name = target_data['name'] else @name = @uri @safe_name = @uri_obj.omit(:password).to_str.sub(%r{^//}, '') end if @name == 'localhost' default = { 'config' => { 'transport' => 'local' } } target_data = Bolt::Util.deep_merge(default, target_data) end @config = target_data['config'] || {} @vars = target_data['vars'] || {} @facts = target_data['facts'] || {} @features = target_data['features'] || Set.new @plugin_hooks = target_data['plugin_hooks'] || {} # When alias is specified in a plan, the key will be `target_alias`, when # alias is specified in inventory the key will be `alias`. @target_alias = target_data['target_alias'] || target_data['alias'] || [] @resources = {} @inventory = inventory validate end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/bolt/inventory/target.rb', line 9 def name @name end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
9 10 11 |
# File 'lib/bolt/inventory/target.rb', line 9 def resources @resources end |
#safe_name ⇒ Object (readonly)
Returns the value of attribute safe_name.
9 10 11 |
# File 'lib/bolt/inventory/target.rb', line 9 def safe_name @safe_name end |
#target_alias ⇒ Object (readonly)
Returns the value of attribute target_alias.
9 10 11 |
# File 'lib/bolt/inventory/target.rb', line 9 def target_alias @target_alias end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
9 10 11 |
# File 'lib/bolt/inventory/target.rb', line 9 def uri @uri end |
Class Method Details
.parse_uri(string) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/bolt/inventory/target.rb', line 273 def self.parse_uri(string) require 'addressable/uri' if string.nil? Addressable::URI.new # Forbid empty uri elsif string.empty? raise Bolt::ParseError, "Could not parse target URI: URI is empty string" elsif string =~ %r{^[^:]+://} Addressable::URI.parse(string) else # Initialize with an empty scheme to ensure we parse the hostname correctly Addressable::URI.parse("//#{string}") end rescue Addressable::URI::InvalidURIError => e raise Bolt::ParseError, "Could not parse target URI: #{e.}" end |
Instance Method Details
#add_facts(new_facts = {}) ⇒ Object
99 100 101 102 |
# File 'lib/bolt/inventory/target.rb', line 99 def add_facts(new_facts = {}) validate_fact_names(new_facts) @facts = Bolt::Util.deep_merge(@facts, new_facts) end |
#config ⇒ Object
242 243 244 |
# File 'lib/bolt/inventory/target.rb', line 242 def config Bolt::Util.deep_merge(group_cache['config'], @config) end |
#facts ⇒ Object
rubocop:enable Naming/AccessorMethodName
95 96 97 |
# File 'lib/bolt/inventory/target.rb', line 95 def facts Bolt::Util.deep_merge(group_cache['facts'], @facts) end |
#features ⇒ Object
104 105 106 |
# File 'lib/bolt/inventory/target.rb', line 104 def features group_cache['features'] + @features end |
#group_cache ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/bolt/inventory/target.rb', line 246 def group_cache if @group_cache.nil? group_data = @inventory.group_data_for(@name) unless group_data && group_data['config'] @logger.debug("Did not find config for #{self} in inventory") end group_data ||= { 'config' => {}, 'vars' => {}, 'facts' => {}, 'features' => Set.new, 'plugin_hooks' => {}, 'target_alias' => [] } @group_cache = group_data end @group_cache end |
#host ⇒ Object
184 185 186 |
# File 'lib/bolt/inventory/target.rb', line 184 def host @uri_obj.hostname || transport_config['host'] end |
#invalidate_config_cache! ⇒ Object
146 147 148 149 |
# File 'lib/bolt/inventory/target.rb', line 146 def invalidate_config_cache! @transport = nil @transport_config = nil end |
#invalidate_group_cache! ⇒ Object
140 141 142 143 144 |
# File 'lib/bolt/inventory/target.rb', line 140 def invalidate_group_cache! @group_cache = nil # The config cache depends on the group cache, so invalidate it as well invalidate_config_cache! end |
#password ⇒ Object
226 227 228 |
# File 'lib/bolt/inventory/target.rb', line 226 def password Addressable::URI.unencode_component(@uri_obj.password) || transport_config['password'] end |
#plugin_hooks ⇒ Object
120 121 122 123 124 |
# File 'lib/bolt/inventory/target.rb', line 120 def plugin_hooks # Merge plugin_hooks from the config file with any defined by the group # or assigned dynamically to the target @inventory.plugins.plugin_hooks.merge(group_cache['plugin_hooks']).merge(@plugin_hooks) end |
#port ⇒ Object
188 189 190 |
# File 'lib/bolt/inventory/target.rb', line 188 def port @uri_obj.port || transport_config['port'] end |
#protocol ⇒ Object
For remote targets, protocol is the value of the URI scheme. For non-remote targets, there is no protocol.
194 195 196 197 198 |
# File 'lib/bolt/inventory/target.rb', line 194 def protocol if remote? @uri_obj.scheme end end |
#remote? ⇒ Boolean
218 219 220 |
# File 'lib/bolt/inventory/target.rb', line 218 def remote? transport == 'remote' end |
#resource(type, title) ⇒ Object
116 117 118 |
# File 'lib/bolt/inventory/target.rb', line 116 def resource(type, title) resources[Bolt::ResourceInstance.format_reference(type, title)] end |
#set_config(key_or_key_path, value) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bolt/inventory/target.rb', line 126 def set_config(key_or_key_path, value) if key_or_key_path.empty? @config = value else *path, key = Array(key_or_key_path) location = path.inject(@config) do |working_object, p| working_object[p] ||= {} end location[key] = value end invalidate_config_cache! end |
#set_feature(feature, value = true) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/bolt/inventory/target.rb', line 108 def set_feature(feature, value = true) if value @features << feature else @features.delete(feature) end end |
#set_local_defaults ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bolt/inventory/target.rb', line 58 def set_local_defaults return if @set_local_default defaults = { 'local' => { 'interpreters' => { '.rb' => RbConfig.ruby } } } old_config = @config @config = Bolt::Util.deep_merge(defaults, @config) invalidate_config_cache! if old_config != @config set_feature('puppet-agent') @set_local_default = true end |
#set_resource(resource) ⇒ Object
rubocop:disable Naming/AccessorMethodName
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bolt/inventory/target.rb', line 71 def set_resource(resource) if (existing_resource = resources[resource.reference]) existing_resource.overwrite_state(resource.state) existing_resource.overwrite_desired_state(resource.desired_state) existing_resource.events = existing_resource.events + resource.events existing_resource else @resources[resource.reference] = resource end end |
#set_var(var_hash) ⇒ Object
This method isn’t actually an accessor and we want the name to correspond to the Puppet function rubocop:disable Naming/AccessorMethodName
90 91 92 |
# File 'lib/bolt/inventory/target.rb', line 90 def set_var(var_hash) @vars.merge!(var_hash) end |
#to_s ⇒ Object
269 270 271 |
# File 'lib/bolt/inventory/target.rb', line 269 def to_s @safe_name end |
#transport ⇒ Object
For remote targets, the transport is always ‘remote’. Otherwise, it will be either the URI scheme or set explicitly.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/bolt/inventory/target.rb', line 202 def transport if @transport.nil? config_transport = @config['transport'] || group_cache.dig('config', 'transport') || @inventory.transport @transport = if @uri_obj.scheme == 'remote' || config_transport == 'remote' 'remote' else @uri_obj.scheme || config_transport end end @transport end |
#transport_config ⇒ Object
We only want to look up transport config keys for the configured transport
232 233 234 235 236 237 238 239 240 |
# File 'lib/bolt/inventory/target.rb', line 232 def transport_config if @transport_config.nil? config = @inventory.config[transport] .merge(group_cache.dig('config', transport), @config[transport]) @transport_config = config end @transport_config end |
#user ⇒ Object
222 223 224 |
# File 'lib/bolt/inventory/target.rb', line 222 def user Addressable::URI.unencode_component(@uri_obj.user) || transport_config['user'] end |
#validate ⇒ Object
Validate the target. This implicitly also primes the group and config caches and resolves any config references in the target’s groups.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/bolt/inventory/target.rb', line 153 def validate unless name.ascii_only? raise Bolt::Inventory::ValidationError.new("Target name must be ASCII characters: #{@name}", nil) end if (illegal_char = @name.match(ILLEGAL_CHARS)) raise ValidationError.new("Illegal character '#{illegal_char}' in target name '#{@name}'", nil) end unless transport.nil? || Bolt::TRANSPORTS.include?(transport.to_sym) raise Bolt::UnknownTransportError.new(transport, uri) end validate_fact_names(facts) transport_config end |
#vars ⇒ Object
rubocop:enable Naming/AccessorMethodName
83 84 85 |
# File 'lib/bolt/inventory/target.rb', line 83 def vars group_cache['vars'].merge(@vars) end |