blob: 8a21a38ccfc8563a28863f7af62ea910c15e9577 [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
Gabe Blackb72f4fb2015-01-20 16:47:13 -080029from autotest_lib.client.common_lib.cros.graphite import autotest_es
Michael Liang6fb58e42014-08-15 11:40:11 -070030
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
Gabe Blackb72f4fb2015-01-20 16:47:13 -080051 result = autotest_es.execute_query(index=options.old, host=options.host,
52 port=options.port, query)
53 print 'Total number of records in index %s: %d' % (options.old,
54 result.total)
Dan Shib8a24d52014-10-21 14:40:02 -070055
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...'
Gabe Blackb72f4fb2015-01-20 16:47:13 -080064 result = es.execute_query(index=options.new, host=options.host,
65 port=options.port, query)
Dan Shib8a24d52014-10-21 14:40:02 -070066 print 'Total number of records in index %s: %d' % (options.new,
Gabe Blackb72f4fb2015-01-20 16:47:13 -080067 result.total)
Dan Shib8a24d52014-10-21 14:40:02 -070068
69 # count_new can be larger than count if new records are added during
70 # reindexing. This check only tries to make sure no record was lost.
71 if count > count_new:
Dan Shibc08d132014-10-27 09:28:55 -070072 raise Exception('Error! There are %d records missing after reindexing. '
73 'Alias will not be updated to the new index. You might '
74 'want to try reindex again.' %
75 (count - count_new))
Dan Shib8a24d52014-10-21 14:40:02 -070076
77 body = {'actions': [{'remove': {'alias': options.alias,
78 'index': options.old}},
79 {'add': {'alias': options.alias,
80 'index': options.new}}
81 ]
Michael Liang6fb58e42014-08-15 11:40:11 -070082 }
Dan Shib8a24d52014-10-21 14:40:02 -070083 client.indices.update_aliases(body=body)
84 print 'alias is updated.'
85 print ('Please verify the new index is working before deleting old index '
86 'with command:\n.curl -XDELETE %s:%s/%s' %
87 (options.host, options.port, options.old))
Michael Liang6fb58e42014-08-15 11:40:11 -070088
89
90if __name__ == '__main__':
91 main()