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.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#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
- #options ⇒ Object
- #password ⇒ Object
- #plugin_hooks ⇒ Object
- #port ⇒ Object
-
#protocol ⇒ Object
For remote targets, protocol is the value of the URI scheme.
- #remote? ⇒ Boolean
- #set_config(key_or_key_path, value) ⇒ Object
- #set_feature(feature, value = true) ⇒ Object
-
#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.
-
#transport_config_cache ⇒ Object
Computing the transport config is expensive as it requires cloning the base config, so we cache the effective config.
- #user ⇒ Object
-
#validate ⇒ Object
Validate the target.
- #vars ⇒ Object
Constructor Details
#initialize(target_data, inventory) ⇒ Target
Returns a new instance of Target.
9 10 11 12 13 14 15 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 |
# File 'lib/bolt/inventory/target.rb', line 9 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 = Logging.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 @config = target_data['config'] || {} @vars = target_data['vars'] || {} @facts = target_data['facts'] || {} @features = target_data['features'] || Set.new @options = target_data['options'] || {} @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'] || [] @inventory = inventory validate end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/bolt/inventory/target.rb', line 7 def name @name end |
#safe_name ⇒ Object (readonly)
Returns the value of attribute safe_name.
7 8 9 |
# File 'lib/bolt/inventory/target.rb', line 7 def safe_name @safe_name end |
#target_alias ⇒ Object (readonly)
Returns the value of attribute target_alias.
7 8 9 |
# File 'lib/bolt/inventory/target.rb', line 7 def target_alias @target_alias end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/bolt/inventory/target.rb', line 7 def uri @uri end |
Class Method Details
.parse_uri(string) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/bolt/inventory/target.rb', line 225 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
62 63 64 |
# File 'lib/bolt/inventory/target.rb', line 62 def add_facts(new_facts = {}) @facts = Bolt::Util.deep_merge(@facts, new_facts) end |
#config ⇒ Object
188 189 190 |
# File 'lib/bolt/inventory/target.rb', line 188 def config Bolt::Util.deep_merge(group_cache['config'], @config) end |
#facts ⇒ Object
rubocop:enable Naming/AccessorMethodName
58 59 60 |
# File 'lib/bolt/inventory/target.rb', line 58 def facts Bolt::Util.deep_merge(group_cache['facts'], @facts) end |
#features ⇒ Object
66 67 68 |
# File 'lib/bolt/inventory/target.rb', line 66 def features group_cache['features'] + @features end |
#group_cache ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/bolt/inventory/target.rb', line 192 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, 'options' => {}, 'plugin_hooks' => {}, 'target_alias' => [] } # This should be handled by `get_targets` if @name == 'localhost' group_data = Bolt::Inventory::Inventory.localhost_defaults(group_data) end @group_cache = group_data end @group_cache end |
#host ⇒ Object
140 141 142 |
# File 'lib/bolt/inventory/target.rb', line 140 def host @uri_obj.hostname || transport_config['host'] end |
#invalidate_config_cache! ⇒ Object
103 104 105 |
# File 'lib/bolt/inventory/target.rb', line 103 def invalidate_config_cache! @transport_config_cache = nil end |
#invalidate_group_cache! ⇒ Object
97 98 99 100 101 |
# File 'lib/bolt/inventory/target.rb', line 97 def invalidate_group_cache! @group_cache = nil # The config cache depends on the group cache, so invalidate it as well invalidate_config_cache! end |
#options ⇒ Object
178 179 180 |
# File 'lib/bolt/inventory/target.rb', line 178 def transport_config.dup end |
#password ⇒ Object
174 175 176 |
# File 'lib/bolt/inventory/target.rb', line 174 def password Addressable::URI.unencode_component(@uri_obj.password) || transport_config['password'] end |
#plugin_hooks ⇒ Object
78 79 80 81 82 |
# File 'lib/bolt/inventory/target.rb', line 78 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
144 145 146 |
# File 'lib/bolt/inventory/target.rb', line 144 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.
150 151 152 153 154 |
# File 'lib/bolt/inventory/target.rb', line 150 def protocol if remote? @uri_obj.scheme end end |
#remote? ⇒ Boolean
166 167 168 |
# File 'lib/bolt/inventory/target.rb', line 166 def remote? @uri_obj.scheme == 'remote' || transport_config_cache['transport'] == 'remote' end |
#set_config(key_or_key_path, value) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bolt/inventory/target.rb', line 84 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
70 71 72 73 74 75 76 |
# File 'lib/bolt/inventory/target.rb', line 70 def set_feature(feature, value = true) if value @features << feature else @features.delete(feature) 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
53 54 55 |
# File 'lib/bolt/inventory/target.rb', line 53 def set_var(var_hash) @vars.merge!(var_hash) end |
#to_s ⇒ Object
221 222 223 |
# File 'lib/bolt/inventory/target.rb', line 221 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.
158 159 160 161 162 163 164 |
# File 'lib/bolt/inventory/target.rb', line 158 def transport if remote? 'remote' else @uri_obj.scheme || transport_config_cache['transport'] end end |
#transport_config ⇒ Object
We only want to look up transport config keys for the configured transport
184 185 186 |
# File 'lib/bolt/inventory/target.rb', line 184 def transport_config transport_config_cache['transports'][transport.to_sym] end |
#transport_config_cache ⇒ Object
Computing the transport config is expensive as it requires cloning the base config, so we cache the effective config
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/bolt/inventory/target.rb', line 109 def transport_config_cache if @transport_config_cache.nil? merged_config = Bolt::Util.deep_merge(group_cache['config'], @config) base_config = @inventory.config transport_data = Bolt::Util.deep_clone(base_config.transports) Bolt::Config.update_transport_hash(base_config.boltdir.path, transport_data, merged_config) transport = merged_config['transport'] || base_config.transport Bolt::TRANSPORTS.each do |name, impl| impl.validate(transport_data[name]) end @transport_config_cache = { 'transport' => transport, 'transports' => transport_data } end @transport_config_cache end |
#user ⇒ Object
170 171 172 |
# File 'lib/bolt/inventory/target.rb', line 170 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.
130 131 132 133 134 135 136 137 138 |
# File 'lib/bolt/inventory/target.rb', line 130 def validate unless name.ascii_only? raise Bolt::Inventory::ValidationError.new("Target name must be ASCII characters: #{@name}", nil) end unless transport.nil? || Bolt::TRANSPORTS.include?(transport.to_sym) raise Bolt::UnknownTransportError.new(transport, uri) end end |
#vars ⇒ Object
46 47 48 |
# File 'lib/bolt/inventory/target.rb', line 46 def vars group_cache['vars'].merge(@vars) end |