-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRundeckLogfileCleanup.py
153 lines (140 loc) · 5.26 KB
/
RundeckLogfileCleanup.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import xml.etree.ElementTree as ET
import time
import json
import sys
import traceback
# Returns list of all the project names
def get_projects():
global PROPERTIES
global HEADERS
project_names = []
try:
url = URL + 'projects'
r = requests.get(url, headers=HEADERS, verify=False,timeout=PROPERTIES['TIMEOUT'])
root = ET.fromstring(r.text.encode('utf-8'))
for project in root:
for name in project.findall('name'):
project_names.append(name.text)
except:
print "Problem with project listing {0}".format(r)
pass
return project_names
# Returns list of all the jobids
def get_jobs_for_project(project_name):
global PROPERTIES
global HEADERS
job_ids = []
try:
url = URL + 'jobs'
payload = { 'project': project_name }
r = requests.get(url, params=payload, headers=HEADERS, verify=False,timeout=PROPERTIES['TIMEOUT'])
root = ET.fromstring(r.text.encode('utf-8'))
for job in root:
job_ids.append( (job.attrib['id'],job.find('name').text) )
except:
print "Problem with job listing {0}".format(r)
pass
return job_ids
# API call to get a page of the executions for a particular job id
def get_executions_for_job(job_id,page):
global PROPERTIES
global HEADERS
root = None
try:
url = URL + 'job/'+job_id+'/executions'
r = requests.get(url, params={'max':PROPERTIES['PAGE_SIZE'],'offset':page*PROPERTIES['PAGE_SIZE']}, headers=HEADERS, verify=False,timeout=PROPERTIES['TIMEOUT'])
root = ET.fromstring(r.text.encode('utf-8'))
except:
print "Problem with execution listing {0}".format(r)
pass
return root
#
# Return a dictionary of execute ids & dates
#
def get_execution_dates(root):
execid_dates = {}
try:
for execution in root:
execution_id = execution.get('id')
for date in execution.findall('date-ended'):
execution_date = date.get('unixtime')
execid_dates[execution_id] = execution_date
except:
pass
return execid_dates
#API call to delete an execution by ID
def delete_execution(execution_id):
global PROPERTIES
global HEADERS
url = URL + 'execution/'+execution_id
try:
r = requests.delete(url, headers=HEADERS, verify=False,timeout=PROPERTIES['TIMEOUT'])
if PROPERTIES['VERBOSE']:
print " Deleted execution id {0} {1} {2}".format( execution_id, r.text, r )
except:
pass
#API call to bulk delete executions by ID
def delete_executions(execution_ids):
global PROPERTIES
global HEADERS
url = URL + 'executions/delete'
try:
r = requests.post(url, headers=HEADERS, data= json.dumps({'ids':execution_ids}) , verify=False,timeout=PROPERTIES['DELETE_TIMEOUT'])
if PROPERTIES['VERBOSE']:
print " Deleted execution ids {0}".format( execution_ids, r.text, r )
except:
try:
print "Problem with execution deletion {0}".format(r)
except:
pass
pass
def delete_test(execution_date):
global PROPERTIES
global TODAY
millis_in_one_day = 1000*60*60*24
return ((TODAY - execution_date) > millis_in_one_day * PROPERTIES['MAXIMUM_DAYS'])
def check_deletion(execid_dates):
delete= ()
for exec_id, exec_date in execid_dates.iteritems():
if delete_test( int(exec_date) ):
delete += (exec_id,)
print " Delete {0} jobs from this page".format( len(delete) )
return delete
#
# Main
#
setting_filename = sys.argv[1] if len(sys.argv)>1 else 'properties.json'
with open(setting_filename,'r') as props_file:
PROPERTIES = json.load( props_file )
protocol='http'
if PROPERTIES['SSL']:
protocol='https'
# disable warnings about unverified https connections
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
URL = '{0}://{1}:{2}/api/{3}/'.format(protocol,PROPERTIES['RUNDECKSERVER'],PROPERTIES['PORT'],PROPERTIES['API_VERSION'])
HEADERS = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': PROPERTIES['API_KEY'] }
TODAY = int(round(time.time() * 1000))
for project in get_projects():
print project
for (jobid,jobname) in get_jobs_for_project(project):
print " {0}".format(jobname.encode('utf-8'))
page = 0
deleteable = ()
more = True
while ( more and len(deleteable)< PROPERTIES['MAX_DELETE'] ) :
try:
execution_root = get_executions_for_job(jobid,page)
print " Page {0} got {1} jobs".format(page,execution_root.attrib['count'])
page += 1
deleteable += check_deletion(get_execution_dates(execution_root))
more = int(execution_root.attrib['count']) == PROPERTIES['PAGE_SIZE']
except:
print traceback.format_exc()
print "Problem with executions {0} {1}".format(execution_root,sys.exc_info()[0])
more = False
if (len(deleteable) > 0 ):
print " Deleting {0} jobs in total".format( len(deleteable) )
delete_executions(deleteable)