Rundeck - сбой работы только при выходе из строя всех узлов

Можно ли установить для общего состояния задания значение ОК, если хотя бы один узел сообщает, что все в порядке? В настоящее время моя работа выполняет задачи на докере и будет успешно выполняться только для лидера и не удастся для других. Я бы хотел, чтобы с работой все было в порядке, если она успешно выполняется хотя бы на одном узле. Это возможно?


person FWise    schedule 11.11.2019    source источник


Ответы (1)


Вы можете сделать это следующим образом:

Во-первых, вам нужно две работы. Первый: указывает на ваши узлы, но для этого задания требуется возможность передать имя узла в текстовое поле фильтра узлов. И второй: вызвать первый через API встроенным скриптом.

Вам просто нужно проверить вторую работу.

Первая работа:

    <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>

Вторая работа терпит неудачу только в том случае, если первая работа терпит неудачу на всех узлах. Если первое задание выполняется хотя бы на одном узле, задание показывает «ОК».

Если вам нужен отдельный скрипт, я оставлю его здесь:

    #!/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.

person MegaDrive68k    schedule 11.11.2019