mcollective-client 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mcollective-client might be problematic. Click here for more details.

Files changed (140) hide show
  1. data/lib/mcollective.rb +32 -23
  2. data/lib/mcollective/agent.rb +5 -0
  3. data/lib/mcollective/agents.rb +5 -16
  4. data/lib/mcollective/aggregate.rb +61 -0
  5. data/lib/mcollective/aggregate/base.rb +40 -0
  6. data/lib/mcollective/aggregate/result.rb +9 -0
  7. data/lib/mcollective/aggregate/result/base.rb +25 -0
  8. data/lib/mcollective/aggregate/result/collection_result.rb +19 -0
  9. data/lib/mcollective/aggregate/result/numeric_result.rb +13 -0
  10. data/lib/mcollective/application.rb +7 -4
  11. data/lib/mcollective/applications.rb +3 -14
  12. data/lib/mcollective/cache.rb +145 -0
  13. data/lib/mcollective/client.rb +10 -87
  14. data/lib/mcollective/config.rb +22 -8
  15. data/lib/mcollective/data.rb +87 -0
  16. data/lib/mcollective/data/base.rb +67 -0
  17. data/lib/mcollective/data/result.rb +40 -0
  18. data/lib/mcollective/ddl.rb +113 -0
  19. data/lib/mcollective/ddl/agentddl.rb +185 -0
  20. data/lib/mcollective/ddl/base.rb +220 -0
  21. data/lib/mcollective/ddl/dataddl.rb +56 -0
  22. data/lib/mcollective/ddl/discoveryddl.rb +52 -0
  23. data/lib/mcollective/ddl/validatorddl.rb +6 -0
  24. data/lib/mcollective/discovery.rb +143 -0
  25. data/lib/mcollective/generators.rb +7 -0
  26. data/lib/mcollective/generators/agent_generator.rb +51 -0
  27. data/lib/mcollective/generators/base.rb +46 -0
  28. data/lib/mcollective/generators/data_generator.rb +51 -0
  29. data/lib/mcollective/generators/templates/action_snippet.erb +13 -0
  30. data/lib/mcollective/generators/templates/data_input_snippet.erb +7 -0
  31. data/lib/mcollective/generators/templates/ddl.erb +8 -0
  32. data/lib/mcollective/generators/templates/plugin.erb +7 -0
  33. data/lib/mcollective/logger/console_logger.rb +15 -15
  34. data/lib/mcollective/matcher.rb +167 -0
  35. data/lib/mcollective/matcher/parser.rb +60 -25
  36. data/lib/mcollective/matcher/scanner.rb +156 -78
  37. data/lib/mcollective/message.rb +47 -6
  38. data/lib/mcollective/monkey_patches.rb +17 -0
  39. data/lib/mcollective/optionparser.rb +18 -1
  40. data/lib/mcollective/pluginmanager.rb +3 -3
  41. data/lib/mcollective/pluginpackager.rb +10 -3
  42. data/lib/mcollective/pluginpackager/agent_definition.rb +28 -20
  43. data/lib/mcollective/pluginpackager/standard_definition.rb +11 -9
  44. data/lib/mcollective/registration/base.rb +3 -1
  45. data/lib/mcollective/rpc.rb +18 -24
  46. data/lib/mcollective/rpc/agent.rb +37 -113
  47. data/lib/mcollective/rpc/client.rb +186 -64
  48. data/lib/mcollective/rpc/helpers.rb +42 -80
  49. data/lib/mcollective/rpc/progress.rb +3 -3
  50. data/lib/mcollective/rpc/reply.rb +37 -13
  51. data/lib/mcollective/rpc/request.rb +17 -6
  52. data/lib/mcollective/rpc/result.rb +9 -5
  53. data/lib/mcollective/rpc/stats.rb +71 -24
  54. data/lib/mcollective/security/base.rb +41 -34
  55. data/lib/mcollective/shell.rb +1 -1
  56. data/lib/mcollective/ssl.rb +34 -0
  57. data/lib/mcollective/util.rb +194 -23
  58. data/lib/mcollective/validator.rb +80 -0
  59. data/spec/fixtures/util/1.in +10 -0
  60. data/spec/fixtures/util/1.out +10 -0
  61. data/spec/fixtures/util/2.in +1 -0
  62. data/spec/fixtures/util/2.out +1 -0
  63. data/spec/fixtures/util/3.in +1 -0
  64. data/spec/fixtures/util/3.out +2 -0
  65. data/spec/fixtures/util/4.in +5 -0
  66. data/spec/fixtures/util/4.out +9 -0
  67. data/spec/spec.opts +1 -1
  68. data/spec/spec_helper.rb +2 -0
  69. data/spec/unit/agents_spec.rb +34 -19
  70. data/spec/unit/aggregate/base_spec.rb +57 -0
  71. data/spec/unit/aggregate/result/base_spec.rb +28 -0
  72. data/spec/unit/aggregate/result/collection_result_spec.rb +18 -0
  73. data/spec/unit/aggregate/result/numeric_result_spec.rb +22 -0
  74. data/spec/unit/aggregate_spec.rb +110 -0
  75. data/spec/unit/application_spec.rb +8 -3
  76. data/spec/unit/applications_spec.rb +2 -2
  77. data/spec/unit/cache_spec.rb +115 -0
  78. data/spec/unit/client_spec.rb +78 -0
  79. data/spec/unit/config_spec.rb +32 -34
  80. data/spec/unit/data/base_spec.rb +90 -0
  81. data/spec/unit/data/result_spec.rb +64 -0
  82. data/spec/unit/data_spec.rb +158 -0
  83. data/spec/unit/ddl/agentddl_spec.rb +217 -0
  84. data/spec/unit/{rpc/ddl_spec.rb → ddl/base_spec.rb} +238 -224
  85. data/spec/unit/ddl/dataddl_spec.rb +65 -0
  86. data/spec/unit/ddl/discoveryddl_spec.rb +58 -0
  87. data/spec/unit/ddl_spec.rb +84 -0
  88. data/spec/unit/discovery_spec.rb +196 -0
  89. data/spec/unit/facts/base_spec.rb +1 -1
  90. data/spec/unit/generators/agent_generator_spec.rb +72 -0
  91. data/spec/unit/generators/base_spec.rb +83 -0
  92. data/spec/unit/generators/data_generator_spec.rb +37 -0
  93. data/spec/unit/generators/snippets/agent_ddl +19 -0
  94. data/spec/unit/generators/snippets/data_ddl +20 -0
  95. data/spec/unit/logger/console_logger_spec.rb +76 -0
  96. data/spec/unit/logger/syslog_logger_spec.rb +2 -2
  97. data/spec/unit/matcher/parser_spec.rb +27 -10
  98. data/spec/unit/matcher/scanner_spec.rb +108 -5
  99. data/spec/unit/matcher_spec.rb +260 -0
  100. data/spec/unit/message_spec.rb +35 -13
  101. data/spec/unit/optionparser_spec.rb +2 -2
  102. data/spec/unit/pluginpackager/agent_definition_spec.rb +59 -42
  103. data/spec/unit/pluginpackager/standard_definition_spec.rb +10 -8
  104. data/spec/unit/pluginpackager_spec.rb +131 -0
  105. data/spec/unit/plugins/mcollective/aggregate/average_spec.rb +45 -0
  106. data/spec/unit/plugins/mcollective/aggregate/sum_spec.rb +31 -0
  107. data/spec/unit/plugins/mcollective/aggregate/summary_spec.rb +45 -0
  108. data/spec/unit/plugins/mcollective/connector/activemq_spec.rb +1 -1
  109. data/spec/unit/plugins/mcollective/connector/rabbitmq_spec.rb +478 -0
  110. data/spec/unit/plugins/mcollective/connector/stomp_spec.rb +2 -0
  111. data/spec/unit/plugins/mcollective/data/agent_data_spec.rb +43 -0
  112. data/spec/unit/plugins/mcollective/data/fstat_data_spec.rb +135 -0
  113. data/spec/unit/plugins/mcollective/discovery/flatfile_spec.rb +48 -0
  114. data/spec/unit/plugins/mcollective/discovery/mc_spec.rb +40 -0
  115. data/spec/unit/plugins/mcollective/packagers/debpackage_packager_spec.rb +41 -15
  116. data/spec/unit/plugins/mcollective/packagers/ospackage_spec.rb +1 -1
  117. data/spec/unit/plugins/mcollective/packagers/rpmpackage_packager_spec.rb +22 -38
  118. data/spec/unit/plugins/mcollective/validator/array_validator_spec.rb +19 -0
  119. data/spec/unit/plugins/mcollective/validator/ipv4address_validator_spec.rb +19 -0
  120. data/spec/unit/plugins/mcollective/validator/ipv6address_validator_spec.rb +19 -0
  121. data/spec/unit/plugins/mcollective/validator/length_validator_spec.rb +19 -0
  122. data/spec/unit/plugins/mcollective/validator/regex_validator_spec.rb +19 -0
  123. data/spec/unit/plugins/mcollective/validator/shellsafe_validator_spec.rb +21 -0
  124. data/spec/unit/plugins/mcollective/validator/typecheck_validator_spec.rb +23 -0
  125. data/spec/unit/registration/base_spec.rb +1 -1
  126. data/spec/unit/rpc/actionrunner_spec.rb +2 -2
  127. data/spec/unit/rpc/agent_spec.rb +41 -65
  128. data/spec/unit/rpc/client_spec.rb +430 -134
  129. data/spec/unit/rpc/reply_spec.rb +31 -1
  130. data/spec/unit/rpc/request_spec.rb +33 -12
  131. data/spec/unit/rpc/result_spec.rb +7 -0
  132. data/spec/unit/rpc/stats_spec.rb +14 -14
  133. data/spec/unit/rpc_spec.rb +16 -0
  134. data/spec/unit/security/base_spec.rb +8 -8
  135. data/spec/unit/ssl_spec.rb +20 -2
  136. data/spec/unit/string_spec.rb +15 -0
  137. data/spec/unit/util_spec.rb +141 -21
  138. data/spec/unit/validator_spec.rb +67 -0
  139. metadata +145 -7
  140. data/lib/mcollective/rpc/ddl.rb +0 -258
@@ -0,0 +1,52 @@
1
+ module MCollective
2
+ module DDL
3
+ # DDL for discovery plugins, a full example can be seen below
4
+ #
5
+ # metadata :name => "mc",
6
+ # :description => "MCollective Broadcast based discovery",
7
+ # :author => "R.I.Pienaar <rip@devco.net>",
8
+ # :license => "ASL 2.0",
9
+ # :version => "0.1",
10
+ # :url => "http://marionette-collective.org/",
11
+ # :timeout => 2
12
+ #
13
+ # discovery do
14
+ # capabilities [:classes, :facts, :identity, :agents, :compound]
15
+ # end
16
+ class DiscoveryDDL<Base
17
+ def discovery_interface
18
+ @entities[:discovery]
19
+ end
20
+
21
+ # records valid capabilities for discovery plugins
22
+ def capabilities(*caps)
23
+ caps = [caps].flatten
24
+
25
+ raise "Discovery plugin capabilities can't be empty" if caps.empty?
26
+
27
+ caps.each do |cap|
28
+ if [:classes, :facts, :identity, :agents, :compound].include?(cap)
29
+ @entities[:discovery][:capabilities] << cap
30
+ else
31
+ raise "%s is not a valid capability, valid capabilities are :classes, :facts, :identity, :agents and :compound" % cap
32
+ end
33
+ end
34
+ end
35
+
36
+ # Creates the definition for new discovery plugins
37
+ #
38
+ # discovery do
39
+ # capabilities [:classes, :facts, :identity, :agents, :compound]
40
+ # end
41
+ def discovery(&block)
42
+ raise "Discovery plugins can only have one definition" if @entities[:discovery]
43
+
44
+ @entities[:discovery] = {:capabilities => []}
45
+
46
+ @current_entity = :discovery
47
+ block.call if block_given?
48
+ @current_entity = nil
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,6 @@
1
+ module MCollective
2
+ module DDL
3
+ class ValidatorDDL<Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,143 @@
1
+ module MCollective
2
+ class Discovery
3
+ def initialize(client)
4
+ @known_methods = find_known_methods
5
+ @default_method = Config.instance.default_discovery_method
6
+ @client = client
7
+ end
8
+
9
+ def find_known_methods
10
+ PluginManager.find("discovery")
11
+ end
12
+
13
+ def has_method?(method)
14
+ @known_methods.include?(method)
15
+ end
16
+
17
+ def force_direct_mode?
18
+ discovery_method != "mc"
19
+ end
20
+
21
+ def discovery_method
22
+ method = "mc"
23
+
24
+ if @client.options[:discovery_method]
25
+ method = @client.options[:discovery_method]
26
+ else
27
+ method = @default_method
28
+ end
29
+
30
+ raise "Unknown discovery method %s" % method unless has_method?(method)
31
+
32
+ unless method == "mc"
33
+ raise "Custom discovery methods require direct addressing mode" unless Config.instance.direct_addressing
34
+ end
35
+
36
+ return method
37
+ end
38
+
39
+ def discovery_class
40
+ method = discovery_method.capitalize
41
+
42
+ PluginManager.loadclass("MCollective::Discovery::#{method}") unless self.class.const_defined?(method)
43
+
44
+ self.class.const_get(method)
45
+ end
46
+
47
+ def ddl
48
+ @ddl ||= DDL.new(discovery_method, :discovery)
49
+
50
+ # if the discovery method got changed we might have an old DDL cached
51
+ # this will detect that and reread the correct DDL from disk
52
+ unless @ddl.meta[:name] == discovery_method
53
+ @ddl = DDL.new(discovery_method, :discovery)
54
+ end
55
+
56
+ return @ddl
57
+ end
58
+
59
+ # Agent filters are always present no matter what, so we cant raise an error if the capabilities
60
+ # suggest the discovery method cant do agents we just have to rely on the discovery plugin to not
61
+ # do stupid things in the presense of a agent filter
62
+ def check_capabilities(filter)
63
+ capabilities = ddl.discovery_interface[:capabilities]
64
+
65
+ unless capabilities.include?(:classes)
66
+ raise "Cannot use class filters while using the '%s' discovery method" % discovery_method unless filter["cf_class"].empty?
67
+ end
68
+
69
+ unless capabilities.include?(:facts)
70
+ raise "Cannot use fact filters while using the '%s' discovery method" % discovery_method unless filter["fact"].empty?
71
+ end
72
+
73
+ unless capabilities.include?(:identity)
74
+ raise "Cannot use identity filters while using the '%s' discovery method" % discovery_method unless filter["identity"].empty?
75
+ end
76
+
77
+ unless capabilities.include?(:compound)
78
+ raise "Cannot use compound filters while using the '%s' discovery method" % discovery_method unless filter["compound"].empty?
79
+ end
80
+ end
81
+
82
+ # checks if compound filters are used and then forces the 'mc' discovery plugin
83
+ def force_discovery_method_by_filter(filter)
84
+ unless discovery_method == "mc"
85
+ unless filter["compound"].empty?
86
+ Log.info "Switching to mc discovery method because compound filters are used"
87
+ @client.options[:discovery_method] = "mc"
88
+
89
+ return true
90
+ end
91
+ end
92
+
93
+ return false
94
+ end
95
+
96
+ # if a compound filter is specified and it has any function
97
+ # then we read the DDL for each of those plugins and sum up
98
+ # the timeout declared in the DDL
99
+ def timeout_for_compound_filter(compound_filter)
100
+ return 0 if compound_filter.nil? || compound_filter.empty?
101
+
102
+ timeout = 0
103
+
104
+ compound_filter.each do |filter|
105
+ filter.each do |statement|
106
+ if statement["fstatement"]
107
+ pluginname = Data.pluginname(statement["fstatement"]["name"])
108
+ ddl = DDL.new(pluginname, :data)
109
+ timeout += ddl.meta[:timeout]
110
+ end
111
+ end
112
+ end
113
+
114
+ timeout
115
+ end
116
+
117
+ def discovery_timeout(timeout, filter)
118
+ timeout = ddl.meta[:timeout] unless timeout
119
+
120
+ unless (filter["compound"] && filter["compound"].empty?)
121
+ timeout + timeout_for_compound_filter(filter["compound"])
122
+ else
123
+ timeout
124
+ end
125
+ end
126
+
127
+ def discover(filter, timeout, limit)
128
+ raise "Limit has to be an integer" unless limit.is_a?(Fixnum)
129
+
130
+ force_discovery_method_by_filter(filter)
131
+
132
+ check_capabilities(filter)
133
+
134
+ discovered = discovery_class.discover(filter, discovery_timeout(timeout, filter), limit, @client)
135
+
136
+ if limit > 0
137
+ return discovered[0,limit]
138
+ else
139
+ return discovered
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,7 @@
1
+ module MCollective
2
+ module Generators
3
+ autoload :Base, "mcollective/generators/base.rb"
4
+ autoload :DataGenerator, "mcollective/generators/data_generator.rb"
5
+ autoload :AgentGenerator, "mcollective/generators/agent_generator.rb"
6
+ end
7
+ end
@@ -0,0 +1,51 @@
1
+ module MCollective
2
+ module Generators
3
+ class AgentGenerator<Base
4
+
5
+ attr_accessor :ddl, :content
6
+
7
+ def initialize(plugin_name, actions = [], name = nil, description = nil, author = nil ,
8
+ license = nil, version = nil, url = nil, timeout = nil)
9
+
10
+ super(name, description, author, license, version, url, timeout)
11
+ @plugin_name = plugin_name
12
+ @actions = actions || []
13
+ @ddl = create_ddl
14
+ @mod_name = "Agent"
15
+ @pclass = "RPC::Agent"
16
+ @content = create_plugin_content
17
+ @plugin = create_plugin_string
18
+ write_plugins
19
+ end
20
+
21
+ def create_ddl
22
+ action_text = ""
23
+ @actions.each_with_index do |action, i|
24
+ action_text += "action \"#{action}\", :description => \"%ACTIONDESCRIPTION%\" do\n"
25
+ action_text += action_help if i == 0
26
+ action_text += "end\n"
27
+ action_text += "\n" unless @actions.size == (i + 1)
28
+ end
29
+ # Use inherited method to create metadata part of the ddl
30
+ create_metadata_string + action_text
31
+ end
32
+
33
+ def create_plugin_content
34
+ content_text = ""
35
+
36
+ # Add actions to agent file
37
+ @actions.each_with_index do |action, i|
38
+ content_text += "%6s%s" % [" ", "action \"#{action}\" do\n"]
39
+ content_text += "%6s%s" % [" ", "end\n"]
40
+ content_text += "\n" unless @actions.size == (i + 1)
41
+ end
42
+ content_text
43
+ end
44
+
45
+ def action_help
46
+ action_snippet = File.read(File.join(File.dirname(__FILE__), "templates", "action_snippet.erb"))
47
+ ERB.new(action_snippet).result
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,46 @@
1
+ module MCollective
2
+ module Generators
3
+ class Base
4
+ attr_accessor :meta, :plugin_name, :mod_name
5
+ def initialize(name, description, author, license, version, url, timeout)
6
+ @meta = {:name => name,
7
+ :description => description,
8
+ :author => author,
9
+ :license => license,
10
+ :version => version,
11
+ :url => url,
12
+ :timeout => timeout}
13
+ end
14
+
15
+ def create_metadata_string
16
+ ddl_template = File.read(File.join(File.dirname(__FILE__), "templates", "ddl.erb"))
17
+ ERB.new(ddl_template, nil, "-").result(binding)
18
+ end
19
+
20
+ def create_plugin_string
21
+ plugin_template = File.read(File.join(File.dirname(__FILE__), "templates", "plugin.erb"))
22
+ ERB.new(plugin_template, nil, "-").result(binding)
23
+ end
24
+
25
+ def write_plugins
26
+ begin
27
+ Dir.mkdir @plugin_name
28
+ dirname = File.join(@plugin_name, @mod_name.downcase)
29
+ Dir.mkdir dirname
30
+ puts "Created plugin directory : #{@plugin_name}"
31
+
32
+ File.open(File.join(dirname, "#{@plugin_name}.ddl"), "w"){|f| f.puts @ddl}
33
+ puts "Created DDL file : #{File.join(dirname, "#{@plugin_name}.ddl")}"
34
+
35
+ File.open(File.join(dirname, "#{@plugin_name}.rb"), "w"){|f| f.puts @plugin}
36
+ puts "Created #{@mod_name} file : #{File.join(dirname, "#{@plugin_name}.rb")}"
37
+ rescue Errno::EEXIST
38
+ raise "cannot generate '#{@plugin_name}' : plugin directory already exists."
39
+ rescue Exception => e
40
+ FileUtils.rm_rf(@plugin_name) if File.directory?(@plugin_name)
41
+ raise "cannot generate plugin - #{e}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ module MCollective
2
+ module Generators
3
+ class DataGenerator<Base
4
+
5
+ attr_accessor :ddl, :content
6
+
7
+ def initialize(plugin_name, outputs = [], name = nil, description = nil, author = nil ,
8
+ license = nil, version = nil, url = nil, timeout = nil)
9
+
10
+ super(name, description, author, license, version, url, timeout)
11
+ @mod_name = "Data"
12
+ @pclass = "Base"
13
+ @plugin_name = plugin_name
14
+ @outputs = outputs
15
+ @ddl = create_ddl
16
+ @content = create_plugin_content
17
+ @plugin = create_plugin_string
18
+ write_plugins
19
+ end
20
+
21
+ def create_ddl
22
+ query_text = "dataquery :description => \"Query information\" do\n"
23
+ query_text += ERB.new(File.read(File.join(File.dirname(__FILE__), "templates", "data_input_snippet.erb"))).result
24
+
25
+ @outputs.each_with_index do |output,i|
26
+ query_text += "%2s%s" % [" ", "output :#{output},\n"]
27
+ query_text += "%9s%s" % [" ", ":description => \"%DESCRIPTION%\",\n"]
28
+ query_text += "%9s%s" % [" ", ":display_as => \"%DESCRIPTION%\"\n"]
29
+ query_text += "\n" unless @outputs.size == (i + 1)
30
+ end
31
+
32
+ query_text += "end"
33
+
34
+ # Use inherited method to create metadata part of the ddl
35
+ create_metadata_string + query_text
36
+ end
37
+
38
+ def create_plugin_content
39
+ content_text = "%6s%s" % [" ", "query do |what|\n"]
40
+
41
+ @outputs.each do |output|
42
+ content_text += "%8s%s" % [" ", "result[:#{output}] = nil\n"]
43
+ end
44
+ content_text += "%6s%s" % [" ", "end\n"]
45
+
46
+ # Add actions to agent file
47
+ content_text
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ # Example Input
2
+ input :name,
3
+ :prompt => "%PROMPT%",
4
+ :description => "%DESCRIPTION%",
5
+ :type => %TYPE%,
6
+ :validation => '%VALIDATION%',
7
+ :optional => %OPTIONAL%,
8
+ :maxlength => %MAXLENGTH%
9
+
10
+ # Example output
11
+ output :name,
12
+ :description => "%DESCRIPTION%",
13
+ :display_as => "%DISPLAYAS%"
@@ -0,0 +1,7 @@
1
+ input :query,
2
+ :prompt => "%PROMP%",
3
+ :description => "%DESCRIPTION%",
4
+ :type => %TYPE%,
5
+ :validation => %VALIDATION%,
6
+ :maxlength => %MAXLENGTH%
7
+
@@ -0,0 +1,8 @@
1
+ metadata :name => "<%= meta[:name] || "%FULLNANE%" -%>",
2
+ :description => "<%= meta[:description] || "%DESCRIPTION%" -%>",
3
+ :author => "<%= meta[:author] || "%AUTHOR%" -%>",
4
+ :license => "<%= meta[:license] || "%LICENSE%" -%>",
5
+ :version => "<%= meta[:version] || "%VERSION%" -%>",
6
+ :url => "<%= meta[:url] || "%URL%" -%>",
7
+ :timeout => <%= meta[:timeout] || "%TIMEOUT%" %>
8
+
@@ -0,0 +1,7 @@
1
+ module MCollective
2
+ module <%= @mod_name%>
3
+ class <%= @plugin_name.capitalize -%><<%= @pclass%>
4
+ <%= @content%>
5
+ end
6
+ end
7
+ end
@@ -1,6 +1,6 @@
1
1
  module MCollective
2
2
  module Logger
3
- # Impliments a syslog based logger using the standard ruby syslog class
3
+ # Implements a syslog based logger using the standard ruby syslog class
4
4
  class Console_logger<Base
5
5
  def start
6
6
  set_level(:info)
@@ -15,22 +15,22 @@ module MCollective
15
15
 
16
16
  def valid_levels
17
17
  {:info => :info,
18
- :warn => :warning,
19
- :debug => :debug,
20
- :fatal => :crit,
21
- :error => :err}
18
+ :warn => :warning,
19
+ :debug => :debug,
20
+ :fatal => :crit,
21
+ :error => :err}
22
22
  end
23
23
 
24
- def log(level, from, msg)
24
+ def log(level, from, msg, normal_output=STDERR, last_resort_output=STDERR)
25
25
  if @known_levels.index(level) >= @known_levels.index(@active_level)
26
26
  time = Time.new.strftime("%Y/%m/%d %H:%M:%S")
27
- lvltxt = colorize(level, level)
28
- STDERR.puts("#{lvltxt} #{time}: #{from} #{msg}")
27
+
28
+ normal_output.puts("%s %s: %s %s" % [colorize(level, level), time, from, msg])
29
29
  end
30
30
  rescue
31
31
  # if this fails we probably cant show the user output at all,
32
32
  # STDERR it as last resort
33
- STDERR.puts("#{level}: #{msg}")
33
+ last_resort_output.puts("#{level}: #{msg}")
34
34
  end
35
35
 
36
36
  # Set some colors for various logging levels, will honor the
@@ -39,11 +39,11 @@ module MCollective
39
39
  def color(level)
40
40
  colorize = Config.instance.color
41
41
 
42
- colors = {:error => "",
43
- :fatal => "",
44
- :warn => "",
45
- :info => "",
46
- :reset => ""}
42
+ colors = {:error => Util.color(:red),
43
+ :fatal => Util.color(:red),
44
+ :warn => Util.color(:yellow),
45
+ :info => Util.color(:green),
46
+ :reset => Util.color(:reset)}
47
47
 
48
48
  if colorize
49
49
  return colors[level] || ""
@@ -54,7 +54,7 @@ module MCollective
54
54
 
55
55
  # Helper to return a string in specific color
56
56
  def colorize(level, msg)
57
- "#{self.color(level)}#{msg}#{self.color(:reset)}"
57
+ "%s%s%s" % [ color(level), msg, color(:reset) ]
58
58
  end
59
59
  end
60
60
  end