#! /bin/bash . /usr/src/m2/framework/various_functions.sh . /usr/src/m2/framework/bash_functions.sh SCRIPT_NAME="ES Install" VERSION="1.4.5" MYSQL_RIVER_VERSION=1.4.0.10 JRE_VERSION=8 MYSQL_CONNECTOR_VERSION=5.1.33 MYSQL_CONNECTOR_VERSION_ROCKY9=8.0.33 ES_VERSION=1.5.2 ES_BASE_DIR=/usr/share/elasticsearch ES_CONF_DIR=/etc/elasticsearch ES_SYSCONF=/etc/sysconfig/elasticsearch ES_SRC_DIR=/usr/src/m2/elasticsearch ES_RESTART=0 k_detect_os fix_systemd_service() { local service_file=/usr/lib/systemd/system/elasticsearch.service if ((centos_version > 6)); then if [[ -e $service_file ]]; then chmod 0644 $service_file # allow to lock memory if grep -q '^#.*LimitMEMLOCK=infinity' $service_file; then sed -i 's%^#.*LimitMEMLOCK=infinity%LimitMEMLOCK=infinity%' $service_file systemctl daemon-reload ES_RESTART=1 fi fi fi } k_start if [ -e $ES_BASE_DIR ]; then echo "Elasticsearch is already installed" # update config file if ! grep -F 'script.groovy.sandbox.enabled: true' $ES_CONF_DIR/elasticsearch.yml &> /dev/null ; then echo "script.groovy.sandbox.enabled: true" >> $ES_CONF_DIR/elasticsearch.yml ES_RESTART=1 fi fix_systemd_service if ((ES_RESTART == 1)); then service elasticsearch restart sleep 10 fi # check if mapping is changed # save current mapping curl -s -XGET 'http://localhost:9200/m2/_mapping/calls?pretty' > /tmp/es_mapping_before # update ES calls mapping curl -s -XPUT 'http://localhost:9200/m2/_mapping/calls?pretty' --data-binary @$ES_SRC_DIR/elasticsearch_mapping &> /dev/null # save mapping after mapping update curl -s -XGET 'http://localhost:9200/m2/_mapping/calls?pretty' > /tmp/es_mapping_after # check if mapping has changed if [ "$(diff /tmp/es_mapping_before /tmp/es_mapping_after | wc -l)" == "0" ]; then echo "Elasticsearch is up to date" echo "If you want to reinstall Elasticsearch, delete the following directories and launch this script again:" echo "rm -fr /usr/share/elasticsearch && rm -fr /etc/elasticsearch && rm -fr /var/lib/elasticsearch" else echo "New Elasticsearch fields added:" echo "$(diff /tmp/es_mapping_before /tmp/es_mapping_after | grep -Po '".*" : {$' | sed 's|\:||' | sed 's| {||')" fi k_exit 0 fi CLUSTERNAME="cluster_$(uuidgen)" NODENAME="m2_server" # Kill resync script, just in case killall -9 es_resync_by_day.sh &> /dev/null # get wget and unzip yum -y install wget unzip glibc.i686 &> /dev/null # download jre 8.x rpm rm -fr /usr/src/jre* k_download_packet jre-$JRE_VERSION.rpm quiet rpm -Uvh /usr/src/jre-$JRE_VERSION.rpm &> /dev/null # delete old elasticsearch installation killall -9 java &> /dev/null rpm -e elasticsearch &> /dev/null &> /dev/null # delete old data rm -fr /var/log/elasticsearch &> /dev/null rm -fr /var/lib/elasticsearch &> /dev/null rm -fr /tmp/elasticsearch &> /dev/null rm -fr /etc/elasticsearch &> /dev/null rm -fr /usr/src/elasticsearch-* # download elasticsearch k_download_packet elasticsearch-$ES_VERSION.noarch.rpm quiet rpm -Uvh /usr/src/elasticsearch-$ES_VERSION.noarch.rpm &> /dev/null fix_systemd_service # install JDBC river k_download_packet elasticsearch-river-jdbc-$MYSQL_RIVER_VERSION.zip quiet $ES_BASE_DIR/bin/plugin --install jdbc --url file:///usr/src/elasticsearch-river-jdbc-$MYSQL_RIVER_VERSION.zip &> /dev/null rm -fr /usr/src/mysql-connector-java-* k_download_packet mysql-connector-java-$MYSQL_CONNECTOR_VERSION.zip quiet unzip /usr/src/mysql-connector-java-$MYSQL_CONNECTOR_VERSION.zip -d /usr/src/ &> /dev/null mkdir -p $ES_BASE_DIR/plugins/jdbc/ cp -fr /usr/src/mysql-connector-java-$MYSQL_CONNECTOR_VERSION/mysql-connector-java-$MYSQL_CONNECTOR_VERSION-bin.jar $ES_BASE_DIR/plugins/jdbc/ if [ "$ROCKY9" == "1" ]; then k_download_packet mysql-connector-j-${MYSQL_CONNECTOR_VERSION_ROCKY9}.jar quiet mkdir -p $ES_BASE_DIR/plugins/jdbc/ cp -fr /usr/src/mysql-connector-j-${MYSQL_CONNECTOR_VERSION_ROCKY9}.jar $ES_BASE_DIR/plugins/jdbc/ fi chmod 644 $ES_BASE_DIR/plugins/jdbc/* # configure elasticsearch sed -i "s|.*cluster\.name:.*|cluster.name: $CLUSTERNAME|" $ES_CONF_DIR/elasticsearch.yml sed -i "s|.*node\.name:.*|node.name: $NODENAME|" $ES_CONF_DIR/elasticsearch.yml sed -i "s|.*bootstrap\.mlockall:.*|bootstrap.mlockall: true|" $ES_CONF_DIR/elasticsearch.yml echo '#network.host: "127.0.0.1"' >> $ES_CONF_DIR/elasticsearch.yml echo "indices.fielddata.cache.size: 60%" >> $ES_CONF_DIR/elasticsearch.yml echo "script.groovy.sandbox.enabled: true" >> $ES_CONF_DIR/elasticsearch.yml cp -f $ES_SRC_DIR/elasticsearch_sysconf $ES_SYSCONF host_name=$(hostname) if [ "$host_name" != "dev.kolmisoft.com" ]; then /usr/src/k_framework/helpers/elasticsearch/es_memory_config.sh &> /dev/null fi # copy to local dir cp -f $ES_SRC_DIR/m2_elasticsearch.sh /usr/local/m2/ &> /dev/null # create symlink k_symlink_create /usr/local/m2/m2_elasticsearch.sh /usr/bin/elasticsearch # run ES on boot k_service_enable elasticsearch &> /dev/null /usr/src/k_framework/maintenance/security/log4j1.x_fix.sh # Copy logging conf cp -f $ES_SRC_DIR/logging.yml /etc/elasticsearch/logging.yml # Configure memory settings /usr/src/k_framework/helpers/elasticsearch/es_memory_config.sh # start ES k_service_start elasticsearch &> /dev/null # create index report "Waiting for Elasticsearch to start" 3 for _ in $(seq 1 60); do echo -n "." sleep 1 curl "http://localhost:9200/?pretty" &> /dev/null if [ "X$?" == "X0" ]; then echo "" report "Elasticsearch started" 0 /usr/bin/elasticsearch index create &> /dev/null sleep 2 curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions" : [ { "add" : { "index" : "mor", "alias" : "m2" } } ] }' &> /dev/null break fi done # Start resync report "Starting Elasticsearch resync" 3 /usr/bin/elasticsearch resync full k_exit "$EXIT_CODE"