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

RUNNING =
'running'
STOPPED =
'stopped'
AUTO_START =
'auto start'
DISABLED =
'disabled'

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #define_resource_requirements, #initialize, #load_new_resource_state, #reload_service, #shared_resource_requirements

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #cookbook_name, #define_resource_requirements, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #set_updated_status, #whyrun_mode?

Methods included from DSL::Recipe

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Method Details

#disable_serviceObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/provider/service/windows.rb', line 123

def disable_service
  if Win32::Service.exists?(@new_resource.service_name)
    if start_type == AUTO_START
      Win32::Service.configure(
        :service_name => @new_resource.service_name,
        :start_type => Win32::Service::DISABLED
      )
      @new_resource.updated_by_last_action(true)
    else
      Chef::Log.debug "#{@new_resource} already disabled - nothing to do"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#enable_serviceObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/provider/service/windows.rb', line 107

def enable_service
  if Win32::Service.exists?(@new_resource.service_name)
    if start_type == AUTO_START
      Chef::Log.debug "#{@new_resource} already enabled - nothing to do"
    else
      Win32::Service.configure(
        :service_name => @new_resource.service_name,
        :start_type => Win32::Service::AUTO_START
      )
      @new_resource.updated_by_last_action(true)
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#load_current_resourceObject



40
41
42
43
44
45
46
47
48
# File 'lib/chef/provider/service/windows.rb', line 40

def load_current_resource
  @current_resource = Chef::Resource::Service.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}"
  @current_resource.enabled(start_type == AUTO_START)
  Chef::Log.debug "#{@new_resource} enabled: #{@current_resource.enabled}"
  @current_resource
end

#restart_serviceObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/provider/service/windows.rb', line 92

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



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

def start_service
  if Win32::Service.exists?(@new_resource.service_name)
    if current_state == RUNNING
      Chef::Log.debug "#{@new_resource} already started - nothing to do"
    else
      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
          Win32::Service.start(@new_resource.service_name)
          wait_for_state(RUNNING)
        end
      end
      @new_resource.updated_by_last_action(true)
    end
  else
      Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#stop_serviceObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/provider/service/windows.rb', line 71

def stop_service
  if Win32::Service.exists?(@new_resource.service_name)
    if current_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)
          wait_for_state(STOPPED)
        end
      end
      @new_resource.updated_by_last_action(true)
    else
      Chef::Log.debug "#{@new_resource} already stopped - nothing to do"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/chef/provider/service/windows.rb', line 36

def whyrun_supported?
  false
end