class Grafana < Formula desc "Gorgeous metric visualizations and dashboards for timeseries databases" homepage "https://grafana.com" url "https://github.com/grafana/grafana/archive/v8.3.6.tar.gz" sha256 "2d5bd2193898d3bd863141578616f9f4e60f89e47164c620d96758f72c3b4224" license "AGPL-3.0-only" head "https://github.com/grafana/grafana.git", branch: "main" bottle do sha256 cellar: :any_skip_relocation, arm64_monterey: "790c6d3c556e2e542da02eed17378f59047d74041c8fef9719ef593e43d95a96" sha256 cellar: :any_skip_relocation, arm64_big_sur: "246d6fb97661f926b23662661149a0ec28f5015b326c14194314aaa599920072" sha256 cellar: :any_skip_relocation, monterey: "652bf8d51d479e5dfdd52240f0e44593f0acbb964b2bba669e9d7f18182bd66b" sha256 cellar: :any_skip_relocation, big_sur: "80500fd6b5bee6e3a6929e00b93b8afa49bfa6de80114166a36708d60c9e5892" sha256 cellar: :any_skip_relocation, catalina: "18d08300260324885b4084a17a5b640acd4d513f5a05f91cd82bf3c90bcc93b9" sha256 cellar: :any_skip_relocation, x86_64_linux: "e8eeafab5fdd7f66ddc5ffd6eb949c553a23ee6a7676eba9702342d0e7bf2470" end depends_on "go" => :build depends_on "node" => :build depends_on "yarn" => :build uses_from_macos "zlib" on_linux do depends_on "fontconfig" depends_on "freetype" end def install ENV["NODE_OPTIONS"] = "--max-old-space-size=4096" system "make", "gen-go" system "go", "run", "build.go", "build" system "yarn", "install" system "yarn", "build" if OS.mac? bin.install Dir["bin/darwin-*/grafana-cli"] bin.install Dir["bin/darwin-*/grafana-server"] else bin.install "bin/linux-amd64/grafana-cli" bin.install "bin/linux-amd64/grafana-server" end (etc/"grafana").mkpath cp("conf/sample.ini", "conf/grafana.ini.example") etc.install "conf/sample.ini" => "grafana/grafana.ini" etc.install "conf/grafana.ini.example" => "grafana/grafana.ini.example" pkgshare.install "conf", "public", "tools" end def post_install (var/"log/grafana").mkpath (var/"lib/grafana/plugins").mkpath end service do run [opt_bin/"grafana-server", "--config", etc/"grafana/grafana.ini", "--homepath", opt_pkgshare, "--packaging=brew", "cfg:default.paths.logs=#{var}/log/grafana", "cfg:default.paths.data=#{var}/lib/grafana", "cfg:default.paths.plugins=#{var}/lib/grafana/plugins"] keep_alive true error_log_path var/"log/grafana-stderr.log" log_path var/"log/grafana-stdout.log" working_dir var/"lib/grafana" end test do require "pty" require "timeout" # first test system bin/"grafana-server", "-v" # avoid stepping on anything that may be present in this directory tdir = File.join(Dir.pwd, "grafana-test") Dir.mkdir(tdir) logdir = File.join(tdir, "log") datadir = File.join(tdir, "data") plugdir = File.join(tdir, "plugins") [logdir, datadir, plugdir].each do |d| Dir.mkdir(d) end Dir.chdir(pkgshare) res = PTY.spawn(bin/"grafana-server", "cfg:default.paths.logs=#{logdir}", "cfg:default.paths.data=#{datadir}", "cfg:default.paths.plugins=#{plugdir}", "cfg:default.server.http_port=50100") r = res[0] w = res[1] pid = res[2] listening = Timeout.timeout(10) do li = false r.each do |l| if /HTTP Server Listen/.match?(l) li = true break end end li end Process.kill("TERM", pid) w.close r.close listening end end