summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornimisha <nimisha.sharad@msystechnologies.com>2017-03-14 17:06:53 +0530
committernimisha <nimisha.sharad@msystechnologies.com>2017-03-31 20:47:07 +0530
commit7a2f9dcef5338b9ca9606bafaecfd90e0c07e97d (patch)
tree4596671d2e19acebbf54b42094523b8e7ee97dc7
parent13f8ae83ba708a868bda63d0f03a706d38ee5051 (diff)
downloadchef-7a2f9dcef5338b9ca9606bafaecfd90e0c07e97d.tar.gz
Added support for random_delay option
Signed-off-by: nimisha <nimisha.sharad@msystechnologies.com>
-rw-r--r--lib/chef/provider/windows_task.rb41
-rw-r--r--lib/chef/resource/windows_task.rb1
2 files changed, 30 insertions, 12 deletions
diff --git a/lib/chef/provider/windows_task.rb b/lib/chef/provider/windows_task.rb
index 2be55bfe10..8a19bdde76 100644
--- a/lib/chef/provider/windows_task.rb
+++ b/lib/chef/provider/windows_task.rb
@@ -73,7 +73,11 @@ class Chef
options['M'] = @new_resource.months unless @new_resource.months.nil?
run_schtasks 'CREATE', options
- set_cwd(new_resource.cwd) if new_resource.cwd
+ xml_options = []
+ xml_options << "cwd" if new_resource.cwd
+ xml_options << "random_delay" if new_resource.random_delay
+ update_task_xml(xml_options) unless xml_options.empty?
+
new_resource.updated_by_last_action true
Chef::Log.info "#{@new_resource} task created"
end
@@ -107,7 +111,9 @@ class Chef
options['IT'] = '' if @new_resource.interactive_enabled
run_schtasks 'CHANGE', options
- set_cwd(new_resource.cwd) if new_resource.cwd != @current_resource.cwd
+ xml_options = ["cwd", "random_delay"]
+
+ update_task_xml(xml_options)
new_resource.updated_by_last_action true
Chef::Log.info "Change #{@new_resource} task ran"
else
@@ -191,7 +197,12 @@ class Chef
@current_resource.user != @new_resource.user
end
- def set_cwd(folder)
+ def update_task_xml(options = [])
+ xml_element_mapping = {
+ "cwd" => "Actions/Exec/WorkingDirectory",
+ "random_delay" => "Triggers/TimeTrigger/RandomDelay"
+ }
+
Chef::Log.debug 'looking for existing tasks'
# we use shell_out here instead of shell_out! because a failure implies that the task does not exist
@@ -201,15 +212,21 @@ class Chef
doc = REXML::Document.new(xml_cmd.stdout)
- Chef::Log.debug 'Removing former CWD if any'
- doc.root.elements.delete('Actions/Exec/WorkingDirectory')
-
- unless folder.nil?
- Chef::Log.debug 'Setting CWD as #folder'
- cwd_element = REXML::Element.new('WorkingDirectory')
- cwd_element.add_text(folder)
- exec_element = doc.root.elements['Actions/Exec']
- exec_element.add_element(cwd_element)
+ options.each do |option|
+ Chef::Log.debug 'Removing former #{option} if any'
+ doc.root.elements.delete(xml_element_mapping[option])
+ option_value = @new_resource.send("#{option}")
+
+ if option_value
+ Chef::Log.debug 'Setting #option as #option_value'
+ split_xml_path = xml_element_mapping[option].split("/") # eg. if xml_element_mapping[option] = "Actions/Exec/WorkingDirectory"
+ element_name = split_xml_path.last # element_name = "WorkingDirectory"
+ cwd_element = REXML::Element.new(element_name)
+ cwd_element.add_text(option_value)
+ element_root = (split_xml_path - [element_name]).join("/") # element_root = 'Actions/Exec'
+ exec_element = doc.root.elements[element_root]
+ exec_element.add_element(cwd_element)
+ end
end
temp_task_file = ::File.join(ENV['TEMP'], 'windows_task.xml')
diff --git a/lib/chef/resource/windows_task.rb b/lib/chef/resource/windows_task.rb
index 1f126afc3c..c81ad15d3b 100644
--- a/lib/chef/resource/windows_task.rb
+++ b/lib/chef/resource/windows_task.rb
@@ -57,6 +57,7 @@ class Chef
property :day, [String, Integer]
property :months, String
property :idle_time, Integer
+ property :random_delay, String
attr_accessor :exists, :status, :enabled
end