summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Murawski <steven.murawski@gmail.com>2015-11-17 15:12:41 -0600
committerSteven Murawski <steven.murawski@gmail.com>2015-11-17 15:12:41 -0600
commit96e588daaf298593a6e6f0cef63e1802e6d72a40 (patch)
tree54cc088a2b2438a306a7148668294956c71d1da6
parent6cb744052c0818902f97630c52d55045fcee92df (diff)
downloadchef-smurawski/dsc_reboots.tar.gz
-rw-r--r--lib/chef/provider/dsc_resource.rb33
-rw-r--r--lib/chef/resource/dsc_resource.rb1
-rw-r--r--spec/unit/provider/dsc_resource_spec.rb8
-rw-r--r--spec/unit/resource/dsc_resource_spec.rb9
4 files changed, 11 insertions, 40 deletions
diff --git a/lib/chef/provider/dsc_resource.rb b/lib/chef/provider/dsc_resource.rb
index 7906aea3f8..c06e062e3c 100644
--- a/lib/chef/provider/dsc_resource.rb
+++ b/lib/chef/provider/dsc_resource.rb
@@ -15,26 +15,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-
require 'chef/util/powershell/cmdlet'
require 'chef/util/dsc/local_configuration_manager'
require 'chef/mixin/powershell_type_coercions'
require 'chef/util/dsc/resource_store'
-
class Chef
class Provider
class DscResource < Chef::Provider
include Chef::Mixin::PowershellTypeCoercions
-
provides :dsc_resource, os: "windows"
-
def initialize(new_resource, run_context)
super
@new_resource = new_resource
@module_name = new_resource.module_name
@reboot_resource = nil
end
-
def action_run
if ! test_resource
converge_by(generate_description) do
@@ -43,14 +38,11 @@ class Chef
end
end
end
-
def load_current_resource
end
-
def whyrun_supported?
true
end
-
def define_resource_requirements
requirements.assert(:run) do |a|
a.assertion { supports_dsc_invoke_resource? }
@@ -68,40 +60,31 @@ class Chef
a.block_action!
end
end
-
protected
-
def local_configuration_manager
@local_configuration_manager ||= Chef::Util::DSC::LocalConfigurationManager.new(
node,
nil
)
end
-
def resource_store
Chef::Util::DSC::ResourceStore.instance
end
-
def supports_dsc_invoke_resource?
run_context && Chef::Platform.supports_dsc_invoke_resource?(node)
end
-
def dsc_refresh_mode_disabled?
Chef::Platform.dsc_refresh_mode_disabled?(node)
end
-
def generate_description
@converge_description
end
-
def dsc_resource_name
new_resource.resource.to_s
end
-
def module_name
@module_name ||= begin
found = resource_store.find(dsc_resource_name)
-
r = case found.length
when 0
raise Chef::Exceptions::ResourceNotFound,
@@ -118,7 +101,6 @@ class Chef
end
end
end
-
def test_resource
result = invoke_resource(:test)
# We really want this information from the verbose stream,
@@ -127,24 +109,20 @@ class Chef
@converge_description = result.stdout
return_dsc_resource_result(result, "InDesiredState")
end
-
def set_resource
result = invoke_resource(:set)
- if return_dsc_resource_result(result, 'RebootRequired')
+ if return_dsc_resource_result(result, 'RebootRequired')
create_reboot_resource
end
result.return_value
end
-
def invoke_resource(method, output_format=:object)
properties = translate_type(@new_resource.properties)
switches = "-Method #{method.to_s} -Name #{@new_resource.resource}"\
" -Property #{properties} -Verbose"
-
if module_name != :none
switches += " -Module #{module_name}"
end
-
cmdlet = Chef::Util::Powershell::Cmdlet.new(
node,
"Invoke-DscResource #{switches}",
@@ -152,7 +130,6 @@ class Chef
)
cmdlet.run!
end
-
def return_dsc_resource_result(result, property_name)
if result.return_value.is_a?(Array)
# WMF Feb 2015 Preview
@@ -162,16 +139,14 @@ class Chef
result.return_value[property_name]
end
end
-
def create_reboot_resource
@reboot_resource = Chef::Resource::Reboot.new(
- "Reboot for #{@new_resource.name}",
+ "Reboot for #{@new_resource.name}",
run_context
).tap do |r|
r.reason("Reboot for #{@new_resource.resource}.")
end
end
-
def reboot_if_required
reboot_action = @new_resource.reboot_action
unless @reboot_resource.nil?
@@ -182,9 +157,9 @@ class Chef
else
Chef::Log.debug("Requesting node reboot with #{reboot_action}.")
@reboot_resource.run_action(reboot_action)
- end
+ end
end
end
end
end
-end
+end \ No newline at end of file
diff --git a/lib/chef/resource/dsc_resource.rb b/lib/chef/resource/dsc_resource.rb
index 95ad141d4f..b6167e76d0 100644
--- a/lib/chef/resource/dsc_resource.rb
+++ b/lib/chef/resource/dsc_resource.rb
@@ -80,7 +80,6 @@ class Chef
@reboot_action
end
end
-
private
def value_of(value)
diff --git a/spec/unit/provider/dsc_resource_spec.rb b/spec/unit/provider/dsc_resource_spec.rb
index dc0da1d1c1..b95b1d7bcf 100644
--- a/spec/unit/provider/dsc_resource_spec.rb
+++ b/spec/unit/provider/dsc_resource_spec.rb
@@ -16,7 +16,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-
require 'chef'
require 'spec_helper'
@@ -34,7 +33,6 @@ describe Chef::Provider::DscResource do
node.automatic[:languages][:powershell][:version] = '4.0'
node
}
-
it 'raises a ProviderNotFound exception' do
expect(provider).not_to receive(:meta_configuration)
expect{provider.run_action(:run)}.to raise_error(
@@ -72,7 +70,7 @@ describe Chef::Provider::DscResource do
provider.run_action(:run)
expect(resource).to be_updated
end
-
+
it 'flags the resource as reboot required when required' do
expect(provider).to receive(:dsc_refresh_mode_disabled?).and_return(true)
expect(provider).to receive(:test_resource).and_return(false)
@@ -82,7 +80,7 @@ describe Chef::Provider::DscResource do
expect(provider).to receive(:create_reboot_resource)
provider.run_action(:run)
end
-
+
it 'does not flag the resource as reboot required when not required' do
expect(provider).to receive(:dsc_refresh_mode_disabled?).and_return(true)
expect(provider).to receive(:test_resource).and_return(false)
@@ -94,4 +92,4 @@ describe Chef::Provider::DscResource do
end
end
end
-end
+end \ No newline at end of file
diff --git a/spec/unit/resource/dsc_resource_spec.rb b/spec/unit/resource/dsc_resource_spec.rb
index 663ad4459e..6fa74a139f 100644
--- a/spec/unit/resource/dsc_resource_spec.rb
+++ b/spec/unit/resource/dsc_resource_spec.rb
@@ -15,9 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-
require 'spec_helper'
-
describe Chef::Resource::DscResource do
let(:dsc_test_resource_name) { 'DSCTest' }
let(:dsc_test_property_name) { :DSCTestProperty }
@@ -31,6 +29,7 @@ describe Chef::Resource::DscResource do
empty_events = Chef::EventDispatch::Dispatcher.new
Chef::RunContext.new(node, {}, empty_events)
}
+
let(:dsc_test_resource) {
Chef::Resource::DscResource.new(dsc_test_resource_name, dsc_test_run_context)
}
@@ -39,7 +38,7 @@ describe Chef::Resource::DscResource do
expect(dsc_test_resource.action).to eq([:run])
end
- it "has an allowed_actions attribute with only the `:run` and `:nothing` attributes" do
+ it "has an ed_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
@@ -53,7 +52,7 @@ describe Chef::Resource::DscResource do
expect(dsc_test_resource.module_name).to eq(dsc_test_resource_name)
end
- it "allows the reboot_action attribute to be set" do
+ it "allows the reboot_action attribute to be set" do
dsc_test_resource.reboot_action(dsc_test_reboot_action)
expect(dsc_test_resource.reboot_action).to eq(dsc_test_reboot_action)
end
@@ -88,4 +87,4 @@ describe Chef::Resource::DscResource do
end
end
end
-end
+end \ No newline at end of file