From 399723ebea7e23538120ea129e83c433da04f436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 21 Jan 2014 23:11:25 +0100 Subject: [PATCH] seperate node specific data from role --- Berksfile | 4 +- Gemfile | 2 + Vagrantfile | 30 ++++++++++---- nodes/node0.json | 41 +++++++++++++++++++ nodes/node1.json | 1 + roles/compute_node.json | 6 +-- roles/head_node.json | 27 ++---------- .../bind/files/default/test/default_test.rb | 10 +++++ .../bind/templates/default/zone.erb | 4 +- site-cookbooks/dhcp/attributes/default.rb | 10 +++-- .../dhcp/files/default/test/default_test.rb | 10 +++++ site-cookbooks/dhcp/recipes/default.rb | 18 +++++++- .../dhcp/templates/default/dhcpd.conf.erb | 17 ++++---- .../templates/default/isc-dhcp-server.erb | 2 + site-cookbooks/main/attributes/head_node.rb | 2 + site-cookbooks/main/metadata.rb | 6 +++ site-cookbooks/main/recipes/compute_node.rb | 3 ++ site-cookbooks/main/recipes/head_node.rb | 10 +++++ spec/coobooks/dhcp_spec.rb | 4 +- spec/coobooks/main_spec.rb | 30 ++++++++++++++ spec/spec_helper.rb | 1 + 21 files changed, 184 insertions(+), 54 deletions(-) create mode 100644 nodes/node0.json create mode 100644 nodes/node1.json create mode 100644 site-cookbooks/bind/files/default/test/default_test.rb create mode 100644 site-cookbooks/dhcp/files/default/test/default_test.rb create mode 100644 site-cookbooks/dhcp/templates/default/isc-dhcp-server.erb create mode 100644 site-cookbooks/main/attributes/head_node.rb create mode 100644 site-cookbooks/main/metadata.rb create mode 100644 site-cookbooks/main/recipes/compute_node.rb create mode 100644 site-cookbooks/main/recipes/head_node.rb create mode 100644 spec/coobooks/main_spec.rb diff --git a/Berksfile b/Berksfile index f29b3a4..7144c7c 100644 --- a/Berksfile +++ b/Berksfile @@ -2,7 +2,8 @@ #^syntax detection site :opscode -cookbook 'apt' +cookbook "apt" +cookbook "minitest-handler" def own_cookbook(name,opts={}) opts = { path: "site-cookbooks/#{name}"}.merge!(opts) @@ -12,3 +13,4 @@ end own_cookbook "dhcp" own_cookbook "bind" own_cookbook "ntp" +own_cookbook "main" diff --git a/Gemfile b/Gemfile index cb511ce..a96a27f 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ gem "chef"#, '~> 0.10.6' gem "pry-nav" gem "chefspec" +gem "minitest-chef-handler" +gem "fauxhai" gem "rspec" gem 'knife-spec' gem "berkshelf" diff --git a/Vagrantfile b/Vagrantfile index 0729220..71153e8 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -4,9 +4,14 @@ # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" +def load_json(name) + path = File.join(File.dirname(__FILE__), "nodes", name) + JSON.load(File.open(path)) +end + boxes = [ - { name: "head_node", ip: '172.28.128.2', role: :head_node }, - #{ name: "compute_node", role: :compute_node, mac: "5CA1AB1E0001" } + { name: "node0.lctp", role: :head_node, mac: "5CA1AB1E0001", json: load_json("node0.json") }, + { name: "node1.lctp", role: :compute_node, mac: "5CA1AB1E0001", json: load_json("node1.json") } ] ["vbguest", "berkshelf"].each do |plugin| @@ -30,23 +35,32 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| chef_default = proc do |chef| chef.cookbooks_path = "cookbooks" - chef.roles_path = "roles" chef.data_bags_path = "data_bags" + chef.roles_path = "roles" end boxes.each do |box| config.vm.define box[:name] do |node| - if box[:ip] - node.vm.network :private_network, ip: box[:ip] - else - node.vm.network :private_network, type: :dhcp, mac: box[:mac] + node.vm.provider :virtualbox do |vb| + vb.gui = true + # 1. adapter: NAT to allow vagrant setup the machine + # 2. adapter: for internal network between nodes + vb.customize ["modifyvm", :id, + "--nic1", "nat", + "--nictype1", "virtio", + "--nic2", "intnet", + "--nictype2", "virtio", + "--intnet2", "lctp", + "--macaddress2", box[:mac]] end + node.vm.hostname = box[:name] + config.vm.provision :chef_solo do |chef| chef_default.call(chef) chef.add_role box[:role].to_s + chef.json = box[:json] end end end - end diff --git a/nodes/node0.json b/nodes/node0.json new file mode 100644 index 0000000..6f2bf94 --- /dev/null +++ b/nodes/node0.json @@ -0,0 +1,41 @@ +{ + "main": { + "head_node": { + "internal_ip": "172.28.128.1" + } + }, + "dhcp" : { + "domain": { + "name": "lctp", + "server": "node0.lctp" + }, + "interface": "eth1", + "subnet": { + "gateway": "node0.lctp", + "prefix": "172.28.128.0", + "netmask": "255.255.255.0" + }, + "hosts": [{ + "name": "node1", + "mac": "5c:a1:ab:1e:00:01", + "ip": "172.28.128.101" + }] + }, + "ntp" : { + "subnets": ["::1", "127.0.0.1", "172.28.128.0/24"] + }, + "bind": { + "zones" : { + "lctp": { + "records": [ + { "name": "node0", "type": "A", "value": "172.28.128.1" }, + { "name": "node1", "type": "A", "value": "172.28.128.101" } + ] + }, + "128.28.172.in-addr.arpa": { + "records": [{ "name": "101", "type": "PTR", "value": "node1" }] + } + }, + "trusted_subnets": ["localhost", "localnets", "172.28.128.0/24"] + } +} diff --git a/nodes/node1.json b/nodes/node1.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/nodes/node1.json @@ -0,0 +1 @@ +{} diff --git a/roles/compute_node.json b/roles/compute_node.json index 3ce18d1..710de28 100644 --- a/roles/compute_node.json +++ b/roles/compute_node.json @@ -4,12 +4,8 @@ "json_class": "Chef::Role", "description": "Compute Node", "default_attributes": { - "ntp": { - "server": "de.pool.ntp.org" - } }, "run_list": [ - "recipe[apt]", - "recipe[ntp]" + "recipe[main::compute_node]" ] } diff --git a/roles/head_node.json b/roles/head_node.json index 64aa736..da45e8f 100644 --- a/roles/head_node.json +++ b/roles/head_node.json @@ -4,32 +4,11 @@ "json_class": "Chef::Role", "description": "Head Node", "default_attributes": { - "dhcp" : { - "hosts": [{ - "name": "node0", - "mac": "5CA1AB1E0001", - "ip": "172.28.128.101" - }] - }, - "ntp" : { - "subnets": ["::1", "127.0.0.1", "172.28.128.0/24"] - }, - "bind": { - "zones" : { - "lctp": { - "records": [{ "name": "node1", "type": "A", "value": "172.28.128.101" }] - }, - "128.28.172.in-addr.arpa": { - "records": [{ "name": "101", "type": "PTR", "value": "node1" }] - } - }, - "trusted_subnets": ["localhost", "localnets", "172.28.128.101"] + "ntp": { + "server": "de.pool.ntp.org" } }, "run_list": [ - "recipe[apt]", - "recipe[dhcp]", - "recipe[ntp]", - "recipe[bind]" + "recipe[main::head_node]" ] } diff --git a/site-cookbooks/bind/files/default/test/default_test.rb b/site-cookbooks/bind/files/default/test/default_test.rb new file mode 100644 index 0000000..c2fec82 --- /dev/null +++ b/site-cookbooks/bind/files/default/test/default_test.rb @@ -0,0 +1,10 @@ +require 'minitest/spec' + +describe_recipe 'bind::default' do + it "starts the named daemon" do + assert_sh("service bind9 status") + end + it "should resolve dns" do + assert_sh("dig localhost @localhost") + end +end diff --git a/site-cookbooks/bind/templates/default/zone.erb b/site-cookbooks/bind/templates/default/zone.erb index de41633..d714608 100644 --- a/site-cookbooks/bind/templates/default/zone.erb +++ b/site-cookbooks/bind/templates/default/zone.erb @@ -4,9 +4,7 @@ 4H ; retry 3W ; expire 1D ) ; minimun - NS <%= @node.hostname %> - -<%= @node.hostname %> A <%= @node.ipaddress %> + NS <%= @node.fqdn %>. <% @zone.records.each do |record| -%> <%= record.name %> <%= record.type %> <%= record.value %> diff --git a/site-cookbooks/dhcp/attributes/default.rb b/site-cookbooks/dhcp/attributes/default.rb index 3c50885..b6c4b2b 100644 --- a/site-cookbooks/dhcp/attributes/default.rb +++ b/site-cookbooks/dhcp/attributes/default.rb @@ -1,3 +1,7 @@ -default.dhcp.domain.name = "lctp" -default.dhcp.domain.server = "lctp" -default.hosts = [] +default.dhcp.domain.name = "privat" +default.dhcp.domain.server = "ns.privat" +default.dhcp.hosts = [] +default.dhcp.interface = "eth0" +default.dhcp.subnet.prefix = "192.168.2.0" +default.dhcp.subnet.netmask = "255.255.255.0" +default.dhcp.subnet.gateway = "192.168.2.1" diff --git a/site-cookbooks/dhcp/files/default/test/default_test.rb b/site-cookbooks/dhcp/files/default/test/default_test.rb new file mode 100644 index 0000000..ef0dbbb --- /dev/null +++ b/site-cookbooks/dhcp/files/default/test/default_test.rb @@ -0,0 +1,10 @@ +require 'minitest/spec' + +describe_recipe 'dhcp::default' do + it "starts the named daemon" do + assert_sh("service isc-dhcp-server status") + end + it "should resolve dns" do + assert_sh("dig localhost @localhost") + end +end diff --git a/site-cookbooks/dhcp/recipes/default.rb b/site-cookbooks/dhcp/recipes/default.rb index 853892b..93956f8 100644 --- a/site-cookbooks/dhcp/recipes/default.rb +++ b/site-cookbooks/dhcp/recipes/default.rb @@ -2,8 +2,24 @@ include_recipe "apt" package "isc-dhcp-server" -template "/etc/dhcpcd.conf" do +template "/etc/dhcp/dhcpd.conf" do owner "root" group "root" + mode "0664" source "dhcpd.conf.erb" + notifies :reload, "service[isc-dhcp-server]" +end + +template "/etc/default/isc-dhcp-server" do + owner "root" + group "root" + mode "0664" + source "isc-dhcp-server.erb" + notifies :reload, "service[isc-dhcp-server]" +end + +service "isc-dhcp-server" do + provider Chef::Provider::Service::Upstart + action [:enable, :start] + supports status: true, start: true, stop: true, restart: true end diff --git a/site-cookbooks/dhcp/templates/default/dhcpd.conf.erb b/site-cookbooks/dhcp/templates/default/dhcpd.conf.erb index 6c3a425..535b464 100644 --- a/site-cookbooks/dhcp/templates/default/dhcpd.conf.erb +++ b/site-cookbooks/dhcp/templates/default/dhcpd.conf.erb @@ -1,15 +1,18 @@ -# Crontab for <%= @node.name %> managed by Chef. Changes will be overwritten. +# dhcpd.conf for <%= @node.name %> managed by Chef. Changes will be overwritten. default-lease-time 600; max-lease-time 7200; # option definitions common to all supported networks... option domain-name <%= @node.dhcp.domain.name %>; option domain-name-servers <%= @node.dhcp.domain.server %>; +subnet <%= @node.dhcp.subnet.prefix %> netmask <%= @node.dhcp.subnet.netmask%> { + option routers <%= @node.dhcp.subnet.gateway%>; +} + <% @node.dhcp.hosts.each do |host| -%> - host <%= host.name %> { - hardware ethernet <%= host.mac %>; - fixed-address <%= "#{host.name}.#{@node.dhcp.domain.name}" %>; - use-host-decl-names true; - next-server <%= host.ip %>; - } +host <%= host.name %> { + hardware ethernet <%= host.mac %>; + fixed-address <%= "#{host.name}.#{@node.dhcp.domain.name}" %>; + use-host-decl-names true; +} <% end -%> diff --git a/site-cookbooks/dhcp/templates/default/isc-dhcp-server.erb b/site-cookbooks/dhcp/templates/default/isc-dhcp-server.erb new file mode 100644 index 0000000..cbc1fc2 --- /dev/null +++ b/site-cookbooks/dhcp/templates/default/isc-dhcp-server.erb @@ -0,0 +1,2 @@ +# /etc/default/isc-dhcp-server for <%= @node.name %> managed by Chef. Changes will be overwritten. +INTERFACES="<%= @node.dhcp.interface %>" diff --git a/site-cookbooks/main/attributes/head_node.rb b/site-cookbooks/main/attributes/head_node.rb new file mode 100644 index 0000000..ff15daa --- /dev/null +++ b/site-cookbooks/main/attributes/head_node.rb @@ -0,0 +1,2 @@ +default.main.head_node.internal_ip = "192.168.2.1" +default.main.head_node.internal_network_device = "eth1" diff --git a/site-cookbooks/main/metadata.rb b/site-cookbooks/main/metadata.rb new file mode 100644 index 0000000..aa24c22 --- /dev/null +++ b/site-cookbooks/main/metadata.rb @@ -0,0 +1,6 @@ +name "main" +depends "apt" +depends "ntp" +depends "dhcp" +depends "bind" +depends "minitest-handler" diff --git a/site-cookbooks/main/recipes/compute_node.rb b/site-cookbooks/main/recipes/compute_node.rb new file mode 100644 index 0000000..b7937de --- /dev/null +++ b/site-cookbooks/main/recipes/compute_node.rb @@ -0,0 +1,3 @@ +include_recipe "minitest-handler" +include_recipe "apt" +include_recipe "ntp" diff --git a/site-cookbooks/main/recipes/head_node.rb b/site-cookbooks/main/recipes/head_node.rb new file mode 100644 index 0000000..806adbd --- /dev/null +++ b/site-cookbooks/main/recipes/head_node.rb @@ -0,0 +1,10 @@ +include_recipe "minitest-handler" +include_recipe "apt" +include_recipe "ntp" +include_recipe "bind" + +ifconfig node.main.head_node.internal_ip do + device node.main.head_node.internal_network_device +end + +include_recipe "dhcp" diff --git a/spec/coobooks/dhcp_spec.rb b/spec/coobooks/dhcp_spec.rb index e93ff5c..31c34e4 100644 --- a/spec/coobooks/dhcp_spec.rb +++ b/spec/coobooks/dhcp_spec.rb @@ -5,14 +5,14 @@ describe 'dhcp::default' do ChefSpec::Runner.new do |node| node.set["dhcp"]["hosts"] =[{ name: "node0", - mac: "5CA1AB1E0001", + mac: "5c:a1:ab:1e:00:01", ip: "172.28.128.101" }] end.converge(described_recipe) end it "should setup dhcp" do - chef_run.should render_file("/etc/dhcpcd.conf").with_content("node0") + chef_run.should render_file("/etc/dhcp/dhcpd.conf").with_content("node0") chef_run.should install_package("isc-dhcp-server") end end diff --git a/spec/coobooks/main_spec.rb b/spec/coobooks/main_spec.rb new file mode 100644 index 0000000..5a82025 --- /dev/null +++ b/spec/coobooks/main_spec.rb @@ -0,0 +1,30 @@ +require_relative '../spec_helper' + +describe 'main::head_node' do + let(:chef_run) do + ChefSpec::Runner.new do |node| + node.set["main"] = { + head_node: { + internal_ip: "10.10.1.1", + internal_network_device: "10.10.1.1" + } + } + end.converge(described_recipe) + end + it "should include cookbooks" do + expect(chef_run).should include_recipe('ntp') + expect(chef_run).should include_recipe('bind') + expect(chef_run).should include_recipe('dhcp') + end +end + +describe 'main::compute_node' do + let(:chef_run) do + ChefSpec::Runner.new do |node| + end.converge(described_recipe) + end + it "should include cookbooks" do + expect(chef_run).should include_recipe('apt') + expect(chef_run).should include_recipe('ntp') + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index da679bb..8f1bd1c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'chefspec' require 'chefspec/berkshelf' require 'pry' +require 'fauxhai' RSpec.configure do |config| config.color_enabled = true