Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cookbook to current development practices #15

Merged
merged 3 commits into from
May 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 15 additions & 41 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,32 @@
---
driver_plugin: vagrant
driver_config:
require_chef_omnibus: true

platforms:
- name: centos-5.9
driver_config:
box: opscode-centos-5.9
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-5.9_provisionerless.box
run_list:

- name: centos-6.4
driver_config:
box: opscode-centos-6.4
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-6.4_provisionerless.box
run_list:

- name: debian-7.1
driver_config:
box: opscode-debian-7.1
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_debian-7.1.0_provisionerless.box
run_list:
- recipe[apt]

- name: ubuntu-10.04
driver_config:
box: opscode-ubuntu-10.04
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box
run_list:
- recipe[apt]
provisioner:
name: chef_zero

platforms:
- name: centos-5.11
- name: centos-6.6
- name: centos-7.1
- name: fedora-21
- name: debian-7.8
- name: ubuntu-12.04
driver_config:
box: opscode-ubuntu-12.04
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box
run_list:
- recipe[apt]

- name: ubuntu-13.04
driver_config:
box: opscode-ubuntu-13.04
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-13.04_provisionerless.box
run_list:
- recipe[apt]
- name: ubuntu-14.04

suites:
- name: default
run_list:
- recipe[minitest-handler]
- recipe[dnsmasq_test::default]
attributes:
dnsmasq:
enable_dns: true
enable_dhcp: true
dhcp_options: ['dhcp-host=01:23:ab:cd:01:02,larry,10.0.0.10']
dns:
server: '8.8.8.8'
dhcp:
dhcp-range: 'eth1,10.0.0.5,10.0.0.15,12h'
domain: 'test.lab'
tftp-root: '/var/lib/tftpboot'
enable-tftp: null
interface: 'eth1'
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
HashSyntax:
Enabled: false
LineLength:
Enabled: false
7 changes: 2 additions & 5 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
site :opscode
source 'https://supermarket.chef.io'

metadata

group :integration do
cookbook "minitest-handler"
cookbook "apt"
cookbook "hosts_file"
cookbook "dnsmasq_test", :path => "./test/cookbooks/dnsmasq_test"
cookbook 'dnsmasq_test', :path => './test/cookbooks/dnsmasq_test'
end
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

gem 'berkshelf', '~> 3.0'
gem 'chefspec', '~> 4.0'
gem 'foodcritic', '~> 4.0'
gem 'rubocop'

group :integration do
gem 'test-kitchen'
gem 'kitchen-vagrant', '~> 0.18'
end
19 changes: 9 additions & 10 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
default[:dnsmasq][:enable_dhcp] = false
default[:dnsmasq][:dhcp] = {}
default[:dnsmasq][:dhcp_options] = []
default[:dnsmasq][:enable_dns] = true
default[:dnsmasq][:dns] = {
default['dnsmasq']['enable_dhcp'] = false
default['dnsmasq']['dhcp'] = {}
default['dnsmasq']['dhcp_options'] = []
default['dnsmasq']['enable_dns'] = true
default['dnsmasq']['dns'] = {
'no-poll' => nil,
'no-resolv' => nil,
'server' => '127.0.0.1'
}
default[:dnsmasq][:dns_options] = []
default[:dnsmasq][:managed_hosts] = {}
default[:dnsmasq][:managed_hosts_bag] = "managed_hosts"
default[:dnsmasq][:user] = 'dnsmasq'

default['dnsmasq']['dns_options'] = []
default['dnsmasq']['managed_hosts'] = {}
default['dnsmasq']['managed_hosts_bag'] = 'managed_hosts'
default['dnsmasq']['user'] = 'dnsmasq'
3 changes: 1 addition & 2 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
maintainer_email 'chrisroberts.code@gmail.com'
license 'Apache 2.0'
description 'Installs and configures dnsmasq'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.2.0'

depends 'hosts_file'

%w{ubuntu debian redhat centos scientific oracle}.each do |os|
%w(ubuntu debian redhat centos scientific oracle).each do |os|
supports os
end
11 changes: 3 additions & 8 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package 'dnsmasq'

include_recipe 'dnsmasq::dns' if node['dnsmasq']['enable_dns']
include_recipe 'dnsmasq::dhcp' if node['dnsmasq']['enable_dhcp']

service 'dnsmasq' do
action [:enable, :start]
end

if(node[:dnsmasq][:enable_dns])
include_recipe 'dnsmasq::dns'
end

if(node[:dnsmasq][:enable_dhcp])
include_recipe 'dnsmasq::dhcp'
end
28 changes: 13 additions & 15 deletions recipes/dhcp.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@

include_recipe 'dnsmasq::default'

if node['dnsmasq']['dhcp']['tftp-root']
directory node['dnsmasq']['dhcp']['tftp-root'] do
owner node['dnsmasq']['user']
mode 0755
recursive true
action :create
end
end
directory node['dnsmasq']['dhcp']['tftp-root'] do
owner node['dnsmasq']['user']
mode 0755
recursive true
action :create
end if node['dnsmasq']['dhcp']['tftp-root'] # ~FC023

template '/etc/dnsmasq.d/dhcp.conf' do
source 'dynamic_config.erb'
mode 0644
variables lazy {{
:config => node['dnsmasq']['dhcp'].to_hash,
:list => node['dnsmasq']['dhcp_options']
}}
notifies :restart, resources(:service => 'dnsmasq'), :immediately
variables lazy {
{
:config => node['dnsmasq']['dhcp'].to_hash,
:list => node['dnsmasq']['dhcp_options']
}
}
notifies :restart, 'service[dnsmasq]', :immediately
end

8 changes: 3 additions & 5 deletions recipes/dns.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
include_recipe 'dnsmasq::default'
include_recipe 'dnsmasq::manage_hostsfile'

dns_config = node[:dnsmasq][:dns].to_hash
unless(node[:dnsmasq][:enable_dhcp])
dns_config['no-dhcp-interface='] = nil
end
dns_config = node['dnsmasq']['dns'].to_hash
dns_config['no-dhcp-interface='] = nil unless node['dnsmasq']['enable_dhcp']

template '/etc/dnsmasq.d/dns.conf' do
source 'dynamic_config.erb'
Expand All @@ -13,5 +11,5 @@
:config => dns_config,
:list => node['dnsmasq']['dns_options']
)
notifies :restart, resources(:service => 'dnsmasq'), :immediately
notifies :restart, 'service[dnsmasq]', :immediately
end
10 changes: 5 additions & 5 deletions recipes/manage_hostsfile.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
begin
managed_hosts_bag = data_bag_item('dnsmasq', node[:dnsmasq][:managed_hosts_bag])
managed_hosts_bag = data_bag_item('dnsmasq', node['dnsmasq']['managed_hosts_bag'])
rescue
Chef::Log.debug "No data bag found for DNSMasq managed hosts file"
Chef::Log.debug 'No data bag found for DNSMasq managed hosts file'
end

managed_hosts = {}
managed_hosts.merge!(managed_hosts_bag['maps']) if managed_hosts_bag
managed_hosts.merge!(node[:dnsmasq][:managed_hosts].to_hash) if node[:dnsmasq][:managed_hosts]
managed_hosts.merge!(node['dnsmasq']['managed_hosts'].to_hash) if node['dnsmasq']['managed_hosts']

managed_hosts.each do |ip, host|
host = host.is_a?(Array) ? host.map{|i|i} : host.split(' ')
host = host.is_a?(Array) ? host.map { |i| i } : host.split(' ')
hosts_file_entry ip do
hostname host.shift
aliases host unless host.empty?
comment 'dnsmasq managed entry'
notifies :restart, resources(:service => 'dnsmasq')
notifies :restart, 'service[dnsmasq]'
end
end

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions test/cookbooks/dnsmasq_test/metadata.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
name 'dnsmasq_test'
version '0.0.1'
depends 'dnsmasq'
10 changes: 2 additions & 8 deletions test/cookbooks/dnsmasq_test/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
node.default[:dnsmasq][:enable_dns] = true
node.default[:dnsmasq][:enable_dhcp] = true

node.default[:dnsmasq][:dhcp] = {
'enable-tftp' => nil,
'interface' => 'eth1'
}

execute 'apt-get update' if platform_family?('debian')
package 'bind-utils' if platform_family?('rhel', 'fedora')
include_recipe 'dnsmasq'
44 changes: 44 additions & 0 deletions test/integration/default/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

describe 'dnsmasq::default' do
describe service('dnsmasq') do
it { should be_enabled }
it { should be_running }
end

describe file('/etc/dnsmasq.d/dhcp.conf') do
it { should be_mode '644' }
its(:content) { should match(/^dhcp-host=01:23:ab:cd:01:02,larry,10\.0\.0\.10$/) }
its(:content) { should match(/^dhcp-range=eth1,10\.0\.0\.5,10\.0\.0\.15,12h$/) }
its(:content) { should match(/^domain=test.lab$/) }
its(:content) { should match(/^enable-tftp$/) }
its(:content) { should match(/^interface=eth1$/) }
its(:content) { should match(%r{^tftp-root=\/var\/lib\/tftpboot$}) }
end

describe file('/etc/dnsmasq.d/dns.conf') do
it { should be_mode '644' }
its(:content) { should match(/^no-poll$/) }
its(:content) { should match(/^no-resolv$/) }
its(:content) { should match(/^server=8\.8\.8\.8$/) }
end

describe command('host google.com') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/^google.com has address/) }
end

describe port(53) do
it { should be_listening }
end

# bootp/dhcp
describe port(67) do
it { should be_listening }
end

# tftp
describe port(69) do
it { should be_listening }
end
end
3 changes: 3 additions & 0 deletions test/integration/helpers/serverspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'serverspec'

set :backend, :exec