/
helpers.rb
69 lines (63 loc) · 2.06 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
module Dhcp
#
# Helper methods that are used in multiple providers
#
module Helpers
#
# Determine if the resource matches this resource's type
#
# @param resource the resource to check
#
# @return [TrueClass, FalseClass] wether or not the resource matches
#
def resource_match?(resource)
# Check for >= Chef 12
return true if Gem::Version.new(Chef::VERSION) >= Gem::Version.new('12.0.0') && resource.declared_type == new_resource.declared_type
# Check for <= Chef 11
return true if Gem::Version.new(Chef::VERSION) < Gem::Version.new('12.0.0') && resource.resource_name == new_resource.resource_name
false
end
#
# List of files to include in list.conf for a collection of subconfigs (e.g. hosts, subnets, groups)
#
# @param [String] sub_dir - The subconfig directory for the config
#
# @return [Array<String>] An array of config files for the subconfig
def includes(sub_dir)
with_run_context :root do
run_context.resource_collection.map do |resource|
next unless resource_match? resource
next unless resource.action == :add || resource.action.include?(:add)
"#{resource.conf_dir}/#{sub_dir}/#{resource.name}.conf"
end.compact
end
end
#
# Defined resource for list.conf to include all subconfig files
#
# @param [String] sub_dir - The subconfig directory for the config
#
def write_include(sub_dir, name)
template "#{new_resource.conf_dir}/#{sub_dir}/list.conf #{name}" do
path "#{new_resource.conf_dir}/#{sub_dir}/list.conf"
cookbook 'dhcp'
source 'list.conf.erb'
owner 'root'
group 'root'
mode '0644'
variables(files: includes(sub_dir))
notifies :restart, "service[#{node['dhcp']['service_name']}]", :delayed
end
end
#
# Escape some special characters
#
# escape . -> - and / -> _
# so 1.1/3 -> 1-1_3
#
def escape(name)
name.tr('.', '-').tr('/', '_')
end
module_function :escape
end
end