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, #cookbook_name, #current_resource, #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, #define_resource_requirements, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #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 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
# File 'lib/chef/provider/windows_task.rb', line 76

def action_create
  if current_resource.exists
    if !(task_need_update? || new_resource.force)
      Chef::Log.info "#{new_resource} task already exists - nothing to do"
      return
    end
    # To merge current resource and new resource attributes
    resource_attributes.each do |attribute|
      new_resource_attribute = new_resource.send(attribute)
      current_resource_attribute = current_resource.send(attribute)
      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?
    options["SD"] = convert_user_date_to_system_date new_resource.start_day unless new_resource.start_day.nil?
  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
  update_task_xml(xml_options) unless xml_options.empty?

  new_resource.updated_by_last_action true
  Chef::Log.info "#{new_resource} task created"
end

#action_deleteObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/chef/provider/windows_task.rb', line 135

def action_delete
  if current_resource.exists
    # always need to force deletion
    run_schtasks "DELETE", "F" => ""
    new_resource.updated_by_last_action true
    Chef::Log.info "#{new_resource} task deleted"
  else
    Chef::Log.warn "#{new_resource} task doesn't exists - nothing to do"
  end
end

#action_disableObject



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chef/provider/windows_task.rb', line 175

def action_disable
  if current_resource.exists
    if current_resource.enabled
      run_schtasks "CHANGE", "DISABLE" => ""
      new_resource.updated_by_last_action true
      Chef::Log.info "#{new_resource} task disabled"
    else
      Chef::Log.warn "#{new_resource} already disabled - nothing to do"
    end
  else
    Chef::Log.warn "#{new_resource} task doesn't exist - nothing to do"
  end
end

#action_enableObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/chef/provider/windows_task.rb', line 160

def action_enable
  if current_resource.exists
    if current_resource.enabled
      Chef::Log.debug "#{new_resource} already enabled - nothing to do"
    else
      run_schtasks "CHANGE", "ENABLE" => ""
      new_resource.updated_by_last_action true
      Chef::Log.info "#{new_resource} task enabled"
    end
  else
    Chef::Log.fatal "#{new_resource} task doesn't exist - nothing to do"
    raise Errno::ENOENT, "#{new_resource}: task does not exist, cannot enable"
  end
end

#action_endObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/provider/windows_task.rb', line 146

def action_end
  if current_resource.exists
    if current_resource.status != :running
      Chef::Log.debug "#{new_resource} is not running - nothing to do"
    else
      run_schtasks "END"
      new_resource.updated_by_last_action true
      Chef::Log.info "#{new_resource} task ended"
    end
  else
    Chef::Log.warn "#{new_resource} task doesn't exist - nothing to do"
  end
end

#action_runObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/provider/windows_task.rb', line 121

def action_run
  if current_resource.exists
    if current_resource.status == :running
      Chef::Log.info "#{new_resource} task is currently running, skipping run"
    else
      run_schtasks "RUN"
      new_resource.updated_by_last_action true
      Chef::Log.info "#{new_resource} task ran"
    end
  else
    Chef::Log.warn "#{new_resource} task doesn't exists - 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