1#!/bin/bash
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
7USAGE="Usage: ./es_create_alias.sh \
8[-s <server>] [-p <port>] [-i <index>] [-a <alias>]
9Example usage: ./es_create_alias.sh -s 172.25.61.45 -p 9200 -i \
10test_index -a test_index_alias"
11
12HELP="${USAGE}\n\n\
13Create a new alias so that we can refer to an index via an alternate name.\n\
14This is useful so we can remap an index without any downtime. \n\
15
16Options:\n\
17  -s IP of server running elasticsearch\n\
18  -p Port of server running elasticsearch\n\
19  -a A new name that we can refer to the index as \n\
20  -i elasticsearch index, i.e. atlantis4.mtv, cautotest, localhost, etc.\n"
21
22SERVER=
23PORT=
24ALIAS=
25INDEX=
26while getopts ":s:p:a:i:" opt; do
27  case $opt in
28    s)
29      SERVER=$OPTARG
30      ;;
31    p)
32      PORT=$OPTARG
33      ;;
34    a)
35      ALIAS=$OPTARG
36      ;;
37    i)
38      INDEX=$OPTARG
39      ;;
40    \?)
41      echo "Invalid option: -$OPTARG" >&2
42      echo "${USAGE}" >&2
43      exit 1
44      ;;
45    :)
46      echo "Option -$OPTARG requires an argument." >&2
47      echo "${USAGE}" >&2
48      exit 1
49      ;;
50  esac
51done
52
53echo "Creating alias ${ALIAS} for index ${INDEX} for
54      ES server at: ${SERVER}:${PORT}..."
55
56
57curl -XPOST ${SERVER}:${PORT}/_aliases -d '
58{
59    "actions": [
60        { "add": {
61            "alias": '"\"${ALIAS}\""',
62            "index": '"\"${INDEX}\""'
63        }}
64    ]
65}'
66
67echo ''