Skip to content

Commit 995a3e8

Browse files
committed
[COOK-3766] Directly use node attributes in templates where possible
This fixes an issue (and future potential issues) where users make a single change to an attribute that is passed to a template. In this case, the `ports.conf.erb` template was rendered by multiple recipes, but a Pull Request only added a required template variable to one of the many declarations where this template was generated by other recipes. This isn't something that will show up in a git diff, and it's also not something that tests can easily test. Therefore, the best solution is to move these quasi-calculated attributes out of the recipes and directly into the template itself.
1 parent f3daf0e commit 995a3e8

5 files changed

Lines changed: 6 additions & 14 deletions

File tree

recipes/default.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,9 @@
183183
end
184184

185185
template "#{node['apache']['dir']}/ports.conf" do
186-
source 'ports.conf.erb'
187-
owner 'root'
188-
group node['apache']['root_group']
189-
variables(
190-
:apache_listen_addresses => node['apache']['listen_addresses'].uniq,
191-
:apache_listen_ports => node['apache']['listen_ports'].map { |p| p.to_i }.uniq
192-
)
186+
source 'ports.conf.erb'
187+
owner 'root'
188+
group node['apache']['root_group']
193189
mode '0644'
194190
notifies :restart, 'service[apache2]'
195191
end

recipes/mod_ssl.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
node.set['apache']['listen_ports'] = node['apache']['listen_ports'] + ['443']
2121
end
2222

23-
ports = node['apache']['listen_ports']
24-
2523
if platform_family?('rhel', 'fedora', 'suse')
2624
package 'mod_ssl' do
2725
notifies :run, 'execute[generate-module-list]', :immediately
@@ -36,7 +34,6 @@
3634
template "#{node['apache']['dir']}/ports.conf" do
3735
source 'ports.conf.erb'
3836
mode '0644'
39-
variables(:apache_listen_ports => ports.map { |p| p.to_i }.uniq)
4037
notifies :restart, 'service[apache2]'
4138
end
4239

templates/default/ports.conf.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# This file was generated by Chef for <%= node['fqdn'] %>.
22
# Do NOT modify this file by hand!
33

4-
<% @apache_listen_ports.each do |port| -%>
5-
<% @apache_listen_addresses.each do |address| -%>
4+
<% node['apache']['listen_ports'].map(&:to_i).uniq.each do |port| -%>
5+
<% node['apache']['listen_addresses'].uniq.each do |address| -%>
66
Listen <%= address.length > 0 ? "#{address}:" : '' %><%= port %>
77
<% end -%>
88
NameVirtualHost *:<%= port %>

test/kitchen/cookbooks/apache2_test/recipes/mod_status_remote.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,4 @@
2222
template "#{node['apache']['dir']}/mods-available/status.conf" do
2323
source "status.conf.erb"
2424
mode "0644"
25-
variables({'remote_host' => node['apache_test']['remote_host_ip']})
2625
end

test/kitchen/cookbooks/apache2_test/templates/default/status.conf.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
Order deny,allow
55
Deny from all
66
Allow from localhost ip6-localhost
7-
Allow from <%= @remote_host %>
7+
Allow from <%= node['apache_test']['remote_host_ip'] %>
88
</Location>
99
</IfModule>

0 commit comments

Comments
 (0)