Py | Rundeck Delete Executions

We were running a Rundeck instance using a File-DB ( not MYSQL) and after a month we found that rundeck webpages were very slow and it was unusable. It was because we had lot of job that runs repeatedly in shorter interval and there were so many execution history. This slowed the whole rundeck web front end.

I tried to use the normal bulk delete operation using the API and it was still very slow to get all the execution IDs and then to delete them. So I used a work around to get the Execution IDs from the log-filename instead of using the rundeck API. It was still slow but it reduced the overall time by like 60-70% as getting the IDs was the time consuming task.

Below is the code

#!/usr/bin/python -tt
'''
Rundeck delete execution
---------------
change-history
---------------
1.0|21-oct-2015|vsubr|created

'''
__version__ = '1.0'

import sys
import time
from datetime import datetime
import re
from os import listdir
from os.path import isfile, join
import requests
import json

#-------  foxtel/AWS App related  --------

RUNDECK_API_KEY  = 'enter the rundeck API key'
RUNDECK_SERVER  = 'http://serverxxxx.com.au'
RUNDECK_PORT  = '4440'
RUNDECK_BASE_URL  = RUNDECK_SERVER +':'+RUNDECK_PORT
EXPIRE_DAYS = 3
TODAY = int(round(time.time() * 1000))
EXPIRE_MILISECONDS = EXPIRE_DAYS * 24 * 60 * 60 * 1000
RUNDECK_JOB_DIR    = '/data/rundeck/prod/job/'

#-------------------------------------------------------------------------
#API call to delete an execution by ID
def deleteExecution(execution_ids):
    #url =  cfg.RUNDECK_SERVER +':'+cfg.RUNDECK_PORT+'/api/12/execution/'+execution_id
 url =  RUNDECK_BASE_URL+'/api/12/executions/delete'
 payload = { 'ids': execution_ids}
 #payload = {"ids": [409147,66]}
 headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': RUNDECK_API_KEY }
 
 print(payload)
 print(headers)
 r = requests.post(url, data=json.dumps(payload), headers=headers)
 #r = requests.post(url,  headers=headers, verify=False)
 print( "response = %s" % r)
 
def deleteUsingIDs(): 
 for job in listdir(RUNDECK_JOB_DIR):
  print("****** %s" % job)
  i=0
  larr_exec_Ids = []
  for eachrun in [f for f in listdir(RUNDECK_JOB_DIR+job+'/logs/') if re.match('.*\.rdlog$', f, re.IGNORECASE)]:
   print("****** %s %s" % (eachrun, re.sub('\.rdlog$','',eachrun)))
   l_exec_id = re.sub('\.rdlog$','',eachrun)
   i=i+1
   larr_exec_Ids.append(l_exec_id)
   if i == 20:
    print(larr_exec_Ids)
    deleteExecution(larr_exec_Ids)
    larr_exec_Ids = []
    i = 0
  
  if len(larr_exec_Ids)> 0:
   deleteExecution(larr_exec_Ids)  

#----------------------------------------------------------------------------
if __name__ == "__main__":
 
 deleteUsingIDs()
#-----------------------------------------------------------------------------

Comments

Popular posts from this blog

Tableau - Accessing Tableau's DB

react-bootstrap-table | header column alignment fix

Tableau : Convert ESRI shapes into Tableau Format