#!/bin/bash

mkdir -p /srv/elasticsearch/data /srv/elasticsearch/logs
chown -R elasticsearch:elasticsearch /srv/elasticsearch

if [ ! -f /etc/elasticsearch/wait-for-dns ];then
    sleep 180
    touch /etc/elasticsearch/wait-for-dns
fi

cat << EOF > /etc/elasticsearch/elasticsearch.yml
cluster.name: ${cluster_name}
network.host: _site_
node.name: es-`curl -s 169.254.169.254/latest/meta-data/local-ipv4`
node.attr.zone: es-`curl 169.254.169.254/latest/meta-data/placement/availability-zone`
node.master: ${master_node}
node.data: ${data_node}
node.ingest: ${ingest_node}
cluster.routing.allocation.awareness.attributes: zone
# cluster.routing.allocation.awareness.force.zone.values: ${zones}
discovery.zen.minimum_master_nodes: `expr ${master_node_count} / 2 + ${master_node_count} % 2`
discovery.zen.ping.unicast.hosts: ["${master_fqdn}"]
path.data: /srv/elasticsearch/data
path.logs: /srv/elasticsearch/logs
EOF


if [ x"${master_node}" = xtrue ];then

    cat << EOF > /srv/elasticsearch/global_es_stats_collector
#!/bin/bash

URL="\$1"
GRAPHITE="\$2"
CLUSTER="\`curl -s "\$URL/_cat/health"|awk '{print \$3}'\`"

# Run 6 times per minute
for f in 00 10 20 30 40 50;do
    curl -s -XGET "\$URL/_cat/shards" \\
    | awk -v "cluster=\$CLUSTER" -v now=\`date +%s\` '
        \$3 == "p" {
            ind = gensub(/[.]/, "_", "g", gensub(/-[0-9.]*\$/, "", "g", \$1));
            host = gensub(/[.]/, "_", "g", \$8);
            key = sprintf("%s.%s", host, ind);
            count = \$5;
            size = gensub(/kb\$/, "", "g", \$6);
            counts[key] += count;
            sizes[key] += size;
        }
        END {
            for (k in counts) {
                printf("stats.counters.syslogv2.%s.%s.messages.count %d %d\n", cluster, k, counts[k], now);
                printf("stats.counters.syslogv2.%s.%s.kbytes.count %d %d\n", cluster, k, sizes[k], now);
            }
        }' \\
    | nc "\$GRAPHITE" 2003
    sleep 10
done
EOF
    chmod +x /srv/elasticsearch/global_es_stats_collector

    cat << EOF > /etc/cron.d/global_es_stats_collector
* * * * * nobody /srv/elasticsearch/global_es_stats_collector http://${master_fqdn}:9200 ${graphite_host}
EOF
fi

# Fix up the JVM options
HALFMEM=`grep MemTotal: /proc/meminfo |awk '{print int($2/1024/1024/2)}'`

# -i -e always works but leaves behind a backup file with -e suffix on non-GNU sed systems
sed -i -e '/^-Xmx/s/[0-9]*.$/'"$HALFMEM"'g/' -e '/^-Xms/s/[0-9]*.$/'"$HALFMEM"'g/' /etc/elasticsearch/jvm.options

sysctl -w vm.max_map_count=262144

systemctl stop elasticsearch || true
systemctl start elasticsearch

echo "twitch_environment=${puppet_environment}" > /etc/facter/facts.d/twitch_environment.txt
echo "twitch_role=${puppet_twitch_role}" > /etc/facter/facts.d/twitch_role.txt
echo "cluster=${puppet_cluster}" > /etc/facter/facts.d/cluster.txt
echo "clean=true" > /etc/facter/facts.d/clean.txt
echo "twitch_team=systems" > /etc/facter/facts.d/twitch_team.txt
puppet agent --test --server=puppet.internal.justin.tv
