-
-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathhelpers.rb
125 lines (106 loc) · 3.89 KB
/
helpers.rb
1
2
3
4
5
6
7
8
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
45
46
47
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
117
118
119
120
121
122
123
124
125
module ElasticsearchCookbook
# Helper methods included by various providers and passed to the template engine
module Helpers
def find_es_resource(run_context, resource_type, resource)
resource_name = resource.name
instance_name = resource.instance_name
# if we are truly given a specific name to find
name_match = begin
find_exact_resource(run_context, resource_type, resource_name)
rescue
nil
end
return name_match if name_match
# first try by instance name attribute
name_instance = begin
find_instance_name_resource(run_context, resource_type, instance_name)
rescue
nil
end
return name_instance if name_instance
# otherwise try the defaults
name_default = begin
find_exact_resource(run_context, resource_type, 'default')
rescue
nil
end
name_elasticsearch = begin
find_exact_resource(run_context, resource_type, 'elasticsearch')
rescue
nil
end
# if we found exactly one default name that matched
return name_default if name_default && !name_elasticsearch
return name_elasticsearch if name_elasticsearch && !name_default
raise "Could not find exactly one #{resource_type} resource, and no specific resource or instance name was given"
end
# find exactly the resource name and type, but raise if there's multiple matches
# see https://github.com/chef/chef/blob/master/lib/chef/resource_collection/resource_set.rb#L80
def find_exact_resource(run_context, resource_type, resource_name)
rc = run_context.resource_collection
result = rc.find(resource_type => resource_name)
if result && result.is_a?(Array)
str = ''
str << "more than one #{resource_type} was found, "
str << 'you must specify a precise resource name'
raise str
end
result
end
def find_instance_name_resource(run_context, resource_type, instance_name)
results = []
rc = run_context.resource_collection
rc.each do |r|
next unless r.resource_name == resource_type && r.instance_name == instance_name
results << r
end
if !results.empty? && results.length > 1
str = ''
str << "more than one #{resource_type} was found, "
str << 'you must specify a precise instance name'
raise str
elsif !results.empty?
return results.first
end
nil
end
# proxy helper for chef sets JVM 8 proxy options
def get_java_proxy_arguments(enabled = true)
return '' unless enabled
require 'uri'
output = ''
if Chef::Config[:http_proxy] && !Chef::Config[:http_proxy].empty?
parsed_uri = URI(Chef::Config[:http_proxy])
output += "-Dhttp.proxyHost=#{parsed_uri.host} -Dhttp.proxyPort=#{parsed_uri.port} "
end
if Chef::Config[:https_proxy] && !Chef::Config[:https_proxy].empty?
parsed_uri = URI(Chef::Config[:https_proxy])
output += "-Dhttps.proxyHost=#{parsed_uri.host} -Dhttps.proxyPort=#{parsed_uri.port} "
end
output
rescue
''
end
end
def es_user
find_es_resource(Chef.run_context, :elasticsearch_user, new_resource)
end
class HashAndMashBlender
attr_accessor :target
def initialize(hash_or_mash_or_whatever)
self.target = hash_or_mash_or_whatever
end
def to_hash
target.each_with_object({}) do |(k, v), hsh|
hsh[k] =
if v.respond_to?(:to_hash)
self.class.new(v).to_hash
elsif v.respond_to?(:to_a)
v.to_a
else
v
end
end
end
end
end