diff options
author | Saku Ytti <saku@ytti.fi> | 2013-04-17 17:48:50 +0300 |
---|---|---|
committer | Saku Ytti <saku@ytti.fi> | 2013-04-17 17:48:50 +0300 |
commit | 9d217025fac3e335c308f02e7377e14ccfdc0e66 (patch) | |
tree | b90d4d04947fe26a9e592e12d8c4352142380c03 /lib/oxidized/output |
Initial commit
Silly for shit-and-giggles attempt at rancid
Diffstat (limited to 'lib/oxidized/output')
-rw-r--r-- | lib/oxidized/output/file.rb | 29 | ||||
-rw-r--r-- | lib/oxidized/output/git.rb | 57 | ||||
-rw-r--r-- | lib/oxidized/output/output.rb | 9 |
3 files changed, 95 insertions, 0 deletions
diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb new file mode 100644 index 0000000..b988c1a --- /dev/null +++ b/lib/oxidized/output/file.rb @@ -0,0 +1,29 @@ +module Oxidized +class OxFile < Output + require 'fileutils' + + def initialize + @cfg = CFG.output[:file] + end + + def setup + if not @cfg + CFG.output[:file] = { + :directory => File.join(Config::Root, 'configs') + } + CFG.save + end + end + + def update node, data, opt={} + file = @cfg[:directory] + if opt[:group] + file = File.join File.dirname(file), opt[:group] + end + FileUtils.mkdir_p file + file = File.join file, node + open(file, 'w') { |fh| fh.write data } + end + +end +end diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb new file mode 100644 index 0000000..77d18bb --- /dev/null +++ b/lib/oxidized/output/git.rb @@ -0,0 +1,57 @@ +module Oxidized +class Git < Output + require 'grit' + require 'oxidized/fix/grit' if RUBY_VERSION[0..1] == '2.' + include Grit + + def initialize + @cfg = CFG.output[:git] + end + + def setup + if not @cfg + CFG.output[:git] = { + :user => 'Oxidized', + :email => 'o@example.com', + :repo => File.join(Config::Root, 'oxidized.git') + } + CFG.save + end + end + + def update file, data, opt={} + msg = opt[:msg] + user = (opt[:user] or @cfg[:user]) + email = (opt[:email] or @cfg[:email]) + repo = @cfg[:repo] + if opt[:group] + repo = File.join File.dirname(repo), opt[:group] + '.git' + end + begin + + repo = Repo.new repo + actor = Actor.new user, email + update_repo repo, file, data, msg, actor + rescue Grit::NoSuchPathError + Repo.init_bare repo + retry + end + end + + private + + def update_repo repo, file, data, msg, actor + index = repo.index + index.read_tree 'master' + old = index.write_tree index.tree, index.current_tree + index.add file, data + new = index.write_tree index.tree, index.current_tree + if old != new + parent = repo.commits(nil, 1).first + parent = [parent] if parent + Log.debug "GIT: comitting #{file}" + index.commit msg, parent, actor + end + end +end +end diff --git a/lib/oxidized/output/output.rb b/lib/oxidized/output/output.rb new file mode 100644 index 0000000..61cb2b5 --- /dev/null +++ b/lib/oxidized/output/output.rb @@ -0,0 +1,9 @@ +module Oxidized + class Output + class << self + def inherited klass + Oxidized.mgr.loader = { :class => klass } + end + end + end +end |