#!/bin/bash
timedatectl set-timezone Europe/Madrid
start_time=`date`
echo "provisioning started: ${start_time}"
#### The user must fill these variables - START
# Fully Qualified Name: ex. "es-server.localenv.com"
MACHINE_HOSTNAME=
# ElasticSearch version to be installed (format X.Y.Z)"
ELASTICSEARCH_VERSION=
#### The user must fill these variables - END
ELASTICSEARCH_USER=usrv
INSTALLER_ARCHIVE_NAME=elasticsearch-${ELASTICSEARCH_VERSION}.deb
NETWORK_INTERFACE=eth0
hostnamectl set-hostname ${MACHINE_HOSTNAME}
# Print some information about the container OS
hostnamectl
# Print some information about the container timezone
timedatectl
#####################################################################
## Get the IP address into an environment variable. This command outputs
## an empty variable if the network interface name is not ${NETWORK_INTERFACE}
#####################################################################
ip_address=`ip -4 addr show ${NETWORK_INTERFACE} | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
adduser --disabled-password --gecos GECOS ${ELASTICSEARCH_USER}
usermod -p "`openssl passwd -1 -salt 5RPVAd ${ELASTICSEARCH_USER}`" ${ELASTICSEARCH_USER}
adduser ${ELASTICSEARCH_USER} sudo
dpkg --install /vagrant/artifacts/elasticsearch-${ELASTICSEARCH_VERSION}-amd64.deb
cp /vagrant/artifacts/memory.options /etc/elasticsearch/jvm.options.d/memory.options
cp /vagrant/artifacts/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml
source_str="#MACHINE_HOSTNAME#"
target_str="${MACHINE_HOSTNAME}"
sed -i "s/$source_str/$target_str/g" /etc/elasticsearch/elasticsearch.yml
/usr/share/elasticsearch/bin/elasticsearch-plugin install --batch ingest-attachment
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service
apt install -y curl
curl -X GET "localhost:9200/?pretty"
end_time=`date`
echo "This container has IP (interface: ${NETWORK_INTERFACE}): ${ip_address}"
echo "If you add this IP to the hostname (${MACHINE_HOSTNAME}) in your hosts file:"
echo " You can connect with user ${ELASTICSEARCH_USER} via ssh (password ${ELASTICSEARCH_USER}): ssh ${ELASTICSEARCH_USER}@${MACHINE_HOSTNAME}"
echo " Your Elastic Search Server instance is is listening on port 9200: you can validate connectivity with 'curl -X GET "${MACHINE_HOSTNAME}:9200/?pretty"'"
echo "provisioning started: ${start_time}"
echo "provisioning ended: ${end_time}"