Class: ChefApply::TargetResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_apply/target_resolver.rb

Defined Under Namespace

Classes: InvalidRange, TooManyRanges, TooManyTargets, UnsupportedProtocol

Constant Summary collapse

MAX_EXPANDED_TARGETS =
24

Instance Method Summary collapse

Constructor Details

#initialize(target, default_protocol, conn_options) ⇒ TargetResolver

Returns a new instance of TargetResolver.



25
26
27
28
29
30
31
32
# File 'lib/chef_apply/target_resolver.rb', line 25

def initialize(target, default_protocol, conn_options)
  @default_proto = default_protocol
  @unparsed_target = target
  @split_targets = @unparsed_target.split(",")
  @conn_options = conn_options.dup
  @default_password = @conn_options.delete(:password)
  @default_user = @conn_options.delete(:user)
end

Instance Method Details

#config_for_target(url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chef_apply/target_resolver.rb', line 48

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



102
103
104
105
# File 'lib/chef_apply/target_resolver.rb', line 102

def expand_targets(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.



78
79
80
81
82
83
84
# File 'lib/chef_apply/target_resolver.rb', line 78

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef_apply/target_resolver.rb', line 86

def prefix_from_target(target)
  if target =~ /^(.+?):\/\/(.*)/
    # We'll store the existing prefix to avoid it interfering
    # with the check further below.
    if ChefApply::Config::SUPPORTED_PROTOCOLS.include? $1.downcase
      prefix = "#{$1}://"
      target = $2
    else
      raise UnsupportedProtocol.new($1)
    end
  else
    prefix = "#{@default_proto}://"
  end
  [prefix, target]
end

#targetsObject

Returns the list of targets as an array of TargetHost instances, them to account for ranges embedded in the target name.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef_apply/target_resolver.rb', line 36

def targets
  return @targets unless @targets.nil?
  expanded_urls = []
  @split_targets.each do |target|
    expanded_urls = (expanded_urls | expand_targets(target))
  end
  @targets = expanded_urls.map do |url|
    config = @conn_options.merge(config_for_target(url))
    TargetHost.new(config.delete(:url), config)
  end
end