Class: Chef::Provider::Service::Windows

Inherits:
Chef::Provider::Service show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/service/windows.rb

Constant Summary collapse

AUTO_START =

Win32::Service.get_start_type

"auto start"
MANUAL =
"demand start"
DISABLED =
"disabled"
RUNNING =

Win32::Service.get_current_state

"running"
STOPPED =
"stopped"
CONTINUE_PENDING =
"continue pending"
PAUSE_PENDING =
"pause pending"
PAUSED =
"paused"
START_PENDING =
"start pending"
STOP_PENDING =
"stop pending"
TIMEOUT =
60
SERVICE_RIGHT =
"SeServiceLogonRight"

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

#action_mask, #action_reload, #action_restart, #action_start, #action_stop, #action_unmask, #define_resource_requirements, #initialize, #load_new_resource_state, #mask_service, #reload_service, #shared_resource_requirements, #supports, #unmask_service, #user_services_requirements

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 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 DSL::PlatformIntrospection

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

Constructor Details

This class inherits a constructor from Chef::Provider::Service

Instance Method Details

#action_configure_startupObject


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/chef/provider/service/windows.rb', line 204

def action_configure_startup
  case @new_resource.startup_type
  when :automatic
    if current_start_type != AUTO_START
      converge_by("set service #{@new_resource} startup type to automatic") do
        set_startup_type(:automatic)
      end
    else
      Chef::Log.debug("#{@new_resource} startup_type already automatic - nothing to do")
    end
  when :manual
    if current_start_type != MANUAL
      converge_by("set service #{@new_resource} startup type to manual") do
        set_startup_type(:manual)
      end
    else
      Chef::Log.debug("#{@new_resource} startup_type already manual - nothing to do")
    end
  when :disabled
    if current_start_type != DISABLED
      converge_by("set service #{@new_resource} startup type to disabled") do
        set_startup_type(:disabled)
      end
    else
      Chef::Log.debug("#{@new_resource} startup_type already disabled - nothing to do")
    end
  end

  # Avoid changing enabled from true/false for now
  @new_resource.enabled(nil)
end

#action_disableObject


191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/chef/provider/service/windows.rb', line 191

def action_disable
  if current_start_type != DISABLED
    converge_by("disable service #{@new_resource}") do
      disable_service
      Chef::Log.info("#{@new_resource} disabled")
    end
  else
    Chef::Log.debug("#{@new_resource} already disabled - nothing to do")
  end
  load_new_resource_state
  @new_resource.enabled(false)
end

#action_enableObject


178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chef/provider/service/windows.rb', line 178

def action_enable
  if current_start_type != AUTO_START
    converge_by("enable service #{@new_resource}") do
      enable_service
      Chef::Log.info("#{@new_resource} enabled")
    end
  else
    Chef::Log.debug("#{@new_resource} already enabled - nothing to do")
  end
  load_new_resource_state
  @new_resource.enabled(true)
end

#disable_serviceObject


170
171
172
173
174
175
176
# File 'lib/chef/provider/service/windows.rb', line 170

def disable_service
  if Win32::Service.exists?(@new_resource.service_name)
    set_startup_type(:disabled)
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#enable_serviceObject


162
163
164
165
166
167
168
# File 'lib/chef/provider/service/windows.rb', line 162

def enable_service
  if Win32::Service.exists?(@new_resource.service_name)
    set_startup_type(:automatic)
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#load_current_resourceObject


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/provider/service/windows.rb', line 52

def load_current_resource
  @current_resource = Chef::Resource::WindowsService.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)
  @current_resource.running(current_state == RUNNING)
  Chef::Log.debug "#{@new_resource} running: #{@current_resource.running}"
  case current_start_type
  when AUTO_START
    @current_resource.enabled(true)
  when DISABLED
    @current_resource.enabled(false)
  end
  Chef::Log.debug "#{@new_resource} enabled: #{@current_resource.enabled}"
  @current_resource
end

#restart_serviceObject


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/provider/service/windows.rb', line 147

def restart_service
  if Win32::Service.exists?(@new_resource.service_name)
    if @new_resource.restart_command
      Chef::Log.debug "#{@new_resource} restarting service using the given restart_command"
      shell_out!(@new_resource.restart_command)
    else
      stop_service
      start_service
    end
    @new_resource.updated_by_last_action(true)
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#start_serviceObject


67
68
69
70
71
72
73
74
75
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
# File 'lib/chef/provider/service/windows.rb', line 67

def start_service
  if Win32::Service.exists?(@new_resource.service_name)
    # reconfiguration is idempotent, so just do it.
    new_config = {
      service_name: @new_resource.service_name,
      service_start_name: @new_resource.run_as_user,
      password: @new_resource.run_as_password,
    }.reject { |k, v| v.nil? || v.length == 0 }

    Win32::Service.configure(new_config)
    Chef::Log.info "#{@new_resource} configured with #{new_config.inspect}"

    if new_config.has_key?(:service_start_name)
      unless Chef::ReservedNames::Win32::Security.(canonicalize_username(new_config[:service_start_name])).include?(SERVICE_RIGHT)
        grant_service_logon(new_config[:service_start_name])
      end
    end

    state = current_state
    if state == RUNNING
      Chef::Log.debug "#{@new_resource} already started - nothing to do"
    elsif state == START_PENDING
      Chef::Log.debug "#{@new_resource} already sent start signal - waiting for start"
      wait_for_state(RUNNING)
    elsif state == STOPPED
      if @new_resource.start_command
        Chef::Log.debug "#{@new_resource} starting service using the given start_command"
        shell_out!(@new_resource.start_command)
      else
        spawn_command_thread do
          begin
            Win32::Service.start(@new_resource.service_name)
          rescue SystemCallError => ex
            if ex.errno == ERROR_SERVICE_LOGON_FAILED
              Chef::Log.error ex.message
              raise Chef::Exceptions::Service,
              "Service #{@new_resource} did not start due to a logon failure (error #{ERROR_SERVICE_LOGON_FAILED}): possibly the specified user '#{@new_resource.run_as_user}' does not have the 'log on as a service' privilege, or the password is incorrect."
            else
              raise ex
            end
          end
        end
        wait_for_state(RUNNING)
      end
      @new_resource.updated_by_last_action(true)
    else
      raise Chef::Exceptions::Service, "Service #{@new_resource} can't be started from state [#{state}]"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#stop_serviceObject


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
# File 'lib/chef/provider/service/windows.rb', line 120

def stop_service
  if Win32::Service.exists?(@new_resource.service_name)
    state = current_state
    if state == RUNNING
      if @new_resource.stop_command
        Chef::Log.debug "#{@new_resource} stopping service using the given stop_command"
        shell_out!(@new_resource.stop_command)
      else
        spawn_command_thread do
          Win32::Service.stop(@new_resource.service_name)
        end
        wait_for_state(STOPPED)
      end
      @new_resource.updated_by_last_action(true)
    elsif state == STOPPED
      Chef::Log.debug "#{@new_resource} already stopped - nothing to do"
    elsif state == STOP_PENDING
      Chef::Log.debug "#{@new_resource} already sent stop signal - waiting for stop"
      wait_for_state(STOPPED)
    else
      raise Chef::Exceptions::Service, "Service #{@new_resource} can't be stopped from state [#{state}]"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end