summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSteven Murawski <steven.murawski@gmail.com>2015-11-13 16:44:06 -0600
committerSteven Murawski <steven.murawski@gmail.com>2015-11-18 10:18:50 -0600
commitfbdf03d3c174f1c2309a497efcf92227e191fdc0 (patch)
treee24c05fa5469eaf1dbf2e39ffa49820428d6708f /lib
parent303c4336bc39b54fffe78b06f2f473a0842be53f (diff)
downloadchef-fbdf03d3c174f1c2309a497efcf92227e191fdc0.tar.gz
Add reboot notification to dsc_resource
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/provider/dsc_resource.rb69
-rw-r--r--lib/chef/resource/dsc_resource.rb12
2 files changed, 49 insertions, 32 deletions
diff --git a/lib/chef/provider/dsc_resource.rb b/lib/chef/provider/dsc_resource.rb
index 65830131ab..c06e062e3c 100644
--- a/lib/chef/provider/dsc_resource.rb
+++ b/lib/chef/provider/dsc_resource.rb
@@ -15,40 +15,34 @@
# 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
result = set_resource
+ reboot_if_required
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? }
@@ -66,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,
@@ -116,37 +101,28 @@ class Chef
end
end
end
-
def test_resource
result = invoke_resource(:test)
# We really want this information from the verbose stream,
# however Invoke-DscResource is not correctly writing to that
# stream and instead just dumping to stdout
@converge_description = result.stdout
-
- if result.return_value.is_a?(Array)
- # WMF Feb 2015 Preview
- result.return_value[0]["InDesiredState"]
- else
- # WMF April 2015 Preview
- result.return_value["InDesiredState"]
- end
+ return_dsc_resource_result(result, "InDesiredState")
end
-
def set_resource
result = invoke_resource(:set)
+ 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}",
@@ -154,7 +130,36 @@ class Chef
)
cmdlet.run!
end
-
+ def return_dsc_resource_result(result, property_name)
+ if result.return_value.is_a?(Array)
+ # WMF Feb 2015 Preview
+ result.return_value[0][property_name]
+ else
+ # WMF April 2015 Preview
+ result.return_value[property_name]
+ end
+ end
+ def create_reboot_resource
+ @reboot_resource = Chef::Resource::Reboot.new(
+ "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?
+ case reboot_action
+ when :nothing
+ Chef::Log.debug("A reboot was requested by the DSC resource, but reboot_action is :nothing.")
+ Chef::Log.debug("This dsc_resource will not reboot the node.")
+ else
+ Chef::Log.debug("Requesting node reboot with #{reboot_action}.")
+ @reboot_resource.run_action(reboot_action)
+ 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 5db00f49ca..b6167e76d0 100644
--- a/lib/chef/resource/dsc_resource.rb
+++ b/lib/chef/resource/dsc_resource.rb
@@ -31,6 +31,7 @@ class Chef
super
@properties = {}
@resource = nil
+ @reboot_action = :nothing
end
def resource(value=nil)
@@ -68,6 +69,17 @@ class Chef
end
end
+ # This property takes the action message for the reboot resource
+ # If the set method of the DSC resource indicate that a reboot
+ # is necessary, reboot_action provides the mechanism for a reboot to
+ # be requested.
+ def reboot_action(value=nil)
+ if value
+ @reboot_action = value
+ else
+ @reboot_action
+ end
+ end
private
def value_of(value)