-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteOldExecutionFromAPI.py
96 lines (83 loc) · 3.45 KB
/
deleteOldExecutionFromAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import requests
import xml.etree.ElementTree as ET
import time
import sys
API_KEY=<api_token>
RUNDECKSERVER = 'http://127.0.0.1'
RUNDECKPORT='4440'
EXPIRE_DAYS = @option.KeepDays@
LIMIT_JOBS = '@option.LimitJobs@'
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
# 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
# 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
# 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):
cnt = 0
for exec_id, exec_date in execid_dates.iteritems():
if isOlderThanExpireDays(int(exec_date), TODAY):
deleteExecution(exec_id)
cnt += 1
return cnt
print "Running on Jobs: " + LIMIT_JOBS;
projects = getProjectNames(listProjects())
for project in projects:
print 'project:\t'+project
jobids = getJobIDs(listJobsForProject(project))
for jobid in jobids:
if ( LIMIT_JOBS is 'ALL' ) or ( LIMIT_JOBS.find(jobid) != -1 ):
sys.stdout.write('\tjobid:\t'+jobid+'\t')
print checkDeletion(getExecutionDate(getExecutionsForAJob(jobid)))