summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkimbernator <groverperson@gmail.com>2021-01-18 15:49:55 -0600
committerkimbernator <groverperson@gmail.com>2021-01-18 19:24:27 -0600
commitf38d8e1c9b27faf9e81a2ea63ca839d7f489f48c (patch)
tree88f9ffa096735e2318f3e92c0dd319293c8c28e0
parentf9088ce709b22ae4e4d9c15451037902f1d8e5c4 (diff)
downloadchef-f38d8e1c9b27faf9e81a2ea63ca839d7f489f48c.tar.gz
add backup functionality to windows_task
Signed-off-by: kimbernator <groverperson@gmail.com>
-rw-r--r--lib/chef/resource/windows_task.rb12
-rw-r--r--lib/chef/util/backup.rb4
2 files changed, 14 insertions, 2 deletions
diff --git a/lib/chef/resource/windows_task.rb b/lib/chef/resource/windows_task.rb
index 29bade29ce..2fab478738 100644
--- a/lib/chef/resource/windows_task.rb
+++ b/lib/chef/resource/windows_task.rb
@@ -21,6 +21,7 @@ require_relative "../resource"
require_relative "../win32/security" if ChefUtils.windows_ruby?
autoload :ISO8601, "iso8601" if ChefUtils.windows_ruby?
require_relative "../util/path_helper"
+require_relative "../util/backup"
require "win32/taskscheduler" if ChefUtils.windows_ruby?
class Chef
@@ -236,6 +237,10 @@ class Chef
introduced: "14.15", default: false,
description: "To start the task at any time after its scheduled time has passed."
+ property :backup, [Integer, FalseClass],
+ introduced: "17.0", default: 5,
+ description: "Number of backups to keep of the task when modified/deleted. Set to false to disable backups."
+
attr_accessor :exists, :task, :command_arguments
VALID_WEEK_DAYS = %w{ mon tue wed thu fri sat sun * }.freeze
@@ -564,6 +569,7 @@ class Chef
def update_task(task)
converge_by("#{new_resource} task updated") do
+ do_backup
task.set_account_information(new_resource.user, new_resource.password, new_resource.interactive_enabled)
task.application_name = new_resource.command if new_resource.command
task.parameters = new_resource.command_arguments if new_resource.command_arguments
@@ -948,6 +954,11 @@ class Chef
def get_day(date)
Date.strptime(date, "%m/%d/%Y").strftime("%a").upcase
end
+
+ def do_backup
+ file = "C:/Windows/System32/Tasks/#{new_resource.task_name}"
+ Chef::Util::Backup.new(new_resource, file).backup!
+ end
end
action :create do
@@ -1006,6 +1017,7 @@ class Chef
if current_resource.exists
logger.trace "#{new_resource} task exists"
converge_by("delete scheduled task #{new_resource}") do
+ do_backup
ts = ::Win32::TaskScheduler.new
ts.delete(current_resource.task_name)
end
diff --git a/lib/chef/util/backup.rb b/lib/chef/util/backup.rb
index e739488fb9..7f8c4e2167 100644
--- a/lib/chef/util/backup.rb
+++ b/lib/chef/util/backup.rb
@@ -64,7 +64,7 @@ class Chef
end
def backup_path
- @backup_path ||= ::File.join(prefix, backup_filename)
+ @backup_path ||= PathHelper.cleanpath(::File.join(prefix, backup_filename))
end
def do_backup
@@ -83,7 +83,7 @@ class Chef
fn = Regexp.escape(::File.basename(path))
Dir.entries(::File.dirname(backup_path)).select do |f|
!!(f =~ /\A#{fn}.chef-[0-9.]*\B/)
- end.map { |f| ::File.join(::File.dirname(backup_path), f) }
+ end.map { |f| PathHelper.cleanpath(::File.join(::File.dirname(backup_path), f)) }
end
def sorted_backup_files