踊る犬.netブログ (旧)

Make logrotate cookbook for Chef possible to work on Ruby1.8

There is a great cookbook posted on GitHub to configure logrotation with Chef though, it may work on Ruby 1.9 or greater. My server Ruby1.8 installed couldn’t run its recipes.
I fixed a part of the cookbook so that it can also work on Ruby1.8:

https://gist.github.com/noradaiko/5219337.js

Diff is as follows:

--- a/cookbooks/logrotate/libraries/logrotate_config.rb
+++ b/cookbooks/logrotate/libraries/logrotate_config.rb
@@ -28,36 +28,46 @@ module CookbookLogrotate
       end
 
       def directives_from hash
-        hash.select { |k, v| DIRECTIVES.include?(k) && v }.keys
+                 Hash[ hash.select { |k, v| DIRECTIVES.include?(k) && v } ].keys
       end
 
       def values_from hash
-        hash.select { |k| VALUES.include? k }
+                 Hash[ hash.select { |k, v|
+                                 VALUES.include? k
+                         }
+                 ]
       end
 
       def paths_from hash
-        hash.select { |k| !(DIRECTIVES_AND_VALUES.include? k) }.inject({}) do | accum_paths, (path, config) |
-          accum_paths[path] = {
-            'directives' => directives_from(config),
-            'values' => values_from(config),
-            'scripts' => scripts_from(config)
-          }
+               pp hash
+        Hash[ hash.select { |k| !(DIRECTIVES_AND_VALUES.include? k) } ].inject({}) do | accum_paths, (path, config) |
+                 pp "----"
+          if config.instance_of?(Mash)
+            accum_paths[path] = {
+              'directives' => directives_from(config),
+              'values' => values_from(config),
+              'scripts' => scripts_from(config)
+            }
 
-          accum_paths
+                       pp accum_paths[path]
+            accum_paths
+          else
+            accum_paths
+                 end
         end
       end
 
       def scripts_from hash
-        defined_scripts = hash.select { |k| SCRIPTS.include? k }
-        defined_scripts.inject({}) do | accum_scripts, (script, lines) |
-          if lines.respond_to? :join
-            accum_scripts[script] = lines.join "n"
-          else
-            accum_scripts[script] = lines
-          end
+          defined_scripts = hash.select { |k| SCRIPTS.include? k }
+          defined_scripts.inject({}) do | accum_scripts, (script, lines) |
+            if lines.respond_to? :join
+              accum_scripts[script] = lines.join "n"
+            else
+              accum_scripts[script] = lines
+            end
 
-          accum_scripts
-        end
+            accum_scripts
+          end
       end
     end

Hope it helps.