homebrew-core/Formula/kafka.rb

110 lines
4.4 KiB
Ruby

class Kafka < Formula
desc "Open-source distributed event streaming platform"
homepage "https://kafka.apache.org/"
url "https://www.apache.org/dyn/closer.lua?path=kafka/3.0.0/kafka_2.13-3.0.0.tgz"
mirror "https://archive.apache.org/dist/kafka/3.0.0/kafka_2.13-3.0.0.tgz"
sha256 "a82728166bbccf406009747a25e1fe52dbcb4d575e4a7a8616429b5818cd02d1"
license "Apache-2.0"
livecheck do
url "https://kafka.apache.org/downloads"
regex(/href=.*?kafka[._-]v?\d+(?:\.\d+)+-(\d+(?:\.\d+)+)\.t/i)
end
bottle do
sha256 cellar: :any_skip_relocation, arm64_monterey: "e216869f2fa4819ba198d34d9a3ee49ce947482568b445c65522f3b7ebe4e7fe"
sha256 cellar: :any_skip_relocation, arm64_big_sur: "188e1850d8be6ca99dbd6b0cef0d9138957a3ff882f80a6cd0c1650cb33201d5"
sha256 cellar: :any_skip_relocation, monterey: "5333488c250dfe056ce86ee0d717d6b0cbbaade9e81f5f5914c27b0fc186b65c"
sha256 cellar: :any_skip_relocation, big_sur: "186f5cb572ee3ab49ad0cbe67b21f14667eb68db025daa6000ee14cd042b6111"
sha256 cellar: :any_skip_relocation, catalina: "186f5cb572ee3ab49ad0cbe67b21f14667eb68db025daa6000ee14cd042b6111"
sha256 cellar: :any_skip_relocation, mojave: "186f5cb572ee3ab49ad0cbe67b21f14667eb68db025daa6000ee14cd042b6111"
sha256 cellar: :any_skip_relocation, x86_64_linux: "9cb29956bab9e7280c3879a2510d12b9f9ce16ce08b7e1258c102b68efa35fac"
end
depends_on "openjdk"
depends_on "zookeeper"
def install
data = var/"lib"
inreplace "config/server.properties",
"log.dirs=/tmp/kafka-logs", "log.dirs=#{data}/kafka-logs"
inreplace "config/zookeeper.properties",
"dataDir=/tmp/zookeeper", "dataDir=#{data}/zookeeper"
# remove Windows scripts
rm_rf "bin/windows"
libexec.install "libs"
prefix.install "bin"
bin.env_script_all_files(libexec/"bin", Language::Java.java_home_env)
Dir["#{bin}/*.sh"].each { |f| mv f, f.to_s.gsub(/.sh$/, "") }
mv "config", "kafka"
etc.install "kafka"
libexec.install_symlink etc/"kafka" => "config"
# create directory for kafka stdout+stderr output logs when run by launchd
(var+"log/kafka").mkpath
end
service do
run [opt_bin/"kafka-server-start", etc/"kafka/server.properties"]
keep_alive true
working_dir HOMEBREW_PREFIX
log_path var/"log/kafka/kafka_output.log"
error_log_path var/"log/kafka/kafka_output.log"
end
test do
ENV["LOG_DIR"] = "#{testpath}/kafkalog"
(testpath/"kafka").mkpath
cp "#{etc}/kafka/zookeeper.properties", testpath/"kafka"
cp "#{etc}/kafka/server.properties", testpath/"kafka"
inreplace "#{testpath}/kafka/zookeeper.properties", "#{var}/lib", testpath
inreplace "#{testpath}/kafka/server.properties", "#{var}/lib", testpath
zk_port = free_port
kafka_port = free_port
inreplace "#{testpath}/kafka/zookeeper.properties", "clientPort=2181", "clientPort=#{zk_port}"
inreplace "#{testpath}/kafka/server.properties" do |s|
s.gsub! "zookeeper.connect=localhost:2181", "zookeeper.connect=localhost:#{zk_port}"
s.gsub! "#listeners=PLAINTEXT://:9092", "listeners=PLAINTEXT://:#{kafka_port}"
end
begin
fork do
exec "#{bin}/zookeeper-server-start #{testpath}/kafka/zookeeper.properties " \
"> #{testpath}/test.zookeeper-server-start.log 2>&1"
end
sleep 15
fork do
exec "#{bin}/kafka-server-start #{testpath}/kafka/server.properties " \
"> #{testpath}/test.kafka-server-start.log 2>&1"
end
sleep 30
system "#{bin}/kafka-topics --bootstrap-server localhost:#{kafka_port} --create --if-not-exists " \
"--replication-factor 1 --partitions 1 --topic test > #{testpath}/kafka/demo.out " \
"2>/dev/null"
pipe_output "#{bin}/kafka-console-producer --bootstrap-server localhost:#{kafka_port} --topic test 2>/dev/null",
"test message"
system "#{bin}/kafka-console-consumer --bootstrap-server localhost:#{kafka_port} --topic test " \
"--from-beginning --max-messages 1 >> #{testpath}/kafka/demo.out 2>/dev/null"
system "#{bin}/kafka-topics --bootstrap-server localhost:#{kafka_port} --delete --topic test " \
">> #{testpath}/kafka/demo.out 2>/dev/null"
ensure
system "#{bin}/kafka-server-stop"
system "#{bin}/zookeeper-server-stop"
sleep 10
end
assert_match(/test message/, File.read("#{testpath}/kafka/demo.out"))
end
end