Class: ChefCore::TargetResolver
- Inherits:
-
Object
- Object
- ChefCore::TargetResolver
- Defined in:
- lib/chef_core/target_resolver.rb
Overview
This class will resolve a provided target host name to one or more TargetHost instances. The target host name passed in to the constructor can contain up to two ranges in the form [0-9] and [a-z]. Multiple target host names can be passed in asa comma-separated list.
Use resolver_inst.targets to get the expanded list of TargetHost instances.
Defined Under Namespace
Classes: InvalidRange, TargetResolverError, TooManyRanges, TooManyTargets, UnsupportedProtocol
Constant Summary collapse
- MAX_EXPANDED_TARGETS =
24
- SUPPORTED_PROTOCOLS =
%w{winrm ssh}.freeze
Instance Method Summary collapse
- #config_for_target(url) ⇒ Object
- #expand_targets(target) ⇒ Object
-
#initialize(target, default_protocol, conn_options, max_expanded_targets: MAX_EXPANDED_TARGETS) ⇒ TargetResolver
constructor
A new instance of TargetResolver.
-
#make_credentials(inline_user, inline_password) ⇒ Object
Merge the inline user/pass with the default user/pass, giving precedence to inline.
- #prefix_from_target(target) ⇒ Object
-
#targets ⇒ Object
Returns the list of targets as an array of TargetHost instances.
Constructor Details
#initialize(target, default_protocol, conn_options, max_expanded_targets: MAX_EXPANDED_TARGETS) ⇒ TargetResolver
Returns a new instance of TargetResolver.
32 33 34 35 36 37 38 39 40 |
# File 'lib/chef_core/target_resolver.rb', line 32 def initialize(target, default_protocol, , max_expanded_targets: MAX_EXPANDED_TARGETS) @max_expanded_targets = @default_proto = default_protocol @unparsed_target = target @split_targets = @unparsed_target.split(",") @conn_options = .dup @default_password = @conn_options.delete(:password) @default_user = @conn_options.delete(:user) end |
Instance Method Details
#config_for_target(url) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chef_core/target_resolver.rb', line 65 def config_for_target(url) prefix, target = prefix_from_target(url) inline_password = nil inline_user = nil host = target # Default greedy-scan of the regex means that # $2 will resolve to content after the final "@" # URL credentials will take precedence over the default :user # in @conn_opts if target =~ /(.*)@(.*)/ inline_credentials = $1 host = $2 # We'll use a non-greedy match to grab everthinmg up to the first ':' # as username if there is no :, credentials is just the username if inline_credentials =~ /(.+?):(.*)/ inline_user = $1 inline_password = $2 else inline_user = inline_credentials end end user, password = make_credentials(inline_user, inline_password) { url: "#{prefix}#{host}", user: user, password: password } end |
#expand_targets(target) ⇒ Object
119 120 121 122 |
# File 'lib/chef_core/target_resolver.rb', line 119 def (target) @current_target = target # Hold onto this for error reporting do_parse([target.downcase]) end |
#make_credentials(inline_user, inline_password) ⇒ Object
Merge the inline user/pass with the default user/pass, giving precedence to inline.
95 96 97 98 99 100 101 |
# File 'lib/chef_core/target_resolver.rb', line 95 def make_credentials(inline_user, inline_password) user = inline_user || @default_user user = nil if user && user.empty? password = (inline_password || @default_password) password = nil if password && password.empty? [user, password] end |
#prefix_from_target(target) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/chef_core/target_resolver.rb', line 103 def prefix_from_target(target) if target =~ %r{^(.+?)://(.*)} # We'll store the existing prefix to avoid it interfering # with the check further below. if SUPPORTED_PROTOCOLS.include? $1.downcase prefix = "#{$1}://" target = $2 else raise UnsupportedProtocol.new($1) end else prefix = "#{@default_proto}://" end [prefix, target] end |
#targets ⇒ Object
Returns the list of targets as an array of TargetHost instances
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/chef_core/target_resolver.rb', line 43 def targets return @targets unless @targets.nil? = [] @split_targets.each do |target| (target).each { |t| << t } end # Apply max_expanded_targets to the total list of resolved # targets, since multiple targets can be comma-separated. # Limiting it only in the expression resolver could still # yield more than the maximum. if .length > @max_expanded_targets raise TooManyTargets.new(.length, @max_expanded_targets) end @targets = .map do |url| config = @conn_options.merge(config_for_target(url)) TargetHost.new(config.delete(:url), config) end end |