Skip to content

Instantly share code, notes, and snippets.

@guzmanbraso
Last active December 21, 2021 07:31
Show Gist options
  • Save guzmanbraso/c45ac5c2ab743c09bf7b5bcbe43e1bde to your computer and use it in GitHub Desktop.
Save guzmanbraso/c45ac5c2ab743c09bf7b5bcbe43e1bde to your computer and use it in GitHub Desktop.
Clean rundeck executions logs older than a given amount of days but keep a minimum amount of executions per job.
#!/bin/bash
# This script will clean executions older than a given amount of days ($DAYS) but will keep at least $KEEP executions.
# Based on @unicolet gist that keeps last 30 exec of each job ( https://gist.github.com/unicolet/af648a97163ce6b44645 )
# Taken from @lucabusin the workflow tables cleaning code ( https://gist.github.com/lucabusin/cad36a45764f2fc2e2daa81e2db4186d )
# setup ~/.mycnf or ~/.pgpass to allow passwordless connection to mysql/postgres
# Comment / Uncomment below to use postgres or mysql.
# Days to keep executions.
DAYS=90
# Minimum Amount to Keep
KEEP=30
# MySQL Connect string (use only DB name if connection is done with .mycnf credentials)
# Ej:
# MYSQL_CONNECT="rundeck"
# MYSQL_CONNECT="-h somehost -u user -ppassword somedbname"
MYSQL_CONNECT="rundeck"
cd /var/lib/rundeck/logs/rundeck
JOBS=`find . -maxdepth 3 -path "*/job/*" -type d`
for j in $JOBS ; do
echo "Processing job $j"
ids=`find $j -iname "*.rdlog" -mtime +$DAYS | sed -e "s/.*\/\([0-9]*\)\.rdlog/\1/" | sort -n -r`
declare -a JOBIDS=($ids)
if [ ${#JOBIDS[@]} -gt $KEEP ]; then
for job in ${JOBIDS[@]:$KEEP};do
# Remove the job run files...
echo " * Deleting job: $job"
#echo " rm -rf $j/logs/$job.*"
rm -rf $j/logs/$job.*
# Get workflow ID for execution
#echo "Getting workflowid: mysql $MYSQL_CONNECT -s -N -e \"SELECT workflow_id FROM execution WHERE id=$job\""
workflowid=`mysql $MYSQL_CONNECT -s -N -e "SELECT workflow_id FROM execution WHERE id=$job"`
# Get all workflowstepids for the execution workflowid
#echo "Getting workflowstepids: mysql $MYSQL_CONNECT -s -N -e \"SELECT workflow_step_id FROM workflow_workflow_step WHERE workflow_commands_id=$workflowid\""
workflowstepids=`mysql $MYSQL_CONNECT -s -N -e "SELECT workflow_step_id FROM workflow_workflow_step WHERE workflow_commands_id=$workflowid"`
declare -a WSIDS=($workflowstepids)
# Iterate over each workflow step id
for workflowstepid in $WSIDS ; do
# Remove workflow step id on workflow_workflow_step
#echo " mysql ${MYSQL_CONNECT} -e 'DELETE FROM workflow_workflow_step WHERE workflow_step_id=$workflowstepid'"
mysql ${MYSQL_CONNECT} -e "DELETE FROM workflow_workflow_step WHERE workflow_step_id=$workflowstepid"
# Remove workflow step id on workflow_step
#echo " mysql ${MYSQL_CONNECT} -e 'DELETE FROM workflow_step WHERE id=$workflowstepid'"
mysql ${MYSQL_CONNECT} -e "DELETE FROM workflow_step WHERE id=$workflowstepid"
done
# Remove job info from execution table
#echo " mysql ${MYSQL_CONNECT} -e 'DELETE FROM execution WHERE id=$job'"
mysql ${MYSQL_CONNECT} -e "DELETE FROM execution WHERE id=$job"
# Remove job info from base_report table
#echo " mysql ${MYSQL_CONNECT} -e 'DELETE FROM base_report WHERE jc_exec_id=$job'"
mysql ${MYSQL_CONNECT} -e "DELETE FROM base_report WHERE jc_exec_id=$job"
# Remove workflow id from workflow table
#echo " mysql ${MYSQL_CONNECT} -e 'DELETE FROM workflow WHERE id=$workflowid'"
mysql ${MYSQL_CONNECT} -e "DELETE FROM workflow WHERE id=$workflowid"
done
fi
done
@Krishnababu505
Copy link

Hello Guzmanbraso,

Thanks for the script it really helps me, when i am using the same script it is keep on executing below step deleting jobs

Deleting job: 3104429
14:48:45 | | | rm -rf ./XYZ/job/81476133-1da6-40fc-7d3c-abf2caedd1f9/logs/3104429.*

Later it is failing with below error after timeout

15:29:20 | | | Result: 2147483647

15:29:21 | | | Failed: NonZeroResultCode: Result code was 2147483647
| ANY04210451 [console] |
15:29:21 | | | Cancellation while running step [1]

Please help me what causes this issue.

Thanks In Advance.

Regards,
Krishna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment