如果一个节点成功,如何停止RunDeck作业在其他节点上运行

问题描述 投票:0回答:1

我是 RunDeck 的新手,我有一个任务来创建一个在 4 个节点上运行的作业。 要求是在一个节点上运行此作业,如果成功则停止执行。如果作业失败则转到下一个节点。 最后,如果所有节点都失败,则将作业标记为失败,否则成功。

我使用了一个选项来在步骤失败时停止执行,但我没有找到任何选项来在步骤成功时停止执行。

你能帮我一下吗?

rundeck
1个回答
0
投票

规则集策略(流程自动化的专有功能)是为这种场景而设计的,看看这个

现在,在 Rundeck OSS 上您需要制作一些 script-fu。我做了一个需要两个作业的例子:一个是需要调度的作业,我们称之为“MyJob”,另一个是“执行器”作业,该作业以特定的逻辑执行第一个作业,并决定是否继续尝试是否下一个节点。

MyJob:这只是一个带有过滤器的简单作业。

- defaultTab: nodes
  description: ''
  executionEnabled: true
  id: 9fcc183b-b4df-4554-b75b-c660eda706b3
  loglevel: INFO
  name: MyJob
  nodeFilterEditable: false
  nodefilters:
    dispatch:
      excludePrecedence: true
      keepgoing: false
      rankOrder: ascending
      successOnEmptyNodeFilter: false
      threadcount: '1'
    filter: ${option.thenode}
  nodesSelectedByDefault: true
  options:
  - name: thenode
  plugins:
    ExecutionLifecycle: {}
  scheduleEnabled: true
  sequence:
    commands:
    - exec: cat file
    keepgoing: true
    strategy: node-first
  uuid: 9fcc183b-b4df-4554-b75b-c660eda706b3

执行器作业:执行具有特定行为的第一个作业:在一个特定节点中发现成功执行后,停止在其余节点上执行。

- defaultTab: nodes
  description: ''
  executionEnabled: true
  id: 18bbd45e-5301-4498-8b92-0c4828194b61
  loglevel: INFO
  name: Executor
  nodeFilterEditable: false
  plugins:
    ExecutionLifecycle: {}
  scheduleEnabled: true
  sequence:
    commands:
    - fileExtension: sh
      interpreterArgsQuoted: false
      script: |-
        #!/bin/bash

            # script that detects only when all nodes fail
            # based on the following answer:
            # https://stackoverflow.com/questions/58798856/rundeck-fail-a-job-only-when-all-nodes-fail

            #####################################################
            # rundeck instance values
            server="localhost"
            port="4440"
            api="46"
            jobid="9fcc183b-b4df-4554-b75b-c660eda706b3"
            token="gYo7evX3rh1YKu1nfNSSgzy0xOMKr2jp"

            #####################################################
            # 1 - succeeded at least in one node
            # 0 - failed, keep trying
            #####################################################
            flag=0

            #####################################################
            # here put all nodes to pass via options, like a list
            mynodes=node00,node01,node02

            #####################################################
            # "prudential" time between actions
            pt="2"

            for currentnode in $(echo $mynodes | sed "s/,/ /g"); do
                if [ "$flag" -gt 0 ]; then
                    echo "the job has been succeeded in $currentnode"
                    exit 0
                else
                    sleep $pt
                    execid=$(curl -s -H accept:application/json --data-urlencode "argString=-thenode $currentnode" http://$server:$port/api/$api/job/$jobid/run?authtoken=$token | jq -r '.id')

                    sleep  $pt
                    status=$(curl -s --location --request GET "http://$server:$port/api/$api/execution/$execid/state?authtoken=$token" | jq -r '.executionState')

                    # if job runs OK, then assign the value 1 to flag
                    if [ "$status" = "SUCCEEDED" ]; then
                        flag=1
                    fi
                fi
            done
      scriptInterpreter: /bin/bash
    keepgoing: false
    strategy: node-first
  uuid: 18bbd45e-5301-4498-8b92-0c4828194b61

如果您需要看看“失业”,这里是脚本

#!/bin/bash

    # script that detects only when all nodes fail
    # based on the following answer:
    # https://stackoverflow.com/questions/58798856/rundeck-fail-a-job-only-when-all-nodes-fail

    #####################################################
    # rundeck instance values
    server="localhost"
    port="4440"
    api="46"
    jobid="9fcc183b-b4df-4554-b75b-c660eda706b3"
    token="gYo7evX3rh1YKu1nfNSSgzy0xOMKr2jp"

    #####################################################
    # 1 - succeeded at least in one node
    # 0 - failed, keep trying
    #####################################################
    flag=0

    #####################################################
    # here put all nodes to pass via options, like a list
    mynodes=node00,node01,node02

    #####################################################
    # "prudential" time between actions
    pt="2"

    for currentnode in $(echo $mynodes | sed "s/,/ /g"); do
        if [ "$flag" -gt 0 ]; then
            echo "the job has been succeeded in $currentnode"
            exit 0
        else
            sleep $pt
            execid=$(curl -s -H accept:application/json --data-urlencode "argString=-thenode $currentnode" http://$server:$port/api/$api/job/$jobid/run?authtoken=$token | jq -r '.id')

            sleep  $pt
            status=$(curl -s --location --request GET "http://$server:$port/api/$api/execution/$execid/state?authtoken=$token" | jq -r '.executionState')

            # if job runs OK, then assign the value 1 to flag
            if [ "$status" = "SUCCEEDED" ]; then
                flag=1
            fi
        fi
    done

逻辑是委托脚本步骤中的行为。

© www.soinside.com 2019 - 2024. All rights reserved.