#!/usr/bin/env bash

set -e


apt-get update
apt-get upgrade -y
apt-get install -y default-jre

# wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add -

# apt-get install -y apt-transport-https default-jre

# echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | tee -a /etc/apt/sources.list.d/elastic-6.x.list

# apt-get update && apt-get install -y elasticsearch

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.2.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.2.deb.sha512
shasum -a 512 -c elasticsearch-6.7.2.deb.sha512 
dpkg -i elasticsearch-6.7.2.deb

sed -i 's/-Xms1g/-Xms6g/g' /etc/elasticsearch/jvm.options
sed -i 's/-Xmx1g/-Xmx6g/g' /etc/elasticsearch/jvm.options
sed -i 's/-Xss1m/-Xss4m/g' /etc/elasticsearch/jvm.options

echo "network.host: 0.0.0.0" >> /etc/elasticsearch/elasticsearch.yml

/bin/systemctl daemon-reload
/bin/systemctl enable elasticsearch.service

systemctl start elasticsearch.service

echo "Sleeping"
sleep 60

curl localhost:9200

ufw allow ssh
ufw allow from 128.197.11.229 to any port 9200

ufw enable

echo "Done."