Michael Lando | 451a340 | 2017-02-19 10:28:42 +0200 | [diff] [blame^] | 1 | import itertools |
| 2 | import string |
| 3 | import json |
| 4 | from datetime import datetime |
| 5 | from elasticsearch import Elasticsearch |
| 6 | import elasticsearch |
| 7 | import elasticsearch.helpers |
| 8 | from elasticsearch.client import IndicesClient, CatClient |
| 9 | import sys, os, getopt |
| 10 | from file_utils import readFileToJson |
| 11 | from config_properties import getGlobalVar |
| 12 | |
| 13 | |
| 14 | |
| 15 | def createIndex(client, indexName, createBody): |
| 16 | try: |
| 17 | print "start createIndex" |
| 18 | if (client == None): |
| 19 | client = Elasticsearch(['localhost']) |
| 20 | esIndexClient = IndicesClient(client) |
| 21 | res = deleteIndex(client, indexName) |
| 22 | if (res != 0): |
| 23 | print "operation failed" |
| 24 | return 2 |
| 25 | create_res=elasticsearch.client.IndicesClient.create(esIndexClient, index=indexName, body=createBody) |
| 26 | print "create index response: ", create_res |
| 27 | if (create_res['acknowledged'] != True): |
| 28 | print "failed to create index" |
| 29 | return 1 |
| 30 | else: |
| 31 | print "index ",indexName, " created successfully" |
| 32 | return 0 |
| 33 | except Exception, error: |
| 34 | print "An exception was thrown!" |
| 35 | print str(error) |
| 36 | return 2 |
| 37 | |
| 38 | |
| 39 | def deleteIndex(client, indexName): |
| 40 | try: |
| 41 | print "start deleteIndex" |
| 42 | if (client == None): |
| 43 | client = Elasticsearch(['localhost']) |
| 44 | esIndexClient = IndicesClient(client) |
| 45 | isExists=elasticsearch.client.IndicesClient.exists(esIndexClient, indexName) |
| 46 | if ( isExists == True ): |
| 47 | delete_res=elasticsearch.client.IndicesClient.delete(esIndexClient, index=indexName) |
| 48 | if (delete_res['acknowledged'] != True): |
| 49 | print "failed to delete index" |
| 50 | return 1 |
| 51 | else: |
| 52 | print "index ",indexName, " deleted" |
| 53 | return 0 |
| 54 | else: |
| 55 | print "index not found - assume already deleted" |
| 56 | return 0 |
| 57 | except Exception, error: |
| 58 | print "An exception was thrown!" |
| 59 | print str(error) |
| 60 | return 2 |
| 61 | |
| 62 | def copyIndex(client, fromIndex, toIndex): |
| 63 | try: |
| 64 | print "start copyIndex" |
| 65 | if (client == None): |
| 66 | client = Elasticsearch(['localhost']) |
| 67 | client.indices.refresh(index=fromIndex) |
| 68 | count=client.search(fromIndex, search_type='count') |
| 69 | print "original index count: ",count |
| 70 | docNum, docErrors = elasticsearch.helpers.reindex(client, fromIndex, toIndex) |
| 71 | print "copy result: ", docNum, docErrors |
| 72 | if (docNum != count['hits']['total']): |
| 73 | print "Failed to copy all documents. expected: ", count['hits']['total'], " actual: ", docNum |
| 74 | return 1 |
| 75 | # if (len(docErrors) != 0): |
| 76 | # print "copy returned with errors" |
| 77 | # print docErrors |
| 78 | # return 1 |
| 79 | return 0 |
| 80 | except Exception, error: |
| 81 | print "An exception was thrown!" |
| 82 | print str(error) |
| 83 | return 2 |
| 84 | |
| 85 | |
| 86 | def usage(): |
| 87 | print 'USAGE: ', sys.argv[0], '-o <operation : create | delete | move> -n <indexName> -a <address> -f <mappingFile (for create)> -t <toIndex (for move operation)>' |
| 88 | |
| 89 | |
| 90 | |
| 91 | def main(argv): |
| 92 | print "start script with ", len(sys.argv), 'arguments.' |
| 93 | print "==============================================" |
| 94 | |
| 95 | try: |
| 96 | opts, args = getopt.getopt(argv, "h:o:a:n:f:t:", ["operation","address","indexName","file","toIndex"]) |
| 97 | except getopt.GetoptError: |
| 98 | usage() |
| 99 | sys.exit(2) |
| 100 | |
| 101 | host = None |
| 102 | for opt, arg in opts: |
| 103 | print opt, arg |
| 104 | if opt == '-h': |
| 105 | usage() |
| 106 | sys.exit(2) |
| 107 | elif opt in ('-f', '--file'): |
| 108 | mapping=readFileToJson(arg) |
| 109 | elif opt in ('-a', '--address'): |
| 110 | host=arg |
| 111 | elif opt in ('-o', '--operation'): |
| 112 | operation=arg |
| 113 | elif opt in ('-n', '--indexName'): |
| 114 | indexName=arg |
| 115 | elif opt in ('-t', '--toIndex'): |
| 116 | destIndexName=arg |
| 117 | |
| 118 | if (operation == None): |
| 119 | usage() |
| 120 | sys.exit(2) |
| 121 | elif (host == None): |
| 122 | print "address is mandatory argument" |
| 123 | usage() |
| 124 | sys.exit(2) |
| 125 | elif operation == 'create': |
| 126 | print "create new index ", indexName |
| 127 | client = Elasticsearch([{'host': host, 'timeout':5}] ) |
| 128 | res = createIndex(client, indexName, mapping) |
| 129 | |
| 130 | elif operation == 'delete': |
| 131 | print "delete index ", indexName |
| 132 | client = Elasticsearch([{'host': host, 'timeout':5}] ) |
| 133 | res = deleteIndex(client, indexName) |
| 134 | |
| 135 | elif operation == 'move': |
| 136 | print "move index ", indexName, " to ", destIndexName |
| 137 | client = Elasticsearch([{'host': host, 'timeout':5}] ) |
| 138 | res = copyIndex(client, indexName, destIndexName) |
| 139 | else: |
| 140 | usage() |
| 141 | exit(2) |
| 142 | if res != 0: |
| 143 | print "ERROR: operation Failed" |
| 144 | exit(1) |
| 145 | |
| 146 | |
| 147 | |
| 148 | if __name__ == "__main__": |
| 149 | main(sys.argv[1:]) |
| 150 | |
| 151 | |