Created
January 22, 2018 15:38
-
-
Save Duologic/34c3e59503bd7842a3f017060a437e90 to your computer and use it in GitHub Desktop.
Clean up old Rundeck logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import xml.etree.ElementTree as ET | |
import time | |
API_KEY='<key>' | |
RUNDECKSERVER = 'http://rundeck.example' | |
RUNDECKPORT='4440' | |
EXPIRE_DAYS = 3 | |
TODAY = int(round(time.time() * 1000)) | |
EXPIRE_MILISECONDS = EXPIRE_DAYS * 24 * 60 * 60 * 1000 | |
# API call to get the list of the existing projects on the server. | |
def listProjects(): | |
url = RUNDECKSERVER +':'+RUNDECKPORT+'/api/1/projects' | |
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': API_KEY } | |
r = requests.get(url, headers=headers, verify=False) | |
return r.text.encode('utf-8') | |
# Returns list of all the project names | |
def getProjectNames(projectsinfo_xml): | |
project_names = [] | |
root = ET.fromstring(projectsinfo_xml) | |
for projects in root: | |
for name in projects.findall('project'): | |
project_names.append(name.find('name').text) | |
return project_names | |
# API call to get the list of the jobs that exist for a project. | |
def listJobsForProject(project_mame): | |
url = RUNDECKSERVER +':'+RUNDECKPORT+'/api/1/jobs' | |
payload = { 'project': project_mame } | |
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': API_KEY } | |
r = requests.get(url, params=payload, headers=headers, verify=False) | |
return r.text.encode('utf-8') | |
# Returns list of all the jobids | |
def getJobIDs(jobsinfo_xml): | |
job_ids = [] | |
root = ET.fromstring(jobsinfo_xml) | |
for jobs in root: | |
for job in jobs: | |
job_ids.append(job.attrib['id']) | |
return job_ids | |
# API call to get the list of the executions for a Job. | |
def getExecutionsForAJob(job_id): | |
url = RUNDECKSERVER +':'+RUNDECKPORT+'/api/1/job/'+job_id+'/executions' | |
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': API_KEY } | |
r = requests.get(url, headers=headers, verify=False) | |
return r.text.encode('utf-8') | |
# Returns a dict {'execution_id01': 'execution_date01', 'execution_id02': 'execution_date02', ... } | |
def getExecutionDate(executionsinfo_xml): | |
execid_dates = {} | |
root = ET.fromstring(executionsinfo_xml) | |
for executions in root: | |
for execution in executions.findall('execution'): | |
execution_id = execution.get('id') | |
for date in execution.findall('date-ended'): | |
execution_date = date.get('unixtime') | |
execid_dates[execution_id] = execution_date | |
return execid_dates | |
#API call to delete an execution by ID | |
def deleteExecution(execution_id): | |
url = RUNDECKSERVER +':'+RUNDECKPORT+'/api/12/execution/'+execution_id | |
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': API_KEY } | |
r = requests.delete(url, headers=headers, verify=False) | |
def isOlderThanExpireDays(execution_date, today): | |
if ((today - execution_date) > EXPIRE_MILISECONDS): | |
return True | |
return False | |
def checkDeletion(execid_dates): | |
for exec_id, exec_date in execid_dates.iteritems(): | |
if isOlderThanExpireDays(int(exec_date), TODAY): | |
deleteExecution(exec_id) | |
projects = getProjectNames(listProjects()) | |
for project in projects: | |
print 'project:\t'+project | |
if project == 'harryporter-config': | |
jobids = getJobIDs(listJobsForProject(project)) | |
for jobid in jobids: | |
print '\tjobid:\t'+jobid | |
checkDeletion(getExecutionDate(getExecutionsForAJob(jobid))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment