• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3# 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
7
8"""This script copies all data from one index into another, and updates the
9alias to point to the new index.
10
11usage: es_reindex.py [-h] [--host HOST] [--port PORT] [--old OLD]
12                     [--new NEW] [--alias ALIAS]
13
14optional arguments:
15  -h, --help            show this help message and exit
16  --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.
21
22"""
23
24import argparse
25
26import common
27from elasticsearch import Elasticsearch
28from elasticsearch import helpers
29from autotest_lib.client.common_lib.cros.graphite import autotest_es
30
31
32def main():
33    """main script. """
34
35    parser = argparse.ArgumentParser()
36    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
46    options = parser.parse_args()
47
48    query = {'query' : {'match_all' : {}},
49             'size': 1}
50
51    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)
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.execute_query(index=options.new, host=options.host,
65                              port=options.port, query)
66    print 'Total number of records in index %s: %d' % (options.new,
67                                                       result.total)
68
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:
72        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))
76
77    body = {'actions': [{'remove': {'alias': options.alias,
78                                    'index': options.old}},
79                        {'add': {'alias': options.alias,
80                                 'index': options.new}}
81                        ]
82            }
83    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))
88
89
90if __name__ == '__main__':
91    main()
92