Last active
November 5, 2018 15:52
-
-
Save olivierlemoal/94afb7466bca26f1d03e to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python3 | |
"""ElasticPurge | |
Usage: | |
elasticpurge.py -i <index> -b <date> [--check] [-p <port>] [-s <server>] | |
elasticpurge.py -i <index> -o <days> [--check] [-p <port>] [-s <server>] | |
elasticpurge.py -i <index> --list [-p <port>] [-s <server>] | |
elasticpurge.py (-h|--help) | |
Options: | |
-h --help Aide. | |
-i <index>, --index <index> Nom de l'index sans la date (ex: lemonldap). | |
-l, --list Lister les indexes disponibles pour l'index indiqué. | |
-b <date>, --delete-before <date> Supprimer les indexes avant une date (ex : -b 23/12/2014). | |
-o <days>, --delete-older <days> Supprimer les indexes plus vieux que le nombre de jours renseignés. | |
-c, --check Mode simulation, ne supprime pas les indexes. | |
-p <port>, --port <port> Port [9200 par défaut]. | |
-s <server>, --server <server> Serveur ElasticSearch [localhost par défaut]. | |
""" | |
from datetime import datetime, timedelta | |
from docopt import docopt | |
from elasticsearch import Elasticsearch | |
class ElasticPurge: | |
def __init__(self, host, port, simulation): | |
self.es = Elasticsearch([{'host': host, 'port': port}]) | |
self.es.search("*") # Check connection | |
self.simulation = simulation | |
def search(self, index_name): | |
res = self.es.indices.get_alias(index=index_name) | |
return sorted(res.keys()) | |
def delete_before_date(self, index_name, date): | |
indexes = self.search(index_name + "*") | |
for index in indexes: | |
try: | |
date_index = datetime.strptime(index.split("-")[1], '%Y.%m.%d') | |
except: | |
print("L'index \"{}\" n'est pas au bon format de date".format(index)) | |
continue | |
if date_index < date: | |
if not self.simulation: | |
self.es.indices.delete(index) | |
print("{} supprimé".format(index)) | |
else: # indexes are sorted, no need to go further | |
break | |
def delete_older_ndays(self, index_name, days): | |
date = datetime.now() - timedelta(days=int(days)) | |
self.delete_before_date(index_name, date) | |
@staticmethod | |
def conv_date(date): | |
return datetime.strptime(date, '%d/%m/%Y') | |
if __name__ == "__main__": | |
arguments = docopt(__doc__, version='0.1') | |
if arguments["--port"]: | |
port = arguments["--port"] | |
else: | |
port = 9200 | |
if arguments["--server"]: | |
host = arguments["--server"] | |
else: | |
host = "localhost" | |
if arguments["--check"]: | |
print("Mode simulation, rien n'est supprimé.") | |
simulation = True | |
else: | |
simulation = False | |
try: | |
ep = ElasticPurge(host=host, port=port, simulation=simulation) | |
except: | |
print("Connexion impossible à la base ElasticSearch.") | |
exit(1) | |
index = arguments["--index"] | |
if(arguments["--delete-before"]): | |
try: | |
date = ep.conv_date(arguments["--delete-before"]) | |
except: | |
print("Veuillez vérifier le format de la date.") | |
exit(1) | |
ep.delete_before_date(index, date) | |
if(arguments["--delete-older"]): | |
ep.delete_older_ndays(index, arguments["--delete-older"]) | |
if(arguments["--list"]): | |
res = ep.search(index + "*") | |
if not res: | |
print("Aucun index trouvé.") | |
else: | |
print("\n".join(res)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment