#
# Cookbook Name:: irrigate-elasticsearch
# Recipe:: default
#
# Copyright (C) 2017 The Orchard
#
# All rights reserved - Do Not Redistribute
#

case node['platform_family']
when 'rhel'
  if node['platform_version'].to_s.start_with?('7')

    # Required packages for Elasticsearch
    packages = %w(
      java-1.8.0-openjdk
      java-1.8.0-openjdk-devel
      unzip
      zip
    )

    packages.each do |name|
      package name
    end

    # System-wide limits, which need to be higher for ES
    set_limit '*' do
      type 'soft'
      item 'nofile'
      value node['elasticsearch']['ulimit']['nofile']['soft']
      use_system true
    end

    set_limit '*' do
      type 'hard'
      item 'nofile'
      value node['elasticsearch']['ulimit']['nofile']['hard']
      use_system true
    end

    elasticsearch_user 'elasticsearch'

    elasticsearch_install 'elasticsearch' do
      version node['elasticsearch']['elasticsearch_version']
    end

    elasticsearch_configure 'elasticsearch' do
      allocated_memory node['elasticsearch']['allocated_memory']
      configuration ({
        'cluster.name' => node['elasticsearch']['cluster_name'],
        'node.name' => node['elasticsearch']['node_name'],
        'http.port' => node['elasticsearch']['http_port'],
      })

      if node['elasticsearch']['elasticsearch_version'].to_s.start_with?('5')
        jvm_options node['elasticsearch']['jvm_options']
      end

      logging(action: node['elasticsearch']['log_level'])
    end

    elasticsearch_service 'elasticsearch'

  end

end
