Skip to content

Commit c5da749

Browse files
author
Jens Rosenboom
committed
Use an array as listen attribute instead of a hash
This PR amends the logic introduced in sous-chefs#378 to use an array as attribute instead of a hash, as this will simplify using this cookbook in other recipes. E.g., if a recipe wants httpd to listen on `addr:port`, it is much easier to just set node.set['apache2']['listen'] += ['addr:port'] instead of first having to check whether the listen hash does have the key `addr` already and then either add it or update the port list.
1 parent 138fbae commit c5da749

File tree

8 files changed

+24
-31
lines changed

8 files changed

+24
-31
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ These are general settings used in recipes and templates. Default
173173
values are noted.
174174

175175
* `node['apache']['version']` - Specifing 2.4 triggers apache 2.4 support. If the platform is known during our test to install 2.4 by default, it will be set to 2.4 for you. Otherwise it falls back to 2.2. This value should be specified as a string.
176-
* `node['apache']['listen']` - Hash of address and arrays of ports that httpd should listen on. Default is any address and port 80 (`{"*" => ["80"]}`).
176+
* `node['apache']['listen']` - Array of address:port combinations that httpd should listen on. Default is any address and port 80 (`["*:80"]`).
177177
* `node['apache']['contact']` - Value for ServerAdmin directive. Default "[email protected]".
178178
* `node['apache']['timeout']` - Value for the Timeout directive. Default is 300.
179179
* `node['apache']['keepalive']` - Value for the KeepAlive directive. Default is On.
@@ -681,9 +681,7 @@ create a basic role for web servers that provide both HTTP and HTTPS:
681681
)
682682
default_attributes(
683683
"apache" => {
684-
"listen" => {
685-
"*" => ["80", "443"]
686-
}
684+
"listen" => ["*:80", "*:443"]
687685
}
688686
)
689687
``````

attributes/default.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@
272272
if node['apache']['service_name'].nil?
273273
default['apache']['service_name'] = node['apache']['package']
274274
end
275-
default['apache']['listen']['*'] = ['80']
275+
default['apache']['listen'] = ['*:80']
276276
default['apache']['contact'] = '[email protected]'
277277
default['apache']['timeout'] = 300
278278
default['apache']['keepalive'] = 'On'

definitions/web_app.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# limitations under the License.
1818
#
1919

20-
define :web_app, :template => 'web_app.conf.erb', :local => false, :enable => true do
20+
define :web_app, :template => 'web_app.conf.erb', :local => false, :enable => true, :server_port => 80 do
2121
application_name = params[:name]
2222

2323
include_recipe 'apache2::default'

libraries/listen.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919
#
20-
require 'chef/mixin/deep_merge'
21-
2220
module Apache2
2321
# Provides method to convert node['apache']['listen_ports'] and node['apache']['listen_addresses'] into new node['apache']['listen']
2422
module Listen
2523
# @param node [Chef::Node] the chef node
2624
# @return [Hash] a hash indexed by address where the values are arrays of ports to listen to
27-
def listen_ports_by_address(node)
28-
Chef::Mixin::DeepMerge.deep_merge(Apache2::Listen.converted_listen_ports_and_addresses(node), node['apache']['listen'].to_hash)
25+
def merge_listen_attributes(node)
26+
(Apache2::Listen.converted_listen_ports_and_addresses(node) + node['apache']['listen']).uniq
2927
end
3028

31-
module_function :listen_ports_by_address
29+
module_function :merge_listen_attributes
3230

33-
private
31+
private_class_method
3432

3533
def self.converted_listen_ports_and_addresses(node)
36-
return {} unless node['apache']['listen_ports'] && node['apache']['listen_addresses']
34+
return [] unless node['apache']['listen_ports'] && node['apache']['listen_addresses']
3735
Chef::Log.warn "node['apache']['listen_ports'] and node['apache']['listen_addresses'] are deprecated in favor of node['apache']['listen']. Please adjust your cookbooks"
3836

39-
node['apache']['listen_addresses'].uniq.each_with_object({}) do |address, listen|
40-
listen[address] = node['apache']['listen_ports'].map(&:to_i)
37+
node['apache']['listen_addresses'].uniq.each_with_object([]) do |address, listen|
38+
node['apache']['listen_ports'].uniq.each do |port|
39+
listen << "#{address}:#{port}"
40+
end
4141
end
4242
end
4343
end

recipes/mod_ssl.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818
#
19-
unless node['apache']['listen'].values.any? { |ports| ports.include?(node['apache']['mod_ssl']['port']) }
20-
node['apache']['listen'].each_key do |address|
21-
node.default['apache']['listen'][address] = node['apache']['listen'][address] + [node['apache']['mod_ssl']['port']]
22-
end
19+
if node['apache']['listen'] == ['*:80']
20+
node.default['apache']['listen'] = ['*:80', "*:#{node['apache']['mod_ssl']['port']}"]
2321
end
2422

2523
include_recipe 'apache2::default'

templates/default/default-site.conf.erb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<% if node['apache']['version'] != '2.4' -%>
2+
NameVirtualHost *:<%= node['apache']['default_site_port'] %>
3+
<% end -%>
14
<VirtualHost *:<%= node['apache']['default_site_port'] %>>
25
ServerAdmin <%= node['apache']['contact'] %>
36

templates/default/ports.conf.erb

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

4-
<% Apache2::Listen.listen_ports_by_address(node).each do |address, ports| -%>
5-
<% ports.map(&:to_i).uniq.each do |port| -%>
6-
<% if address.empty? -%>
7-
Listen <%= port %>
8-
<% else -%>
9-
Listen <%= address %>:<%= port %>
10-
<% end -%>
11-
<% if node['apache']['version'] != "2.4" -%>
12-
NameVirtualHost *:<%= port %>
13-
<% end -%>
14-
<% end -%>
4+
<% Apache2::Listen.merge_listen_attributes(node).each do |addr| -%>
5+
Listen <%= addr %>
156
<% end -%>

templates/default/web_app.conf.erb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<VirtualHost *:<%= @params[:server_port] || Apache2::Listen.listen_ports_by_address(node).values.map(&:to_a).flatten.first %>>
1+
<% if node['apache']['version'] != '2.4' -%>
2+
NameVirtualHost *:<%= @params[:server_port] %>
3+
<% end -%>
4+
<VirtualHost *:<%= @params[:server_port] %>>
25
ServerName <%= @params[:server_name] %>
36
<% if @params[:server_aliases] -%>
47
ServerAlias <%= @params[:server_aliases].join " " %>

0 commit comments

Comments
 (0)