diff options
Diffstat (limited to 'lib/oxidized/hook')
| -rw-r--r-- | lib/oxidized/hook/awssns.rb | 5 | ||||
| -rw-r--r-- | lib/oxidized/hook/ciscosparkdiff.rb | 49 | ||||
| -rw-r--r-- | lib/oxidized/hook/exec.rb | 9 | ||||
| -rw-r--r-- | lib/oxidized/hook/githubrepo.rb | 34 | ||||
| -rw-r--r-- | lib/oxidized/hook/slackdiff.rb | 77 | ||||
| -rw-r--r-- | lib/oxidized/hook/xmppdiff.rb | 66 | 
6 files changed, 144 insertions, 96 deletions
| diff --git a/lib/oxidized/hook/awssns.rb b/lib/oxidized/hook/awssns.rb index dbc2d47..183cd2c 100644 --- a/lib/oxidized/hook/awssns.rb +++ b/lib/oxidized/hook/awssns.rb @@ -19,9 +19,8 @@ class AwsSns < Oxidized::Hook          :node => ctx.node.name.to_s        )      end -    topic.publish({ +    topic.publish(        message: message.to_json -    }) +    )    end -  end diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb new file mode 100644 index 0000000..e45d7c6 --- /dev/null +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -0,0 +1,49 @@ +require 'cisco_spark' + +# defaults to posting a diff, if messageformat is supplied them a message will be posted too +# diffenable defaults to true +# Modified from slackdiff + +class CiscoSparkDiff < Oxidized::Hook +  def validate_cfg! +    raise KeyError, 'hook.accesskey is required' unless cfg.has_key?('accesskey') +    raise KeyError, 'hook.space is required' unless cfg.has_key?('space') +  end + +  def run_hook(ctx) +    return unless ctx.node +    return unless ctx.event.to_s == "post_store" +    log "Connecting to Cisco Spark" +    CiscoSpark.configure do |config| +      config.api_key = cfg.accesskey +      config.proxy = cfg.proxy if cfg.has_key?('proxy') +    end +    space = cfg.space +    client = CiscoSpark::Room.new(id: space) +    client.fetch +    log "Connected" +    diffenable = true +    if cfg.has_key?('diff') == true +      if cfg.diff == false +        diffenable = false +      end +    end +    if diffenable == true +      gitoutput = ctx.node.output.new +      diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil +      title = ctx.node.name.to_s +      log "Posting diff as snippet to #{cfg.space}" +      message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join) +      room = CiscoSpark::Room.new(id: space) +      room.send_message(message) +    end +    if cfg.has_key?('message') == true +      log cfg.message +      msg = cfg.message % { :node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase } +      log msg +      log "Posting message to #{cfg.space}" +      client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) +    end +    log "Finished" +  end +end diff --git a/lib/oxidized/hook/exec.rb b/lib/oxidized/hook/exec.rb index 3f984c2..069b888 100644 --- a/lib/oxidized/hook/exec.rb +++ b/lib/oxidized/hook/exec.rb @@ -23,10 +23,9 @@ class Exec < Oxidized::Hook        @cmd = cfg.cmd        raise "invalid cmd value" unless @cmd.is_a?(String) || @cmd.is_a?(Array)      end -    rescue RuntimeError => e      raise ArgumentError, -      "#{self.class.name}: configuration invalid: #{e.message}" +          "#{self.class.name}: configuration invalid: #{e.message}"    end    def run_hook ctx @@ -45,7 +44,7 @@ class Exec < Oxidized::Hook    def run_cmd! env      pid, status = nil, nil      Timeout.timeout(@timeout) do -      pid = spawn env, @cmd , :unsetenv_others => true +      pid = spawn env, @cmd, :unsetenv_others => true        pid, status = wait2 pid        unless status.exitstatus.zero?          msg = "#{@cmd.inspect} failed with exit value #{status.exitstatus}" @@ -53,11 +52,11 @@ class Exec < Oxidized::Hook          raise msg        end      end -  rescue TimeoutError +  rescue Timeout::Error      kill "TERM", pid      msg = "#{@cmd} timed out"      log msg, :error -    raise TimeoutError, msg +    raise Timeout::Error, msg    end    def make_env ctx diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb index f74b22a..e077d5d 100644 --- a/lib/oxidized/hook/githubrepo.rb +++ b/lib/oxidized/hook/githubrepo.rb @@ -35,26 +35,32 @@ class GithubRepo < Oxidized::Hook      end      Rugged::Commit.create(repo, { -      parents: [repo.head.target, their_branch.target], -      tree: merge_index.write_tree(repo), -      message: "Merge remote-tracking branch '#{their_branch.name}'", -      update_ref: "HEAD" -    }) +                            parents: [repo.head.target, their_branch.target], +                            tree: merge_index.write_tree(repo), +                            message: "Merge remote-tracking branch '#{their_branch.name}'", +                            update_ref: "HEAD" +                          })    end    private    def credentials -    @credentials ||= if cfg.has_key?('username') && cfg.has_key?('password') -      log "Using https auth", :debug -      Rugged::Credentials::UserPassword.new(username: cfg.username, password: cfg.password) -    else -      if cfg.has_key?('publickey') && cfg.has_key?('privatekey') -        log "Using ssh auth with key", :debug -        Rugged::Credentials::SshKey.new(username: 'git', publickey: File.expand_path(cfg.publickey), privatekey: File.expand_path(cfg.privatekey), passphrase: ENV["OXIDIZED_SSH_PASSPHRASE"]) +    Proc.new do |url, username_from_url, allowed_types| +      if cfg.has_key?('username') +        git_user = cfg.username +      else +        git_user = username_from_url ? username_from_url : 'git' +      end + +      if cfg.has_key?('password') +        log "Authenticating using username and password as '#{git_user}'", :debug +        Rugged::Credentials::UserPassword.new(username: git_user, password: cfg.password) +      elsif cfg.has_key?('publickey') && cfg.has_key?('privatekey') +        log "Authenticating using ssh keys as '#{git_user}'", :debug +        Rugged::Credentials::SshKey.new(username: git_user, publickey: File.expand_path(cfg.publickey), privatekey: File.expand_path(cfg.privatekey), passphrase: ENV["OXIDIZED_SSH_PASSPHRASE"])        else -        log "Using ssh auth with agentforwarding", :debug -        Rugged::Credentials::SshKeyFromAgent.new(username: 'git') +        log "Authenticating using ssh agent as '#{git_user}'", :debug +        Rugged::Credentials::SshKeyFromAgent.new(username: git_user)        end      end    end diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb index 7cd4465..baaf291 100644 --- a/lib/oxidized/hook/slackdiff.rb +++ b/lib/oxidized/hook/slackdiff.rb @@ -10,47 +10,44 @@ class SlackDiff < Oxidized::Hook    end    def run_hook(ctx) -    if ctx.node -      if ctx.event.to_s == "post_store" -        log "Connecting to slack" -        Slack.configure do |config| -           config.token = cfg.token -           config.proxy = cfg.proxy if cfg.has_key?('proxy') -        end -        client = Slack::Client.new -        client.auth_test -        log "Connected" -        # diff snippet - default -        diffenable = true -        if cfg.has_key?('diff') == true -          if cfg.diff == false -            diffenable = false -          end -        end -        if diffenable == true -          gitoutput = ctx.node.output.new -          diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil -          unless diff == "no diffs" -            title = "#{ctx.node.name.to_s} #{ctx.node.group.to_s} #{ctx.node.model.class.name.to_s.downcase}" -            log "Posting diff as snippet to #{cfg.channel}" -            client.files_upload(channels: cfg.channel, as_user: true, -                                content: diff[:patch].lines.to_a[4..-1].join, -                                filetype: "diff", -                                title: title, -                                filename: "change" -                               ) -          end -        end -        # message custom formatted - optional -        if cfg.has_key?('message') == true -          log cfg.message -          msg = cfg.message % {:node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase} -          log msg -          log "Posting message to #{cfg.channel}" -          client.chat_postMessage(channel: cfg.channel, text: msg,  as_user: true) -        end -        log "Finished" +    return unless ctx.node +    return unless ctx.event.to_s == "post_store" +    log "Connecting to slack" +    Slack.configure do |config| +      config.token = cfg.token +      config.proxy = cfg.proxy if cfg.has_key?('proxy') +    end +    client = Slack::Client.new +    client.auth_test +    log "Connected" +    # diff snippet - default +    diffenable = true +    if cfg.has_key?('diff') == true +      if cfg.diff == false +        diffenable = false +      end +    end +    if diffenable == true +      gitoutput = ctx.node.output.new +      diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil +      unless diff == "no diffs" +        title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}" +        log "Posting diff as snippet to #{cfg.channel}" +        client.files_upload(channels: cfg.channel, as_user: true, +                            content: diff[:patch].lines.to_a[4..-1].join, +                            filetype: "diff", +                            title: title, +                            filename: "change")        end      end +    # message custom formatted - optional +    if cfg.has_key?('message') == true +      log cfg.message +      msg = cfg.message % { :node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase } +      log msg +      log "Posting message to #{cfg.channel}" +      client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) +    end +    log "Finished"    end  end diff --git a/lib/oxidized/hook/xmppdiff.rb b/lib/oxidized/hook/xmppdiff.rb index 396d1b3..6acb172 100644 --- a/lib/oxidized/hook/xmppdiff.rb +++ b/lib/oxidized/hook/xmppdiff.rb @@ -7,54 +7,52 @@ class XMPPDiff < Oxidized::Hook      raise KeyError, 'hook.password is required' unless cfg.has_key?('password')      raise KeyError, 'hook.channel is required' unless cfg.has_key?('channel')      raise KeyError, 'hook.nick is required' unless cfg.has_key?('nick') - end +  end    def run_hook(ctx) -    if ctx.node -      if ctx.event.to_s == "post_store" -        begin -          Timeout::timeout(15) do -            gitoutput = ctx.node.output.new -            diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil - -            interesting = diff[:patch].lines.to_a[4..-1].any? { |line| -              ["+", "-"].include?(line[0]) and not ["#", "!"].include?(line[1]) -            } -            interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '-' } -            interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '+' } +    return unless ctx.node +    return unless ctx.event.to_s == "post_store" +    begin +      Timeout.timeout(15) do +        gitoutput = ctx.node.output.new +        diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil + +        interesting = diff[:patch].lines.to_a[4..-1].any? do |line| +          ["+", "-"].include?(line[0]) and not ["#", "!"].include?(line[1]) +        end +        interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '-' } +        interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '+' } -            if interesting -              log "Connecting to XMPP" -              client = Jabber::Client.new(Jabber::JID.new(cfg.jid)) -              client.connect -              sleep 1 -              client.auth(cfg.password) -              sleep 1 +        if interesting +          log "Connecting to XMPP" +          client = Jabber::Client.new(Jabber::JID.new(cfg.jid)) +          client.connect +          sleep 1 +          client.auth(cfg.password) +          sleep 1 -              log "Connected" +          log "Connected" -              m = Jabber::MUC::SimpleMUCClient.new(client) -              m.join(cfg.channel + "/" + cfg.nick) +          m = Jabber::MUC::SimpleMUCClient.new(client) +          m.join(cfg.channel + "/" + cfg.nick) -              log "Joined" +          log "Joined" -              title = "#{ctx.node.name.to_s} #{ctx.node.group.to_s} #{ctx.node.model.class.name.to_s.downcase}" -              log "Posting diff as snippet to #{cfg.channel}" +          title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}" +          log "Posting diff as snippet to #{cfg.channel}" -              m.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join) +          m.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join) -              sleep 1 +          sleep 1 -              client.close +          client.close -              log "Finished" +          log "Finished" -            end -          end -        rescue Timeout::Error -          log "timed out"          end        end +    rescue Timeout::Error +      log "timed out"      end    end  end | 
