right_agent 2.0.7-x86-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +82 -0
  3. data/Rakefile +113 -0
  4. data/lib/right_agent.rb +59 -0
  5. data/lib/right_agent/actor.rb +182 -0
  6. data/lib/right_agent/actor_registry.rb +76 -0
  7. data/lib/right_agent/actors/agent_manager.rb +232 -0
  8. data/lib/right_agent/agent.rb +1149 -0
  9. data/lib/right_agent/agent_config.rb +480 -0
  10. data/lib/right_agent/agent_identity.rb +210 -0
  11. data/lib/right_agent/agent_tag_manager.rb +237 -0
  12. data/lib/right_agent/audit_formatter.rb +107 -0
  13. data/lib/right_agent/clients.rb +31 -0
  14. data/lib/right_agent/clients/api_client.rb +383 -0
  15. data/lib/right_agent/clients/auth_client.rb +247 -0
  16. data/lib/right_agent/clients/balanced_http_client.rb +369 -0
  17. data/lib/right_agent/clients/base_retry_client.rb +495 -0
  18. data/lib/right_agent/clients/right_http_client.rb +279 -0
  19. data/lib/right_agent/clients/router_client.rb +493 -0
  20. data/lib/right_agent/command.rb +30 -0
  21. data/lib/right_agent/command/agent_manager_commands.rb +150 -0
  22. data/lib/right_agent/command/command_client.rb +136 -0
  23. data/lib/right_agent/command/command_constants.rb +33 -0
  24. data/lib/right_agent/command/command_io.rb +126 -0
  25. data/lib/right_agent/command/command_parser.rb +87 -0
  26. data/lib/right_agent/command/command_runner.rb +118 -0
  27. data/lib/right_agent/command/command_serializer.rb +63 -0
  28. data/lib/right_agent/connectivity_checker.rb +179 -0
  29. data/lib/right_agent/console.rb +65 -0
  30. data/lib/right_agent/core_payload_types.rb +44 -0
  31. data/lib/right_agent/core_payload_types/cookbook.rb +61 -0
  32. data/lib/right_agent/core_payload_types/cookbook_position.rb +46 -0
  33. data/lib/right_agent/core_payload_types/cookbook_repository.rb +116 -0
  34. data/lib/right_agent/core_payload_types/cookbook_sequence.rb +70 -0
  35. data/lib/right_agent/core_payload_types/dev_repositories.rb +100 -0
  36. data/lib/right_agent/core_payload_types/dev_repository.rb +76 -0
  37. data/lib/right_agent/core_payload_types/event_categories.rb +38 -0
  38. data/lib/right_agent/core_payload_types/executable_bundle.rb +130 -0
  39. data/lib/right_agent/core_payload_types/login_policy.rb +72 -0
  40. data/lib/right_agent/core_payload_types/login_user.rb +79 -0
  41. data/lib/right_agent/core_payload_types/planned_volume.rb +94 -0
  42. data/lib/right_agent/core_payload_types/recipe_instantiation.rb +73 -0
  43. data/lib/right_agent/core_payload_types/repositories_bundle.rb +50 -0
  44. data/lib/right_agent/core_payload_types/right_script_attachment.rb +95 -0
  45. data/lib/right_agent/core_payload_types/right_script_instantiation.rb +94 -0
  46. data/lib/right_agent/core_payload_types/runlist_policy.rb +44 -0
  47. data/lib/right_agent/core_payload_types/secure_document.rb +66 -0
  48. data/lib/right_agent/core_payload_types/secure_document_location.rb +63 -0
  49. data/lib/right_agent/core_payload_types/software_repository_instantiation.rb +61 -0
  50. data/lib/right_agent/daemonize.rb +35 -0
  51. data/lib/right_agent/dispatched_cache.rb +109 -0
  52. data/lib/right_agent/dispatcher.rb +272 -0
  53. data/lib/right_agent/enrollment_result.rb +221 -0
  54. data/lib/right_agent/exceptions.rb +87 -0
  55. data/lib/right_agent/history.rb +145 -0
  56. data/lib/right_agent/log.rb +460 -0
  57. data/lib/right_agent/minimal.rb +46 -0
  58. data/lib/right_agent/monkey_patches.rb +30 -0
  59. data/lib/right_agent/monkey_patches/ruby_patch.rb +55 -0
  60. data/lib/right_agent/monkey_patches/ruby_patch/array_patch.rb +29 -0
  61. data/lib/right_agent/monkey_patches/ruby_patch/darwin_patch.rb +24 -0
  62. data/lib/right_agent/monkey_patches/ruby_patch/linux_patch.rb +24 -0
  63. data/lib/right_agent/monkey_patches/ruby_patch/linux_patch/file_patch.rb +30 -0
  64. data/lib/right_agent/monkey_patches/ruby_patch/object_patch.rb +49 -0
  65. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch.rb +32 -0
  66. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/file_patch.rb +60 -0
  67. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/process_patch.rb +63 -0
  68. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/stdio_patch.rb +27 -0
  69. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/time_patch.rb +55 -0
  70. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/win32ole_patch.rb +34 -0
  71. data/lib/right_agent/multiplexer.rb +102 -0
  72. data/lib/right_agent/offline_handler.rb +270 -0
  73. data/lib/right_agent/operation_result.rb +300 -0
  74. data/lib/right_agent/packets.rb +673 -0
  75. data/lib/right_agent/payload_formatter.rb +104 -0
  76. data/lib/right_agent/pending_requests.rb +128 -0
  77. data/lib/right_agent/pid_file.rb +159 -0
  78. data/lib/right_agent/platform.rb +770 -0
  79. data/lib/right_agent/platform/unix/darwin/platform.rb +102 -0
  80. data/lib/right_agent/platform/unix/linux/platform.rb +305 -0
  81. data/lib/right_agent/platform/unix/platform.rb +226 -0
  82. data/lib/right_agent/platform/windows/mingw/platform.rb +447 -0
  83. data/lib/right_agent/platform/windows/mswin/platform.rb +236 -0
  84. data/lib/right_agent/platform/windows/platform.rb +1808 -0
  85. data/lib/right_agent/protocol_version_mixin.rb +69 -0
  86. data/lib/right_agent/retryable_request.rb +195 -0
  87. data/lib/right_agent/scripts/agent_controller.rb +543 -0
  88. data/lib/right_agent/scripts/agent_deployer.rb +400 -0
  89. data/lib/right_agent/scripts/common_parser.rb +160 -0
  90. data/lib/right_agent/scripts/log_level_manager.rb +192 -0
  91. data/lib/right_agent/scripts/stats_manager.rb +268 -0
  92. data/lib/right_agent/scripts/usage.rb +58 -0
  93. data/lib/right_agent/secure_identity.rb +92 -0
  94. data/lib/right_agent/security.rb +32 -0
  95. data/lib/right_agent/security/cached_certificate_store_proxy.rb +77 -0
  96. data/lib/right_agent/security/certificate.rb +102 -0
  97. data/lib/right_agent/security/certificate_cache.rb +89 -0
  98. data/lib/right_agent/security/distinguished_name.rb +56 -0
  99. data/lib/right_agent/security/encrypted_document.rb +83 -0
  100. data/lib/right_agent/security/rsa_key_pair.rb +76 -0
  101. data/lib/right_agent/security/signature.rb +86 -0
  102. data/lib/right_agent/security/static_certificate_store.rb +85 -0
  103. data/lib/right_agent/sender.rb +792 -0
  104. data/lib/right_agent/serialize.rb +29 -0
  105. data/lib/right_agent/serialize/message_pack.rb +107 -0
  106. data/lib/right_agent/serialize/secure_serializer.rb +151 -0
  107. data/lib/right_agent/serialize/secure_serializer_initializer.rb +47 -0
  108. data/lib/right_agent/serialize/serializable.rb +151 -0
  109. data/lib/right_agent/serialize/serializer.rb +159 -0
  110. data/lib/right_agent/subprocess.rb +38 -0
  111. data/lib/right_agent/tracer.rb +124 -0
  112. data/right_agent.gemspec +101 -0
  113. data/spec/actor_registry_spec.rb +80 -0
  114. data/spec/actor_spec.rb +162 -0
  115. data/spec/agent_config_spec.rb +235 -0
  116. data/spec/agent_identity_spec.rb +78 -0
  117. data/spec/agent_spec.rb +734 -0
  118. data/spec/agent_tag_manager_spec.rb +319 -0
  119. data/spec/clients/api_client_spec.rb +423 -0
  120. data/spec/clients/auth_client_spec.rb +272 -0
  121. data/spec/clients/balanced_http_client_spec.rb +576 -0
  122. data/spec/clients/base_retry_client_spec.rb +635 -0
  123. data/spec/clients/router_client_spec.rb +594 -0
  124. data/spec/clients/spec_helper.rb +111 -0
  125. data/spec/command/agent_manager_commands_spec.rb +51 -0
  126. data/spec/command/command_io_spec.rb +93 -0
  127. data/spec/command/command_parser_spec.rb +79 -0
  128. data/spec/command/command_runner_spec.rb +107 -0
  129. data/spec/command/command_serializer_spec.rb +51 -0
  130. data/spec/connectivity_checker_spec.rb +83 -0
  131. data/spec/core_payload_types/dev_repositories_spec.rb +64 -0
  132. data/spec/core_payload_types/dev_repository_spec.rb +33 -0
  133. data/spec/core_payload_types/executable_bundle_spec.rb +67 -0
  134. data/spec/core_payload_types/login_user_spec.rb +102 -0
  135. data/spec/core_payload_types/recipe_instantiation_spec.rb +81 -0
  136. data/spec/core_payload_types/right_script_attachment_spec.rb +65 -0
  137. data/spec/core_payload_types/right_script_instantiation_spec.rb +79 -0
  138. data/spec/core_payload_types/spec_helper.rb +23 -0
  139. data/spec/dispatched_cache_spec.rb +136 -0
  140. data/spec/dispatcher_spec.rb +324 -0
  141. data/spec/enrollment_result_spec.rb +53 -0
  142. data/spec/history_spec.rb +246 -0
  143. data/spec/log_spec.rb +192 -0
  144. data/spec/monkey_patches/eventmachine_spec.rb +62 -0
  145. data/spec/multiplexer_spec.rb +48 -0
  146. data/spec/offline_handler_spec.rb +340 -0
  147. data/spec/operation_result_spec.rb +208 -0
  148. data/spec/packets_spec.rb +461 -0
  149. data/spec/pending_requests_spec.rb +136 -0
  150. data/spec/platform/spec_helper.rb +216 -0
  151. data/spec/platform/unix/darwin/platform_spec.rb +181 -0
  152. data/spec/platform/unix/linux/platform_spec.rb +540 -0
  153. data/spec/platform/unix/spec_helper.rb +149 -0
  154. data/spec/platform/windows/mingw/platform_spec.rb +222 -0
  155. data/spec/platform/windows/mswin/platform_spec.rb +259 -0
  156. data/spec/platform/windows/spec_helper.rb +720 -0
  157. data/spec/retryable_request_spec.rb +306 -0
  158. data/spec/secure_identity_spec.rb +50 -0
  159. data/spec/security/cached_certificate_store_proxy_spec.rb +62 -0
  160. data/spec/security/certificate_cache_spec.rb +71 -0
  161. data/spec/security/certificate_spec.rb +49 -0
  162. data/spec/security/distinguished_name_spec.rb +46 -0
  163. data/spec/security/encrypted_document_spec.rb +55 -0
  164. data/spec/security/rsa_key_pair_spec.rb +55 -0
  165. data/spec/security/signature_spec.rb +66 -0
  166. data/spec/security/static_certificate_store_spec.rb +58 -0
  167. data/spec/sender_spec.rb +1045 -0
  168. data/spec/serialize/message_pack_spec.rb +131 -0
  169. data/spec/serialize/secure_serializer_spec.rb +132 -0
  170. data/spec/serialize/serializable_spec.rb +90 -0
  171. data/spec/serialize/serializer_spec.rb +197 -0
  172. data/spec/spec.opts +2 -0
  173. data/spec/spec.win32.opts +1 -0
  174. data/spec/spec_helper.rb +130 -0
  175. data/spec/tracer_spec.rb +114 -0
  176. metadata +447 -0
@@ -0,0 +1,460 @@
1
+ #
2
+ # Copyright (c) 2009-2011 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ # Protect this code from being loaded more than once since very painful
24
+ # discovering that the singleton got re-instantiated thus losing any class
25
+ # instance variable settings
26
+ unless defined?(RIGHTSCALE_LOG_DEFINED)
27
+
28
+ RIGHTSCALE_LOG_DEFINED = true
29
+
30
+ require 'logger'
31
+ require 'right_support'
32
+
33
+ require File.expand_path(File.join(File.dirname(__FILE__), 'platform'))
34
+ require File.expand_path(File.join(File.dirname(__FILE__), 'multiplexer'))
35
+ require File.expand_path(File.join(File.dirname(__FILE__), 'exceptions'))
36
+
37
+ module RightScale
38
+
39
+ # Logs both to syslog and to local file
40
+ class Log
41
+
42
+ # Expecting use of RightScale patched Singleton so that clients of this
43
+ # class do not need to use '.instance' in Log calls
44
+ include RightSupport::Ruby::EasySingleton
45
+
46
+ # Default formatter for a Log
47
+ class Formatter < Logger::Formatter
48
+ @@show_time = true
49
+
50
+ # Set whether to show time in logged messages
51
+ #
52
+ # === Parameters
53
+ # show(Boolean):: Whether time should be shown
54
+ def show_time=(show=false)
55
+ @@show_time = show
56
+ end
57
+
58
+ # Prints a log message as 'datetime progname[pid]: message' if @@show_time == true;
59
+ # otherwise, doesn't print the datetime
60
+ #
61
+ # === Parameters
62
+ # severity(String):: Severity of event
63
+ # time(Time):: Date-time
64
+ # progname(String):: Program name
65
+ # msg(Object):: Message object that can be converted to a string
66
+ #
67
+ # === Return
68
+ # Formatted message
69
+ def call(severity, time, progname, msg)
70
+ if @@show_time
71
+ sprintf("%s %s[%d]: %s\n", format_datetime(time), progname, Process.pid, msg2str(msg))
72
+ else
73
+ sprintf("%s[%d]: %s\n", progname, Process.pid, msg2str(msg))
74
+ end
75
+ end
76
+
77
+ # Converts some argument to a Logger.severity() call to a string
78
+ # Regular strings pass through like normal, Exceptions get formatted
79
+ # as "message (class)\nbacktrace", and other random stuff gets put
80
+ # through "object.inspect"
81
+ #
82
+ # === Parameters
83
+ # msg(Object):: Message object to be converted to string
84
+ #
85
+ # === Return
86
+ # String
87
+ def msg2str(msg)
88
+ case msg
89
+ when ::String
90
+ msg
91
+ when ::Exception
92
+ "#{ msg.message } (#{ msg.class })\n" <<
93
+ (msg.backtrace || []).join("\n")
94
+ else
95
+ msg.inspect
96
+ end
97
+ end
98
+ end
99
+
100
+ # Map of log levels symbols associated with corresponding Logger constant
101
+ LEVELS_MAP = {:debug => Logger::DEBUG,
102
+ :info => Logger::INFO,
103
+ :warn => Logger::WARN,
104
+ :error => Logger::ERROR,
105
+ :fatal => Logger::FATAL} unless defined?(LEVELS_MAP)
106
+
107
+ @@inverted_levels_map = nil
108
+
109
+ # Undefine warn to prevent Kernel#warn from being called
110
+ undef warn
111
+
112
+ # And on the eigenclass
113
+ class <<self
114
+ undef warn
115
+ end
116
+
117
+ def initialize
118
+ # Was log ever used?
119
+ @initialized = false
120
+ @logger = RightSupport::Log::NullLogger.new # ensures respond_to? works before init is called
121
+ end
122
+
123
+ # Forward all method calls to underlying Logger object created with init
124
+ # Return the result of only the first registered logger to keep the interface
125
+ # consistent with that of a Logger
126
+ #
127
+ # === Parameters
128
+ # m(Symbol):: Forwarded method name
129
+ # args(Array):: Forwarded method arguments
130
+ #
131
+ # === Return
132
+ # (Object):: Result from first registered logger
133
+ def method_missing(m, *args)
134
+ init unless @initialized
135
+ @logger.level = level_from_sym(level) if @level_frozen
136
+ @logger.send(m, *args)
137
+ end
138
+
139
+ # Determine whether this object, or its method_missing proxy, responds
140
+ # to the given method name. This follows the best practice of always
141
+ # overriding #respond_to? whenever one implements dynamic dispatch
142
+ # via #method_missing.
143
+ #
144
+ # === Parameters
145
+ # m(Symbol):: Forwarded method name
146
+ #
147
+ # === Return
148
+ # (true|false):: True if this object or its proxy responds to the names method, false otherwise
149
+ def respond_to?(m)
150
+ super(m) || @logger.respond_to?(m)
151
+ end
152
+
153
+ # Log warning and optionally append exception information
154
+ #
155
+ # === Parameters
156
+ # description(String):: Error description
157
+ # exception(Exception|String):: Associated exception or other parenthetical error information
158
+ # backtrace(Symbol):: Exception backtrace extent: :no_trace, :caller, or :trace,
159
+ # defaults to :caller
160
+ #
161
+ # === Return
162
+ # (Object):: Result from first registered logger
163
+ def warning(description, exception = nil, backtrace = :caller)
164
+ init unless @initialized
165
+ @logger.warn(format(description, exception, backtrace))
166
+ end
167
+
168
+ alias :warn :warning
169
+
170
+ # Log error and optionally append exception information
171
+ #
172
+ # === Parameters
173
+ # description(String):: Error description
174
+ # exception(Exception|String):: Associated exception or other parenthetical error information
175
+ # backtrace(Symbol):: Exception backtrace extent: :no_trace, :caller, or :trace,
176
+ # defaults to :caller
177
+ #
178
+ # === Return
179
+ # (Object):: Result from first registered logger
180
+ def error(description, exception = nil, backtrace = :caller)
181
+ init unless @initialized
182
+ @logger.error(format(description, exception, backtrace))
183
+ end
184
+
185
+ # Format error information
186
+ #
187
+ # === Parameters
188
+ # description(String):: Error description
189
+ # exception(Exception|String):: Associated exception or other parenthetical error information
190
+ # backtrace(Symbol):: Exception backtrace extent: :no_trace, :caller, or :trace,
191
+ # defaults to :caller
192
+ #
193
+ # === Return
194
+ # (Object):: Result from first registered logger
195
+ def format(description, exception = nil, backtrace = :caller)
196
+ if exception
197
+ if exception.respond_to?(:message)
198
+ description += " (#{exception.class}: #{exception.message}"
199
+ else
200
+ description += " (#{exception}"
201
+ backtrace = :no_trace
202
+ end
203
+ case backtrace
204
+ when :no_trace then description += ")"
205
+ when :caller then description += " in " + exception.backtrace[0] + ")"
206
+ when :trace then description += " in\n " + exception.backtrace.join("\n ") + ")"
207
+ end
208
+ end
209
+ description
210
+ end
211
+
212
+ # Map symbol log level to Logger constant
213
+ #
214
+ # === Parameters
215
+ # sym(Symbol):: Log level symbol, one of :debug, :info, :warn, :error or :fatal
216
+ #
217
+ # === Return
218
+ # lvl(Constant):: One of Logger::DEBUG ... Logger::FATAL
219
+ #
220
+ # === Raise
221
+ # (ArgumentError):: if level symbol is invalid
222
+ def level_from_sym(sym)
223
+ raise ArgumentError, "Invalid log level symbol :#{sym}" unless LEVELS_MAP.include?(sym)
224
+ lvl = LEVELS_MAP[sym]
225
+ end
226
+
227
+ # Map Logger log level constant to symbol
228
+ #
229
+ # === Parameters
230
+ # lvl(Constant):: Log level constant, one of Logger::DEBUG ... Logger::FATAL
231
+ #
232
+ # === Return
233
+ # sym(Symbol):: One of :debug, :info, :warn, :error or :fatal
234
+ #
235
+ # === Raise
236
+ # (ArgumentError):: if level is invalid
237
+ def level_to_sym(lvl)
238
+ @@inverted_levels_map ||= LEVELS_MAP.invert
239
+ raise ArgumentError, "Invalid log level: #{lvl}" unless @@inverted_levels_map.include?(lvl)
240
+ sym = @@inverted_levels_map[lvl]
241
+ end
242
+
243
+ # Read access to internal multiplexer
244
+ #
245
+ # === Return
246
+ # logger(RightScale::Multiplexer):: Multiplexer logger
247
+ def logger
248
+ init unless @initialized
249
+ logger = @logger
250
+ end
251
+
252
+ # Add new logger to list of multiplexed loggers
253
+ #
254
+ # === Parameters
255
+ # logger(Object):: Logger that should get log messages
256
+ #
257
+ # === Return
258
+ # @logger(RightScale::Multiplexer):: Multiplexer logger
259
+ def add_logger(logger)
260
+ init unless @initialized
261
+ logger.level = level_from_sym(Log.instance.level)
262
+ @logger.add(logger)
263
+ end
264
+
265
+ # Remove logger from list of multiplexed loggers
266
+ #
267
+ # === Parameters
268
+ # logger(Object):: Logger to be removed
269
+ #
270
+ # === Return
271
+ # @logger(RightScale::Multiplexer):: Multiplexer logger
272
+ def remove_logger(logger)
273
+ init unless @initialized
274
+ @logger.remove(logger)
275
+ end
276
+
277
+ # Set whether syslog should be used or to log to an agent-specific file
278
+ # This should be called before anything else
279
+ #
280
+ # === Parameters
281
+ # val(Boolean):: Whether syslog should be used (false) or
282
+ # a agent-specific log file (true)
283
+ #
284
+ # === Raise
285
+ # RuntimeError:: If logger is already initialized
286
+ def log_to_file_only(val)
287
+ raise 'Logger already initialized' if @initialized
288
+ @log_to_file_only = !!val
289
+ end
290
+
291
+ # Was logger initialized?
292
+ #
293
+ # === Return
294
+ # true:: if logger has been initialized
295
+ # false:: Otherwise
296
+ def initialized
297
+ @initialized
298
+ end
299
+
300
+ # Sets the syslog program name that will be reported
301
+ # Can only be successfully called before logging is
302
+ # initialized
303
+ #
304
+ # === Parameters
305
+ # prog_name(String):: An arbitrary string, or "nil" to use
306
+ # the default name that is based on the agent's identity
307
+ #
308
+ # === Return
309
+ # program_name(String):: The input string
310
+ #
311
+ # === Raise
312
+ # RuntimeError:: If logger is already initialized
313
+ def program_name=(prog_name)
314
+ raise 'Logger already initialized' if @initialized
315
+ @program_name = prog_name
316
+ end
317
+
318
+ # Sets the syslog facility that will be used when emitting syslog messages.
319
+ # Can only be successfully called before logging is initialized
320
+ #
321
+ # === Parameters
322
+ # facility(String):: A syslog facility name, e.g. 'user' or 'local0'
323
+ #
324
+ # === Return
325
+ # program_name(String):: The input string
326
+ #
327
+ # === Raise
328
+ # RuntimeError:: If logger is already initialized
329
+ def facility=(facility)
330
+ raise 'Logger already initialized' if @initialized
331
+ @facility = facility
332
+ end
333
+
334
+ # Sets the level for the Logger by symbol or by Logger constant
335
+ #
336
+ # === Parameters
337
+ # level(Object):: One of :debug, :info, :warn, :error, :fatal or
338
+ # one of "debug", "info", "warn", "error", "fatal" or
339
+ # one of Logger::INFO ... Logger::FATAL
340
+ #
341
+ # === Return
342
+ # level(Symbol):: New log level, or current level if frozen
343
+ def level=(level)
344
+ init unless @initialized
345
+ unless @level_frozen
346
+ new_level = case level
347
+ when Symbol then
348
+ level_from_sym(level)
349
+ when String then
350
+ level_from_sym(level.to_sym)
351
+ else
352
+ level
353
+ end
354
+ if new_level != @level
355
+ @logger.info("[setup] Setting log level to #{level_to_sym(new_level).to_s.upcase}")
356
+ if new_level == Logger::DEBUG && !RightScale::Platform.windows?
357
+ @logger.info("[setup] Check syslog configuration to ensure debug messages are not discarded!")
358
+ else
359
+ end
360
+ @logger.level = @level = new_level
361
+ end
362
+ # Notify even if unchanged since don't know when callback was set
363
+ @notify.each { |n| n.call(@level) } if @notify
364
+ end
365
+ level = level_to_sym(@level)
366
+ end
367
+
368
+ # Current log level
369
+ #
370
+ # === Return
371
+ # level(Symbol):: One of :debug, :info, :warn, :error or :fatal
372
+ def level
373
+ init unless @initialized
374
+ level = level_to_sym(@level)
375
+ end
376
+
377
+ # Register callback to be activated when there is a logging configuration change
378
+ # Currently the only logging change reported is log level
379
+ #
380
+ # === Parameters
381
+ # callback(Proc):: Block to be activated with following parameter when log level changes:
382
+ # log_level(Symbol):: Current log level
383
+ #
384
+ # === Return
385
+ # true:: Always return true
386
+ def notify(callback)
387
+ @notify = (@notify ||= []) << callback
388
+ true
389
+ end
390
+
391
+ # Force log level to debug and disregard
392
+ # any further attempt to change it
393
+ #
394
+ # === Return
395
+ # true:: Always return true
396
+ def force_debug
397
+ self.level = :debug
398
+ @level_frozen = true
399
+ end
400
+
401
+ # Force use of given logger and override all defaults
402
+ #
403
+ # === Parameters
404
+ # logger(Logger):: Logger compatible object
405
+ #
406
+ # === Return
407
+ # true:: Always return true
408
+ def force_logger(logger)
409
+ @initialized = true
410
+ @logger = logger
411
+ end
412
+
413
+ protected
414
+
415
+ # Initialize logger
416
+ #
417
+ # === Parameters
418
+ # identity(String):: Log identity
419
+ # path(String):: Log directory path
420
+ # opts[:force](TrueClass|FalseClass):: Whether to re-initialize if logger
421
+ # is already initialized
422
+ # opts[:print](TrueClass|FalseClass):: Whether to print to STDOUT log destination
423
+ #
424
+ # === Return
425
+ # logger(RightScale::Multiplexer):: logger instance
426
+ def init(identity=nil, path=nil, opts={})
427
+ if opts[:force] || !@initialized
428
+ @initialized = true
429
+ @level_frozen = false
430
+ logger = nil
431
+
432
+ if @log_to_file_only || Platform.windows?
433
+ if path
434
+ file = File.join(path, "#{identity}.log")
435
+ else
436
+ file = STDOUT
437
+ end
438
+ $stderr.puts "Logging to #{file}" if opts[:print]
439
+ logger = Logger.new(file)
440
+ logger.formatter = Formatter.new
441
+ logger.progname = @program_name || identity || 'RightAgent'
442
+ logger.formatter.datetime_format = "%b %d %H:%M:%S"
443
+ else
444
+ $stderr.puts "Logging to syslog" if opts[:print]
445
+ program_name = @program_name || identity || 'RightAgent'
446
+ facility = @facility || 'local0'
447
+ logger = RightSupport::Log::SystemLogger.new(program_name, :facility=>facility)
448
+ end
449
+
450
+ @logger = Multiplexer.new(logger)
451
+ self.level = :info
452
+ end
453
+ @logger
454
+ end
455
+
456
+ end # Log
457
+
458
+ end # RightScale
459
+
460
+ end # Unless already defined