Skip to content

Instantly share code, notes, and snippets.

@ricardodestro
Last active September 19, 2017 17:48
Show Gist options
  • Save ricardodestro/15e9c95bf54219d86c66485ac374099a to your computer and use it in GitHub Desktop.
Save ricardodestro/15e9c95bf54219d86c66485ac374099a to your computer and use it in GitHub Desktop.
Rundeck Clean Execution History
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovy.json.JsonSlurper
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
/**
* Script to clean Rundeck's Executions History
*
* Change Api Policy permition to allow execution delete
*
*{FILE /etc/rundeck/apitoken.aclpolicy}
*description: API project level access control
*context:
* project: '.*' # all projects
*for:
* resource:
* - equals:
* kind: job
* allow: [create,delete] # allow create and delete jobs
* - equals:
* kind: node
* allow: [read,create,update,refresh] # allow refresh node sources
* - equals:
* kind: event
* allow: [read,create] # allow read/create events
* adhoc:
* - allow: [read,run,kill] # allow running/killing adhoc jobs and read output
* job:
* - allow: [create,read,update,delete,run,kill] # allow create/read/write/delete/run/kill of all jobs
* node:
* - allow: [read,run] # allow read/run for all nodes
*by:
* group: api_token_group
*
*---
*
*description: API Application level access control
*context:
* application: 'rundeck'
*for:
* resource:
* - equals:
* kind: system
* allow: [read] # allow read of system info
* project:
* - match:
* name: '.*'
* allow: [read,delete_execution] # allow view of all projects
* storage:
* - match:
* path: '(keys|keys/.*)'
* allow: '*' # allow all access to manage stored keys
*by:
* group: api_token_group
*{FILE}
*
*
* @author destro
*/
class RundeckHistoryClean {
def clean = { authToken, beforeDays, host, project ->
println 'Starting history clean'
def GET_EXECUTIONS_URL = "http://${host}/api/14/project/${project}/executions?authtoken=${authToken}&olderFilter=${beforeDays}d&format=json&max=1000"
def slurper = new JsonSlurper()
// Execution List
def executionList = slurper.parseText(GET_EXECUTIONS_URL.toURL().text)
executionList.executions.each { execution ->
def id = execution.id
def DELETE_EXECUTION_URL = "http://${host}/api/12/execution/${id}/?authtoken=${authToken}&format=json"
def http = new HTTPBuilder(DELETE_EXECUTION_URL)
http.request( Method.DELETE ) { req ->
response.success = { resp, reader ->
println execution.id + ' DELETED'
}
response.failure = { resp ->
println execution.id + ' ERROR ' + resp.statusLine.statusCode
}
}
}
println 'Done!'
}
static main(args) {
if (args.length != 4 ) {
println 'Usage: <AUTH_TOKEN> <BEFORE_DAYS> <HOST> <PROJECT>'
return
}
println 'Begin'
RundeckHistoryClean main = new RundeckHistoryClean()
def authToken = args[0];
def beforeDays = args[1];
def host = args[2];
def project = args[3];
main.clean( authToken, beforeDays, host, project )
println 'Finished!'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment