Skip to content

Instantly share code, notes, and snippets.

@marquesds
Created December 22, 2016 15:59
Show Gist options
  • Save marquesds/59c6250a30056c5fb297d0397a26060f to your computer and use it in GitHub Desktop.
Save marquesds/59c6250a30056c5fb297d0397a26060f to your computer and use it in GitHub Desktop.
class Rundeck():
def __init__(self, api_token, url='http://127.0.0.1', port=4440):
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
self.logger = logging.getLogger(__name__)
self.api_token = api_token
self.url = url
self.port = port
def listProjects(self):
""" API call to get the list of the existing projects on the server. """
url = '{}:{}{}'.format(self.url, self.port, '/api/17/projects')
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': self.api_token}
r = requests.get(url, headers=headers, verify=False)
return r.text.encode('utf-8')
def getProjectNames(self, projectsinfo_xml):
""" Returns list of all the project names """
project_names = []
soup = BeautifulSoup(projectsinfo_xml, 'html.parser')
projects = soup.findAll('project')
for name in projects:
try:
project_names.append(name.find('name').text)
except Exception as e:
self.logger.error(e)
return project_names
def getExecutionsForAProject(self, project_id):
""" API call to get the list of the executions for a Job. """
url = '{}:{}{}{}{}'.format(
self.url, self.port, '/api/17/project/',
project_id, '/executions?max=300000&olderFilter=3m'
)
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': self.api_token}
r = requests.get(url, headers=headers, verify=False)
return r.text.encode('utf-8')
def getExecutionIds(self, executionsinfo_xml):
ids = []
soup = BeautifulSoup(executionsinfo_xml, 'html.parser')
executions = soup.findAll('execution')
for execution in executions:
try:
ids.append(execution.attrs['id'])
except Exception as e:
self.logger.error(e)
return ids
def bulkDeleteExecutions(self, ids):
""" API call to delete a batch of ids """
url = '{}:{}{}'.format(self.url, self.port, '/api/17/executions/delete')
data = {"ids": ids}
headers = {'Content-Type': 'application/json','X-RunDeck-Auth-Token': self.api_token}
return requests.post(url, headers=headers, data=json.dumps(data))
def cleanup(self):
projects = self.getProjectNames(self.listProjects())
for project in projects:
self.logger.info('\tproject:\t' + project)
execid_ids = self.getExecutionIds(self.getExecutionsForAProject(project))
self.logger.info(execid_ids)
self.bulkDeleteExecutions(execid_ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment