• nexus 数据库备份任务webhook 通知-另外一种方法


    使用benthos 做为webhook,是一种方法,功能很强大,但是有点复杂,所以换了一个更简单直接的webhook 工具
    根据请求的数据,只处理关于db exporter 任务部分的消息,然后就是调用shell 执行一些逻辑处理

    环境处理

    使用docker-compose 运行

    • docker-compose 文件
    version: "3"
    services:
      webhook:
        image: almir/webhook
        command: ["-verbose", "-hooks=/etc/webhook/hooks.json","-hotreload"]
        volumes:
        - "./hooks.json:/etc/webhook/hooks.json"
        - "./app.sh:/app.sh"
        ports:
        - "9000:9000"
      nexus:
        image: sonatype/nexus3
        ports:
        - "8081:8081"
        volumes:
        - ./nexus-data:/nexus-data
        - ./backup/:/backup/
    • webhook 配置文件

      hooks.json 主要是定义webhook 的match 规则,以及匹配规则之后执行的shell,因为是post 请求,所以使用了payload source

    [
        {
          "id": "webhook",
          "execute-command": "/app.sh",
          "command-working-directory": "/",
          "include-command-output-in-response":true,
          "pass-arguments-to-command":
          [
            {
              "source": "payload",
              "context": "audit.context"
            },
            {
              "source": "payload",
              "type": "audit.type"
            }
          ],
          "trigger-rule":
          {
            "and":
            [
              {
                "match":
                {
                  "type": "value",
                  "value": "Admin - Export databases for backup",
                  "parameter":
                  {
                    "source": "payload",
                    "name": "audit.context"
                  }
                }
              },
              {
                "match":
                {
                  "type": "value",
                  "value": "finished",
                  "parameter":
                  {
                    "source": "payload",
                    "name": "audit.type"
                  }
                }
              }
            ]
          }
        }
      ]
    • 执行shell

      为了测试,很简单,就是echo 信息

    #!/bin/sh
    echo "this is a demo app"

    启动&&测试

    • 启动
    docker-compose up -d
    • 配置nexus webhook (audit) 以及db exporter task

      主要是通过global 的webhook 通知db exporter 任务已经完成,然后完成之后可以执行一些固定的shell 脚本

    audit 配置,路径是更具json 定义文件编写的


    db exporter task 配置,这个可以根据实际自己调整

    • 测试
      点击db exporter 的run 即可
      效果
    webhook_1 | [webhook] 2019/04/28 11:45:00 [2bb51c] error extracting command arguments: couldn't retrieve argument for {Source:payload Name: EnvName: Base64Decode:false}
    webhook_1 | [webhook] 2019/04/28 11:45:00 [2bb51c] error extracting command arguments: couldn't retrieve argument for {Source:payload Name: EnvName: Base64Decode:false}
    webhook_1 | [webhook] 2019/04/28 11:45:00 [2bb51c] executing /app.sh (/app.sh) with arguments ["/app.sh" "" ""] and environment [] using / as cwd
    webhook_1 | [webhook] 2019/04/28 11:45:00 [2bb51c] command output: this is a demo app
    webhook_1 | 
    webhook_1 | [webhook] 2019/04/28 11:45:00 [2bb51c] finished handling webhook
    webhook_1 | [webhook] 2019/04/28 11:45:00 200 | 2.733775ms | webhook:9000 | POST /hooks/webhook 
    webhook_1 | [webhook] 2019/04/28 11:50:00 [e1803a] incoming HTTP request from 172.19.0.3:58026
    webhook_1 | [webhook] 2019/04/28 11:50:00 [e1803a] webhook got matched
    webhook_1 | [webhook] 2019/04/28 11:50:00 [e1803a] webhook got matched, but didn't get triggered because the trigger rules were not satisfied
    webhook_1 | [webhook] 2019/04/28 11:50:00 200 | 198.993µs | webhook:9000 | POST /hooks/webhook 
    webhook_1 | [webhook] 2019/04/28 11:50:00 [3dc5af] incoming HTTP request from 172.19.0.3:58028
    webhook_1 | [webhook] 2019/04/28 11:50:00 [3dc5af] webhook got matched
    webhook_1 | [webhook] 2019/04/28 11:50:00 [3dc5af] webhook got matched, but didn't get triggered because the trigger rules were not satisfied
    webhook_1 | [webhook] 2019/04/28 11:50:00 200 | 143.131µs | webhook:9000 | POST /hooks/webhook 
    webhook_1 | [webhook] 2019/04/28 11:50:00 [3496eb] incoming HTTP request from 172.19.0.3:58030
    webhook_1 | [webhook] 2019/04/28 11:50:00 [3496eb] webhook got matched
    webhook_1 | [webhook] 2019/04/28 11:50:00 [3496eb] webhook got matched, but didn't get triggered because the trigger rules were not satisfied

    说明

    使用adnanh/webhook 比使用benthos 方便简单好多,功能已经够用了

    参考资料

    https://github.com/rongfengliang/nexus-webhook-dbbackup
    https://github.com/adnanh/webhook/

  • 相关阅读:
    创建本地源,使用yum install
    查找SCAN大量块的一个sql
    好的代码像首诗,差的代码像坨屎。
    ps
    eclipse程序正确运行却有红叉
    JS中文乱码解决方案
    初学JQuery
    初学JQuery 2
    大神的电脑软件
    eclipse导入已存在于workspace的项目
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/10786184.html
Copyright © 2020-2023  润新知