Class: Chef::Application::WindowsServiceManager

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/chef/application/windows_service_manager.rb

Overview

This class is used to create and manage a windows service. Service should be created using Daemon class from win32/service gem. For an example see: Chef::Application::WindowsService

Outside programs are expected to use this class to manage windows services.

Instance Method Summary collapse

Constructor Details

#initialize(service_options) ⇒ WindowsServiceManager

Returns a new instance of WindowsServiceManager

Raises:

  • (ArgumentError)

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef/application/windows_service_manager.rb', line 74

def initialize(service_options)
  # having to call super in initialize is the most annoying
  # anti-pattern :(
  super()

  raise ArgumentError, "Service definition is not provided" if service_options.nil?

  required_options = %i{service_name service_display_name service_description service_file_path}

  required_options.each do |req_option|
    unless service_options.key?(req_option)
      raise ArgumentError, "Service definition doesn't contain required option #{req_option}"
    end
  end

  @service_name = service_options[:service_name]
  @service_display_name = service_options[:service_display_name]
  @service_description = service_options[:service_description]
  @service_file_path = service_options[:service_file_path]
  @service_start_name = service_options[:run_as_user]
  @password = service_options[:run_as_password]
  @delayed_start = service_options[:delayed_start]
  @dependencies = service_options[:dependencies]
end

Instance Method Details

#run(params = ARGV) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/application/windows_service_manager.rb', line 99

def run(params = ARGV)
  parse_options(params)

  case config[:action]
  when "install"
    if service_exists?
      puts "Service #{@service_name} already exists on the system."
    else
      ruby = File.join(RbConfig::CONFIG["bindir"], "ruby")

      opts = ""
      opts << " -c #{config[:config_file]}" if config[:config_file]
      opts << " -L #{config[:log_location]}" if config[:log_location]

      # Quote the full paths to deal with possible spaces in the path name.
      # Also ensure all forward slashes are backslashes
      cmd = "\"#{ruby}\" \"#{@service_file_path}\" #{opts}".gsub(File::SEPARATOR, File::ALT_SEPARATOR)

      ::Win32::Service.new(
        service_name: @service_name,
        display_name: @service_display_name,
        description: @service_description,
        # Prior to 0.8.5, win32-service creates interactive services by default,
        # and we don't want that, so we need to override the service type.
        service_type: ::Win32::Service::SERVICE_WIN32_OWN_PROCESS,
        start_type: ::Win32::Service::SERVICE_AUTO_START,
        binary_path_name: cmd,
        service_start_name: @service_start_name,
        password: @password,
        dependencies: @dependencies
      )
      unless @delayed_start.nil?
        ::Win32::Service.configure(
          service_name: @service_name,
          delayed_start: @delayed_start
        )
      end
      puts "Service '#{@service_name}' has successfully been installed."
    end
  when "status"
    if !service_exists?
      puts "Service #{@service_name} doesn't exist on the system."
    else
      puts "State of #{@service_name} service is: #{current_state}"
    end
  when "start"
    # TODO: allow override of startup parameters here?
    take_action("start", RUNNING)
  when "stop"
    take_action("stop", STOPPED)
  when "uninstall", "delete"
    take_action("stop", STOPPED)
    unless service_exists?
      puts "Service #{@service_name} doesn't exist on the system."
    else
      ::Win32::Service.delete(@service_name)
      puts "Service #{@service_name} deleted"
    end
  when "pause"
    take_action("pause", PAUSED)
  when "resume"
    take_action("resume", RUNNING)
  end
end