Skip to content

Instantly share code, notes, and snippets.

@fengjijiao
Last active May 19, 2022 02:18
Show Gist options
  • Save fengjijiao/dbfd4c8d42d17639fcdaa567d5533d21 to your computer and use it in GitHub Desktop.
Save fengjijiao/dbfd4c8d42d17639fcdaa567d5533d21 to your computer and use it in GitHub Desktop.
simple to install elasticsearch
#!/bin/bash
#elasticsearch 8.2.0
#test only centos7
ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.2.0-linux-x86_64.tar.gz
ES_INSTALL_DIR=/usr/local/elasticsearch
ES_TMP_DIR=/tmp/elasticsearch
REQUIRED_SOFTWARE_LIST=(wget tar sed)
#perl-Digest-SHA
function check_software() {
check_software_bool $1
if [ $? == 1 ]; then
echo "${1} installed"
else
echo "${1} not installed"
echo "start to install ${1}"
yum install $1 -y
fi
}
function check_software_bool() {
RESULT=`whereis $1`
RESULT_LENGTH=`echo -n $RESULT | wc -c`
PACK_LENGTH=`echo -n $1 | wc -c`
PACK_LENGTH=`expr $PACK_LENGTH + 1`
if [ $RESULT_LENGTH -gt $PACK_LENGTH ]; then
return 1
else
return 0
fi
}
function required_software_check() {
yum update -y
for i in "${REQUIRED_SOFTWARE_LIST[@]}"
do
check_software $i
done
}
function check_error() {
if [ $? -ne 0 ];then
echo $1
remove
exit 1
fi
}
function install() {
required_software_check
mkdir $ES_TMP_DIR
mkdir ${ES_INSTALL_DIR}
groupadd elasticsearch
useradd -r -s /bin/false -g elasticsearch elasticsearch
SOURCE_PWD=`pwd`
cd $ES_TMP_DIR
wget -O elasticsearch.tar.gz $ES_DOWNLOAD_URL
check_error "downlaod elasticsearch.tar.gz failed!"
# wget -O elasticsearch.tar.gz.sha512 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz.sha512
# shasum -a 512 -c elasticsearch.tar.gz.sha512
tar -xzf elasticsearch.tar.gz --strip-components 1 -C ${ES_INSTALL_DIR}
check_error "decompression elasticsearch.tar.gz failed!"
chown -R elasticsearch:elasticsearch $ES_INSTALL_DIR
chmod -R 750 $ES_INSTALL_DIR
cat << EOT > /etc/systemd/system/elasticsearch.service
[Unit]
Description=elasticsearch
Wants=network.target
After=syslog.target network-online.target
[Service]
User=elasticsearch
Type=simple
Environment=GOGC=20
ExecStart=${ES_INSTALL_DIR}/bin/elasticsearch
Restart=on-failure
RestartSec=10
KillMode=process
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOT
systemctl enable --now elasticsearch
check_error "start service failed!"
cd $SOURCE_PWD
rm -rf ${ES_TMP_DIR}
result=`${ES_INSTALL_DIR}/bin/elasticsearch-reset-password`
echo $result
clear
echo "install finished!"
echo "use ${ES_INSTALL_DIR}/bin/elasticsearch-reset-password -u elastic to get password!"
}
function remove() {
systemctl stop elasticsearch
systemctl disable elasticsearch
rm -rf /etc/systemd/system/elasticsearch.service
rm -rf $ES_INSTALL_DIR
userdel elasticsearch
groupdel elasticsearch
clear
echo "remove finished!"
}
function help() {
cat << EOF
elasticsearch install helper!!!
usage:
./elasticsearch.sh menu
install: install elasticsearch
remove: remove elasticsearch
EOF
}
function main() {
if [ $# -lt 1 ]; then
help
else
case $1 in
install)
install
;;
remove)
remove
;;
*)
echo 'command not found!'
help
;;
esac
fi
exit 0
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment