【问题标题】:Rundeck - Job call other jobRundeck - 工作调用其他工作
【发布时间】:2020-09-15 01:36:31
【问题描述】:

我需要知道如何从其他工作中调用工作。我的目标是 Job A 调用 Job B 并发送 数组参数。

基本上,Job B 包含启动/停止基础设施服务的逻辑,Job A 包含获取所有服务和执行操作的逻辑。

如果不可能,你知道有什么替代方法可以做到吗?

问候

【问题讨论】:

    标签: rundeck


    【解决方案1】:

    您有两种选择。第一个是使用option list(您可以使用文本框选择)上定义的操作创建父作业,并使用job reference steps 上的参数将该操作传递给子作业。

    基本上,子作业会评估该字符串并根据该字符串采取一些行动。我做了一个工作示例(在 Rundeck 3.3.3 上测试)。

    父母工作:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option delimiter=',' enforcedvalues='true' multivalued='true' name='services' values='stop_httpd,stop_ircd,stop_pop3,start_httpd,start_ircd,start_pop3' valuesListDelimiter=','>
              <label>Action to your services</label>
            </option>
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>5c6e9603-6adf-4732-8b33-4e92068f1dcb</id>
        <loglevel>INFO</loglevel>
        <name>ParentJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <description>only for debug</description>
            <exec>echo "Services selected: ${option.services}"</exec>
            <plugins />
          </command>
          <command>
            <jobref name='HttpService' nodeStep='true'>
              <arg line='-action ${option.services}' />
              <uuid>58ca8e25-4473-4ba3-89be-29cfe6487b22</uuid>
            </jobref>
          </command>
          <command>
            <jobref group='services' name='IrcdService' nodeStep='true'>
              <arg line='-action ${option.services}' />
              <uuid>ea2b2e0f-f140-4716-a207-1690a42c2a59</uuid>
            </jobref>
          </command>
          <command>
            <jobref group='services' name='Pop3Service' nodeStep='true'>
              <arg line='-action ${option.services}' />
              <uuid>df9152d8-c4df-4698-b7c7-e8fdd622c59b</uuid>
            </jobref>
          </command>
        </sequence>
        <uuid>5c6e9603-6adf-4732-8b33-4e92068f1dcb</uuid>
      </job>
    </joblist>
    

    子作业(服务):

    httpd:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='action' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <group>services</group>
        <id>58ca8e25-4473-4ba3-89be-29cfe6487b22</id>
        <loglevel>INFO</loglevel>
        <name>HttpService</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[# current service
    service=httpd
    
    # check if the service is running
    check_service () {
        # just a simulation, the service is up
        echo up
    }
    
    # up / down logic
    if [[ @option.action@ == *"start_httpd"* ]]; then
        if [[ $(check_service) == "up" ]]; then
            echo "Nothing to do, the service is running."
        else
            echo "starting $service service..."
            echo "$service started succefully"
        fi
    elif [[ @option.action@ == *"stop_httpd"* ]]; then
        if [[ $(check_service) == "down" ]]; then
            echo "Nothing to do, the service is down."
        else
            echo "stopping $service service..."
            echo "$service stopped succefully"
        fi
    else
        echo "Parameter not recognized, use start or stop."
    fi
    ]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>58ca8e25-4473-4ba3-89be-29cfe6487b22</uuid>
      </job>
    </joblist>
    

    pop3:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='action' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <group>services</group>
        <id>df9152d8-c4df-4698-b7c7-e8fdd622c59b</id>
        <loglevel>INFO</loglevel>
        <name>Pop3Service</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[# current service
    service=pop3
    
    # check if the service is running
    check_service () {
        # just a simulation, the service is up
        echo up
    }
    
    # up / down logic
    if [[ @option.action@ == *"start_pop3"* ]]; then
        if [[ $(check_service) == "up" ]]; then
            echo "Nothing to do, the service is running."
        else
            echo "starting $service service..."
            echo "$service started succefully"
        fi
    elif [[ @option.action@ == *"stop_pop3"* ]]; then
        if [[ $(check_service) == "down" ]]; then
            echo "Nothing to do, the service is down."
        else
            echo "stopping $service service..."
            echo "$service stopped succefully"
        fi
    else
        echo "Parameter not recognized, use start or stop."
    fi
    ]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>df9152d8-c4df-4698-b7c7-e8fdd622c59b</uuid>
      </job>
    </joblist>
    

    ircd:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='action' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <group>services</group>
        <id>ea2b2e0f-f140-4716-a207-1690a42c2a59</id>
        <loglevel>INFO</loglevel>
        <name>IrcdService</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[# current service
    service=ircd
    
    # check if the service is running
    check_service () {
        # just a simulation, the service is up
        echo up
    }
    
    # up / down logic
    if [[ @option.action@ == *"start_ircd"* ]]; then
        if [[ $(check_service) == "up" ]]; then
            echo "Nothing to do, the service is running."
        else
            echo "starting $service service..."
            echo "$service started succefully"
        fi
    elif [[ @option.action@ == *"stop_ircd"* ]]; then
        if [[ $(check_service) == "down" ]]; then
            echo "Nothing to do, the service is down."
        else
            echo "stopping $service service..."
            echo "$service stopped succefully"
        fi
    else
        echo "Parameter not recognized, use start or stop."
    fi
    ]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>ea2b2e0f-f140-4716-a207-1690a42c2a59</uuid>
      </job>
    </joblist>
    

    另一种方法是使用带有服务名称的类似操作列表,并在父作业上放置一个 bash 脚本迭代,该脚本通过RDCLI 调用服务。停止服务的示例:

    父母工作:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option delimiter=',' enforcedvalues='true' multivalued='true' name='services' values='httpd,ircd,pop3' valuesListDelimiter=','>
              <label>Services to Stop</label>
            </option>
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>bb1206e8-f00c-4aa6-b7de-71c2fe8a43b1</id>
        <loglevel>INFO</loglevel>
        <name>StopServices</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[#!/bin/sh
    
    echo "Services to stop: @option.services@"
    
    DataList=@option.services@
    
    Field_Separator=$IFS
    
    # set comma as internal field separator for the string list
    IFS=,
    for val in $DataList;
        do
            rd run -p @job.project@ -j $val -- -opt stop
            sleep 2
        done 
    
    IFS=$Field_Separator]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/sh</scriptinterpreter>
          </command>
        </sequence>
        <uuid>bb1206e8-f00c-4aa6-b7de-71c2fe8a43b1</uuid>
      </job>
    </joblist>
    

    童工(服务):

    httpd:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description>Apache httpd Service.</description>
        <executionEnabled>true</executionEnabled>
        <group>services</group>
        <id>db05488e-0566-4ab9-b43e-e057a55f1198</id>
        <loglevel>INFO</loglevel>
        <name>httpd</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[# current service
    service=httpd
    
    # check if the service is running
    check_service () {
        # just a simulation, the service is up
        echo up
    }
    
    # up / down logic
    if [ @option.opt@ == "start" ]; then
        if [ $(check_service) == "up" ]; then
            echo "Nothing to do, the service is running."
        else
            echo "starting $service service..."
            echo "$service started succefully"
        fi
    elif [ @option.opt@ == "stop" ]; then
        if [ $(check_service) == "down" ]; then
            echo "Nothing to do, the service is down."
        else
            echo "stopping $service service..."
            echo "$service stopped succefully"
        fi
    else
        echo "Parameter not recognized, use start or stop."
    fi
    ]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>db05488e-0566-4ab9-b43e-e057a55f1198</uuid>
      </job>
    </joblist>
    

    pop3:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description>Dovecot pop3 Service.</description>
        <executionEnabled>true</executionEnabled>
        <group>services</group>
        <id>7285547c-99e1-4688-b180-a272327f590d</id>
        <loglevel>INFO</loglevel>
        <name>pop3</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[# current service
    service=pop3
    
    # check if the service is running
    check_service () {
        # just a simulation, the service is up
        echo up
    }
    
    # up / down logic
    if [ @option.opt@ == "start" ]; then
        if [ $(check_service) == "up" ]; then
            echo "Nothing to do, the service is running."
        else
            echo "starting $service service..."
            echo "$service started succefully"
        fi
    elif [ @option.opt@ == "stop" ]; then
        if [ $(check_service) == "down" ]; then
            echo "Nothing to do, the service is down."
        else
            echo "stopping $service service..."
            echo "$service stopped succefully"
        fi
    else
        echo "Parameter not recognized, use start or stop."
    fi
    ]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>7285547c-99e1-4688-b180-a272327f590d</uuid>
      </job>
    </joblist>
    

    ircd:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description>IRC Daemon Service</description>
        <executionEnabled>true</executionEnabled>
        <group>services</group>
        <id>a214976f-d8e8-4ce8-89af-d6b247b618e4</id>
        <loglevel>INFO</loglevel>
        <name>ircd</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[# current service
    service=ircd
    
    # check if the service is running
    check_service () {
        # just a simulation, the service is up
        echo up
    }
    
    # up / down logic
    if [ @option.opt@ == "start" ]; then
        if [ $(check_service) == "up" ]; then
            echo "Nothing to do, the service is running."
        else
            echo "starting $service service..."
            echo "$service started succefully"
        fi
    elif [ @option.opt@ == "stop" ]; then
        if [ $(check_service) == "down" ]; then
            echo "Nothing to do, the service is down."
        else
            echo "stopping $service service..."
            echo "$service stopped succefully"
        fi
    else
        echo "Parameter not recognized, use start or stop."
    fi
    ]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>a214976f-d8e8-4ce8-89af-d6b247b618e4</uuid>
      </job>
    </joblist>
    

    如您所见,这基本上是脚本。最简单的方法是使用 ruleset strategy,可在 Rundeck Enterprise 上找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2016-10-17
      相关资源
      最近更新 更多