The previous posts have built the components to allow us to get projects, jobs, executions and remove executions from a Rundeck instance via its API.

Now, we can just wrap them up :)

The list of packages we need is:

import time
import datetime
import sys
import logging

For convenience, I've declared a bunch of constants we can use throughout the project:

EXPIRE_DAYS = 30
SIZE=100
MAXCOUNT=1000
OFFSET=0
BATCH_SIZE=20
EXPIRE_MILISECONDS = EXPIRE_DAYS * 24 * 60 * 60 * 1000

log = logging.getLogger(__name__)

As we already have all the helper functions defined, we only need to wrap them in a convenient execution command:

def main():
    import argparse

    # The parser
    parser = argparse.ArgumentParser()
    parser.add_argument("host")
    parser.add_argument("port")
    parser.add_argument("api_key")

    args = parser.parse_args()

    projects = getProjectNames(listProjects(args.host, args.port, args.api_key))
    deleted = 0
    for project in projects:
        log.info('project:\t'+project)
        jobids = getJobIDs(listJobsForProject(args.host, args.port, args.api_key, project))
        for jobid in jobids:
            log.info('\tjobid:\t'+jobid)
            executions = getAllExecutionsForAJob(args.host, args.port,
                args.api_key, jobid, MAXCOUNT)

            # Save the list of timestamps in a file
            with open(jobid + '.csv', 'wt') as f:
                for execution in executions:
                    f.write("%s,%s\n" % (execution,executions[execution]))
            deleted += checkDeletion(args.host, args.port, args.api_key,
                executions)
        print('I have deleted :%d: jobs for %s' % (deleted,project))

The logis is fairly simple:

  1. Get the list of projects
  2. For each project, get its list of jobs
  3. For each job, get its list of executions
  4. Build a list of executions older than EXPIRE_DAYS
  5. Remove the old executions, 30 at a time.

It then gets simply wrapped:

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    main()

Now, you can call the script:

python ./clean_rundeck.py <server-id> <server-port> <api-key>

HTH,