require 'formula' class Elasticsearch < Formula url 'https://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.18.7.tar.gz' homepage 'http://www.elasticsearch.org' md5 'c4de29abf930693b0a4290df3250e128' def cluster_name "elasticsearch_#{ENV['USER']}" end def install # Remove Windows files rm_f Dir["bin/*.bat"] # Move JARs from lib to libexec according to homebrew conventions libexec.install Dir['lib/*.jar'] (libexec+'sigar').install Dir['lib/sigar/*.jar'] # Install everything directly into folder prefix.install Dir['*'] # Set up ElasticSearch for local development: inreplace "#{prefix}/config/elasticsearch.yml" do |s| # 1. Give the cluster a unique name s.gsub! /#\s*cluster\.name\: elasticsearch/, "cluster.name: #{cluster_name}" # 2. Configure paths s.gsub! /#\s*path\.data\: [^\n]+/, "path.data: #{var}/elasticsearch/" s.gsub! /#\s*path\.logs\: [^\n]+/, "path.logs: #{var}/log/elasticsearch/" end inreplace "#{bin}/elasticsearch.in.sh" do |s| # Replace CLASSPATH paths to use libexec instead of lib s.gsub! /ES_HOME\/lib\//, "ES_HOME/libexec/" end inreplace "#{bin}/elasticsearch" do |s| # Set ES_HOME to prefix value s.gsub! /^ES_HOME=.*$/, "ES_HOME=#{prefix}" end inreplace "#{bin}/plugin" do |s| # Set ES_HOME to prefix value s.gsub! /^ES_HOME=.*$/, "ES_HOME=#{prefix}" # Replace CLASSPATH paths to use libexec instead of lib s.gsub! /-cp \".*\"/, '-cp "$ES_HOME/libexec/*"' end # Write .plist file for `launchd` plist_path.write startup_plist plist_path.chmod 0644 end def caveats <<-EOS.undent If this is your first install, automatically load ElasticSearch on login with: mkdir -p ~/Library/LaunchAgents ln -nfs #{plist_path} ~/Library/LaunchAgents/ launchctl load -wF ~/Library/LaunchAgents/#{plist_path.basename} If this is an upgrade and you already have the #{plist_path.basename} loaded: launchctl unload -w ~/Library/LaunchAgents/#{plist_path.basename} ln -nfs #{plist_path} ~/Library/LaunchAgents/ launchctl load -wF ~/Library/LaunchAgents/#{plist_path.basename} To stop the ElasticSearch daemon: launchctl unload -wF ~/Library/LaunchAgents/#{plist_path.basename} To start ElasticSearch manually: elasticsearch -f -D es.config=#{prefix}/config/elasticsearch.yml See the 'elasticsearch.yml' file for configuration options. You'll find the ElasticSearch log here: open #{var}/log/elasticsearch/#{cluster_name}.log The folder with cluster data is here: open #{var}/elasticsearch/#{cluster_name}/ You should see ElasticSearch running: open http://localhost:9200/ EOS end def startup_plist <<-PLIST.undent KeepAlive Label #{plist_name} ProgramArguments #{HOMEBREW_PREFIX}/bin/elasticsearch -f -D es.config=#{prefix}/config/elasticsearch.yml RunAtLoad UserName #{ENV['USER']} WorkingDirectory #{var} StandardErrorPath /dev/null StandardOutPath /dev/null PLIST end end