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

Inherits:
Init show all
Defined in:
lib/chef/provider/service/windows.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods inherited from Init

#reload_service

Methods inherited from Simple

#reload_service

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #reload_service

Methods included from Mixin::Command

chdir_or_tmpdir, handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(node, new_resource, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Windows

Returns a new instance of Windows.



23
24
25
26
# File 'lib/chef/provider/service/windows.rb', line 23

def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
  super(node, new_resource, collection, definitions, cookbook_loader)
  @init_command = "sc"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#disable_serviceObject



113
114
115
116
117
118
119
120
121
# File 'lib/chef/provider/service/windows.rb', line 113

def disable_service()
  begin
    Chef::Log.debug result = IO.popen("#{@init_command} config #{@new_resource.service_name} start= disabled").readlines.join
    result.include?('SUCCESS')
  rescue
    Chef::Log.debug "Failed to disable service #{@new_resource.service_name}"
    false
  end
end

#enable_serviceObject



103
104
105
106
107
108
109
110
111
# File 'lib/chef/provider/service/windows.rb', line 103

def enable_service()
  begin
    Chef::Log.debug result = IO.popen("#{@init_command} config #{@new_resource.service_name} start= #{determine_startup_type}").readlines.join
    result.include?('SUCCESS')
  rescue
    Chef::Log.debug "Failed to enable service #{@new_resource.service_name}"
    false
  end
end

#load_current_resourceObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef/provider/service/windows.rb', line 28

def load_current_resource
  @current_resource = Chef::Resource::Service.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)
  status = IO.popen("#{@init_command} query #{@new_resource.service_name}").entries
  raise Chef::Exceptions::Exec, "Service #{@new_resource.service_name} does not exist.\n#{status.join}\n" if status[0].include?("FAILED 1060")

  begin
    started = status[3].include?("4")
    @current_resource.running started

    start_type = IO.popen("#{@init_command} qc #{@new_resource.service_name}").entries[4]
    @current_resource.enabled(start_type.include?('2') || start_type.include?('3') ? true : false)

    Chef::Log.debug "#{@new_resource}: running: #{@current_resource.running}"
  rescue StandardError
    raise Chef::Exceptions::Exec
  rescue Chef::Exceptions::Exec
    Chef::Log.debug "Failed to determine the current status of the service, assuming it is not running"
    @current_resource.running false
    nil
  end
  @current_resource
end

#restart_serviceObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/provider/service/windows.rb', line 84

def restart_service
  begin
    if @new_resource.restart_command
      Chef::Log.debug "restarting service using the given restart_command"
      result = IO.popen(@new_resource.restart_command).readlines
      Chef::Log.debug result.join
    else
      Chef::Log.debug IO.popen("#{@init_command} stop #{@new_resource.service_name}").readlines.join
      sleep 1
      result = IO.popen("#{@init_command} start #{@new_resource.service_name}").readlines
      Chef::Log.debug result.join
    end
    result[3].include?('4') || result.include?('2')
  rescue
    Chef::Log.debug "Failed to restart service #{@new_resource.service_name}"
    false
  end
end

#start_serviceObject



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

def start_service
  begin
    result = if @new_resource.start_command
               Chef::Log.debug "starting service using the given start_command"
               IO.popen(@new_resource.start_command).readlines
             else
               IO.popen("#{@init_command} start #{@new_resource.service_name}").readlines
             end
    Chef::Log.debug result.join
    result[3].include?('4') || result.include?('2') ? true : false
  rescue
    Chef::Log.debug "Failed to start service #{@new_resource.service_name}"
    false
  end
end

#stop_serviceObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/provider/service/windows.rb', line 68

def stop_service
  begin
    Chef::Log.debug "stopping service using the given stop_command"
    result = if @new_resource.stop_command
               IO.popen(@new_resource.stop_command).readlines
             else
               IO.popen("#{@init_command} stop #{@new_resource.service_name}").readlines
             end
    Chef::Log.debug result.join
    result[3].include?('1')
  rescue
    Chef::Log.debug "Failed to stop service #{@new_resource.service_name}"
    false
  end
end