########################################
### function: install logstash, this version of es has authentication
### author: T_T
### create date: 2019-11
### modify date: 2019-11
########################################

## Useage ##
if [ -z "$1" ]; then
  echo "Useage : sh $0 clustername {KAFKA_SERVERS ES_SERVERS ES_PASSWORD}"
  echo "Default: sh $0 clustername 127.0.0.1:9092 127.0.0.1:9200 ******"
  exit 1
fi

set +e
## ENV ##
source /etc/profile
set -e
pd=$PWD
CLUSTER_NAME=$1
KAFKA_SERVERS=$2
ES_SERVERS=$3
ES_USER=$4
ES_PASSWORD="$(echo ${5} | sed -e 's/&/\\\\\&/g' -e 's/\//\\\\\//g')"
if [ -z "$ES_PASSWORD" ]; then
  echo "es_password is empty"
  exit 10
fi
environment_type=$6
jvm_mem="1g"
if [ "$environment_type" != "prod" ]; then
  jvm_mem="256m"
fi

logstash_version="logstash-7.17.14"

export COSMIC_CLUSTER_NAME=${COSMIC_CLUSTER_NAME:-ierp}
export MC_CLUSTER_NAME=${MC_CLUSTER_NAME:-mc}
export KAFKA_SERVERS=${KAFKA_SERVERS:-127.0.0.1:9092}
export ES_SERVERS=${ES_SERVERS:-127.0.0.1:9200}
export ES_USER=${ES_USER:-elastic}
export ES_PASSWORD=${ES_PASSWORD}

export CLUSTER_NAME=${CLUSTER_NAME:-${COSMIC_CLUSTER_NAME}}
export COSMIC_KAFKA_GROUP_ID=${COSMIC_KAFKA_GROUP_ID:-$CLUSTER_NAME}
export MC_KAFKA_GROUP_ID=${MC_KAFKA_GROUP_ID:-$MC_CLUSTER_NAME}
export COSMIC_KAFKA_TOPIC=${COSMIC_KAFKA_TOPIC:-"${CLUSTER_NAME}-log"}
export MC_KAFKA_TOPIC=${MC_KAFKA_TOPIC:-"${MC_CLUSTER_NAME}-log"}
export COSMIC_ES_INDEX=${COSMIC_ES_INDEX:-"${CLUSTER_NAME}-log-%{+yyyy-MM-dd}"}
export MC_ES_INDEX=${MC_ES_INDEX:-"${MC_CLUSTER_NAME}-log-%{+yyyy-MM-dd}"}

## install logstash ##
if [ -z "$JAVA_HOME" ]; then
  echo "jdk not be installed,please install jdk first!"
  exit 2
fi
tar -xzf $pd/$logstash_version.tar.gz
## Modify config file ##

## logstash-keystore add es_pwd
if [ -f $pd/$logstash_version/config/logstash.keystore ]; then
  rm -f $pd/$logstash_version/config/logstash.keystore
fi
echo "y" | $pd/$logstash_version/bin/logstash-keystore create

exist_es_pwd=$($pd/$logstash_version/bin/logstash-keystore list | grep "\<es_pwd\>" || true)
if [ -z ${exist_es_pwd} ]; then
  echo "${ES_PASSWORD}" | $pd/$logstash_version/bin/logstash-keystore add es_pwd
else
  $pd/$logstash_version/bin/logstash-keystore remove es_pwd
  echo "${ES_PASSWORD}" | $pd/$logstash_version/bin/logstash-keystore add es_pwd
fi

if [[ ! -f $pd/$logstash_version/config/javalog.conf ]]; then
  cp $pd/javalog.conf $pd/$logstash_version/config/
fi

sed -i "s/bootstrap_servers .*/bootstrap_servers => \"${KAFKA_SERVERS}\"/g" $pd/$logstash_version/config/javalog.conf
sed -i "3,13 {s/group_id .*/group_id => \"${COSMIC_KAFKA_GROUP_ID}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "15,25 {s/group_id .*/group_id => \"${MC_KAFKA_GROUP_ID}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "3,13 {s/topics .*/topics => \"${COSMIC_KAFKA_TOPIC}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "15,25 {s/topics .*/topics => \"${MC_KAFKA_TOPIC}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "s/hosts .*/hosts  => \"${ES_SERVERS}\"/g" $pd/$logstash_version/config/javalog.conf
sed -i "29,37 {s/index .*/index  => \"${COSMIC_ES_INDEX}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "39,47 {s/index .*/index  => \"${MC_ES_INDEX}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "s/user .*/user => \"${ES_USER}\"/g" $pd/$logstash_version/config/javalog.conf
#sed -i "s/password .*/password => \"${ES_PASSWORD}\"/g" $pd/$logstash_version/config/javalog.conf
sed -i "s/password .*/password => \"\${es_pwd}\"/g" $pd/$logstash_version/config/javalog.conf
# 修改jvm.options，调大jvm堆栈内存
sed -E -i "s/^-Xms[0-9]+[kmg]?$/-Xms${jvm_mem}/" $pd/$logstash_version/config/jvm.options
sed -E -i "s/^-Xmx[0-9]+[kmg]?$/-Xmx${jvm_mem}/" $pd/$logstash_version/config/jvm.options

## Add logstash user ##
start_lg_user="logstash"
groupadd -f $start_lg_user
IF_START_USER=$(sed -n '/^'${start_lg_user}':/p' /etc/passwd)
if [ -z "${IF_START_USER}" ]; then
  useradd -g $start_lg_user -M -s /sbin/nologin $start_lg_user
fi
chown -R $start_lg_user. $pd

## register systemd ##
#systemctl stop logstash
cat > /usr/lib/systemd/system/logstash.service << EOF
[Unit]
Description=logstash server (broker)
After=syslog.target network.target 

[Service]
Type=simple
User=$start_lg_user
Group=$start_lg_user
Environment="PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin"
Environment="JAVA_HOME=$JAVA_HOME"
ExecStart=$pd/$logstash_version/bin/logstash "--path.config" "$pd/$logstash_version/config/javalog.conf"
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

## server options ##
systemctl daemon-reload
systemctl restart logstash
systemctl enable logstash

## remove useless file
rm -rf $pd/$logstash_version/vendor/bundle/jruby/2.5.0/gems/redis-3.3.5/test/support/ssl/*
