File ergo.spec of Package ergo

#
# spec file for package ergo
#
# Copyright (c) 2024 SUSE LLC
# Copyright (c) 2022-2024 Georg Pfuetzenreuter <mail+rpm@georg-pfuetzenreuter.net>
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

# `osc service ra && awk '/^go/{print $2}' ergo/go.mod`
%define minimum_gover 1.23

%if 0%{?mageia}
#work around Mageia bug "/usr/lib/rpm/redhat/brp-mangle-shebangs: Permission denied"
%undefine __brp_mangle_shebangs
%endif

%bcond_without systemd

%if 0%{?suse_version}
%bcond_without sysuser
%endif

Name:           ergo
Version:        2.15.0
Release:        0
Summary:        IRCv3 Server
License:        MIT
Group:          Productivity/Networking/IRC
URL:            https://ergo.chat/
Source0:        %{name}-%{version}.tar.gz
Source1:        vendor.tar.gz
Source2:        system-user-ergo.conf
Source3:        apparmor-%{name}
Patch0:         default.yaml.patch
Patch1:         ergo.service.patch
# Common dependencies:
%if !0%{?mageia}
# maybe pkgconfig would work?
BuildRequires:  systemd-rpm-macros
%endif
%if ! %{with sysuser}
Requires(pre):  %{_bindir}/getent
Requires(pre):  %{_sbindir}/useradd
%endif
# Include fdupes where possible:
%if 0%{?suse_version} || 0%{?fedora}
BuildRequires:  fdupes
%endif
# Dependencies for openSUSE and SLES:
%if 0%{?suse_version}
BuildRequires:  go >= %{minimum_gover}
BuildRequires:  golang-packaging
BuildRequires:  apparmor-rpm-macros
%endif
%if %{with sysuser}
BuildRequires:  sysuser-tools 
%sysusers_requires
%endif
# Dependencies shared by Fedora and CentOS:
%if 0%{?fedora} || 0%{?centos_version}
BuildRequires:  go-rpm-macros
BuildRequires:  golang >= %{minimum_gover}
%endif
%if 0%{?fedora} || 0%{?centos_version}
%gopkg
%endif
%if 0%{?mageia}
%gopkg
BuildRequires:  golang
%endif
%if %{with systemd}
BuildRequires:  pkgconfig(systemd)
%systemd_ordering
%endif

%{go_nostrip}

%define commit %(awk '/^commit/{print $2}' %{_sourcedir}/%{name}.obsinfo)

%description
Ergo is a modern IRC server written in Go.

%prep
%setup -q -n %{name}-%{version}
%autopatch -p1
%setup -q -T -D -a 1

%build
go build -mod=vendor -v -p 4 -x -buildmode=pie -ldflags="-w -X main.commit=%{commit} -X main.version="

%if %{with sysuser}
%sysusers_generate_pre %{SOURCE2} %{name} system-user-ergo.conf
%endif

%install
install -v -d -m 0755 %{buildroot}%{_bindir} %{buildroot}%{_sbindir} %{buildroot}%{_datadir}/%{name}/languages
install -v -d -m 0750 %{buildroot}%{_sysconfdir}/%{name} %{buildroot}%{_sysconfdir}/ssl/%{name} %{buildroot}%{_sharedstatedir}/%{name} %{buildroot}%{_unitdir} %{buildroot}%{_sysusersdir}

install -v -m 0755 %{name} %{buildroot}%{_bindir}
install -v -m 0660 %{_builddir}/%{name}-%{version}/default.yaml %{buildroot}%{_sysconfdir}/%{name}/ircd.yaml
%if %{with systemd}
install -v -m 0644 %{_builddir}/%{name}-%{version}/distrib/systemd/%{name}.service %{buildroot}%{_unitdir}/%{name}.service
%endif

%if %{with sysuser}
install -v -m 0644 %{SOURCE2} %{buildroot}%{_sysusersdir}
%endif

%if 0%{?suse_version}
install -v -D -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/apparmor.d/usr.bin.%{name}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}
%endif

find %{_builddir}/%{name}-%{version}/languages -maxdepth 1 -type f \( -name '*.json' -o -name '*.yaml' \) \
    -execdir install -m 0644 '{}' %{buildroot}%{_datadir}/%{name}/languages \;

%if 0%{?suse_version} || 0%{?fedora}
%fdupes %{buildroot}/%{_datadir}
%endif

%check
%if 0%{?suse_version}
%{gotest} github.com/ergochat/ergo
%endif

%if 0%{?fedora} || 0%{?centos_version}
%gocheck
%endif

%files
%if %{with sysuser}
%{_sysusersdir}/system-user-ergo.conf
%endif
%if 0%{?suse_version}
%{_sbindir}/rc%{name}
%dir %{_sysconfdir}/apparmor.d
%config %attr(644,root,root) %{_sysconfdir}/apparmor.d/usr.bin.%{name}
%endif

%license LICENSE
%doc README.md
%attr(755,root,root) %{_bindir}/%{name}
%dir %attr (755,root,ergo) %{_sysconfdir}/%{name}
%dir %{_sysconfdir}/ssl
%dir %attr (750,root,ergo) %{_sysconfdir}/ssl/%{name}
%dir %attr (700,ergo,ergo) %{_sharedstatedir}/%{name}
%attr (-,root,ergo) %config(noreplace) %{_sysconfdir}/%{name}/ircd.yaml
%ghost %attr (640,root,ergo) %{_sysconfdir}/ssl/%{name}/crt
%ghost %attr (640,root,ergo) %{_sysconfdir}/ssl/%{name}/key
%dir %{_datadir}/%{name}
%dir %{_datadir}/%{name}/languages
%{_datadir}/%{name}/languages/*.json
%{_datadir}/%{name}/languages/*.yaml
%if %{with systemd}
%{_unitdir}/%{name}.service
%endif

%if ! %{with sysuser}
%pre
%{_bindir}/getent group ergo > /dev/null || %{_sbindir}/groupadd -r ergo
%{_bindir}/getent passwd ergo > /dev/null || %{_sbindir}/useradd -r -g ergo -d %{_sharedstatedir}/ergo -s /sbin/nologin ergo
%endif
%if %{with sysuser}
%pre -f ergo.pre
%if %{with systemd}
%if 0%{?suse_version}
%service_add_pre ergo.service
%endif
%endif
%endif

%if %{with systemd}
%preun
%if 0%{?suse_version}
%service_del_preun ergo.service
%endif
%if 0%{?fedora} || 0%{?centos_version}
%systemd_preun ergo.service
%endif
%endif

%post
%if %{with systemd}
%if 0%{?suse_version}
%apparmor_reload usr.bin.%{name}
%service_add_post ergo.service
%endif
%if 0%{?fedora} || 0%{?centos_version}
%systemd_post ergo.service
%endif
%endif

ergo_upgradedb() {
  su %{name} -s /bin/sh -c '%{_bindir}/%{name} upgradedb --conf %{_sysconfdir}/%{name}/ircd.yaml'
}

if [ $1 -eq 1 ]
then
    if [ ! -f %{_sysconfdir}/ssl/%{name}/crt -a ! -f %{_sysconfdir}/ssl/%{name}/key ]
    then
        if %{_bindir}/%{name} mkcerts --conf %{_sysconfdir}/%{name}/ircd.yaml --quiet
        then
            chown root:ergo %{_sysconfdir}/ssl/%{name}/crt
            chown root:ergo %{_sysconfdir}/ssl/%{name}/key
            chmod 640 %{_sysconfdir}/ssl/%{name}/key
            echo "Generated a self signed certificate. Please replace %{_sysconfdir}/ssl/%{name}/crt and %{_sysconfdir}/ssl/%{name}/key for production use."
        fi
    fi
fi
if [ -f %{_sharedstatedir}/%{name}/ircd.db ]
then
    if systemctl -q is-active ergo
    then
      if [ $1 -gt 1 ]
      then
        # assume default value for systems without /etc/sysconfig/services
        DISABLE_RESTART_ON_UPDATE=no
        if [ -f /etc/sysconfig/services ]
        then
          . /etc/sysconfig/services
        fi
        if [ "$DISABLE_RESTART_ON_UPDATE" == 'no' ]
        then
          if [ -f %{_sharedstatedir}/%{name}/ircd.db ]
          then
            echo 'Stopping ergo ...'
            systemctl stop ergo
            if systemctl -q is-active ergo
            then
              echo 'WARNING: Failed to stop Ergo, database will not be upgraded! Investigate this manually.'
            else
              ergo_upgradedb
              echo 'Starting ergo ...'
              systemctl start ergo
              if ! systemctl -q is-active ergo
              then
                echo 'WARNING: Failed to start Ergo, you need to investigate this.'
              fi
            fi
          fi
        else
          echo 'NOTE: DISABLE_RESTART_ON_UPDATE is enabled!'
          echo 'If you do not have Ergo configured with "autoupgrade: true", you need to manually perform an offline database upgrade!'
        fi

      fi
    else
      # ergo is not running, an upgradedb call can't hurt
      ergo_upgradedb
    fi
else
    if su %{name} -s /bin/sh -c '%{_bindir}/%{name} initdb --conf %{_sysconfdir}/%{name}/ircd.yaml --quiet'
    then
        chown ergo:ergo %{_sharedstatedir}/%{name}/ircd.db
        chmod 600 %{_sharedstatedir}/%{name}/ircd.db
    fi
fi

if [ $1 -eq 1 ]
then
    echo "Welcome to Ergo!"
    echo "To enable the service (auto-start): \`sudo systemctl enable ergo\`"
    echo "To start the service now: \`sudo systemctl start ergo\`"
    echo "The configuration is located in %{_sysconfdir}/ergo/ircd.yaml"
fi

%if %{with systemd}
%postun
%if 0%{?suse_version}
%service_del_postun ergo.service
%endif
%if 0%{?fedora} || 0%{?centos_version}
%systemd_postun ergo.service
%endif
%endif

%changelog
openSUSE Build Service is sponsored by