|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +##################### function ######################### |
| 4 | +_report_err() { echo "${MYNAME}: Error: $*" >&2 ; } |
| 5 | + |
| 6 | +# if the output is terminal, display infomation in colors. |
| 7 | +if [ -t 1 ] |
| 8 | +then |
| 9 | + RED="$( echo -e "\e[31m" )" |
| 10 | + HL_RED="$( echo -e "\e[31;1m" )" |
| 11 | + HL_BLUE="$( echo -e "\e[34;1m" )" |
| 12 | + |
| 13 | + NORMAL="$( echo -e "\e[0m" )" |
| 14 | +fi |
| 15 | + |
| 16 | +_hl_red() { echo "$HL_RED""$@""$NORMAL";} |
| 17 | +_hl_blue() { echo "$HL_BLUE""$@""$NORMAL";} |
| 18 | + |
| 19 | +_trace() { |
| 20 | + echo $(_hl_blue ' ->') "$@" >&2 |
| 21 | +} |
| 22 | + |
| 23 | +_print_fatal() { |
| 24 | + echo $(_hl_red '==>') "$@" >&2 |
| 25 | +} |
| 26 | + |
| 27 | +function check_mkdir() { |
| 28 | + local dir=$1 |
| 29 | + |
| 30 | + if [ x"$dir" == "x" ]; then |
| 31 | + _trace "dir string is null." |
| 32 | + return 1 |
| 33 | + elif [ -d "${dir}" ]; then |
| 34 | + _trace "dir of ${dir} is existed, skip mkdir." |
| 35 | + return 0 |
| 36 | + else |
| 37 | + _trace "mkdir ${dir}" |
| 38 | + mkdir -p ${dir} |
| 39 | + return $? |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +usage() { |
| 44 | + cat << USAGE |
| 45 | +Usage: bash ${MYNAME} [options]. |
| 46 | +
|
| 47 | +Options: |
| 48 | + -d, --data_dir dir Data dir for elasticsearch. |
| 49 | + -h, --help Print this help infomation. |
| 50 | +
|
| 51 | +USAGE |
| 52 | + |
| 53 | + exit 1 |
| 54 | +} |
| 55 | + |
| 56 | +# |
| 57 | +# Parses command-line options. |
| 58 | +# usage: _parse_options "$@" || exit $? |
| 59 | +# |
| 60 | +function _parse_options() |
| 61 | +{ |
| 62 | + declare -a argv |
| 63 | + |
| 64 | + while [[ $# -gt 0 ]]; do |
| 65 | + case $1 in |
| 66 | + -d|--data_dir) |
| 67 | + g_DATA_DIR="${2}" |
| 68 | + shift 2 |
| 69 | + ;; |
| 70 | + -f|--file) |
| 71 | + g_ELASTICSOFT="${2}" |
| 72 | + shift 2 |
| 73 | + ;; |
| 74 | + -h|--help) |
| 75 | + usage |
| 76 | + exit |
| 77 | + ;; |
| 78 | + --) |
| 79 | + shift |
| 80 | + argv=("${argv[@]}" "${@}") |
| 81 | + break |
| 82 | + ;; |
| 83 | + -*) |
| 84 | + _print_fatal "command line: unrecognized option $1" >&2 |
| 85 | + return 1 |
| 86 | + ;; |
| 87 | + *) |
| 88 | + argv=("${argv[@]}" "${1}") |
| 89 | + shift |
| 90 | + ;; |
| 91 | + esac |
| 92 | + done |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +g_ELASTICSOFT="${g_ELASTICSOFT:-elasticsearch-1.4.4.noarch.rpm}" |
| 97 | + |
| 98 | +_parse_options "${@}" || usage |
| 99 | + |
| 100 | +if command -v "elasticsearch" >/dev/null; then |
| 101 | + _trace "elasticsearch is intalled." |
| 102 | + exit 0 |
| 103 | +fi |
| 104 | + |
| 105 | +if [ -f "$g_ELASTICSOFT" ]; then |
| 106 | + rpm -ivh "$g_ELASTICSOFT" |
| 107 | + |
| 108 | + if [ $? -eq 0 ]; then |
| 109 | + _trace "Install elasticsearch success." |
| 110 | + else |
| 111 | + _print_fatal "Install elasticsearch failed, please check it yourself!" |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | +fi |
| 115 | + |
| 116 | +g_DATA_DIR="${g_DATA_DIR:-/opt/elasticsearch}" |
| 117 | + |
| 118 | +if [ ! -d "${g_DATA_DIR}" ]; then |
| 119 | + mkdir -p ${g_DATA_DIR} |
| 120 | +fi |
| 121 | + |
| 122 | +for dir in data work logs; do |
| 123 | + check_mkdir "${g_DATA_DIR}/${dir}" |
| 124 | +done |
| 125 | + |
| 126 | +_trace "chown of dir ${g_DATA_DIR} to elasticsearch:elasticsearch" |
| 127 | +chown -R elasticsearch:elasticsearch "${g_DATA_DIR}" |
| 128 | + |
| 129 | +g_CONF_FILE="elasticsearch.yml" |
| 130 | +if [ -f "${g_CONF_FILE}" ]; then |
| 131 | + _trace "cp yaml configure file to /etc/elasticsearch" |
| 132 | + cp -rf "${g_CONF_FILE}" /etc/elasticsearch/ |
| 133 | +else |
| 134 | + _trace "yaml configure file ${g_CONF_FILE} is not existed." |
| 135 | +fi |
| 136 | + |
| 137 | +if [ $? -eq 0 ]; then |
| 138 | + sudo /sbin/chkconfig --add elasticsearch |
| 139 | +fi |
| 140 | + |
0 commit comments