diff options
-rwxr-xr-x | bin/chef-service-manager | 8 | ||||
-rw-r--r-- | lib/chef/application/windows_service_manager.rb | 79 |
2 files changed, 43 insertions, 44 deletions
diff --git a/bin/chef-service-manager b/bin/chef-service-manager index c7f2fa43a9..781fd116de 100755 --- a/bin/chef-service-manager +++ b/bin/chef-service-manager @@ -24,7 +24,13 @@ require 'chef' require 'chef/application/windows_service_manager' if Chef::Platform.windows? - Chef::Application::WindowsServiceManager.new.run + chef_client_service = { + :service_name => "chef-client", + :service_display_name => "Chef Client Service", + :service_description => "Runs Opscode Chef Client on regular, configurable intervals.", + :service_file_path => File.expand_path(File.join(File.dirname(__FILE__), '../lib/chef/application/windows_service.rb')) + } + Chef::Application::WindowsServiceManager.new(chef_client_service).run else puts "chef-service-manager is only available on Windows platforms." end diff --git a/lib/chef/application/windows_service_manager.rb b/lib/chef/application/windows_service_manager.rb index ef93e88517..83e89b9cfb 100644 --- a/lib/chef/application/windows_service_manager.rb +++ b/lib/chef/application/windows_service_manager.rb @@ -28,7 +28,7 @@ class Chef option :action, :short => "-a ACTION", :long => "--action ACTION", - :default => "start", + :default => "status", :description => "Action to carry out on chef-service (install, uninstall, status, start, stop, pause, or resume)" option :config_file, @@ -43,18 +43,6 @@ class Chef :description => "Set the log file location for chef-service", :default => "#{ENV['SYSTEMDRIVE']}/chef/client.log" - option :splay, - :short => "-s SECONDS", - :long => "--splay SECONDS", - :description => "The splay time for running at intervals, in seconds", - :proc => lambda { |s| s.to_i } - - option :interval, - :short => "-i SECONDS", - :long => "--interval SECONDS", - :description => "Set the number of seconds to wait between chef-client runs", - :proc => lambda { |s| s.to_i } - option :help, :short => "-h", :long => "--help", @@ -64,57 +52,62 @@ class Chef :show_options => true, :exit => 0 - CHEF_SERVICE_NAME = "chef-client" - CHEF_SERVICE_DISPLAY_NAME = "Chef-Client Service" - CHEF_SERVICE_DESCRIPTION = "Runs chef-client periodically" - + def initialize(service_options) + # having to call super in initialize is the most annoying + # anti-pattern :( + super() + + @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] + end + def run parse_options case config[:action] when 'install' if service_exists? - puts "Service #{CHEF_SERVICE_NAME} already exists on the system." + puts "Service #{@service_name} already exists on the system." else ruby = File.join(RbConfig::CONFIG['bindir'], 'ruby') - path = File.expand_path(File.join(File.dirname(__FILE__), 'windows_service.rb')) - + opts = "" opts << " -c #{config[:config_file]}" if config[:config_file] opts << " -L #{config[:log_location]}" if config[:log_location] - opts << " -i #{config[:interval]}" if config[:interval] - opts << " -s #{config[:splay]}" if config[:splay] - + # Quote the full paths to deal with possible spaces in the path name. # Also ensure all forward slashes are backslashes - cmd = "\"#{ruby}\" \"#{path}\" #{opts}".gsub(File::SEPARATOR, File::ALT_SEPARATOR) - + cmd = "\"#{ruby}\" \"#{@service_file_path}\" #{opts}".gsub(File::SEPARATOR, File::ALT_SEPARATOR) + ::Win32::Service.new( - :service_name => CHEF_SERVICE_NAME, - :display_name => CHEF_SERVICE_DISPLAY_NAME, - :description => CHEF_SERVICE_DESCRIPTION, + :service_name => @service_name, + :display_name => @service_display_name, + :description => @service_description, :start_type => ::Win32::Service::SERVICE_AUTO_START, - :binary_path_name => cmd) - puts "Service '#{CHEF_SERVICE_NAME}' has successfully been installed." + :binary_path_name => cmd + ) + puts "Service '#{@service_name}' has successfully been installed." end when 'status' if !service_exists? - puts "Service #{CHEF_SERVICE_NAME} doesn't exist on the system." + puts "Service #{@service_name} doesn't exist on the system." else - puts "State of #{CHEF_SERVICE_NAME} service is: #{current_state}" + 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) + take_action('stop', STOPPED) when 'uninstall', 'delete' take_action('stop', STOPPED) unless service_exists? - puts "Service #{CHEF_SERVICE_NAME} doesn't exist on the system." + puts "Service #{@service_name} doesn't exist on the system." else - ::Win32::Service.delete(CHEF_SERVICE_NAME) - puts "Service #{CHEF_SERVICE_NAME} deleted" + ::Win32::Service.delete(@service_name) + puts "Service #{@service_name} deleted" end when 'pause' take_action('pause', PAUSED) @@ -131,25 +124,25 @@ class Chef PAUSED = "paused" def service_exists? - return ::Win32::Service.exists?(CHEF_SERVICE_NAME) + return ::Win32::Service.exists?(@service_name) end - + def take_action(action=nil, desired_state=nil) if service_exists? if current_state != desired_state - ::Win32::Service.send(action, CHEF_SERVICE_NAME) + ::Win32::Service.send(action, @service_name) wait_for_state(desired_state) - puts "Service '#{CHEF_SERVICE_NAME}' is now '#{current_state}'." + puts "Service '#{@service_name}' is now '#{current_state}'." else - puts "Service '#{CHEF_SERVICE_NAME}' is already '#{desired_state}'." + puts "Service '#{@service_name}' is already '#{desired_state}'." end else - puts "Cannot '#{action}' service '#{CHEF_SERVICE_NAME}', service does not exist." + puts "Cannot '#{action}' service '#{@service_name}', service does not exist." end end def current_state - ::Win32::Service.status(CHEF_SERVICE_NAME).current_state + ::Win32::Service.status(@service_name).current_state end # Helper method that waits for a status to change its state since state |