How to get steps status of a Rundeck job to a file

百般思念 提交于 2021-02-11 12:50:30

问题


I have a Parent job in Rundeck and in steps I have multiple reference jobs (that are created using "Job Reference - Execute another Job for each Node") and internally each of these reference jobs has different tasks.

Is there as way I can get Steps status (Pass or Fail) of the Parent job's to a file?

The purpose of this is to generate a report and attach to a mail which will have the success or failure of each step.


回答1:


As you may have noticed, Rundeck only takes the Parent Job execution on Job Reference Step workflows (this is by design), in this case, we will have to "play" a bit with Rundeck.

We need the executions of the child jobs, for that, we will have to make them run individually, capture the state of each of these executions and place it in a template Markdown file which can be sent in a notification as an email template.

To call each job we will create a "fake parent job" which runs each child jobs individually (via API using cURL) and save the result (using jq to extract the values) on an Markdown file in an inline-script step, this is the 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

And within a Job Definition it would look like this:

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

If you check the "notifications" section you will notice that it will send an email if it executes correctly or if it fails. You need to configure Rundeck so that it can send emails. I leave the steps to configure it:

  1. Stop your Rundeck service.

  2. Add the e-mail configuration on the rundeck-config.properties file (usually at /etc/rundeck path):

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

More info here.

  1. Add the template configuration, specific for your job, also on rundeck-config.properties file (check the line 3, is the file path generated by the job script):
# 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. Start your rundeck service.

At the moment of executing your job, you will see the individual execution and the report in the inbox.



来源:https://stackoverflow.com/questions/64590927/how-to-get-steps-status-of-a-rundeck-job-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!