|
| 1 | +# |
| 2 | +# Cookbook Name:: CHEF-37 |
| 3 | +# Recipe:: default |
| 4 | +# |
| 5 | +# Copyright 2009, Example Com |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +cookbook_dir = "/var/chef/cookbooks/CHEF-37" |
| 21 | + |
| 22 | +file_dir_list = { |
| 23 | + :fqdn => File.expand_path(File.join(cookbook_dir, "files", "host-#{node[:fqdn]}")), |
| 24 | + :default => File.expand_path(File.join(cookbook_dir, "files", "default")), |
| 25 | + :platform => File.expand_path(File.join(cookbook_dir, "files", node[:platform])), |
| 26 | + :platform_version => File.expand_path(File.join(cookbook_dir, "files", "#{node[:platform]}-#{node[:platform_version]}")) |
| 27 | +} |
| 28 | + |
| 29 | +template_dir_list = { |
| 30 | + :fqdn => File.expand_path(File.join(cookbook_dir, "templates", "host-#{node[:fqdn]}")), |
| 31 | + :default => File.expand_path(File.join(cookbook_dir, "templates", "default")), |
| 32 | + :platform => File.expand_path(File.join(cookbook_dir, "templates", node[:platform])), |
| 33 | + :platform_version => File.expand_path(File.join(cookbook_dir, "templates", "#{node[:platform]}-#{node[:platform_version]}")) |
| 34 | +} |
| 35 | + |
| 36 | +contents = "Frozen in the place I hide, not afraid to paint my sky with some who say I have lost my mind" |
| 37 | +template_contents = '<%= @who %> sky with some who say I have lost my mind' |
| 38 | + |
| 39 | +# Create the directory and every version of the file |
| 40 | +[ :fqdn, :default, :platform, :platform_version ].each do |dirname| |
| 41 | + directory file_dir_list[dirname] do |
| 42 | + action :create |
| 43 | + end |
| 44 | + |
| 45 | + execute "create-#{dirname}-chef-37.txt" do |
| 46 | + command "echo '#{dirname}: #{contents}' > #{file_dir_list[dirname]}/chef-37.txt" |
| 47 | + end |
| 48 | + |
| 49 | + directory template_dir_list[dirname] do |
| 50 | + action :create |
| 51 | + end |
| 52 | + |
| 53 | + execute "create-template-#{dirname}-chef-37.txt" do |
| 54 | + command "echo '#{dirname}: #{template_contents}' > #{template_dir_list[dirname]}/chef-37-#{dirname}.txt.erb" |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +# Then work down - fetch the file, should be the most specific one. |
| 59 | +# Then delete that one from the cookbooks on the server, since we don't want it to |
| 60 | +# interfere with our next test. |
| 61 | +[ :fqdn, :platform_version, :platform, :default ].each do |dirname| |
| 62 | + remote_file "/tmp/chef-37-#{dirname}.txt" do |
| 63 | + source "chef-37.txt" |
| 64 | + action :create |
| 65 | + end |
| 66 | + |
| 67 | + file "#{file_dir_list[dirname]}/chef-37.txt" do |
| 68 | + action :delete |
| 69 | + end |
| 70 | + |
| 71 | + template "/tmp/chef-template-37-#{dirname}.txt" do |
| 72 | + source "chef-37-#{dirname}.txt.erb" |
| 73 | + variables({ |
| 74 | + :who => "paint my" |
| 75 | + }) |
| 76 | + action :create |
| 77 | + end |
| 78 | + |
| 79 | + file "#{template_dir_list[dirname]}/chef-37-#{dirname}.txt.erb" do |
| 80 | + action :delete |
| 81 | + end |
| 82 | +end |
0 commit comments