#!/bin/bash

###### Information ############################################################################
# Name:          setup_runners_service.sh
# Maintainer:    ODFE Infra Team
# Language:      Shell
#
# About:         Setup ES/KIBANA for integTests on *NIX based ODFE distros w/wo Security
#
# Usage:         ./setup_runners_service.sh $SETUP_ACTION $ARCHITECTURE
#                $SETUP_ACTION: --es | --es-nosec | --kibana | --kibana-nosec (required)
#                $ARCHITECTURE: x64 | arm64 (required)
#
# Requirements:  This script assumes java 14 is already installed on the servers
#
# Starting Date: 2020-07-27
# Modified Date: 2021-01-06
###############################################################################################

# This script allows users to manually assign parameters
if [ "$#" -lt 2 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]
then
  echo "Please assign atleast 2 parameters when running this script"
  echo "Example: $0 \$SETUP_ACTION \$ARCHITECTURE"
  echo "Example: $0 \"--es | --es-nosec | --kibana | --kibana-nosec \" \"x64 | arm64 \""
  exit 1
fi

PLATFORM="macos"
SETUP_ACTION=$1; echo SETUP_ACTION $SETUP_ACTION
ARCHITECTURE=$2; echo ARCHITECTURE $ARCHITECTURE
SETUP_PACKAGES="python3 git unzip wget jq"

echo "install required packages"
brew install $SETUP_PACKAGES

REPO_ROOT=`git rev-parse --show-toplevel`
ROOT=`dirname $(realpath $0)`; cd $ROOT
ES_VERSION=`$REPO_ROOT/release-tools/scripts/version-info.sh --es`; echo ES_VERSION: $ES_VERSION
OD_VERSION=`$REPO_ROOT/release-tools/scripts/version-info.sh --od`; echo OD_VERSION: $OD_VERSION
MANIFEST_FILE=$REPO_ROOT/release-tools/scripts/manifest.yml

ES_PACKAGE_NAME="opendistroforelasticsearch-${OD_VERSION}"
ES_ROOT="${ROOT}/odfe-testing/${ES_PACKAGE_NAME}"
KIBANA_PACKAGE_NAME="opendistroforelasticsearch-kibana"
KIBANA_ROOT="${ROOT}/kibana-testing/${KIBANA_PACKAGE_NAME}"

S3_RELEASE_BASEURL=`yq eval '.urls.ODFE.releases' $MANIFEST_FILE`
S3_RELEASE_FINAL_BUILD=`yq eval '.urls.ODFE.releases_final_build' $MANIFEST_FILE | gsed 's/\///g'`
S3_RELEASE_BUCKET=`echo $S3_RELEASE_BASEURL | awk -F '/' '{print $3}'`
PLUGIN_PATH=`yq eval '.urls.ODFE.releases' $MANIFEST_FILE | gsed "s/^.*$S3_RELEASE_BUCKET\///g"`

#####################################################################################################

echo "############################################################"
echo "Setup ES/KIBANA to start with YES/NO security configurations"
echo "User enters ${SETUP_ACTION}"
echo "############################################################"

echo "setup parameters"
echo $JAVA_HOME
export PATH=$JAVA_HOME:$PATH
which java
java -version
sudo sysctl -w vm.max_map_count=262144
sudo chmod -R 777 /dev/shm

mkdir -p $ES_ROOT
echo "s3://$S3_RELEASE_BUCKET/${PLUGIN_PATH}${OD_VERSION}/odfe/$ES_PACKAGE_NAME-macos-$ARCHITECTURE.tar.gz"
aws s3 cp s3://$S3_RELEASE_BUCKET/${PLUGIN_PATH}${OD_VERSION}/odfe/$ES_PACKAGE_NAME-$PLATFORM-$ARCHITECTURE.tar.gz . --quiet; echo $?
tar -zxf $ES_PACKAGE_NAME-$PLATFORM-$ARCHITECTURE.tar.gz -C $ES_ROOT --strip-components 1


#####################################################################################################

echo "setup es"

# everything needs es
mkdir -p snapshots
echo "path.repo: [\"$PWD/snapshots\"]" >> $ES_ROOT/config/elasticsearch.yml
# Increase the number of allowed script compilations. The SQL integ tests use a lot of scripts.
echo "script.context.field.max_compilations_rate: 1000/1m" >> $ES_ROOT/config/elasticsearch.yml

if [ "$SETUP_ACTION" = "--es" ]
then
  cd $ES_ROOT
  nohup ./opendistro-tar-install.sh > install.log 2>&1 &
  sleep 60
  cat install.log
  kill -9 `ps -ef | grep [e]lasticsearch | awk '{print $2}'`
  gsed -i /^node.max_local_storage_nodes/d ./config/elasticsearch.yml
  nohup ./opendistro-tar-install.sh > install.log 2>&1 &
  sleep 60
  cat install.log
  echo "Sleep 120 seconds"
  sleep 120
  curl -XGET https://localhost:9200 -u admin:admin --insecure
  curl -XGET https://localhost:9200/_cat/plugins?v -u admin:admin --insecure
  curl -XGET https://localhost:9200/_cluster/health?pretty -u admin:admin --insecure
  echo "es start"
  netstat -ntlp
  cd $REPO_ROOT
  exit 0
fi

#####################################################################################################

# *nosec remove es-security
if [ "$SETUP_ACTION" = "--es-nosec" ] || [ "$SETUP_ACTION" = "--kibana-nosec" ]
then
  echo "remove es security"
  gsed -i /install_demo_configuration/d $ES_ROOT/opendistro-tar-install.sh
  $ES_ROOT/bin/elasticsearch-plugin remove opendistro_security
  ls -l $ES_ROOT/plugins
  gsed -i '/http\.port/s/^# *//' $ES_ROOT/config/elasticsearch.yml
fi

if [ "$SETUP_ACTION" = "--es-nosec" ]
then
  cd $ES_ROOT
  nohup ./opendistro-tar-install.sh > install.log 2>&1 &
  sleep 60
  cat install.log
  echo "Sleep 120 seconds"
  sleep 120
  curl -XGET http://localhost:9200
  curl -XGET http://localhost:9200/_cat/plugins?v
  curl -XGET http://localhost:9200/_cluster/health?pretty
  echo "es-nosec start"
  netstat -ntlp
  cd $REPO_ROOT
  exit 0
fi

#####################################################################################################

# kibana* needs kibana
if [ "$SETUP_ACTION" = "--kibana" ] || [ "$SETUP_ACTION" = "--kibana-nosec" ]
then
  echo "setup kibana"
  mkdir -p $KIBANA_ROOT
  echo "s3://$S3_RELEASE_BUCKET/${PLUGIN_PATH}${OD_VERSION}/odfe/$KIBANA_PACKAGE_NAME-$OD_VERSION-macos-$ARCHITECTURE.tar.gz"
  aws s3 cp s3://$S3_RELEASE_BUCKET/${PLUGIN_PATH}${OD_VERSION}/odfe/$KIBANA_PACKAGE_NAME-$OD_VERSION-macos-$ARCHITECTURE.tar.gz . --quiet; echo $?
  tar -zxf $KIBANA_PACKAGE_NAME-$OD_VERSION-macos-$ARCHITECTURE.tar.gz -C $KIBANA_ROOT --strip-components 1
fi

if [ "$SETUP_ACTION" = "--kibana" ]
then
  cd $ES_ROOT
  nohup ./opendistro-tar-install.sh > install.log 2>&1 &
  sleep 60
  cat install.log
  kill -9 `ps -ef | grep [e]lasticsearch | awk '{print $2}'`
  gsed -i /^node.max_local_storage_nodes/d ./config/elasticsearch.yml
  echo "opendistro_security.unsupported.restapi.allow_securityconfig_modification: true" >> ./config/elasticsearch.yml
  nohup ./opendistro-tar-install.sh > install.log 2>&1 &
  sleep 60
  cat install.log
  cd $KIBANA_ROOT
  nohup ./bin/kibana > kibana_install.log 2>&1 &
  sleep 60
  cat kibana_install.log
  echo "Sleep 120 seconds"
  sleep 120
  curl -XGET https://localhost:9200 -u admin:admin --insecure
  curl -XGET https://localhost:9200/_cat/plugins?v -u admin:admin --insecure
  curl -XGET https://localhost:9200/_cluster/health?pretty -u admin:admin --insecure
  # kibana can still use http to check status
  curl -v -XGET http://localhost:5601
  #curl http://localhost:5601/_cat/plugins?v
  curl -v -XGET http://localhost:5601/api/status
  echo "es & kibana start"
  netstat -ntlp
  cd $REPO_ROOT
  exit 0
fi

#####################################################################################################

# kibana-nosec remove kibana-security
if [ "$SETUP_ACTION" = "--kibana-nosec" ]
then
  echo "remove kibana security"
  $KIBANA_ROOT/bin/kibana-plugin remove opendistroSecurityKibana 
  gsed -i /^opendistro_security/d $KIBANA_ROOT/config/kibana.yml
  gsed -i 's/https/http/' $KIBANA_ROOT/config/kibana.yml
  cd $ES_ROOT
  nohup ./opendistro-tar-install.sh > install.log 2>&1 &
  sleep 60
  cat install.log
  cd $KIBANA_ROOT
  nohup ./bin/kibana > kibana_install.log 2>&1 &
  sleep 60
  cat kibana_install.log
  echo "Sleep 120 seconds"
  sleep 120
  curl -XGET http://localhost:9200
  curl -XGET http://localhost:9200/_cat/plugins?v
  curl -XGET http://localhost:9200/_cluster/health?pretty
  curl -v -XGET http://localhost:5601
  curl http://localhost:5601/_cat/plugins?v
  curl -v -XGET http://localhost:5601/api/status
  echo "es & kibana-nosec start"
  netstat -ntlp
  cd $REPO_ROOT
  exit 0
fi


