blob: 0bc5a71c820e8802505600f118d15c8f7dbc0794 [file] [log] [blame]
Dan Shib8a24d52014-10-21 14:40:02 -07001#!/usr/bin/python
2
Michael Liang6fb58e42014-08-15 11:40:11 -07003# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
Michael Liang6fb58e42014-08-15 11:40:11 -07007
Dan Shib8a24d52014-10-21 14:40:02 -07008"""This script copies all data from one index into another, and updates the
9alias to point to the new index.
Michael Liang6fb58e42014-08-15 11:40:11 -070010
Dan Shib8a24d52014-10-21 14:40:02 -070011usage: es_reindex.py [-h] [--host HOST] [--port PORT] [--old OLD]
12 [--new NEW] [--alias ALIAS]
Michael Liang6fb58e42014-08-15 11:40:11 -070013
14optional arguments:
15 -h, --help show this help message and exit
Dan Shib8a24d52014-10-21 14:40:02 -070016 --host HOST name of ES server.
17 --port PORT
18 --old OLD Name of the old index.
19 --new NEW Name of the new index.
20 --alias ALIAS alias to be pointed to the new index.
Michael Liang6fb58e42014-08-15 11:40:11 -070021
22"""
23
Michael Liang6fb58e42014-08-15 11:40:11 -070024import argparse
25
26import common
Dan Shib8a24d52014-10-21 14:40:02 -070027from elasticsearch import Elasticsearch
28from elasticsearch import helpers
Michael Liang6fb58e42014-08-15 11:40:11 -070029from autotest_lib.client.common_lib.cros.graphite import es_utils
30
31
32def main():
33 """main script. """
34
35 parser = argparse.ArgumentParser()
Dan Shib8a24d52014-10-21 14:40:02 -070036 parser.add_argument('--host', type=str, dest='host',
37 help='name of ES server.')
38 parser.add_argument('--port', type=str, dest='port', default=9200)
39 parser.add_argument('--old', type=str, dest='old',
40 help='Name of the old index.')
41 parser.add_argument('--new', type=str, dest='new',
42 help='Name of the new index.')
43 parser.add_argument('--alias', type=str, dest='alias',
44 help='alias to be pointed to the new index.')
45
Michael Liang6fb58e42014-08-15 11:40:11 -070046 options = parser.parse_args()
Dan Shib8a24d52014-10-21 14:40:02 -070047
48 query = {'query' : {'match_all' : {}},
49 'size': 1}
50
51 result = es_utils.execute_query(query, options.old, options.host,
52 options.port)
53 count = result['hits']['total']
54 print 'Total number of records in index %s: %d' % (options.old, count)
55
56 print ('Re-index: %s to index: %s for server %s:%s' %
57 (options.old, options.new, options.host, options.port))
58
59 client = Elasticsearch(hosts=[{'host': options.host, 'port': options.port}])
60 helpers.reindex(client, options.old, options.new)
61 print 'reindex completed.'
62
63 print 'Checking records in the new index...'
64 result = es_utils.execute_query(query, options.new, options.host,
65 options.port)
66 count_new = result['hits']['total']
67 print 'Total number of records in index %s: %d' % (options.new,
68 count_new)
69
70 # count_new can be larger than count if new records are added during
71 # reindexing. This check only tries to make sure no record was lost.
72 if count > count_new:
73 print ('Error! There are %d records missing after reindexing. Alias '
74 'will not be updated to the new index. You might want to try '
75 'reindex again.' %
76 (count - count_new))
77
78 body = {'actions': [{'remove': {'alias': options.alias,
79 'index': options.old}},
80 {'add': {'alias': options.alias,
81 'index': options.new}}
82 ]
Michael Liang6fb58e42014-08-15 11:40:11 -070083 }
Dan Shib8a24d52014-10-21 14:40:02 -070084 client.indices.update_aliases(body=body)
85 print 'alias is updated.'
86 print ('Please verify the new index is working before deleting old index '
87 'with command:\n.curl -XDELETE %s:%s/%s' %
88 (options.host, options.port, options.old))
Michael Liang6fb58e42014-08-15 11:40:11 -070089
90
91if __name__ == '__main__':
92 main()