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

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods inherited from Simple

#ps_cmd, #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, #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, build_from_file, #cookbook_name, #node, #resource_collection

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(new_resource, run_context) ⇒ Windows

Returns a new instance of Windows.



26
27
28
29
# File 'lib/chef/provider/service/windows.rb', line 26

def initialize(new_resource, run_context)
  super
  @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



130
131
132
133
134
135
136
137
138
# File 'lib/chef/provider/service/windows.rb', line 130

def disable_service()
  begin
    popen4("#{@init_command} config #{@new_resource.service_name} start= disabled") do |pid, stdin, stdout, stderr|
      stdout.readlines.join =~ /SUCCESS/
    end
  rescue Exception => e
    raise Chef::Exceptions::Service, "Failed to start service #{@new_resource.service_name}: #{e.message}"
  end
end

#enable_serviceObject



120
121
122
123
124
125
126
127
128
# File 'lib/chef/provider/service/windows.rb', line 120

def enable_service()
  begin
    popen4("#{@init_command} config #{@new_resource.service_name} start= #{determine_startup_type}") do |pid, stdin, stdout, stderr|
      stdout.readlines.join =~ /SUCCESS/
    end
  rescue Exception => e
    raise Chef::Exceptions::Service, "Failed to start service #{@new_resource.service_name}: #{e.message}"
  end
end

#io_popen(command) ⇒ Object



31
32
33
34
35
36
# File 'lib/chef/provider/service/windows.rb', line 31

def io_popen(command)
  io = IO.popen(command)
  entries = io.readlines
  io.close
  entries
end

#load_current_resourceObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/provider/service/windows.rb', line 38

def load_current_resource
  @current_resource = Chef::Resource::Service.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)
  begin
    # Check if service is running
    status = popen4("#{@init_command} query #{@new_resource.service_name}") do |pid, stdin, stdout, stderr|
      stdout.each_line do |line|
        raise Chef::Exceptions::Service, "Service #{@new_resource.service_name} does not exist.\n#{stdout}\n" if line =~ /FAILED 1060/
        @current_resource.running true if line =~/RUNNING/
      end
    end

    # Check if service is enabled
    status = popen4("#{@init_command} qc #{@new_resource.service_name}") do |pid, stdin, stdout, stderr|
      stdout.each_line do |line|
        raise Chef::Exceptions::Service, "Service #{@new_resource.service_name} does not exist.\n#{stdout}\n" if line =~ /FAILED 1060/
        @current_resource.enabled true if line =~/AUTO_START/
      end
    end

    Chef::Log.debug "#{@new_resource} running: #{@current_resource.running}"
  rescue Exception => e
    raise Chef::Exceptions::Service, "Exception determining state of service #{@new_resource.service_name}: #{e.message}"
  end
  @current_resource
end

#restart_serviceObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provider/service/windows.rb', line 103

def restart_service
  begin
    if @new_resource.restart_command
      Chef::Log.debug "#{@new_resource} restarting service using the given restart_command"
      popen4(@new_resource.restart_command) do |pid, stdin, stdout, stderr|
        Chef::Log.debug stdout.readlines
      end
    else
      stop_service
      sleep 1
      start_service
    end
  rescue Exception => e
    raise Chef::Exceptions::Service, "Failed to start service #{@new_resource.service_name}: #{e.message}"
  end
end

#start_serviceObject



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

def start_service
  begin
    if @new_resource.start_command
      popen4(@new_resource.start_command) do |pid, stdin, stdout, stderr|
        Chef::Log.debug stdout.readlines
      end
    else
      popen4("#{@init_command} start #{@new_resource.service_name}") do |pid, stdin, stdout, stderr|
        output = stdout.readlines
        Chef::Log.debug output.join
        output.join =~ /RUNNING/ || output.join =~ /START_PENDING/ ? true : false
      end
    end
  rescue Exception => e
    raise Chef::Exceptions::Service, "Failed to start service #{@new_resource.service_name}: #{e.message}"
  end
end

#stop_serviceObject



83
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 83

def stop_service
  begin
    if @new_resource.stop_command
      Chef::Log.debug "#{@new_resource} stopping service using the given stop_command"
      popen4(@new_resource.stop_command) do |pid, stdin, stdout, stderr|
        Chef::Log.debug stdout.readlines
      end
    else
      popen4("#{@init_command} stop #{@new_resource.service_name}") do |pid, stdin, stdout, stderr|
        output = stdout.readlines
        Chef::Log.debug output.join
        raise Chef::Exceptions::Service, "Service #{@new_resource.service_name} has dependencies and cannot be stopped.\n" if output.join =~ /FAILED 1051/
        output.join =~ /1/
      end
    end
  rescue Exception => e
    raise Chef::Exceptions::Service, "Failed to start service #{@new_resource.service_name}: #{e.message}"
  end
end