Class: Chef::Provider::WindowsTask

Inherits:
Chef::Provider show all
Includes:
Mixin::PowershellOut, Mixin::ShellOut
Defined in:
lib/chef/provider/windows_task.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::ShellOut

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #define_resource_requirements, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

Methods included from Mixin::PowershellExec

#powershell_exec

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

This class inherits a constructor from Chef::Provider

Instance Method Details

#action_createObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chef/provider/windows_task.rb', line 76

def action_create
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    if !(task_need_update? || new_resource.force)
      logger.info "#{new_resource} task does not need updating and force is not specified - nothing to do"
      return
    end
    # Setting the attributes of new_resource as current_resource.
    # This is required to handle update scenarios when the user specifies
    # only those attributes in the recipe which require update
    resource_attributes.each do |attribute|
      new_resource_attribute = new_resource.send(attribute)
      current_resource_attribute = current_resource.send(attribute)
      # We accept start_day in mm/dd/yyyy format only. Hence while copying the start_day from system to new_resource.start_day,
      # we are converting from system date format to mm/dd/yyyy
      current_resource_attribute = convert_system_date_to_mm_dd_yyyy(current_resource_attribute) if attribute == "start_day" && current_resource_attribute != "N/A"
      # Convert start_time into 24hr time format
      current_resource_attribute = DateTime.parse(current_resource_attribute).strftime("%H:%M") if attribute == "start_time" && current_resource_attribute != "N/A"
      new_resource.send("#{attribute}=", current_resource_attribute ) if current_resource_attribute && new_resource_attribute.nil?
    end
  end
  basic_validation
  options = {}
  options["F"] = "" if new_resource.force || task_need_update?
  if schedule == :none
    options["SC"] = :once
    options["ST"] = "00:00"
    options["SD"] = convert_user_date_to_system_date "12/12/2012"
  else
    options["SC"] = schedule
    options["ST"] = new_resource.start_time unless new_resource.start_time.nil? || new_resource.start_time == "N/A"
    options["SD"] = convert_user_date_to_system_date new_resource.start_day unless new_resource.start_day.nil? || new_resource.start_day == "N/A"
  end
  options["MO"] = new_resource.frequency_modifier if frequency_modifier_allowed
  options["I"]  = new_resource.idle_time unless new_resource.idle_time.nil?
  options["TR"] = new_resource.command
  options["RU"] = new_resource.user
  options["RP"] = new_resource.password if use_password?
  options["RL"] = "HIGHEST" if new_resource.run_level == :highest
  options["IT"] = "" if new_resource.interactive_enabled
  options["D"] = new_resource.day if new_resource.day
  options["M"] = new_resource.months unless new_resource.months.nil?
  run_schtasks "CREATE", options
  xml_options = []
  xml_options << "cwd" if new_resource.cwd
  xml_options << "random_delay" if new_resource.random_delay
  xml_options << "execution_time_limit" if new_resource.execution_time_limit

  converge_by("#{new_resource} task created") do
    update_task_xml(xml_options) unless xml_options.empty?
  end
end

#action_deleteObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/provider/windows_task.rb', line 144

def action_delete
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    converge_by("delete scheduled task #{new_resource}") do
      # always need to force deletion
      run_schtasks "DELETE", "F" => ""
    end
  else
    logger.warn "#{new_resource} task does not exist - nothing to do"
  end
end

#action_disableObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/chef/provider/windows_task.rb', line 187

def action_disable
  if current_resource.exists
    logger.info "#{new_resource} task exists"
    if current_resource.enabled
      converge_by("#{new_resource} task disabled") do
        run_schtasks "CHANGE", "DISABLE" => ""
      end
    else
      logger.warn "#{new_resource} already disabled - nothing to do"
    end
  else
    logger.warn "#{new_resource} task does not exist - nothing to do"
  end
end

#action_enableObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chef/provider/windows_task.rb', line 171

def action_enable
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    if current_resource.enabled
      logger.trace "#{new_resource} already enabled - nothing to do"
    else
      converge_by("#{new_resource} task enabled") do
        run_schtasks "CHANGE", "ENABLE" => ""
      end
    end
  else
    logger.fatal "#{new_resource} task does not exist - nothing to do"
    raise Errno::ENOENT, "#{new_resource}: task does not exist, cannot enable"
  end
end

#action_endObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chef/provider/windows_task.rb', line 156

def action_end
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    if current_resource.status != :running
      logger.trace "#{new_resource} is not running - nothing to do"
    else
      converge_by("#{new_resource} task ended") do
        run_schtasks "END"
      end
    end
  else
    logger.warn "#{new_resource} task does not exist - nothing to do"
  end
end

#action_runObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/provider/windows_task.rb', line 129

def action_run
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    if current_resource.status == :running
      logger.info "#{new_resource} task is currently running, skipping run"
    else
      converge_by("run scheduled task #{new_resource}") do
        run_schtasks "RUN"
      end
    end
  else
    logger.warn "#{new_resource} task does not exist - nothing to do"
  end
end

#basic_validationObject

This method checks if task and command attributes exist since those two are mandatory attributes to create a schedules task.



63
64
65
66
67
68
69
# File 'lib/chef/provider/windows_task.rb', line 63

def basic_validation
  validate = []
  validate << "Command" if new_resource.command.nil? || new_resource.command.empty?
  validate << "Task Name" if new_resource.task_name.nil? || new_resource.task_name.empty?
  return true if validate.empty?
  raise Chef::Exceptions::ValidationFailed.new "Value for '#{validate.join(', ')}' option cannot be empty"
end

#load_current_resourceObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/chef/provider/windows_task.rb', line 32

def load_current_resource
  self.current_resource = Chef::Resource::WindowsTask.new(new_resource.name)
  pathed_task_name = new_resource.task_name.start_with?('\\') ? new_resource.task_name : "\\#{new_resource.task_name}"

  current_resource.task_name(pathed_task_name)
  task_hash = load_task_hash(pathed_task_name)

  set_current_resource(task_hash) if task_hash.respond_to?(:[]) && task_hash[:TaskName] == pathed_task_name
  current_resource
end

#resource_attributesObject

get array of windows task resource attributes



72
73
74
# File 'lib/chef/provider/windows_task.rb', line 72

def resource_attributes
  %w{ command user run_level cwd frequency_modifier frequency idle_time random_delay execution_time_limit start_day start_time }
end

#set_current_resource(task_hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/provider/windows_task.rb', line 43

def set_current_resource(task_hash)
  current_resource.exists = true
  current_resource.command(task_hash[:TaskToRun])
  current_resource.cwd(task_hash[:StartIn]) unless task_hash[:StartIn] == "N/A"
  current_resource.user(task_hash[:RunAsUser])
  set_current_run_level task_hash[:run_level]
  set_current_frequency task_hash
  current_resource.day(task_hash[:day]) if task_hash[:day]
  current_resource.months(task_hash[:months]) if task_hash[:months]
  set_current_idle_time(task_hash[:idle_time]) if task_hash[:idle_time]
  current_resource.random_delay(task_hash[:random_delay]) if task_hash[:random_delay]
  # schtask sets execution_time_limit as PT72H by default
  current_resource.execution_time_limit(task_hash[:execution_time_limit] || "PT72H")
  current_resource.status = :running if task_hash[:Status] == "Running"
  current_resource.enabled = true if task_hash[:ScheduledTaskState] == "Enabled"
  current_resource.start_time = task_hash[:StartTime] if task_hash[:StartTime]
  current_resource.start_day = task_hash[:StartDate] if task_hash[:StartDate]
end