【发布时间】:2020-10-29 12:16:06
【问题描述】:
我在 Rundeck 中有一个父作业,并且在步骤中我有多个参考作业(使用“作业参考 - 为每个节点执行另一个作业”创建)并且在内部每个这些参考作业都有不同的任务。
有没有办法将父作业的步骤状态(通过或失败)保存到文件中?
这样做的目的是生成一个报告并附加到一封邮件中,该邮件将记录每个步骤的成功或失败。
【问题讨论】:
我在 Rundeck 中有一个父作业,并且在步骤中我有多个参考作业(使用“作业参考 - 为每个节点执行另一个作业”创建)并且在内部每个这些参考作业都有不同的任务。
有没有办法将父作业的步骤状态(通过或失败)保存到文件中?
这样做的目的是生成一个报告并附加到一封邮件中,该邮件将记录每个步骤的成功或失败。
【问题讨论】:
您可能已经注意到,Rundeck 仅在 Job Reference Step 工作流上执行 Parent Job(这是设计使然),在这种情况下,我们将不得不与 Rundeck “玩”一点。
我们需要子作业的执行,为此,我们必须让它们单独运行,捕获每个执行的状态并将其放入模板Markdown file,该模板可以作为通知发送email template.
要调用每个作业,我们将创建一个“假父作业”,它单独运行每个子作业(通过 API 使用 cURL)并将结果(使用 jq 提取值)保存在 @987654326 @ 在inline-script 步骤中,这是脚本:
# 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 以便它可以发送电子邮件。我留下了配置它的步骤:
停止您的 Rundeck 服务。
在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
更多信息here。
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)
在执行您的工作时,您将看到individual execution 和inbox 中的报告。
【讨论】: