繁体   English   中英

如何将 Rundeck 作业的步骤状态获取到文件

[英]How to get steps status of a Rundeck job to a file

我在 Rundeck 中有一个父作业,并且在步骤中我有多个参考作业(使用“作业参考 - 为每个节点执行另一个作业”创建),并且这些参考作业中的每一个在内部都有不同的任务。

有没有办法将父作业的步骤状态(通过或失败)发送到文件中?

这样做的目的是生成一份报告并附在一封邮件中,该邮件将显示每个步骤的成功或失败。

您可能已经注意到,Rundeck 仅在作业参考步骤工作流上执行父作业(这是设计使然),在这种情况下,我们将不得不对 Rundeck 进行一些“玩”。

我们需要子作业的执行,为此,我们必须让它们单独运行,捕获每个执行的状态并将其放置在模板Markdown 文件中,该文件可以作为电子邮件模板在通知中发送

为了调用每个作业,我们将创建一个“假父作业”,它单独运行每个子作业(通过API使用cURL )并将结果(使用jq提取值)保存在一个内联脚本步骤中的Markdown 文件中,这是剧本:

# script that obtains individual child jobs and uses it to a mail notification
# https://stackoverflow.com/questions/64590927/how-to-get-steps-status-of-a-rundeck-job-to-a-file

#####################################################
# rundeck instance values
rdeck_server="your_rundeck_host" #rundeck hostname
rdeck_port="4440" # rundeck tcp port
rdeck_api="36" # rundeck api version
jobid="9c667cb5-7f93-4c01-99b0-3249e75887e4 1c861d46-17a3-45ee-954d-74c6d7b597a0 ae9e455e-ca7a-440e-9ab4-bfd25827b287" # space separated child jobs id's
token="bmqlGuhEcekSyNtWAwuizER4YoJBgZdI" # user token

#####################################################
# "prudential" time between actions (in seconds)
pt="2"

#####################################################
# clean the file
echo "" > myfile.md

#####################################################
# add a header to a file
mydate=$(date +'%m/%d/%Y')
echo "# $mydate report" >> myfile.md

#####################################################
# 1) run the job via API and store the execution ID.
# 2) takes the execution ID and store job status via API
for myid in $jobid # iterate over jobs id's
    do
        # sleep
        sleep $pt
        
        # save the execution id (to get the status later) on a variable named "execid"
        execid=$(curl -s -H accept:application/json --location --request POST "http://$rdeck_server:$rdeck_port/api/$rdeck_api/job/$myid/run?authtoken=$token" | jq -r '.id')
        
        # sleep
        sleep $pt
        
        # save the status on a variable named "status"
        status=$(curl -s --location --request GET "http://$rdeck_server:$rdeck_port/api/$rdeck_api/execution/$execid/state?authtoken=$token" | jq -r '.executionState')
        
        # put the status on a file
        echo "* job $myid is $status" >> myfile.md
        
        # rundeck friendly output message
        echo "job $myid is done, status: $status"
done

在工作定义中,它看起来像这样:

<joblist>
  <job>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>eb4826bc-cc49-46b5-9aff-351afb529197</id>
    <loglevel>INFO</loglevel>
    <name>FakeParent</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <notification>
      <onfailure>
        <email attachType='file' recipients='it@example.net' subject='info' />
      </onfailure>
      <onsuccess>
        <email attachType='file' recipients='it@example.net' subject='info' />
      </onsuccess>
    </notification>
    <notifyAvgDurationThreshold />
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <exec>echo "starting..."</exec>
      </command>
      <command>
        <fileExtension>.sh</fileExtension>
        <script><![CDATA[# script that obtains individual child jobs and uses it to a mail notification
# https://stackoverflow.com/questions/64590927/how-to-get-steps-status-of-a-rundeck-job-to-a-file

#####################################################
# rundeck instance values
rdeck_server="your_rundeck_host" #rundeck hostname
rdeck_port="4440" # rundeck tcp port
rdeck_api="36" # rundeck api version
jobid="9c667cb5-7f93-4c01-99b0-3249e75887e4 1c861d46-17a3-45ee-954d-74c6d7b597a0 ae9e455e-ca7a-440e-9ab4-bfd25827b287" # space separated child jobs id's
token="bmqlGuhEcekSyNtWAwuizER4YoJBgZdI" # user token

#####################################################
# "prudential" time between actions (in seconds)
pt="2"

#####################################################
# clean the file
echo "" > myfile.md

#####################################################
# add a header to a file
mydate=$(date +'%m/%d/%Y')
echo "# $mydate report" >> myfile.md

#####################################################
# 1) run the job via API and store the execution ID.
# 2) takes the execution ID and store job status via API
for myid in $jobid # iterate over jobs id's
    do
        # sleep
        sleep $pt
        
        # save the execution id (to get the status later) on a variable named "execid"
        execid=$(curl -s -H accept:application/json --location --request POST "http://$rdeck_server:$rdeck_port/api/$rdeck_api/job/$myid/run?authtoken=$token" | jq -r '.id')
        
        # sleep
        sleep $pt
        
        # save the status on a variable named "status"
        status=$(curl -s --location --request GET "http://$rdeck_server:$rdeck_port/api/$rdeck_api/execution/$execid/state?authtoken=$token" | jq -r '.executionState')
        
        # put the status on a file
        echo "* job $myid is $status" >> myfile.md
        
        # rundeck friendly output message
        echo "job $myid is done, status: $status"
done]]></script>
        <scriptargs />
        <scriptinterpreter>/bin/bash</scriptinterpreter>
      </command>
      <command>
        <exec>echo "done!"</exec>
      </command>
    </sequence>
    <uuid>eb4826bc-cc49-46b5-9aff-351afb529197</uuid>
  </job>
</joblist>

如果您检查“通知”部分,您会注意到它会在正确执行或失败时发送一封电子邮件。 您需要配置 Rundeck 以便它可以发送电子邮件。 我留下了配置它的步骤:

  1. 停止您的 Rundeck 服务。

  2. rundeck-config.properties文件中添加电子邮件配置(通常在/etc/rundeck路径):

# e-mail notification settings
grails.mail.host=your-smtp-host.com
grails.mail.port=25
grails.mail.username=your-username
grails.mail.password=yourpassword

更多信息在这里

  1. rundeck-config.properties文件中添加特定于您的作业的模板配置(检查第 3 行,是作业脚本生成的文件路径):
# project and job specific
rundeck.mail.YOUR-PROJECT-NAME.YOUR-JOB-NAME.template.subject=your-subject-string
rundeck.mail.YOUR-PROJECT-NAME.YOUR-JOB-NAME.template.file=/path/to/your/myfile.md
rundeck.mail.YOUR-PROJECT-NAME.YOUR-JOB-NAME.template.log.formatted=true # (if true, prefix log lines with context information)
  1. 启动您的 rundeck 服务。

在执行作业的那一刻,您将在收件箱中看到单个执行和报告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM