diff options
| -rw-r--r-- | lib/oxidized/model/model.rb | 35 | ||||
| -rw-r--r-- | lib/oxidized/model/outputs.rb | 37 | ||||
| -rw-r--r-- | lib/oxidized/output/file.rb | 4 | ||||
| -rw-r--r-- | lib/oxidized/output/git.rb | 44 | ||||
| -rw-r--r-- | lib/oxidized/output/output.rb | 5 | ||||
| -rw-r--r-- | lib/oxidized/string.rb | 10 | 
6 files changed, 107 insertions, 28 deletions
| diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb index c3d8760..7258912 100644 --- a/lib/oxidized/model/model.rb +++ b/lib/oxidized/model/model.rb @@ -1,3 +1,5 @@ +require_relative 'outputs' +  module Oxidized    class Model      include Oxidized::Config::Vars @@ -77,17 +79,16 @@ module Oxidized      def cmd string, &block        out = @input.cmd string        return false unless out -      out = Oxidized::String.new out        self.class.cmds[:all].each do |all_block| -        out = instance_exec out, string, &all_block +        out = instance_exec Oxidized::String.new(out), string, &all_block        end        if vars :remove_secret          self.class.cmds[:secret].each do |all_block| -          out = instance_exec out, string, &all_block +          out = instance_exec Oxidized::String.new(out), string, &all_block          end        end -      out = instance_exec out, &block if block -      out +      out = instance_exec Oxidized::String.new(out), &block if block +      process_cmd_output out, string      end      def output @@ -124,20 +125,20 @@ module Oxidized      end      def get -      data, pre = '', '' +      outputs = Outputs.new        procs = self.class.procs +      procs[:pre].each do |pre_proc| +        outputs << instance_eval(&pre_proc) +      end        self.class.cmds[:cmd].each do |command, block|          out = cmd command, &block          return false unless out -        data << out.to_s -      end -      procs[:pre].each do |pre_proc| -        pre << instance_eval(&pre_proc).to_s +        outputs << out        end        procs[:post].each do |post_proc| -        data << instance_eval(&post_proc).to_s +        outputs << instance_eval(&post_proc)        end -      pre + data +      outputs      end      def comment _comment @@ -148,5 +149,15 @@ module Oxidized        data      end +    private + +    def process_cmd_output output, name +      if output.class != Oxidized::String +        output = Oxidized::String.new output +      end +      output.set_cmd(name) +      output +    end +    end  end diff --git a/lib/oxidized/model/outputs.rb b/lib/oxidized/model/outputs.rb new file mode 100644 index 0000000..10015f8 --- /dev/null +++ b/lib/oxidized/model/outputs.rb @@ -0,0 +1,37 @@ +module Oxidized +  class Model +    class Outputs + +      def to_cfg +        type_to_str(nil) +      end + +      def type_to_str want_type +        type(want_type).map { |out| out }.join +      end + +      def << output +        @outputs << output +      end + +      def all +        @outputs +      end + +      def type type +        @outputs.select { |out| out.type==type } +      end + +      def types +        @outputs.map { |out| out.type }.uniq.compact +      end + +      private + +      def initialize +        @outputs = [] +      end + +    end +  end +end diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 2d08106..03c878a 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -14,14 +14,14 @@ class OxidizedFile < Output      end    end -  def store node, data, opt={} +  def store node, outputs, 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 } +    open(file, 'w') { |fh| fh.write outputs.to_cfg }    end    def fetch node, group diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb index a3f32af..df76da3 100644 --- a/lib/oxidized/output/git.rb +++ b/lib/oxidized/output/git.rb @@ -21,23 +21,28 @@ class Git < Output      end    end -  def store 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 = Rugged::Repository.new repo -      update_repo repo, file, data, msg, user, email -    rescue Rugged::OSError, Rugged::RepositoryError -      Rugged::Repository.init_at repo, :bare -      retry +  def store file, outputs, opt={} +    @msg   = opt[:msg] +    @user  = (opt[:user]  or @cfg.user) +    @email = (opt[:email] or @cfg.email) +    @opt   = opt +    repo   = @cfg.repo + +    outputs.types.each do |type| +      type_cfg = '' +      type_repo = File.join File.dirname(repo), type + '.git' +      outputs.type(type).each do |output| +        (type_cfg << output; next) if not output.name +        type_file = file + '--' + output.name +        update type_repo, type_file, output +      end +      update type_repo, file, type_cfg unless type_cfg.empty?      end + +    update repo, file, outputs.to_cfg    end +    def fetch node, group      begin        repo = @cfg.repo @@ -55,6 +60,17 @@ class Git < Output    private +  def update repo, file, data +    if @opt[:group] +      repo = File.join File.dirname(repo), @opt[:group] + '.git' +    end +    repo = Rugged::Repository.new repo +    update_repo repo, file, data, @msg, @user, @email +  rescue Rugged::OSError, Rugged::RepositoryError +    Rugged::Repository.init_at repo, :bare +    retry +  end +    def update_repo repo, file, data, msg, user, email      oid = repo.write data, :blob      index = repo.index diff --git a/lib/oxidized/output/output.rb b/lib/oxidized/output/output.rb index eaf149a..1355d03 100644 --- a/lib/oxidized/output/output.rb +++ b/lib/oxidized/output/output.rb @@ -1,5 +1,10 @@  module Oxidized    class Output      class NoConfig < OxidizedError; end + +    def cfg_to_str cfg +      cfg.select{ |h| h[:type]=='cfg' }.map{ |h| h[:data] }.join +    end +    end  end diff --git a/lib/oxidized/string.rb b/lib/oxidized/string.rb index 35dc5af..d95bcae 100644 --- a/lib/oxidized/string.rb +++ b/lib/oxidized/string.rb @@ -1,13 +1,23 @@  module Oxidized    # Used in models, contains convenience methods    class String < String +    attr_accessor :type, :cmd, :name +      # @return [Oxidized::String] copy of self with last line removed      def cut_tail        Oxidized::String.new each_line.to_a[0..-2].join      end +      # @return [Oxidized::String] copy of self with first line removed      def cut_head        Oxidized::String.new each_line.to_a[1..-1].join      end + +    # sets @cmd and @name unless @name is already set +    def set_cmd command +      @cmd  = command +      @name = @cmd.strip.gsub(/\s+/, '_') if @name == nil +    end +    end  end | 
