forked from phwitservices/RundeckLogfileCleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter H. Walls
authored and
Peter H. Walls
committed
May 4, 2015
1 parent
155989b
commit 3501c9f
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/python | ||
import requests | ||
import xml.etree.ElementTree as ET | ||
import time | ||
import json | ||
import sys | ||
|
||
# Returns list of all the project names | ||
def get_projects(): | ||
project_names = [] | ||
try: | ||
url = URL + 'projects' | ||
r = requests.get(url, headers=HEADERS, verify=False,timeout=PROPERTIES['TIMEOUT']) | ||
root = ET.fromstring(r.text) | ||
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): | ||
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) | ||
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): | ||
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).find("executions") | ||
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): | ||
url = URL + 'execution/'+execution_id | ||
try: | ||
r = requests.delete(url, headers=HEADERS, verify=False,timeout=PROPERTIES['TIMEOUT']) | ||
if PROPERTIES['VERBOSE']: | ||
print "\t\t\tDeleted 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): | ||
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 "\t\t\tDeleted 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, today): | ||
return ((today - execution_date) > MILLISECONDS_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), TODAY): | ||
delete += (exec_id,) | ||
print "\t\tDelete {0} jobs from this page".format( len(delete) ) | ||
return delete | ||
|
||
# | ||
# Main | ||
# | ||
|
||
PROPERTIES = json.read('properties.json','r') | ||
|
||
API_VERSION = 12 | ||
URL = 'http://{0}:{1}/api/{2}/'.format( PROPERTIES['RUNDECKSERVER'],PROPERTIES['PORT'],API_VERSION) | ||
HEADERS = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': PROPERTIES['API_KEY'] } | ||
|
||
MILLISECONDS_IN_ONE_DAY = 1000*60*60*24 | ||
TODAY = int(round(time.time() * 1000)) | ||
|
||
for project in get_projects(): | ||
print project | ||
for (jobid,jobname) in get_jobs_for_project(project): | ||
print "\t{0}".format(jobname) | ||
page = 0 | ||
deleteable = () | ||
more = True | ||
while more : | ||
try: | ||
execution_root = get_executions_for_job(jobid,page) | ||
print "\t\tPage {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 "Problem with executions {0} {1}".format(execution_root,sys.exc_info()[0]) | ||
more = False | ||
if (len(deleteable) > 0 ): | ||
print "\t\tDeleting {0} jobs in total".format( len(deleteable) ) | ||
delete_executions(deleteable) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"RUNDECKSERVER": "rundeck server name or IP address", | ||
"PORT": 4440, | ||
"API_KEY": "API Key with correct privileges", | ||
"PAGE_SIZE": 1000, | ||
"MAXIMUM_DAYS": 90, | ||
"TIMEOUT": 60, | ||
"DELETE_TIMEOUT": 1200, | ||
"VERBOSE": false | ||
} |