##
##  cockroach.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 information
Name:         cockroach
Summary:      Scalable, Distributed, Transactional SQL Database
URL:          http://www.cockroachlabs.com/
Vendor:       Spencer Kimball & Peter Mattis
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      20.2.8
Release:      20210424

#   list of sources
Source0:      https://binaries.cockroachdb.com/cockroach-v%{version}.src.tgz
Source1:      rc.cockroach
Source2:      cockroach.sh
Source3:      cockroach-psql.sh
Source4:      cockroach-cert.sh
Source5:      cockroach-haproxy.cfg
Source6:      cockroach-gobetween.toml
Source7:      cockroach-consul.hcl
Source8:      cockroach.conf
Patch0:       cockroach.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go, pkgconfig, cmake, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  lzo, zlib
PreReq:       lzo, zlib

%description
    CockroachDB is a scalable and distributed SQL database built on top
    of the transactional and consistent key/value store RocksDB. The
    primary design goals are support for ACID transactions, horizontal
    scalability, and survivability, hence the name. CockroachDB
    uses the RAFT consensus algorithm for consistency across its
    nodes. CockroachDB aims to tolerate disk, machine, rack, and even
    datacenter failures with minimal latency disruption and no manual
    intervention. CockroachDB nodes are symmetric, i.e., they are all
    equal in their role and authority, as a design goal of CockroachDB
    is homogenous deployment with minimal configuration. CockroachDB
    speaks the PostgreSQL wire protocol and hence can be used with any
    PostgreSQL client. CockroachDB, at the SQL level, also supports many
    PostgreSQL SQL functionalities.

%track
    prog cockroach = {
        version   = %{version}
        url       = https://www.cockroachlabs.com/docs/releases/
        regex     = cockroach-v(\d+\.\d+(?:\.\d+)?)\.src\.tgz
    }

%prep
    %setup -q -n cockroach-v%{version}
    %patch -p0

%build
    #   build program
    go_env_cc=`go env CC`
    platform=`$go_env_cc -dumpmachine`
    native="`pwd`/native/$platform"
    cdeps=`cd src/github.com/cockroachdb/cockroach/c-deps && pwd`
    cppflags="-I$cdeps/crypropp -I$cdeps/jemalloc/include -I$cdeps/protobuf/src -I$cdeps/rocksdb/include -I$cdeps/rocksdb/include/rocksdb -I$cdeps"
    ldflags="-L$native/cryptopp -L$native/jemalloc/src -L$native/protobuf -L$native/rocksdb"
    export HOME=`pwd`
    export GOPATH=`pwd`
    export CGO_CFLAGS="$cppflags %{l_cflags} %{l_cppflags} -Wno-deprecated-declarations"
    export CGO_CXXFLAGS="$cppflags %{l_cxxflags} %{l_cppflags} -Wno-deprecated-declarations"
    export CGO_LDFLAGS="$ldflags %{l_ldflags} -lstdc++ -lm"
    export GOFLAGS="-x"
    export CC="gcc"
    export CXX="g++"
    export CFLAGS="%{l_cflags -O}"
    export CXXFLAGS="%{l_cxxflags -O}"
    export CPPFLAGS="$cppflags %{l_cppflags}"
    export LDFLAGS="$ldflags %{l_ldflags}"
    cd src/github.com/cockroachdb/cockroach
    %{l_make} %{l_mflags} build

    #   build manual pages
    ./cockroach gen man

    #   build bash autocomplete script
    ./cockroach gen autocomplete

%install
    #   create directory hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/cockroach \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/cockroach/certs \
        $RPM_BUILD_ROOT%{l_prefix}/share/cockroach \
        $RPM_BUILD_ROOT%{l_prefix}/var/cockroach/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/cockroach/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/cockroach/db

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/cockroachdb/cockroach/cockroach \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/cockroach/cockroach

    #   install wrapper scripts
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE cockroach.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/cockroach
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE cockroach-psql.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/cockroach-psql
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE cockroach-cert.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/cockroach-cert

    #   install manual pages
    %{l_shtool} install -c -m 644 \
        src/github.com/cockroachdb/cockroach/man/man1/*.1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1/

    #   install server configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE cockroach.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/cockroach/

    #   install bash autocomplete script
    %{l_shtool} install -c -m 644 \
        src/github.com/cockroachdb/cockroach/cockroach.bash \
        $RPM_BUILD_ROOT%{l_prefix}/share/cockroach/

    #   install foreign tool configuration snippets
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE cockroach-haproxy.cfg} \
        %{SOURCE cockroach-gobetween.toml} \
        %{SOURCE cockroach-consul.hcl} \
        $RPM_BUILD_ROOT%{l_prefix}/share/cockroach/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.cockroach} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, create database and certs/keys
        echo "Generating Certificates/Keys" | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/bin/cockroach-cert ca
        $RPM_INSTALL_PREFIX/bin/cockroach-cert server localhost 127.0.0.1
        $RPM_INSTALL_PREFIX/bin/cockroach-cert client root

        #   display some initial hints, too
        ( echo "An initial CockroachDB was configured with the standard"
          echo "certificate/key pairs. You can start CockroachDB and connect"
          echo "to it on \"localhost\" with database user \"root\":"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc cockroach start"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach sql \\%{l_nil}"
          echo "      [--user=root] [--host=127.0.0.1] [--database=system]"
          echo ""
          echo "For production use, you can reconfigure it to listen on external"
          echo "IP address. But for this the server certificate has to be regenerated:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc cockroach stop"
          echo "    \$ vi $RPM_INSTALL_PREFIX/etc/cockroach/cockroach.conf"
          echo "    | host=<ipaddress>"
          echo "    | http-host=<ipaddress>"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach-cert server <hostname> <ipaddress> [...]"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc cockroach start"
          echo "Then you have to connect through the external IP address from now on:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach sql [--host=<ipaddress>]"
          echo ""
          echo "For production use, you usually also want to establish a non-privileged"
          echo "user with a dedicated database:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach-cert client <username>"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach user set <username> --password"
          echo "    | Enter password:   <password>"
          echo "    | Confirm password: <password>"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach sql"
          echo "    | CREATE DATABASE <database>;"
          echo "    | GRANT ALL ON DATABASE <database> TO <username>;"
          echo "    | \\q"
          echo "After this, user <username> can connect with the CockroachDB CLI:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach sql --user=<username> --database=<database>"
          echo "Alternatively, you can also use the more convenient PostgreSQL psql(1) CLI:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach-psql <username> <database>"
          echo ""
          echo "The CockroachDB admin interface you can reach under URL:"
          echo "    https://<ipaddress>:8080/"
          echo ""
          echo "CockroachDB uses the PostgreSQL protocol. The connection string is:"
          echo "    postgresql://<username>@<ipaddress>:26257/<database>?sslmode=require&%{l_nil}"
          echo "    sslcert=$RPM_INSTALL_PREFIX/etc/cockroach/certs/client.<username>.crt&%{l_nil}"
          echo "    sslkey=$RPM_INSTALL_PREFIX/etc/cockroach/certs/client.<username>.key"
          echo ""
          echo "For cluster usage, configure each(!) node with:"
          echo "    \$ vi $RPM_INSTALL_PREFIX/etc/cockroach/cockroach.conf"
          echo "    | advertise-host=<ipaddress>"
          echo "    | join=<otherip1>,<otherip2>"
          echo "Then initialize the cluster on one(!) of the machines with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach init"
          echo "Finally, check the current cluster nodes with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/cockroach node status"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} cockroach status 2>/dev/null`
        [ ".$cockroach_active" = .yes ] && %{l_rc} cockroach restart
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    if [ $1 -eq 0 ]; then
        %{l_rc} cockroach stop 2>/dev/null
        rm -f  $RPM_INSTALL_PREFIX/etc/cockroach/certs/*.crt >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/etc/cockroach/certs/*.key >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/cockroach/log/*       >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/cockroach/run/*       >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/cockroach/db/*        >/dev/null 2>&1 || true
    fi
    exit 0

