summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2014-10-16 15:43:58 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2014-10-22 13:03:30 -0700
commit7412b9721d853bc7afb78c5dc1f3d74441596dd1 (patch)
tree8c6bf6248cb6657f4486e556c57ae3cd2f3a9bf3
parent7993d05ac6251b06093f282becf75be7a8a49bd0 (diff)
downloadchef-7412b9721d853bc7afb78c5dc1f3d74441596dd1.tar.gz
Use windows evt log by default on windows
-rw-r--r--client.rb.windows3
-rw-r--r--lib/chef/client.rb20
-rw-r--r--lib/chef/config.rb10
-rw-r--r--lib/chef/event_loggers/base.rb62
-rw-r--r--lib/chef/event_loggers/windows_eventlog.rb (renamed from lib/chef/logging/windows_eventlog.rb)12
-rw-r--r--lib/chef/logging/eventlog.rb24
-rw-r--r--spec/functional/event_loggers/windows_eventlog_spec.rb (renamed from spec/functional/logging/windows_eventlog_spec.rb)10
7 files changed, 106 insertions, 35 deletions
diff --git a/client.rb.windows b/client.rb.windows
deleted file mode 100644
index ebff235620..0000000000
--- a/client.rb.windows
+++ /dev/null
@@ -1,3 +0,0 @@
-require 'chef/logging/windows_eventlog'
-
-add_event_logger Chef::Logging::WindowsEventLogger.new
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index e8ff352116..4f37bd0ee3 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -36,6 +36,8 @@ require 'chef/cookbook/file_vendor'
require 'chef/cookbook/file_system_file_vendor'
require 'chef/cookbook/remote_file_vendor'
require 'chef/event_dispatch/dispatcher'
+require 'chef/event_loggers/base'
+require 'chef/event_loggers/windows_eventlog'
require 'chef/formatters/base'
require 'chef/formatters/doc'
require 'chef/formatters/minimal'
@@ -151,7 +153,7 @@ class Chef
@runner = nil
@ohai = Ohai::System.new
- event_handlers = configure_formatters
+ event_handlers = configure_formatters + configure_event_loggers
event_handlers += Array(Chef::Config[:event_handlers])
@events = EventDispatch::Dispatcher.new(*event_handlers)
@@ -191,6 +193,22 @@ class Chef
end
end
+ def configure_event_loggers
+ if Chef::Config.disable_event_logger
+ []
+ else
+ Chef::Config.event_loggers.map do |evt_logger|
+ case evt_logger
+ when Symbol
+ Chef::EventLoggers.new(evt_logger)
+ when Class
+ evt_logger.new
+ else
+ end
+ end
+ end
+ end
+
# Instantiates a Chef::Node object, possibly loading the node's prior state
# when using chef-client. Delegates to policy_builder
#
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 755e203f58..d033a2981b 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -78,7 +78,6 @@ class Chef
formatters << [name, file_path]
end
- #
def self.add_event_logger(logger)
event_handlers << logger
end
@@ -456,6 +455,15 @@ class Chef
# Event Handlers
default :event_handlers, []
+ default :disable_event_loggers, false
+ default :event_loggers do
+ evt_loggers = []
+ if Chef::Platform::windows?
+ evt_loggers << :win_evt
+ end
+ evt_loggers
+ end
+
# Exception Handlers
default :exception_handlers, []
diff --git a/lib/chef/event_loggers/base.rb b/lib/chef/event_loggers/base.rb
new file mode 100644
index 0000000000..1f676dd516
--- /dev/null
+++ b/lib/chef/event_loggers/base.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Jay Mundrawala (<jdm@getchef.com>)
+#
+# Copyright:: 2014, Chef Software, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/event_dispatch/base'
+
+class Chef
+ module EventLoggers
+ class UnknownEventLogger < StandardError; end
+ class UnavailableEventLogger < StandardError; end
+
+ def self.event_loggers_by_name
+ @event_loggers_by_name ||= {}
+ end
+
+ def self.register(name, logger)
+ event_loggers_by_name[name.to_s] = logger
+ end
+
+ def self.by_name(name)
+ event_loggers_by_name[name]
+ end
+
+ def self.available_event_loggers
+ event_loggers_by_name.select do |key, val|
+ val.available?
+ end.keys
+ end
+
+ def self.new(name)
+ event_logger_class = by_name(name.to_s) or
+ raise UnknownEventLogger, "No event logger found for #{name} (available: #{available_event_loggers.join(', ')})"
+ raise UnavailableEventLogger unless available_event_loggers.include? name.to_s
+ event_logger_class.new
+ end
+
+ class Base < EventDispatch::Base
+ def self.short_name(name)
+ Chef::EventLoggers.register(name, self)
+ end
+
+ # Returns true if this implementation of EventLoggers can be used
+ def self.available?
+ false
+ end
+ end
+ end
+end
diff --git a/lib/chef/logging/windows_eventlog.rb b/lib/chef/event_loggers/windows_eventlog.rb
index fc444414b5..e3bbbfa1e6 100644
--- a/lib/chef/logging/windows_eventlog.rb
+++ b/lib/chef/event_loggers/windows_eventlog.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
-require 'chef/logging/eventlog'
+require 'chef/event_loggers/base'
require 'chef/platform/query_helpers'
if Chef::Platform::windows?
@@ -30,8 +30,10 @@ if Chef::Platform::windows?
end
class Chef
- module Logging
- class WindowsEventLogger < EventDispatch::Base
+ module EventLoggers
+ class WindowsEventLogger < EventLoggers::Base
+ short_name(:win_evt)
+
# These must match those that are defined in the manifest file
RUN_START_EVENT_ID = 10000
RUN_STARTED_EVENT_ID = 10001
@@ -44,6 +46,10 @@ class Chef
# Since we must install the event logger, this is not really configurable
SOURCE = 'Chef'
+ def self.available?
+ return Chef::Platform::windows?
+ end
+
def initialize
@eventlog = EventLog::open('Application')
end
diff --git a/lib/chef/logging/eventlog.rb b/lib/chef/logging/eventlog.rb
deleted file mode 100644
index 7157785d32..0000000000
--- a/lib/chef/logging/eventlog.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-#
-# Author:: Jay Mundrawala (<jdm@getchef.com>)
-#
-# Copyright:: 2014, Chef Software, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'chef/event_dispatch/base'
-
-class Chef
- module Logging
- end
-end
diff --git a/spec/functional/logging/windows_eventlog_spec.rb b/spec/functional/event_loggers/windows_eventlog_spec.rb
index b86595e053..9da9f60fa9 100644
--- a/spec/functional/logging/windows_eventlog_spec.rb
+++ b/spec/functional/event_loggers/windows_eventlog_spec.rb
@@ -18,17 +18,17 @@
require 'spec_helper'
require 'securerandom'
-require 'chef/logging/windows_eventlog'
+require 'chef/event_loggers/windows_eventlog'
if Chef::Platform.windows?
require 'win32/eventlog'
include Win32
end
-describe Chef::Logging::WindowsEventLogger, :windows_only do
+describe Chef::EventLoggers::WindowsEventLogger, :windows_only do
let(:run_id) { SecureRandom.uuid }
let(:version) { SecureRandom.uuid }
let(:elapsed_time) { SecureRandom.random_number(100) }
- let(:logger) { Chef::Logging::WindowsEventLogger.new }
+ let(:logger) { Chef::EventLoggers::WindowsEventLogger.new }
let(:flags) { nil }
let(:node) { nil }
let(:run_status) { double('Run Status', {run_id: run_id, elapsed_time: elapsed_time }) }
@@ -36,6 +36,10 @@ describe Chef::Logging::WindowsEventLogger, :windows_only do
let!(:offset) { event_log.read_last_event.record_number }
let(:mock_exception) { double('Exception', {message: SecureRandom.uuid, backtrace:[SecureRandom.uuid, SecureRandom.uuid]})}
+ it 'is available' do
+ Chef::EventLoggers::WindowsEventLogger.available?.should be_true
+ end
+
it 'writes run_start event with event_id 10000 and contains version' do
logger.run_start(version)