Class: Chef::Provider::WindowsTask

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

Constant Summary collapse

MONTHS =
{
  JAN: TaskScheduler::JANUARY,
  FEB: TaskScheduler::FEBRUARY,
  MAR: TaskScheduler::MARCH,
  APR: TaskScheduler::APRIL,
  MAY: TaskScheduler::MAY,
  JUN: TaskScheduler::JUNE,
  JUL: TaskScheduler::JULY,
  AUG: TaskScheduler::AUGUST,
  SEP: TaskScheduler::SEPTEMBER,
  OCT: TaskScheduler::OCTOBER,
  NOV: TaskScheduler::NOVEMBER,
  DEC: TaskScheduler::DECEMBER,
}.freeze
DAYS_OF_WEEK =
{ MON: TaskScheduler::MONDAY,
TUE: TaskScheduler::TUESDAY,
WED: TaskScheduler::WEDNESDAY,
THU: TaskScheduler::THURSDAY,
FRI: TaskScheduler::FRIDAY,
SAT: TaskScheduler::SATURDAY,
SUN: TaskScheduler::SUNDAY }.freeze
WEEKS_OF_MONTH =
{
  FIRST: TaskScheduler::FIRST_WEEK,
  SECOND: TaskScheduler::SECOND_WEEK,
  THIRD: TaskScheduler::THIRD_WEEK,
  FOURTH: TaskScheduler::FOURTH_WEEK,
}.freeze
DAYS_OF_MONTH =
{
  1 => TaskScheduler::TASK_FIRST,
  2 => TaskScheduler::TASK_SECOND,
  3 => TaskScheduler::TASK_THIRD,
  4 => TaskScheduler::TASK_FOURTH,
  5 => TaskScheduler::TASK_FIFTH,
  6 => TaskScheduler::TASK_SIXTH,
  7 => TaskScheduler::TASK_SEVENTH,
  8 => TaskScheduler::TASK_EIGHTH,
  9 => TaskScheduler::TASK_NINETH,
  10 => TaskScheduler::TASK_TENTH,
  11 => TaskScheduler::TASK_ELEVENTH,
  12 => TaskScheduler::TASK_TWELFTH,
  13 => TaskScheduler::TASK_THIRTEENTH,
  14 => TaskScheduler::TASK_FOURTEENTH,
  15 => TaskScheduler::TASK_FIFTEENTH,
  16 => TaskScheduler::TASK_SIXTEENTH,
  17 => TaskScheduler::TASK_SEVENTEENTH,
  18 => TaskScheduler::TASK_EIGHTEENTH,
  19 => TaskScheduler::TASK_NINETEENTH,
  20 => TaskScheduler::TASK_TWENTIETH,
  21 => TaskScheduler::TASK_TWENTY_FIRST,
  22 => TaskScheduler::TASK_TWENTY_SECOND,
  23 => TaskScheduler::TASK_TWENTY_THIRD,
  24 => TaskScheduler::TASK_TWENTY_FOURTH,
  25 => TaskScheduler::TASK_TWENTY_FIFTH,
  26 => TaskScheduler::TASK_TWENTY_SIXTH,
  27 => TaskScheduler::TASK_TWENTY_SEVENTH,
  28 => TaskScheduler::TASK_TWENTY_EIGHTH,
  29 => TaskScheduler::TASK_TWENTY_NINTH,
  30 => TaskScheduler::TASK_THIRTYETH,
  31 => TaskScheduler::TASK_THIRTY_FIRST,
}.freeze
PRIORITY =
{ "critical" => 0, "highest" => 1,  "above_normal_2" => 2 , "above_normal_3" => 3, "normal_4" => 4,
"normal_5" => 5, "normal_6" => 6, "below_normal_7" => 7, "below_normal_8" => 8, "lowest" => 9, "idle" => 10 }.freeze

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

apply_default_env, maybe_add_timeout, #shell_out, #shell_out!

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::PowershellExec

#powershell_exec

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

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!, #resources, #with_run_context

Constructor Details

This class inherits a constructor from Chef::Provider

Instance Method Details

#action_createObject Also known as: action_change



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/provider/windows_task.rb', line 118

def action_create
  set_command_and_arguments if new_resource.command

  if current_resource.exists
    logger.trace "#{new_resource} task exist."
    unless (task_needs_update?(current_resource.task)) || (new_resource.force)
      logger.info "#{new_resource} task does not need updating and force is not specified - nothing to do"
      return
    end

    # if start_day and start_time is not set by user current date and time will be set while updating any property
    set_start_day_and_time unless new_resource.frequency == :none
    update_task(current_resource.task)
  else
    basic_validation
    set_start_day_and_time
    converge_by("#{new_resource} task created") do
      task = TaskScheduler.new
      if new_resource.frequency == :none
        task.new_work_item(new_resource.task_name, {}, { user: new_resource.user, password: new_resource.password, interactive: new_resource.interactive_enabled })
        task.activate(new_resource.task_name)
      else
        task.new_work_item(new_resource.task_name, trigger, { user: new_resource.user, password: new_resource.password, interactive: new_resource.interactive_enabled })
      end
      task.application_name = new_resource.command
      task.parameters = new_resource.command_arguments if new_resource.command_arguments
      task.working_directory = new_resource.cwd if new_resource.cwd
      task.configure_settings(config_settings)
      task.configure_principals(principal_settings)
      task.(new_resource.user, new_resource.password, new_resource.interactive_enabled)
      task.creator = new_resource.user
      task.description = new_resource.description unless new_resource.description.nil?
      task.activate(new_resource.task_name)
    end
  end
end

#action_deleteObject



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/chef/provider/windows_task.rb', line 170

def action_delete
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    converge_by("delete scheduled task #{new_resource}") do
      ts = TaskScheduler.new
      ts.delete(current_resource.task_name)
    end
  else
    logger.warn "#{new_resource} task does not exist - nothing to do"
  end
end

#action_disableObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/chef/provider/windows_task.rb', line 214

def action_disable
  if current_resource.exists
    logger.info "#{new_resource} task exists"
    if %w{ready running}.include?(current_resource.task.status)
      converge_by("#{new_resource} task disabled") do
        # TODO: in win32-taskscheduler there is no method whcih disbales the task so currently calling disable with schtasks.exe
        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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef/provider/windows_task.rb', line 197

def action_enable
  if current_resource.exists
    logger.trace "#{new_resource} task exists"
    if current_resource.task.status == "not scheduled"
      converge_by("#{new_resource} task enabled") do
        # TODO wind32-taskscheduler currently not having any method to handle this so using schtasks.exe here
        run_schtasks "CHANGE", "ENABLE" => ""
      end
    else
      logger.trace "#{new_resource} already enabled - nothing to do"
    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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/chef/provider/windows_task.rb', line 182

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

#action_runObject



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

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

#load_current_resourceObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/provider/windows_task.rb', line 105

def load_current_resource
  @current_resource = Chef::Resource::WindowsTask.new(new_resource.name)
  task = TaskScheduler.new(new_resource.task_name, nil, "\\", false)
  @current_resource.exists = task.exists?(new_resource.task_name)
  if @current_resource.exists
    task.get_task(new_resource.task_name)
    @current_resource.task = task
    pathed_task_name = new_resource.task_name.start_with?('\\') ? new_resource.task_name : "\\#{new_resource.task_name}"
    @current_resource.task_name(pathed_task_name)
  end
  @current_resource
end