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


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/provider/windows_task.rb', line 60

def action_create
  if @current_resource.exists && !(task_need_update? || @new_resource.force)
    Chef::Log.info "#{@new_resource} task already exists - nothing to do"
  else
    options = {}
    options["F"] = "" if @new_resource.force || task_need_update?
    options["SC"] = schedule
    options["MO"] = @new_resource.frequency_modifier if frequency_modifier_allowed
    options["I"]  = @new_resource.idle_time unless @new_resource.idle_time.nil?
    options["SD"] = @new_resource.start_day unless @new_resource.start_day.nil?
    options["ST"] = @new_resource.start_time unless @new_resource.start_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
end

#action_deleteObject


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

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


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

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


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

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


116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef/provider/windows_task.rb', line 116

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


91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/provider/windows_task.rb', line 91

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

#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
  @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

#set_current_resource(task_hash) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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]
  @current_resource.execution_time_limit(task_hash[:execution_time_limit]) if task_hash[:execution_time_limit]

  @current_resource.status = :running if task_hash[:Status] == "Running"
  @current_resource.enabled = true if task_hash[:ScheduledTaskState] == "Enabled"
end