From 0daaaf8a8f897a3aa3fca5c392b20f4fa0dbd667 Mon Sep 17 00:00:00 2001 From: Bogdan Katynski Date: Thu, 13 Jul 2017 16:45:28 +0100 Subject: [PATCH 1/3] Add support for attribute-driven entries This commit adds the default recipe which creates hosts file entries based on a node attribute. This new functionality can simplify the use of the hosts file cookbook in some cases because it makes managing the hosts file possible with just data. --- attributes/default.rb | 15 +++++++ recipes/default.rb | 34 +++++++++++++++ spec/unit/default_spec.rb | 92 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 recipes/default.rb create mode 100644 spec/unit/default_spec.rb diff --git a/attributes/default.rb b/attributes/default.rb index a6a97079..5e4de8e2 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -5,6 +5,7 @@ # # Copyright:: 2012-2013, Seth Vargo # Copyright:: 2012, CustomInk, LCC +# Copyright:: 2017, Workday, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -20,3 +21,17 @@ # default['hostsfile']['path'] = nil + +# Weather to create an entry for the FQDN in /etc/hosts +default['hostsfile']['add_fqdn'] = false + +# Entries to be added to /etc/hosts defined in the form: +# +# '1.2.3.4' => { +# hostname: 'test.example.com', +# unique: true/false, +# ... +# } +# +# The entries can use any attribute supported by the hostsfile_entry resource +default['hostsfile']['entries'] = {} diff --git a/recipes/default.rb b/recipes/default.rb new file mode 100644 index 00000000..14a6bdb5 --- /dev/null +++ b/recipes/default.rb @@ -0,0 +1,34 @@ +# +# rubocop:disable AsciiComments +# +# Author:: Bogdan Katyński +# Cookbook Name:: hostsfile +# Recipe:: default +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# rubocop:enable AsciiComments + +# Create a mapping for the FQDN +hostsfile_entry node['ipaddress'] do + hostname node['fqdn'] + action :create + unique true + only_if { node['hostsfile']['add_fqdn'] } +end + +node['hostsfile']['entries'].each do |ip, attrs| + hostsfile_entry ip do + attrs.each { |a, v| send(a, v) } + end +end diff --git a/spec/unit/default_spec.rb b/spec/unit/default_spec.rb new file mode 100644 index 00000000..6e2d1305 --- /dev/null +++ b/spec/unit/default_spec.rb @@ -0,0 +1,92 @@ +# +# rubocop:disable AsciiComments +# +# Author:: Bogdan Katyński +# Cookbook Name:: hostsfile +# Spec:: default +# +# Copyright:: 2017, Bogdan Katyński +# Copyright:: 2017, Workday, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# rubocop:enable AsciiComments +# + +require 'spec_helper' + +describe 'hostsfile::default' do + let(:runner) { ChefSpec::SoloRunner.new } + let(:node) { chef_run.node } + let(:chef_run) { runner.converge(described_recipe) } + + context 'When all attributes are default' do + it 'does not update /etc/hosts' do + expect(chef_run).not_to create_hostsfile_entry(node['ipaddress']) + end + end + + context 'When add_fqdn attr is true' do + let(:runner) do + ChefSpec::SoloRunner.new do |node| + node.override['hostsfile']['add_fqdn'] = true + end + end + it 'creates a mapping for the fqdn in /etc/hosts' do + expect(chef_run).to create_hostsfile_entry(node['ipaddress']) + .with(unique: true, hostname: node['fqdn']) + end + end + + context 'when there are hostsfile entries in the node attribute' do + let(:entries) do + { + '1.2.3.4' => { + hostname: 'host1.some.test.domain', + unique: false + }, + '2.3.4.5' => { + hostname: 'host2.some.test.domain', + unique: true, + aliases: %w(alias1.some.test.domain alias2.some.test.domain) + } + } + end + let(:runner) do + ChefSpec::SoloRunner.new do |node| + node.override['hostsfile']['entries'] = entries + end + end + + it 'creates mappings for all entries in /etc/hosts' do + entries.each do |ip, attrs| + expect(chef_run).to create_hostsfile_entry(ip).with(attrs) + end + end + + context 'when there is a hostfile entry with an unsupported attribute' do + let(:entries) do + { + '1.2.3.4' => { + hostname: 'host1.some.test.domain', + action: :unsupported_action + } + } + end + + it 'raises an error' do + expect { chef_run }.to raise_error(Chef::Exceptions::ValidationFailed) + end + end + end +end From 11e599da2fecb85e7cb14c1e78a5086df2d9a39c Mon Sep 17 00:00:00 2001 From: bby-bishopclark <30503374+bby-bishopclark@users.noreply.github.com> Date: Wed, 17 Oct 2018 08:54:47 -0700 Subject: [PATCH 2/3] typo we're not meteorologists --- attributes/default.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/attributes/default.rb b/attributes/default.rb index 5e4de8e2..e65185af 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -22,7 +22,7 @@ default['hostsfile']['path'] = nil -# Weather to create an entry for the FQDN in /etc/hosts +# Whether to create an entry for the FQDN in /etc/hosts default['hostsfile']['add_fqdn'] = false # Entries to be added to /etc/hosts defined in the form: From b6073f7a3db8268f60ef0225f80636fba6bd2e1f Mon Sep 17 00:00:00 2001 From: Bogdan Katynski Date: Thu, 20 Dec 2018 23:58:40 +0000 Subject: [PATCH 3/3] Fix linter warnings --- recipes/default.rb | 3 --- spec/unit/default_spec.rb | 14 +++++--------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/recipes/default.rb b/recipes/default.rb index 14a6bdb5..fe3615f8 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -1,6 +1,4 @@ # -# rubocop:disable AsciiComments -# # Author:: Bogdan Katyński # Cookbook Name:: hostsfile # Recipe:: default @@ -17,7 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# rubocop:enable AsciiComments # Create a mapping for the FQDN hostsfile_entry node['ipaddress'] do diff --git a/spec/unit/default_spec.rb b/spec/unit/default_spec.rb index 6e2d1305..cb26a7ce 100644 --- a/spec/unit/default_spec.rb +++ b/spec/unit/default_spec.rb @@ -1,6 +1,4 @@ # -# rubocop:disable AsciiComments -# # Author:: Bogdan Katyński # Cookbook Name:: hostsfile # Spec:: default @@ -20,8 +18,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# rubocop:enable AsciiComments -# require 'spec_helper' @@ -53,13 +49,13 @@ { '1.2.3.4' => { hostname: 'host1.some.test.domain', - unique: false + unique: false, }, '2.3.4.5' => { hostname: 'host2.some.test.domain', unique: true, - aliases: %w(alias1.some.test.domain alias2.some.test.domain) - } + aliases: %w(alias1.some.test.domain alias2.some.test.domain), + }, } end let(:runner) do @@ -79,8 +75,8 @@ { '1.2.3.4' => { hostname: 'host1.some.test.domain', - action: :unsupported_action - } + action: :unsupported_action, + }, } end