Skip to content

Instantly share code, notes, and snippets.

@sldenazis
Created March 29, 2017 15:45
Show Gist options
  • Save sldenazis/8f845eafa3a7326daeeee03f0d69d5fa to your computer and use it in GitHub Desktop.
Save sldenazis/8f845eafa3a7326daeeee03f0d69d5fa to your computer and use it in GitHub Desktop.
Clean execution logs for rundeck (with mysql database)
#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
function getFileConfig()
{
local file_config="/etc/rundeck/rundeck-config.properties"
echo ${file_config}
}
function getDBHost()
{
local db_hostname=`
grep ^dataSource.url $(getFileConfig) \
| cut -d'=' -f2 \
| cut -d'/' -f3
`
echo ${db_hostname}
}
function getDBName()
{
local db_name=`
grep ^dataSource.url $(getFileConfig) \
| cut -d'=' -f2 \
| cut -d'/' -f4 \
| cut -d'?' -f1
`
echo ${db_name}
}
function getDBUser()
{
local db_user=`
grep ^dataSource.user $(getFileConfig) \
| cut -d'=' -f2`
echo ${db_user}
}
function getDBPass()
{
local db_pass=`
grep ^dataSource.password $(getFileConfig) \
| cut -d'=' -f2`
echo ${db_pass}
}
function main()
{
#Validate project
local project=${1:-invalid}
if [ "${project}" == "invalid" ]
then
usage >&2
exit 2
fi
local path_project_jobs="/var/lib/rundeck/logs/rundeck/${project}/job"
if [[ ! -d "${path_project_jobs}" ]]
then
echo "Error! \"${path_project_jobs}\" does not exist, check the project name!" >&2
exit 3
else
file_ids=`mktemp /tmp/delete_ids_XXXXXXX`
file_ids_comma=`mktemp /tmp/delete_ids_comma_XXXXXXX`
file_sql=`mktemp /tmp/delete_ids_sql_XXXXXXX`
query_begin="DELETE FROM $(getDBName).execution WHERE id IN ("
query_end=")"
cd "${path_project_jobs}"
find -type f -name "*.state.json" -mtime +$2 \
| cut -d'/' -f4 \
| grep -E -o '[0-9]+' > ${file_ids}
#Parse to sql query
/usr/bin/paste -d, -s ${file_ids} > ${file_ids_comma}
echo ${query_begin}$(cat $file_ids_comma)${query_end} > ${file_sql}
#Delete registers from database
mysql -h $(getDBHost) -u $(getDBUser) $(getDBName) -p$(getDBPass) < ${file_sql}
if [[ $? -eq 0 ]]
then
#Delete files from filesystem
for id in $(cat $file_ids)
do
rm -fv */logs/$id.state.json \
&& rm -fv */logs/$id.rdlog
done
fi
fi
return 0
}
main $@
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment