diff options
-rw-r--r-- | lib/chef/knife/bootstrap/templates/chef-full.erb | 5 | ||||
-rw-r--r-- | lib/chef/knife/core/bootstrap_context.rb | 25 | ||||
-rw-r--r-- | spec/data/client.d_00/00-foo.rb | 2 | ||||
-rw-r--r-- | spec/data/client.d_00/bar | 1 | ||||
-rw-r--r-- | spec/data/client.d_01/foo/bar.rb | 1 | ||||
-rw-r--r-- | spec/unit/knife/bootstrap_spec.rb | 49 |
6 files changed, 83 insertions, 0 deletions
diff --git a/lib/chef/knife/bootstrap/templates/chef-full.erb b/lib/chef/knife/bootstrap/templates/chef-full.erb index 020645c869..6007ff9859 100644 --- a/lib/chef/knife/bootstrap/templates/chef-full.erb +++ b/lib/chef/knife/bootstrap/templates/chef-full.erb @@ -226,6 +226,11 @@ cat > /etc/chef/first-boot.json <<EOP <%= Chef::JSONCompat.to_json(first_boot) %> EOP +<% unless client_d.empty? -%> +mkdir -p /etc/chef/client.d +<%= client_d %> +<% end -%> + echo "Starting the first Chef Client run..." <%= start_chef %>' diff --git a/lib/chef/knife/core/bootstrap_context.rb b/lib/chef/knife/core/bootstrap_context.rb index b05cae688c..6f1c234796 100644 --- a/lib/chef/knife/core/bootstrap_context.rb +++ b/lib/chef/knife/core/bootstrap_context.rb @@ -18,6 +18,7 @@ require "chef/run_list" require "chef/util/path_helper" +require "pathname" class Chef class Knife @@ -52,6 +53,10 @@ class Chef end end + def client_d + @cliend_d ||= client_d_content + end + def encrypted_data_bag_secret @secret end @@ -195,6 +200,26 @@ validation_client_name "#{@chef_config[:validation_client_name]}" content end + def client_d_content + content = "" + if @chef_config[:client_d_dir] && File.exist?(@chef_config[:client_d_dir]) + root = Pathname(@chef_config[:client_d_dir]) + root.find do |f| + relative = f.relative_path_from(root) + if f != root + file_on_node = "/etc/chef/client.d/#{relative}" + if f.directory? + content << "mkdir #{file_on_node}\n" + else + content << "cat > #{file_on_node} <<'EOP'\n" + + f.read + "\nEOP\n" + end + end + end + end + content + end + end end end diff --git a/spec/data/client.d_00/00-foo.rb b/spec/data/client.d_00/00-foo.rb new file mode 100644 index 0000000000..44a763aca1 --- /dev/null +++ b/spec/data/client.d_00/00-foo.rb @@ -0,0 +1,2 @@ +# 00-foo.rb +# d6f9b976-289c-4149-baf7-81e6ffecf228 diff --git a/spec/data/client.d_00/bar b/spec/data/client.d_00/bar new file mode 100644 index 0000000000..72dca4d5e4 --- /dev/null +++ b/spec/data/client.d_00/bar @@ -0,0 +1 @@ +1 / 0 diff --git a/spec/data/client.d_01/foo/bar.rb b/spec/data/client.d_01/foo/bar.rb new file mode 100644 index 0000000000..72dca4d5e4 --- /dev/null +++ b/spec/data/client.d_01/foo/bar.rb @@ -0,0 +1 @@ +1 / 0 diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb index 3425b94c76..8ad5c338c3 100644 --- a/spec/unit/knife/bootstrap_spec.rb +++ b/spec/unit/knife/bootstrap_spec.rb @@ -458,6 +458,55 @@ describe Chef::Knife::Bootstrap do end end + describe "when transferring client.d" do + + let(:rendered_template) do + knife.merge_configs + knife.render_template + end + + before do + Chef::Config[:client_d_dir] = client_d_dir + end + + context "when client_d_dir is nil" do + let(:client_d_dir) { nil } + + it "does not create /etc/chef/client.d" do + expect(rendered_template).not_to match(%r{mkdir -p /etc/chef/client\.d}) + end + end + + context "when client_d_dir is set" do + let(:client_d_dir) { Chef::Util::PathHelper.cleanpath( + File.join(File.dirname(__FILE__), "../../data/client.d_00")) } + + it "creates /etc/chef/client.d" do + expect(rendered_template).to match("mkdir -p /etc/chef/client\.d") + end + + context "a flat directory structure" do + it "creates a file 00-foo.rb" do + expect(rendered_template).to match("cat > /etc/chef/client.d/00-foo.rb <<'EOP'") + expect(rendered_template).to match("d6f9b976-289c-4149-baf7-81e6ffecf228") + end + it "creates a file bar" do + expect(rendered_template).to match("cat > /etc/chef/client.d/bar <<'EOP'") + expect(rendered_template).to match("1 / 0") + end + end + + context "a nested directory structure" do + let(:client_d_dir) { Chef::Util::PathHelper.cleanpath( + File.join(File.dirname(__FILE__), "../../data/client.d_01")) } + it "creates a file foo/bar.rb" do + expect(rendered_template).to match("cat > /etc/chef/client.d/foo/bar.rb <<'EOP'") + expect(rendered_template).to match("1 / 0") + end + end + end + end + describe "handling policyfile options" do context "when only policy_name is given" do |