class Opensearch < Formula desc "Open source distributed and RESTful search engine" homepage "https://github.com/opensearch-project/OpenSearch" url "https://github.com/opensearch-project/OpenSearch/archive/refs/tags/3.5.0.tar.gz" sha256 "b57b3bf4945d6759f31fbc76042c8ffdb5159bd12caf7738865f1b08d93f55c6" license "Apache-2.0" bottle do sha256 cellar: :any_skip_relocation, arm64_tahoe: "86ef68a72cdf2c2195157289cfe84b40c25907a8dac42440455eb7a671ec4ff9" sha256 cellar: :any_skip_relocation, arm64_sequoia: "df1fae55af0a3745c82d5ec3ad43702b30cc13142de0f1537390c83b37ae7460" sha256 cellar: :any_skip_relocation, arm64_sonoma: "d40b37789f2608a23b35850c845849909665c8a2a8f86e5da50dae0200bd9ca9" sha256 cellar: :any_skip_relocation, sonoma: "d2ffadce6eb4d5fbf29d006f5e0fcbcf03f3192a96f2fb3bd78e4a36b9eed9f7" sha256 cellar: :any_skip_relocation, arm64_linux: "d4c7449efd06d179d4b505fb21e6919d32310e0003aae71d56574feaa1671b80" sha256 cellar: :any_skip_relocation, x86_64_linux: "1be1ab2eb8ab7c984570e732f6a9476236c603d7a46950e93053d568d216b12d" end depends_on "gradle@8" => :build depends_on "openjdk" def install platform = OS.kernel_name.downcase platform += "-arm64" if Hardware::CPU.arm? system "gradle", "-Dbuild.snapshot=false", ":distribution:archives:no-jdk-#{platform}-tar:assemble" mkdir "tar" do # Extract the package to the tar directory system "tar", "--strip-components=1", "-xf", Dir["../distribution/archives/no-jdk-#{platform}-tar/build/distributions/opensearch-*.tar.gz"].first # Install into package directory libexec.install "bin", "lib", "modules", "agent" # Set up Opensearch for local development: inreplace "config/opensearch.yml" do |s| # 1. Give the cluster a unique name s.gsub!(/#\s*cluster\.name: .*/, "cluster.name: opensearch_homebrew") # 2. Configure paths s.sub!(%r{#\s*path\.data: /path/to.+$}, "path.data: #{var}/lib/opensearch/") s.sub!(%r{#\s*path\.logs: /path/to.+$}, "path.logs: #{var}/log/opensearch/") end inreplace "config/jvm.options", %r{logs/gc.log}, "#{var}/log/opensearch/gc.log" # add placeholder to avoid removal of empty directory touch "config/jvm.options.d/.keepme" # Move config files into etc (etc/"opensearch").install Dir["config/*"] end inreplace libexec/"bin/opensearch-env", "if [ -z \"$OPENSEARCH_PATH_CONF\" ]; then OPENSEARCH_PATH_CONF=\"$OPENSEARCH_HOME\"/config; fi", "if [ -z \"$OPENSEARCH_PATH_CONF\" ]; then OPENSEARCH_PATH_CONF=\"#{etc}/opensearch\"; fi" bin.install libexec/"bin/opensearch", libexec/"bin/opensearch-keystore", libexec/"bin/opensearch-plugin", libexec/"bin/opensearch-shard" bin.env_script_all_files(libexec/"bin", JAVA_HOME: Formula["openjdk"].opt_prefix) end def post_install # Make sure runtime directories exist (var/"lib/opensearch").mkpath (var/"log/opensearch").mkpath ln_s etc/"opensearch", libexec/"config" unless (libexec/"config").exist? (var/"opensearch/plugins").mkpath ln_s var/"opensearch/plugins", libexec/"plugins" unless (libexec/"plugins").exist? (var/"opensearch/extensions").mkpath ln_s var/"opensearch/extensions", libexec/"extensions" unless (libexec/"extensions").exist? # fix test not being able to create keystore because of sandbox permissions system bin/"opensearch-keystore", "create" unless (etc/"opensearch/opensearch.keystore").exist? end def caveats <<~EOS Data: #{var}/lib/opensearch/ Logs: #{var}/log/opensearch/opensearch_homebrew.log Plugins: #{var}/opensearch/plugins/ Config: #{etc}/opensearch/ EOS end service do run opt_bin/"opensearch" working_dir var log_path var/"log/opensearch.log" error_log_path var/"log/opensearch.log" end test do port = free_port (testpath/"data").mkdir (testpath/"logs").mkdir spawn bin/"opensearch", "-Ehttp.port=#{port}", "-Epath.data=#{testpath}/data", "-Epath.logs=#{testpath}/logs" sleep 60 output = shell_output("curl -s -XGET localhost:#{port}/") assert_equal "opensearch", JSON.parse(output)["version"]["distribution"] system bin/"opensearch-plugin", "list" end end