Rundeck-仅在所有节点都失败时才使作业失败 [英] Rundeck - Fail a job only when all nodes fail

查看:57
本文介绍了Rundeck-仅在所有节点都失败时才使作业失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果至少一个节点报告正常",是否可以将整体作业状态设置为正常"? 目前,我的工作是在docker上运行任务,并且仅在领导者上运行成功,而在其他领导者上失败.我想拥有它,以便该工作正常,只要它已在至少一个节点上成功运行即可.这可能吗?

Is it possible to have an overall job status set to OK if at least one node reports ok? Currently my job runs tasks on docker and will only run succeed on the leader and will fail on the others. I would like to have it so the job is OK so long as it has run successfully on at least one node. Is this possible?

推荐答案

您可以通过以下方式做到这一点:

You can do that in this way:

首先,您需要两项工作.第一个:指向您的节点,但是此作业需要一个选项来在节点过滤器文本框中传递节点名称.第二个是:使用内联脚本通过API调用第一个.

First, you need two jobs. The first one: points to your nodes, but this job needs an option to pass the node name in the node filter textbox. And the second one: to call the first one via API with an inline script.

您只需要检查第二份工作即可.

You just need to check the second job.

第一份工作:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='thenode' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <dispatch>
          <excludePrecedence>true</excludePrecedence>
          <keepgoing>false</keepgoing>
          <rankOrder>ascending</rankOrder>
          <successOnEmptyNodeFilter>false</successOnEmptyNodeFilter>
          <threadcount>1</threadcount>
        </dispatch>
        <executionEnabled>true</executionEnabled>
        <id>9fcc183b-b4df-4554-b75b-c660eda706b3</id>
        <loglevel>INFO</loglevel>
        <name>TestFWISE</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <nodefilters>
          <filter>${option.thenode}</filter>
        </nodefilters>
        <nodesSelectedByDefault>true</nodesSelectedByDefault>
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='true' strategy='node-first'>
          <command>
            <exec>sh hello.sh</exec>
          </command>
        </sequence>
        <uuid>9fcc183b-b4df-4554-b75b-c660eda706b3</uuid>
      </job>
    </joblist>

第二份工作:

    <joblist>
      <job>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>18bbd45e-5301-4498-8b92-0c4828194b61</id>
        <loglevel>INFO</loglevel>
        <name>Runner</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>sh</fileExtension>
            <script><![CDATA[#!/bin/bash

    # script that detect only when all nodes fail
    # https://stackoverflow.com/questions/58798856/rundeck-fail-a-job-only-when-all-nodes-fail

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

    #####################################################
    # 1 - succeeded at least in one node
    # 0 - failed in all nodes
    #####################################################
    flag="0"

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

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

    #####################################################
    # 1) run the job via API and store the execution ID.
    # 2) takes the execution ID and store job status via API
    # 3) with job status decides if runs at least on one node or not.
    for currentnode in $mynodes
        do
            sleep $pt

            execid=$(curl -H accept:application/json --data-urlencode "argString=-thenode $currentnode" http://$server:$port/api/$api/job/$jobid/run?authtoken=$token | jq -r '.id')

            # only for debug
            echo "execution id: $execid"

            sleep  $pt

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

            # only for debug
            echo "status is: $status"

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

    sleep  $pt

    # only for debug
    echo "flag value: $flag"

    #####################################################
    # now is time to check the flag
    if [ "$flag" -eq "1" ]
        then
            echo "the job has been succeeded at least in one node."
            exit 0 # rundeck job ends normally :3
        else
            echo "the job has been failed in all nodes."
            exit 1 # rundeck job ends with error :(
    fi]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>18bbd45e-5301-4498-8b92-0c4828194b61</uuid>
      </job>
    </joblist>

仅当您的第一个作业在所有节点上都失败时,第二个作业才会失败.如果第一个作业至少在一个节点中运行,则该作业将显示确定".

The second job fails only if your first job fails in all nodes. If the first job runs at least in one node the job shows "OK".

如果您需要将脚本分隔开,我留在这里:

If you need the script separated I leave it here:

    #!/bin/bash

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

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

    #####################################################
    # 1 - succeeded at least in one node
    # 0 - failed in all nodes
    #####################################################
    flag="0"

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

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

    #####################################################
    # 1) run the job via API and store the execution ID.
    # 2) takes the execution ID and store job status via API
    # 3) with job status decides if runs at least on one node or not.
    for currentnode in $mynodes
        do
            sleep $pt

            execid=$(curl -H accept:application/json --data-urlencode "argString=-thenode $currentnode" http://$server:$port/api/$api/job/$jobid/run?authtoken=$token | jq -r '.id')

            # only for debug
            echo "execution id: $execid"

            sleep  $pt

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

            # only for debug
            echo "status is: $status"

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

    sleep  $pt

    # only for debug
    echo "flag value: $flag"

    #####################################################
    # now is time to check the flag
    if [ "$flag" -eq "1" ]
        then
            echo "the job has been succeeded at least in one node."
            exit 0 # rundeck job ends normally :3
        else
            echo "the job has been failed in all nodes."
            exit 1 # rundeck job ends with error :(
    fi

注意:该脚本需要 JQ 才能起作用.

NOTE: The script needs JQ to work.

这篇关于Rundeck-仅在所有节点都失败时才使作业失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆