##
##  jaeger.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_jaeger_base 1.21.0
%define       V_jaeger_snap 20210102

#   package information
Name:         jaeger
Summary:      Event Tracing System
URL:          https://uber.github.io/jaeger/
Vendor:       Uber Technologies
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      MIT
Version:      %{V_jaeger_base}.%{V_jaeger_snap}
Release:      20210102

#   package options
%option       with_agent     yes
%option       with_collector yes
%option       with_query     yes

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/jaeger/jaeger-%{V_jaeger_snap}.tar.xz
Source1:      rc.jaeger
Source2:      jaeger-agent.cfg
Source3:      jaeger-collector.cfg
Source4:      jaeger-query.cfg

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go, npm
PreReq:       OpenPKG, openpkg >= 20160101, elasticsearch

%description
    Jaeger is an OpenTracing-compatible distributed tracing system,
    used for monitoring microservice-based architectures. It provides:
    distributed context propagation, distributed transaction
    monitoring, root cause analysis, service dependency analysis,
    and performance/latency optimization. It stores the data in
    ElasticSearch. Jaeger has three components: an agent service local
    to the application (jaeger-agent), a central collector service for
    storing information (jaeger-collector) and a central query service
    for retrieving information (jaeger-query).

%track
    prog jaeger:release = {
        version   = %{V_jaeger_base}
        url       = https://github.com/jaegertracing/jaeger/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }
    prog jaeger:snapshot = {
        version   = %{V_jaeger_snap}
        url       = http://download.openpkg.org/components/versioned/jaeger/
        regex     = jaeger-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n jaeger

%build
    #   adjust sources
    %{l_shtool} subst \
        -e 's;jaeger-ui-build/build/;%{l_prefix}/share/jaeger/ui/;g' \
        src/github.com/jaegertracing/jaeger/cmd/query/app/static_handler.go \
        src/github.com/jaegertracing/jaeger/cmd/query/app/builder/builder_flags.go

    #   build server program
    (   export GOPATH=`pwd`
        cd src/github.com/jaegertracing/jaeger
        mkdir bin
%if "%{with_query}" == "yes"
        go build -v -o bin/jaeger-query     ./cmd/query/main.go
%endif
%if "%{with_collector}" == "yes"
        go build -v -o bin/jaeger-collector ./cmd/collector/main.go
%endif
%if "%{with_agent}" == "yes"
        go build -v -o bin/jaeger-agent     ./cmd/agent/main.go
%endif
    ) || exit $?

    #   build UI program
%if "%{with_query}" == "yes"
    (   cd src/github.com/jaegertracing/jaeger/jaeger-ui
        HOME=`pwd`
        npm run build
        find packages/jaeger-ui/build -name "*.map" -print | xargs rm -f
    ) || exit $?
%endif

%install
    #   create directory hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jaeger \
%if "%{with_query}" == "yes"
        $RPM_BUILD_ROOT%{l_prefix}/share/jaeger/ui \
%endif
%if "%{with_collector}" == "yes"
        $RPM_BUILD_ROOT%{l_prefix}/share/jaeger/db \
%endif
        $RPM_BUILD_ROOT%{l_prefix}/var/jaeger/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/jaeger/run

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/jaegertracing/jaeger/bin/* \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install UI part
%if "%{with_query}" == "yes"
    cp -rp \
        src/github.com/jaegertracing/jaeger/jaeger-ui/packages/jaeger-ui/build/* \
        $RPM_BUILD_ROOT%{l_prefix}/share/jaeger/ui/
%endif

    #   install DB scripts
%if "%{with_collector}" == "yes"
    %{l_shtool} install -c -m 644 \
        src/github.com/jaegertracing/jaeger/plugin/storage/cassandra/schema/create.sh \
        src/github.com/jaegertracing/jaeger/plugin/storage/cassandra/schema/*.tmpl \
        $RPM_BUILD_ROOT%{l_prefix}/share/jaeger/db/
%endif

    #   install run-command script
%if "%{with_collector}" == "yes"
    daemons="${daemons}${daemons+ }collector"
%endif
%if "%{with_agent}" == "yes"
    daemons="${daemons}${daemons+ }agent"
%endif
%if "%{with_query}" == "yes"
    daemons="${daemons}${daemons+ }query"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@daemons@;$daemons;g" \
        %{SOURCE rc.jaeger} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install default configuration files
%if "%{with_agent}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE jaeger-agent.cfg} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jaeger/
%endif
%if "%{with_collector}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE jaeger-collector.cfg} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jaeger/
%endif
%if "%{with_query}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE jaeger-query.cfg} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jaeger/
%endif

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/jaeger/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/jaeger/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        (
%if "%{with_collector}" == "yes"
          echo "To complete this installation of Jaeger please start ElasticSearch and"
          echo "initialize the Jaeger database like this:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc elasticsearch start"
%endif
          echo "Start Jaeger by running"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc jaeger start"
%if "%{with_query}" == "yes"
          echo "and access the Jaeger UI under:"
          echo "   http://127.0.0.1:FIXME/"
%endif
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} jaeger status 2>/dev/null`
        [ ".$jaeger_active" = .yes ] && %{l_rc} jaeger restart
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    if [ $1 -eq 0 ]; then
        #   stop service
        %{l_rc} jaeger stop 2>/dev/null

        #   remove log files
        rm -f  $RPM_INSTALL_PREFIX/var/jaeger/log/* >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/jaeger/run/* >/dev/null 2>&1 || true
    fi
    exit 0

