Merge "prototypes: bifrost: Check we are running the destroy-env.sh as root"
[releng.git] / utils / test / scripts / kibana_cleanup.py
1 #! /usr/bin/env python
2 import logging
3 import argparse
4 import shared_utils
5 import json
6 import urlparse
7
8 logger = logging.getLogger('clear_kibana')
9 logger.setLevel(logging.DEBUG)
10 file_handler = logging.FileHandler('/var/log/{}.log'.format('clear_kibana'))
11 file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
12 logger.addHandler(file_handler)
13
14
15 def delete_all(url, es_creds):
16     ids = shared_utils.get_elastic_data(url, es_creds, body=None, field='_id')
17     for id in ids:
18         del_url = '/'.join([url, id])
19         shared_utils.delete_request(del_url, es_creds)
20
21
22 if __name__ == '__main__':
23     parser = argparse.ArgumentParser(description='Delete saved kibana searches, visualizations and dashboards')
24     parser.add_argument('-e', '--elasticsearch-url', default='http://localhost:9200',
25                         help='the url of elasticsearch, defaults to http://localhost:9200')
26
27     parser.add_argument('-u', '--elasticsearch-username', default=None,
28                         help='The username with password for elasticsearch in format username:password')
29
30     args = parser.parse_args()
31     base_elastic_url = args.elasticsearch_url
32     es_creds = args.elasticsearch_username
33
34     urls = (urlparse.urljoin(base_elastic_url, '/.kibana/visualization'),
35             urlparse.urljoin(base_elastic_url, '/.kibana/dashboard'),
36             urlparse.urljoin(base_elastic_url, '/.kibana/search'))
37
38     for url in urls:
39         delete_all(url, es_creds)
40