From 30bc08765f1f1624b6164782cb7530bee5d6b061 Mon Sep 17 00:00:00 2001 From: Adam Edwards Date: Tue, 19 Aug 2014 06:52:36 -0700 Subject: Add configurationdata attribute support --- lib/chef/provider/dsc_script.rb | 28 +++++++++++++++++-- lib/chef/resource/dsc_script.rb | 30 +++++++++++++++++--- lib/chef/util/dsc/configuration_generator.rb | 2 +- spec/unit/resource/dsc_script_spec.rb | 41 ++++++++++++++++++++++------ 4 files changed, 86 insertions(+), 15 deletions(-) diff --git a/lib/chef/provider/dsc_script.rb b/lib/chef/provider/dsc_script.rb index 2adb32937f..122b7e6198 100644 --- a/lib/chef/provider/dsc_script.rb +++ b/lib/chef/provider/dsc_script.rb @@ -59,12 +59,14 @@ class Chef protected def run_configuration(operation) - config_directory = ::Dir.mktmpdir("dsc-script") + config_directory = ::Dir.mktmpdir("chef-dsc-script") + configuration_data_path = get_configuration_data_path(config_directory) + configuration_flags = get_augmented_configuration_flags(configuration_data_path) config_manager = Chef::Util::DSC::LocalConfigurationManager.new(@run_context.node, config_directory) begin - configuration_document = generate_configuration_document(config_directory, @dsc_resource.flags) + configuration_document = generate_configuration_document(config_directory, configuration_flags) @operations[operation].call(config_manager, configuration_document) rescue Exception => e Chef::Log.error("DSC operation failed: #{e.message.to_s}") @@ -74,6 +76,16 @@ class Chef end end + def get_augmented_configuration_flags(configuration_data_path) + updated_flags = nil + if configuration_data_path + updated_flags = @dsc_resource.flags.nil? ? {} : @dsc_resource.flags.dup + Chef::Util::PathHelper.validate_path(configuration_data_path) + updated_flags[:configurationdata] = configuration_data_path + end + updated_flags + end + def generate_configuration_document(config_directory, configuration_flags) shellout_flags = { :cwd => @dsc_resource.cwd, @@ -90,6 +102,18 @@ class Chef end end + def get_configuration_data_path(config_directory) + if @dsc_resource.configuration_data_script + @dsc_resource.configuration_data_script + elsif @dsc_resource.configuration_data + configuration_data_path = "#{config_directory}/chef_dsc_config_data.psd1" + ::File.open(configuration_data_path, 'wt') do | script | + script.write(@dsc_resource.configuration_data) + end + configuration_data_path + end + end + def configuration_name @dsc_resource.configuration_name || @dsc_resource.name end diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb index 10d90bd065..37546c3b6a 100644 --- a/lib/chef/resource/dsc_script.rb +++ b/lib/chef/resource/dsc_script.rb @@ -25,16 +25,16 @@ class Chef def initialize(name, run_context=nil) super @allowed_actions.push(:run) - @action = 'run' + @action = :run provider(Chef::Provider::DscScript) end def code(arg=nil) if arg && command - raise ArgumentError, "Only one of 'code' and 'command' properties may be specified" + raise ArgumentError, "Only one of 'code' and 'command' attributes may be specified" end if arg && configuration_name - raise ArgumentError, "Attribute `code` may not be set if `configuration_name` is set" + raise ArgumentError, "The 'code' and 'command' attributes may not be used together" end set_or_return( :code, @@ -56,7 +56,7 @@ class Chef def command(arg=nil) if arg && code - raise ArgumentError, "Only one of 'code' and 'command' properties may be specified" + raise ArgumentError, "The 'code' and 'command' attributes may not be used together" end set_or_return( :command, @@ -65,6 +65,28 @@ class Chef ) end + def configuration_data(arg=nil) + if arg && configuration_data_script + raise ArgumentError, "The 'configuration_data' and 'configuration_data_script' attributes may not be used together" + end + set_or_return( + :configuration_data, + arg, + :kind_of => [ String ] + ) + end + + def configuration_data_script(arg=nil) + if arg && configuration_data + raise ArgumentError, "The 'configuration_data' and 'configuration_data_script' attributes may not be used together" + end + set_or_return( + :configuration_data_script, + arg, + :kind_of => [ String ] + ) + end + def flags(arg=nil) set_or_return( :flags, diff --git a/lib/chef/util/dsc/configuration_generator.rb b/lib/chef/util/dsc/configuration_generator.rb index ea949bb9c4..0385bad0c4 100644 --- a/lib/chef/util/dsc/configuration_generator.rb +++ b/lib/chef/util/dsc/configuration_generator.rb @@ -81,7 +81,7 @@ class Chef::Util::DSC end def configuration_code(code, configuration_name) - "$ProgressPreference = 'SilentlyContinue';Configuration '#{configuration_name}'\n{\n\t#{code}\n}\n" + "$ProgressPreference = 'SilentlyContinue';Configuration '#{configuration_name}'\n{\n\tnode 'localhost'\n{\n\t#{code}\n}}\n" end def configuration_document_generation_code(configuration_script, configuration_name) diff --git a/spec/unit/resource/dsc_script_spec.rb b/spec/unit/resource/dsc_script_spec.rb index 79ad34ee8d..7156079937 100644 --- a/spec/unit/resource/dsc_script_spec.rb +++ b/spec/unit/resource/dsc_script_spec.rb @@ -31,13 +31,23 @@ describe Chef::Resource::DscScript do let(:configuration_code) {'echo "This is supposed to create a configuration document."'} let(:configuration_path) {'c:/myconfigs/formatc.ps1'} let(:configuration_name) { 'formatme' } + let(:configuration_data) { '@{AllNodes = @( @{ NodeName = "localhost"; PSDscAllowPlainTextPassword = $true })}' } + let(:configuration_data_script) { 'c:/myconfigs/data/safedata.psd1' } - it "allows the configuration attribute to be set" do + it "has a default action of `:run`" do + expect(dsc_test_resource.action).to eq(:run) + end + + it "has an allowed_actions attribute with only the `:run` and `:nothing` attributes" do + expect(dsc_test_resource.allowed_actions.to_set).to eq([:run,:nothing].to_set) + end + + it "allows the code attribute to be set" do dsc_test_resource.code(configuration_code) expect(dsc_test_resource.code).to eq(configuration_code) end - it "allows the path attribute to be set" do + it "allows the command attribute to be set" do dsc_test_resource.command(configuration_path) expect(dsc_test_resource.command).to eq(configuration_path) end @@ -47,23 +57,38 @@ describe Chef::Resource::DscScript do expect(dsc_test_resource.configuration_name).to eq(configuration_name) end - it "raises an ArgumentError exception if an attempt is made to set the configuration attribute when the path attribute is already set" do + it "allows the configuration_data attribute to be set" do + dsc_test_resource.configuration_data(configuration_data) + expect(dsc_test_resource.configuration_data).to eq(configuration_data) + end + + it "allows the configuration_data_script attribute to be set" do + dsc_test_resource.configuration_data_script(configuration_data_script) + expect(dsc_test_resource.configuration_data_script).to eq(configuration_data_script) + end + + it "raises an ArgumentError exception if an attempt is made to set the code attribute when the command attribute is already set" do dsc_test_resource.command(configuration_path) expect { dsc_test_resource.code(configuration_code) }.to raise_error(ArgumentError) end - it "raises an ArgumentError exception if an attempt is made to set the path attribute when the configuration attribute is already set" do + it "raises an ArgumentError exception if an attempt is made to set the command attribute when the code attribute is already set" do dsc_test_resource.code(configuration_code) expect { dsc_test_resource.command(configuration_path) }.to raise_error(ArgumentError) end - it "raises an ArgumentError exception if an attempt is made to set the configuration_name attribute when the configuration attribute is already set" do + it "raises an ArgumentError exception if an attempt is made to set the configuration_name attribute when the code attribute is already set" do dsc_test_resource.code(configuration_code) expect { dsc_test_resource.configuration_name(configuration_name) }.to raise_error(ArgumentError) end - it "raises an ArgumentError exception if an attempt is made to set the configuration attribute when the configuration_name attribute is already set" do - dsc_test_resource.configuration_name(configuration_name) - expect { dsc_test_resource.code(configuration_code) }.to raise_error(ArgumentError) + it "raises an ArgumentError exception if an attempt is made to set the configuration_data attribute when the configuration_data_script attribute is already set" do + dsc_test_resource.configuration_data_script(configuration_data_script) + expect { dsc_test_resource.configuration_data(configuration_data) }.to raise_error(ArgumentError) + end + + it "raises an ArgumentError exception if an attempt is made to set the configuration_data_script attribute when the configuration_data attribute is already set" do + dsc_test_resource.configuration_data(configuration_data) + expect { dsc_test_resource.configuration_data_script(configuration_data_script) }.to raise_error(ArgumentError) end end -- cgit v1.2.1