File obs-server.spec of Package obs-server

#
# spec file for package obs-server
#
# Copyright (c) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
#
# 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 http://bugs.opensuse.org/
#


%if 0%{?fedora} || 0%{?rhel}
%global apache_user apache
%global apache_group apache
%bcond_without selinux
%else
%global apache_user wwwrun
%global apache_group www
%bcond_with selinux
%endif

%if !0%{?_httpd_confdir:1}
%define _httpd_confdir %{_sysconfdir}/httpd/conf.d
%endif

%define secret_key_file %{_datadir}/obs-api/config/secret.key

%if 0%{?suse_version} >= 1315
%define reload_on_update() %{?nil:
	test -n "$FIRST_ARG" || FIRST_ARG=$1
	if test "$FIRST_ARG" -ge 1 ; then
	   test -f /etc/sysconfig/services && . /etc/sysconfig/services
	   if test "$YAST_IS_RUNNING" != "instsys" -a "$DISABLE_RESTART_ON_UPDATE" != yes ; then
	      test -x /bin/systemctl && /bin/systemctl daemon-reload >/dev/null 2>&1 || :
	      for service in %{?*} ; do
		 test -x /bin/systemctl && /bin/systemctl reload $service >/dev/null 2>&1 || :
	      done
	   fi
	fi
	%nil
}
%endif

# Macros for Suse compatibility.

%if 0%{?fedora} || 0%{?rhel}
%define enable_services() %{?nil:
  for service in %{?*}; do
    /sbin/chkconfig --add $service
  done
  %{nil}
}
%else
%define enable_services() %{nil}
%endif

%if !0%{?stop_on_removal:1}
%if 0%{?fedora} || 0%{?rhel}
%define stop_on_removal() %{?nil:
  if [ "$1" = 0 ]; then
    for service in %{?*}; do
      /sbin/service $service stop >/dev/null 2>&1
      /sbin/chkconfig --del $service
    done
  fi
  %{nil}
}
%else
%define stop_on_removal() %{nil}
%endif
%endif

%if !0%{?restart_on_update:1}
%if 0%{?fedora} || 0%{?rhel}
%define restart_on_update() %{?nil:
  if [ "$1" -ge 1 ]; then
    for service in %{?*}; do
      /sbin/service $service condrestart >/dev/null 2>&1
    done
  fi
  %{nil}
}
%else
%define restart_on_update() %{nil}
%endif
%endif

%if !0%{?reload_on_update:1}
%if 0%{?fedora} || 0%{?rhel}
%define reload_on_update() %{?nil:
  if [ "$1" -ge 1 ]; then
    for service in %{?*}; do
      /sbin/service $service reload >/dev/null 2>&1
    done
  fi
  %{nil}
}
%else
%define restart_on_update() %{nil}
%endif
%endif

%if !0%{?run_permissions:1}
%define run_permissions() %{nil}
%endif

%if !0%{?fillup_and_insserv:1}
%define fillup_and_insserv(n:) %{nil}
%endif

%if !0%{?insserv_cleanup:1}
%define insserv_cleanup() %{nil}
%endif

%if !0%{?verifyscript:1}
%define verifyscript(n:) %{nil}
%endif

%if !0%{?verify_permissions:1}
%define verify_permissions() %{nil}
%endif

%if !0%{?service_add_pre:1}
%define service_add_pre() %{nil}
%endif

%if !0%{?service_add_post:1}
%define service_add_post() %systemd_post %{?*}
%endif

%if !0%{?service_del_preun:1}
%define service_del_preun() %systemd_preun %{?*}
%endif

%if !0%{?service_del_postun:1}
%define service_del_postun() %systemd_postun_with_restart %{?*}
%endif

%if 0%{fedora} < 29
%define patches_to_apply %(a=(%{patches}); echo ${a[@]:0:((${#a[@]}-6))})
%else
%define patches_to_apply %{patches}
%endif

Name:           obs-server
Summary:        The Open Build Service -- Server Component
License:        GPL-2.0 and GPL-3.0
%if 0%{?suse_version} < 1210 && 0%{?suse_version:1}
Group:          Productivity/Networking/Web/Utilities
%endif
Version:        2.9.6
Release:        1
Url:            http://www.openbuildservice.org
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
Source0:        https://github.com/openSUSE/open-build-service/archive/%{version}/open-build-service-%{version}.tar.gz
Source1:        find-requires.sh
Source2:        obs-server-rc-compat
Source3:        crawler-user-agents.json
Patch1:         obs-server-2.9-0001-api-Use-bundler_ext-instead-of-bundler-if-Gemfile.in.patch
Patch2:         obs-server-2.9-0002-api-Require-json-gem-explicitly-for-tests.patch
Patch3:         obs-server-2.9-0003-api-Log-server-name-to-test-log-for-backend-log-mess.patch
Patch4:         obs-server-2.9-0004-api-Use-OBS_API_PREFIX-instead-of-explicit-pathname-.patch
Patch5:         obs-server-2.9-0005-backend-Make-OBS_BACKEND_DATA_DIR-variable-configura.patch
Patch6:         obs-server-2.9-0006-dist-Set-proper-Apache-user-group-when-installing-sc.patch
Patch7:         obs-server-2.9-0007-backend-Set-proper-pathnames-when-installing-backend.patch
Patch8:         obs-server-2.9-0008-dist-Set-proper-pathnames-when-installing-dist-files.patch
Patch9:         obs-server-2.9-0009-dist-Use-configuration-variables-instead-of-hardcode.patch
Patch10:        obs-server-2.9-0010-api-Use-absolute-pathnames-for-apidocs-in-config-env.patch
Patch11:        obs-server-2.9-0011-dist-Make-fillup-file-location-and-name-configurable.patch
Patch12:        obs-server-2.9-0012-dist-Make-obs-devel-doc-location-configutrable.patch
Patch13:        obs-server-2.9-0013-backend-Fix-cleanup-in-BSSched-EventSource-Directory.patch
Patch14:        obs-server-2.9-0014-api-Autodetect-mysqld-and-memcached-pathname-for-api.patch
Patch15:        obs-server-2.9-0015-api-Specify-mysqld-pidfile-explicitly-for-api-test.patch
Patch16:        obs-server-2.9-0016-webui-Wait-for-flash-message-to-appear-in-webui-test.patch
Patch17:        obs-server-2.9-0017-dist-Make-tests-respect-INITDDIR-and-SBINDIR-configu.patch
Patch18:        obs-server-2.9-0018-dist-api-backend-Make-backend-ports-configurable.patch
Patch19:        obs-server-2.9-0019-dist-backend-Make-backend-log-directory-configurable.patch
Patch20:        obs-server-2.9-0020-api-Make-api-log-directory-configurable.patch
Patch21:        obs-server-2.9-0021-dist-Allow-worker-to-use-tmpfs-for-other-virtualisat.patch
Patch22:        obs-server-2.9-0022-ci-Fix-compatibility-with-Ruby-2.3-in-webui-helper-t.patch
Patch23:        obs-server-2.9-0023-Revert-ci-Downgrade-Airbrake-ruby-to-solve-Errbit-bu.patch
Patch24:        obs-server-2.9-0024-frontend-Require-less-specific-version-of-rspec-rail.patch
Patch25:        obs-server-2.9-0025-ci-Nomalise-schema-files-more-before-comparing-them.patch
Patch26:        obs-server-2.9-0026-api-Convert-tables-created-without-default-charset-t.patch
Patch27:        obs-server-2.9-0027-dist-Use-python2-instead-of-python-as-interpreter.patch
Patch28:        obs-server-2.9-0028-frontend-Require-mini_mime-gem-explicitly.patch
Patch29:        obs-server-2.9-0029-frontend-Require-minimal-version-of-sanitize-webmock.patch
Patch30:        obs-server-2.9-0030-backend-Make-chekconstraints-in-repserver-not-fail-o.patch
Patch31:        obs-server-2.9-0031-ci-Make-test-backend-crash-more-prominent.patch
Patch32:        obs-server-2.9-0032-ci-Actually-wait-for-scheduler-when-test-backend-is-.patch
Patch33:        obs-server-2.9-0033-frontend-Drop-halt_callback_chains_on_return_false.patch
Patch34:        obs-server-2.9-0034-Update-data_migrate-to-version-5.2.0.patch
Patch35:        obs-server-2.9-0035-frontend-Rails-5.2-Add-a-primary-key-to-data-migrati.patch
Patch36:        obs-server-2.9-0036-webui-ci-Configure-Capybara-with-webrick.patch
Patch37:        obs-server-2.9-0037-webui-ci-Adapt-tests-to-update-Capybara.patch
Patch38:        obs-server-2.9-0038-ci-Make-feature-test-more-flexible.patch
Patch39:        obs-server-2.9-0039-ci-Add-rspec-retry.patch
Patch40:        obs-server-2.9-0040-backend-Support-new-VM-types-without-modifying-worke.patch
Patch41:        obs-server-2.9-0041-ci-Make-validator-unit-test-more-flexible.patch
Patch42:        obs-server-2.9-0042-frontend-Properly-remove-annotate-gem.patch
Patch43:        obs-server-2.9-0043-api-fix-build-for-mariadb-10.4.patch
Patch44:        obs-server-2.9-0044-frontend-Require-some-dependencies-explicitly.patch
Patch45:        obs-server-2.9-0045-backend-support-newer-gpg-2.1.patch
Patch46:        obs-server-2.9-0046-backend-bs_dodup-support-xz-compressed-Packages-file.patch
Patch47:        obs-server-2.9-0047-backend-createrelease_debian-use-utc-instead-of-loca.patch
Patch101:       obs-server-2.9-0101-frontend-Update-OBS-to-Rails-version-5.2.patch
Patch102:       obs-server-2.9-0102-frontend-Rails-5.2-Add-a-prefix-to-kiwi-preference-e.patch
Patch103:       obs-server-2.9-0103-frontend-Rails-5.2-Fix-checking-if-data-migration-wa.patch
Patch104:       obs-server-2.9-0104-frontend-Rails-5.2-Update-database-rake-task.patch
Patch105:       obs-server-2.9-0105-frontend-Rails-5.2-Update-callback-in-bs-request-mod.patch
Patch106:       obs-server-2.9-0106-Fix-rails-5.2.2-incompability.patch
BuildRequires:  python-devel
# make sure this is in sync with the RAILS_GEM_VERSION specified in the
# config/environment.rb of the various applications.
# atm the obs rails version patch above unifies that setting among the applications
# also see requires in the obs-server-api sub package
BuildRequires:  build >= 20170315
BuildRequires:  inst-source-utils
BuildRequires:  perl(BSSolv) >= 0.07
BuildRequires:  perl-Compress-Zlib
BuildRequires:  perl-Diff-LibXDiff
BuildRequires:  perl-File-Sync >= 0.10
BuildRequires:  perl-JSON-XS
BuildRequires:  perl-Net-SSLeay
BuildRequires:  perl-Socket-MsgHdr
BuildRequires:  perl-TimeDate
BuildRequires:  perl-XML-Parser
BuildRequires:  perl-XML-Simple
BuildRequires:  perl(Digest::MD5)
BuildRequires:  perl(Digest::SHA)
BuildRequires:  perl(Devel::Cover)
BuildRequires:  perl(Test::Simple) > 1
BuildRequires:  perl(Test::Harness)
BuildRequires:  perl(Archive::Tar)
BuildRequires:  procps
# Required by the test suite (contains /usr/bin/Xvfb)
BuildRequires:  /usr/bin/Xvfb
Requires(pre):  %{_sbindir}/useradd %{_sbindir}/groupadd
BuildArch:      noarch
Requires:       obs-common
Requires:       build >= 20170315
Requires:       perl(BSSolv) >= 0.14
Requires:       perl(Date::Parse)
# Required by source server
Requires:       diffutils
Requires(pre):  git-core
Requires:       patch
%if 0%{?suse_version:1}
# require the createrepo and python-yum version which got validated during testsuite run
Requires:       %(/bin/bash -c 'rpm -q --qf "%%{name} = %%{version}-%%{release}" createrepo')
Requires:       %(/bin/bash -c 'rpm -q --qf "%%{name} = %%{version}-%%{release}" python-yum')
%else
Requires:       createrepo >= 0.10.4
%endif

BuildRequires:  xz

%if 0%{?suse_version:1}
BuildRequires:  fdupes
PreReq:         %insserv_prereq permissions pwdutils
%endif

%if 0%{?fedora} || 0%{?rhel}
Requires:       chkconfig
%endif

%if 0%{?suse_version:1}
Recommends:     yum yum-metadata-parser repoview dpkg
Recommends:     deb >= 1.5
Recommends:     lvm2
Recommends:     openslp-server
Recommends:     obs-signd
Recommends:     inst-source-utils
Recommends:     perl-Diff-LibXDiff
%else
Requires:       dpkg
%endif
Requires:       perl-Compress-Zlib
Requires:       perl-File-Sync >= 0.10
Requires:       perl-JSON-XS
Requires:       perl-Net-SSLeay
Requires:       perl-Socket-MsgHdr
Requires:       perl-XML-Parser
Requires:       perl-XML-Simple
Requires:       perl(Digest::MD5)
Requires:       perl(Digest::SHA)
Requires:       perl(Archive::Tar)

%if %{with selinux}
BuildRequires:  selinux-policy-devel
BuildRequires:  checkpolicy
%endif

Obsoletes:     obs-source_service < 2.9
Obsoletes:     obs-productconverter < 2.9
Provides:      obs-source_service = %{version}
Provides:      obs-productconverter = %{version}

Recommends:     obs-service-download_url
Recommends:     obs-service-verify_file

%if 0%{?suse_version} >= 1210
BuildRequires: systemd-rpm-macros
%endif
%if 0%{fedora} || 0%{?rhel}
BuildRequires: systemd-units
%endif

%{?systemd_requires}

%description
The Open Build Service (OBS) backend is used to store all sources and binaries. It also
calculates the need for new build jobs and distributes it.

%package -n obs-worker
Requires:       obs-common
Requires:       cpio
Requires:       curl
Requires:       perl-Compress-Zlib
Requires:       perl-TimeDate
Requires:       perl-XML-Parser
Requires:       screen
# for build script
Requires:       psmisc
# For runlevel script:
Requires:       curl
Recommends:     openslp lvm2
Requires:       bash
Requires:       binutils
Requires:       bsdtar
Summary:        The Open Build Service -- Build Host Component
%if 0%{?suse_version} && 0%{?suse_version} < 1210
Group:          Productivity/Networking/Web/Utilities
%endif
%if 0%{?suse_version}
PreReq:         %insserv_prereq
%endif
%if 0%{?suse_version} <= 1030
Requires:       lzma
%endif
Requires:       util-linux >= 2.16
# the following may not even exist depending on the architecture
Recommends:     powerpc32

%description -n obs-worker
This is the obs build host, to be installed on each machine building
packages in this obs installation.  Install it alongside obs-server to
run a local playground test installation.

%package -n obs-common
Summary:        The Open Build Service -- base configuration files
%if 0%{?suse_version} && 0%{?suse_version} < 1210
Group:          Productivity/Networking/Web/Utilities
%endif
%if 0%{?suse_version}
PreReq:         %fillup_prereq
%endif

%description -n obs-common
This is a package providing basic configuration files.

%package -n obs-api
Summary:        The Open Build Service -- The API and WEBUI
%if 0%{?suse_version} && 0%{?suse_version} < 1210
Group:          Productivity/Networking/Web/Utilities
%endif
%if 0%{?suse_version}
PreReq:         %insserv_prereq
%endif
Requires:       obs-common
%if 0%{?suse_version} >= 1330
Requires(pre):  group(www)
%endif

%if 0%{?fedora} >= 19
Requires: ruby(release)
Requires: ruby(rubygems)
%endif

%if 0%{?suse_version:1}
#For apache
Requires:       apache2 apache2-mod_xforward rubygem-passenger-apache2 ruby2.5-rubygem-passenger
%endif

# memcache is required for session data
Requires:       memcached
Conflicts:      memcached < 1.4

Requires:       mysql

%if %{with selinux}
Requires:       policycoreutils
%endif

Requires:       ruby >= 2.0
# needed for fulltext searching
Requires:       sphinx >= 2.1.8
BuildRequires:  obs-api-testsuite-deps = %{version}
BuildRequires:  rubygem(ruby-ldap)
# For doc generation
BuildRequires:  rubygem(i18n)
# for test suite:
BuildRequires:  createrepo >= 0.10.4
BuildRequires:  curl
BuildRequires:  memcached >= 1.4
BuildRequires:  mysql
BuildRequires:  mysql-server
%if 0%{?suse_version:1}
BuildRequires:  netcfg
BuildRequires:  xorg-x11-Xvnc
BuildRequires:  xorg-x11-server
BuildRequires:  xorg-x11-server-extra
%endif
BuildRequires:  nodejs
# write down dependencies for production
BuildRequires:  rubygem(bundler)
Requires:       %(bash %{S:1} %{S:0} "production" %{patches_to_apply})
# for rebuild_time
Requires:       perl(GD)

%if 0%{?fedora} || 0%{?rhel}
Requires:       chkconfig
%endif

%if 0%{?suse_version:1}
Requires:       ghostscript-fonts-std
%else
Requires:       ghostscript-fonts
%if 0%{?fedora} || 0%{?rhel}
Requires:       urw-fonts
%endif
%endif

Obsoletes:      obs-api-doc

%description -n obs-api
This is the API server instance, and the web client for the
OBS.

%package -n obs-devel
Summary:        The Open Build Service -- The API and WEBUI Testsuite
%if 0%{?suse_version} < 1210 && 0%{?suse_version:1}
Group:          Productivity/Networking/Web/Utilities
%endif
Obsoletes:      obs-webui-testsuite
Requires:       obs-api = %{version}-%{release}
%requires_eq obs-api-testsuite-deps

%description -n obs-devel
Install to track dependencies for git

%package -n obs-utils
Summary:        The Open Build Service -- utilities
%if 0%{?suse_version} < 1210 && 0%{?suse_version:1}
Group:          Productivity/Networking/Web/Utilities
%endif
Requires:       build
Requires:       osc

%description -n obs-utils
obs_project_update is a tool to copy a packages of a project from one obs to another

%package -n obs-tests-appliance

Summary:  The Open Build Service -- Test cases for installed appliances

Requires: obs-server = %{version}
Requires: obs-api = %{version}

%if 0%{?suse_version} < 1210 && 0%{?suse_version:1}
Group:          Productivity/Networking/Web/Utilities
%endif

%description -n obs-tests-appliance
This package contains test cases for testing a installed appliances.
 Test cases can be for example:
 * checks for setup-appliance.sh
 * checks if database setup worked correctly
 * checks if required service came up properly

%package -n obs-cloud-uploader
Summary:       The Open Build Service -- Image Cloud Uploader
Requires:      obs-server
Requires:      aws-cli
%if 0%{?suse_version} > 1315 || 0%{fedora} >= 28
Requires:      python3-ec2uploadimg
%else
Requires:      python-ec2uploadimg
%endif
Group:         Productivity/Networking/Web/Utilities

%description -n obs-cloud-uploader
This package contains all the necessary tools for upload images to the cloud.

%package plugin-notify_hermes
Summary:        The Open Build Service -- notification plugin for Hermes
Group:          Productivity/Networking/Web/Utilities
BuildArch:      noarch
Requires:       %{name}

%description plugin-notify_hermes
Plugin for Open Build Service to send event notifications
through Hermes notification distribution system.

See http://en.opensuse.org/openSUSE:Hermes for details.

%package plugin-notify_rabbitmq
Summary:        The Open Build Service -- notification plugin for RabbitMQ
Group:          Productivity/Networking/Web/Utilities
BuildArch:      noarch
Requires:       %{name}

%description plugin-notify_rabbitmq
Plugin for Open Build Service to send event notifications
through RabbitMQ is open source message broker.

See http://www.rabbitmq.com/ for details.

#--------------------------------------------------------------------------------
%prep

%setup -q -n open-build-service-%version

%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch5 -p1
%patch6 -p1
%patch7 -p1
%patch8 -p1
%patch9 -p1
%patch10 -p1
%patch11 -p1
%patch12 -p1
%patch13 -p1
%patch14 -p1
%patch15 -p1
%patch16 -p1
%patch17 -p1
%patch18 -p1
%patch19 -p1
%patch20 -p1
%patch21 -p1
%patch22 -p1
%patch23 -p1
%patch24 -p1
%patch25 -p1
%patch26 -p1
%patch27 -p1
%patch28 -p1
%patch29 -p1
%patch30 -p1
%patch31 -p1
%patch32 -p1
%patch33 -p1
%patch34 -p1
%patch35 -p1
%patch36 -p1
%patch37 -p1
%patch38 -p1
%patch39 -p1
%patch40 -p1
%patch41 -p1
%patch42 -p1
%patch43 -p1
%patch44 -p1
%patch45 -p1
%patch46 -p1
%patch47 -p1
%if 0%{fedora} >= 29
%patch101 -p1
%patch102 -p1
%patch103 -p1
%patch104 -p1
%patch105 -p1
%patch106 -p1
%endif

# drop build script, we require the installed one from own package
rm -rf src/backend/build
find . -name .git\* \
       -o -name Capfile \
       -o -name deploy.rb \
       -o -name Gemfile.lock \
       | xargs rm -rf

#Run the test suite with current config version
cp %{SOURCE3} src/api/config/

%if 0%{?fedora} || 0%{?rhel}
sed -i \
    -e '1s|^#!/usr/bin/env ruby\.ruby.*|#!/usr/bin/ruby|' \
    -e '1s|^#!/usr/bin/env ruby|#!/usr/bin/ruby|' \
    -e '1s|^#! */usr/bin/ruby\.ruby.*|#!/usr/bin/ruby|' \
    -e '1s|^#!/usr/bin/env rake\.ruby.*|#!/usr/bin/rake|' \
    docs/api/restility/bin/* \
    src/api/Rakefile \
    src/api/bin/* \
    src/api/script/*
sed -i \
    -e 's|ruby\.ruby[0-9.]*|ruby|g' \
    -e 's|rake\.ruby[0-9.]*|rake|g' \
    -e 's|rails\.ruby[0-9.]*|rails|g' \
    -e 's|bundle\.ruby[0-9.]*|bundle|g' \
    -e 's|rspec\.ruby[0-9.]*|rspec|g' \
    -e 's|clockworkd\.ruby[0-9.]*|clockworkd|g' \
    dist/setup-appliance.sh \
    dist/obsapidelayed \
    src/api/db/migrate/*.rb \
    src/api/test/unit/*.rb \
    src/api/script/* \
    src/api/Makefile
sed -i \
    -e 's|/usr/bin/bundle[^ ]* exec ||g' \
    dist/obs_api_delayed_jobs_monitor.cron
%endif

%if %{with selinux}
mkdir src/selinux
pushd src/selinux

cat > obs-api.te <<EOF
policy_module(obs-api, 1.0)

require {
        type var_log_t;
        type passenger_tmp_t;
        type passenger_t;
        type httpd_t;
        class sock_file write;
        class file { ioctl open getattr append };
}

#============= httpd_t ==============
allow httpd_t passenger_tmp_t:sock_file write;

#============= passenger_t ==============
allow passenger_t var_log_t:file { ioctl open getattr append };
corenet_tcp_connect_memcache_port(passenger_t)
corenet_tcp_connect_mysqld_port(passenger_t)
corenet_tcp_connect_unreserved_ports(passenger_t)
dev_manage_sysfs_dirs(passenger_t)
init_rw_stream_sockets(passenger_t)
EOF

cat > obs-api.fc <<EOF
/var/log/obs-api(/.*)? system_u:object_r:passenger_log_t:s0
%{_datadir}/obs-api/tmp(/.*)? system_u:object_r:passenger_tmp_t:s0
/var/lib/obs/repos(/.*)? system_u:object_r:httpd_sys_content_t:s0
EOF

popd
%endif

%build
export DESTDIR=$RPM_BUILD_ROOT
# we need it for the test suite or it may silently succeed
test -x /usr/bin/Xvfb

#
# generate apidocs
#
make

%if %{with selinux}
pushd src/selinux
make -f %{_datadir}/selinux/devel/Makefile
popd
%endif

%install
export DESTDIR=$RPM_BUILD_ROOT

%if 0%{?suse_version} < 1300
  perl -p -i -e 's/^APACHE_VHOST_CONF=.*/APACHE_VHOST_CONF=obs-apache2.conf/' Makefile.include
%endif

sed -i \
    -e 's|^BINDIR=.*|BINDIR=%{_bindir}|' \
    -e 's|^SBINDIR=.*|SBINDIR=%{_sbindir}|' \
    -e 's|^INITDDIR=.*|INITDDIR=%{_initddir}|' \
    -e 's|^OBS_BACKEND_DATA_DIR=.*|OBS_BACKEND_DATA_DIR=%{_sharedstatedir}/obs|' \
    -e 's|^OBS_BACKEND_LOG_DIR=.*|OBS_BACKEND_LOG_DIR=%{_localstatedir}/log/obs|' \
    -e 's|^OBS_DOCUMENT_ROOT=.*|OBS_DOCUMENT_ROOT=%{_datadir}/obs|' \
    -e 's|^OBS_API_PREFIX=.*|OBS_API_PREFIX=%{_datadir}/obs-api|' \
    -e 's|^OBS_API_LOG_DIR=.*|OBS_API_LOG_DIR=%{_localstatedir}/log/obs-api|' \
    -e 's|^OBS_APIDOCS_PREFIX=.*|OBS_APIDOCS_PREFIX=%{_datadir}/obs-api-doc|' \
%if 0%{?fedora} || 0%{?rhel}
    -e 's|^OBS_FILLUP_DIR=.*|OBS_FILLUP_DIR=%{_sysconfdir}/sysconfig|' \
    -e 's|^OBS_FILLUP_FILE=.*|OBS_FILLUP_FILE=obs-server|' \
    -e 's|^OBS_DEVEL_DOC_DIR=.*|OBS_DEVEL_DOC_DIR=%{_docdir}/obs-devel|' \
    -e 's|^OBS_SRCSERVER_PORT=.*|OBS_SRCSERVER_PORT=15352|' \
    -e 's|^OBS_REPOSERVER_PORT=.*|OBS_REPOSERVER_PORT=15252|' \
    -e 's|^OBS_SERVICESERVER_PORT=.*|OBS_SERVICESERVER_PORT=15152|' \
    -e 's|^APACHE_USER=.*|APACHE_USER=apache|' \
    -e 's|^APACHE_GROUP=.*|APACHE_GROUP=apache|' \
    -e 's|^APACHE_CONIFGDIR=.*|APACHE_CONIFGDIR=/etc/httpd|' \
    -e 's|^APACHE_CONIFGDIR_VHOST=.*|APACHE_CONIFGDIR_VHOST=%{_httpd_confdir}|' \
%endif
    Makefile.include

export OBS_VERSION="%{version}"
DESTDIR=%{buildroot} RAILS_RELATIVE_URL_ROOT=../.. make install

# Clean messages left over from installation
%{__cp} /dev/null %{buildroot}%{_localstatedir}/log/obs-api/production.log

%if 0%{?fedora} || 0%{?rhel}
%{__mv} %{buildroot}%{_httpd_confdir}/obs.conf %{buildroot}%{_httpd_confdir}/obs.conf.dist
%endif

#
# turn duplicates into hard links
#
# There's dupes between webui and api:
%if 0%{?suse_version} >= 1030
%fdupes $RPM_BUILD_ROOT%{_datadir}/obs-api $RPM_BUILD_ROOT%{_datadir}/obs-api-doc
%endif

# fix build for SLE 11
%if 0%{?suse_version} < 1315
touch %{buildroot}/%{secret_key_file}
chmod 0640 %{buildroot}/%{secret_key_file}
%endif

# To get the most current user agent list we need i-net access. Thus we fetch
# the file via an OBS service and install it here.
install -m 0644 %{SOURCE3} %{buildroot}%{_datadir}/obs-api/config

# drop testcases for now
rm -rf %{buildroot}%{_datadir}/obs-api/spec

# fail when Makefiles created a directory
if ! test -L %{buildroot}/usr/lib/obs/server/build; then
  echo "/usr/lib/obs/server/build is not a symlink!"
  exit 1
fi

install -m 755 $RPM_BUILD_DIR/open-build-service-%version/dist/clouduploader.rb $RPM_BUILD_ROOT/%{_bindir}/clouduploader
mkdir -p $RPM_BUILD_ROOT/etc/obs/cloudupload
install -m 644 $RPM_BUILD_DIR/open-build-service-%version/dist/ec2utils.conf.example $RPM_BUILD_ROOT/etc/obs/cloudupload/.ec2utils.conf
mkdir -p $RPM_BUILD_ROOT/etc/obs/cloudupload/.aws
install -m 644 $RPM_BUILD_DIR/open-build-service-%version/dist/aws_credentials.example $RPM_BUILD_ROOT/etc/obs/cloudupload/.aws/credentials

rm %{buildroot}%{_datadir}/obs-api/Gemfile.lock
mv %{buildroot}%{_datadir}/obs-api/Gemfile %{buildroot}%{_datadir}/obs-api/Gemfile.in
sed -i \
    -e "s/'activerecord'/'active_record'/" \
    -e "s/'actionmailer'/'action_mailer'/" \
    -e "s/'yajl-ruby'/'yajl'/" \
    %{buildroot}%{_datadir}/obs-api/Gemfile.in
rm -rf %{buildroot}%{_datadir}/obs-api/.bundle

%if %{with selinux}
%{__mkdir_p} %{buildroot}%{_datadir}/selinux/packages/obs-api
%{__install} -m644 src/selinux/obs-api.pp \
    %{buildroot}%{_datadir}/selinux/packages/obs-api/obs-api.pp
%endif

%if !0%{?suse_version:1}
install -d -m 755 $RPM_BUILD_ROOT%{_datadir}/obs
install -m 0755 %{SOURCE2} $RPM_BUILD_ROOT%{_datadir}/obs/rc.compat
sed -i \
    -e 's|\. /etc/rc.status|. %{_datadir}/obs/rc.compat|' \
    $RPM_BUILD_ROOT%{_initddir}/*
%endif

sed -i \
    -e 's|/usr/bin/bundle[^ ]* exec ||' \
    $RPM_BUILD_ROOT%{_initddir}/obsapidelayed

%check
%if 0%{?disable_obs_test_suite}
echo "WARNING:"
echo "WARNING: OBS test suite got skipped!"
echo "WARNING:"
exit 0
%endif

export TZ=GMT
export LANG=C.UTF-8
export DESTDIR=$RPM_BUILD_ROOT
# check installed backend
pushd $RPM_BUILD_ROOT/usr/lib/obs/server/
rm -rf build
ln -sf /usr/lib/build build # just for %%check, it is a %%ghost
popd

# run in build environment
pushd src/backend/
rm -rf build
ln -sf /usr/lib/build build
popd

####
# start backend testing
pushd $RPM_BUILD_ROOT/usr/lib/obs/server/
%if 0%{?disable_obs_backend_test_suite:1} < 1
# TODO: move syntax check to backend test suite
for i in bs_*; do
  perl -wc "$i"
done
bash $RPM_BUILD_DIR/open-build-service-%version/src/backend/testdata/test_dispatcher || exit 1
popd

make -C src/backend test
%endif

####
# start api testing
#
%if 0%{?disable_obs_frontend_test_suite:1} < 1
make -C src/api test
%endif

####
# distribution tests
%if 0%{?disable_obs_dist_test_suite:1} < 1
make -C dist test
%endif

%pre
getent passwd obsservicerun >/dev/null || \
    /usr/sbin/useradd -r -g obsrun -d /usr/lib/obs -s %{sbin}/nologin \
    -c "User for the build service source service" obsservicerun

%service_add_pre obsdeltastore

exit 0

%pre -n obs-common
getent group obsrun >/dev/null || %{_sbindir}/groupadd -r obsrun
getent passwd obsrun >/dev/null || \
    %{_sbindir}/useradd -r -g obsrun -d /usr/lib/obs -s %{_sbindir}/nologin \
    -c "User for build service backend" obsrun
exit 0

%preun
%stop_on_removal obssrcserver obsrepserver obsdispatcher obsscheduler obspublisher obswarden obssigner obsdodup obsservicedispatch obsservice

%service_del_preun obsdeltastore

%preun -n obs-worker
%stop_on_removal obsworker

%preun -n obs-cloud-uploader
%stop_on_removal obsclouduploadworker obsclouduploadserver

%post
%enable_services obssrcserver obsrepserver obsdispatcher obsscheduler obspublisher obswarden obssigner obsdodup obsdeltastore
%if 0%{?suse_version} >= 1315
%reload_on_update obssrcserver obsrepserver obsdispatcher obspublisher obswarden obssigner obsdodup obsservicedispatch obsservice
%else
%restart_on_update obssrcserver obsrepserver obsdispatcher obspublisher obswarden obssigner obsdodup obsservicedispatch obsservice
%endif
# systemd kills the init script executing the reload first on reload....
%restart_on_update obsscheduler
%service_add_post obsdeltastore

%post -n obs-cloud-uploader
%enable_services obsservice
%if 0%{?suse_version} >= 1315
%reload_on_update obsservice obsclouduploadworker obsclouduploadserver
%else
%restart_on_update obsservice obsclouduploadworker obsclouduploadserver
%endif

%posttrans
# this changes from directory to symlink. rpm can not handle this itself.
if [ -e /usr/lib/obs/server/build -a ! -L /usr/lib/obs/server/build ]; then
  rm -rf /usr/lib/obs/server/build
fi
if [ ! -e /usr/lib/obs/server/build ]; then
  ln -sf ../../build /usr/lib/obs/server/build
fi

%postun
%insserv_cleanup
%service_del_postun obsdeltastore
# cleanup empty directory just in case
rmdir %{_sharedstatedir}/obs 2> /dev/null || :

%verifyscript -n obs-server
%verify_permissions

%post -n obs-worker
%enable_services obsworker
# NOT used on purpose: restart_on_update obsworker
# This can cause problems when building chroot
# and bs_worker is anyway updating itself at runtime based on server code

%pre -n obs-api
getent passwd obsapidelayed >/dev/null || \
  %{_sbindir}/useradd -r -s /bin/bash -c "User for build service api delayed jobs" \
                      -d %{_datadir}/obs-api -g %{apache_group} obsapidelayed

%post -n obs-common
%enable_services obsstoragesetup
%{fillup_and_insserv -n obs-server}

%post -n obs-api
if [ ! -e %{secret_key_file} ]; then
  pushd .
  cd %{_datadir}/obs-api/config
  ( umask 0077; cd %{_datadir}/obs-api && RAILS_ENV=production rake secret > %{secret_key_file} ) || exit 1
  popd
fi
chmod 0640 %{secret_key_file}
chown root.%{apache_group} %{secret_key_file}

# update config
sed -i -e 's,[ ]*adapter: mysql$,  adapter: mysql2,' %{_datadir}/obs-api/config/database.yml
touch %{_localstatedir}/log/obs-api/production.log
chown %{apache_user}:%{apache_group} %{_localstatedir}/log/obs-api/production.log

%if %{with selinux}
semodule -i %{_datadir}/selinux/packages/obs-api/obs-api.pp
restorecon -R %{_localstatedir}/log/obs-api
%endif

%restart_on_update apache2
%restart_on_update memcached
%restart_on_update obsapisetup
%restart_on_update obsapidelayed

su %{apache_user} -s /bin/sh -c 'cd %{_datadir}/obs-api && RAILS_ENV=production rake db:migrate:with_data'

%postun -n obs-api
%insserv_cleanup
%if %{with selinux}
test "$1" -eq 0 && semodule -r obs-api >/dev/null 2>&1 || :
%endif

%files
%defattr(-,root,root)
%doc dist/{README.UPDATERS,README.SETUP} docs/openSUSE.org.xml ReleaseNotes-* README.md COPYING AUTHORS
%dir /etc/slp.reg.d
%dir /usr/lib/obs
%dir /usr/lib/obs/server
%config(noreplace) /etc/logrotate.d/obs-server
%{_initddir}/obsdispatcher
%{_initddir}/obspublisher
%{_initddir}/obsrepserver
%{_initddir}/obsscheduler
%{_initddir}/obssrcserver
%{_initddir}/obswarden
%{_initddir}/obsdodup
%{_unitdir}/obsdeltastore.service
%{_initddir}/obsservicedispatch
%{_initddir}/obssigner
%{_sbindir}/obs_admin
%{_sbindir}/obs_serverstatus
%{_sbindir}/rcobsdispatcher
%{_sbindir}/rcobspublisher
%{_sbindir}/rcobsrepserver
%{_sbindir}/rcobsscheduler
%{_sbindir}/rcobssrcserver
%{_sbindir}/rcobswarden
%{_sbindir}/rcobsdodup
%{_sbindir}/rcobsdeltastore
%{_sbindir}/rcobsservicedispatch
%{_sbindir}/rcobssigner
%dir /usr/lib/obs/server/plugins
/usr/lib/obs/server/BSDispatcher
/usr/lib/obs/server/BSRepServer
/usr/lib/obs/server/BSSched
/usr/lib/obs/server/BSSrcServer
/usr/lib/obs/server/XML
/usr/lib/obs/server/*.pm
/usr/lib/obs/server/BSConfig.pm.template
/usr/lib/obs/server/DESIGN
/usr/lib/obs/server/License
/usr/lib/obs/server/README
/usr/lib/obs/server/bs_admin
/usr/lib/obs/server/bs_cleanup
/usr/lib/obs/server/bs_archivereq
/usr/lib/obs/server/bs_check_consistency
/usr/lib/obs/server/bs_deltastore
/usr/lib/obs/server/bs_servicedispatch
/usr/lib/obs/server/bs_dodup
/usr/lib/obs/server/bs_getbinariesproxy
/usr/lib/obs/server/bs_mergechanges
/usr/lib/obs/server/bs_mkarchrepo
/usr/lib/obs/server/bs_notar
/usr/lib/obs/server/bs_dispatch
/usr/lib/obs/server/bs_publish
/usr/lib/obs/server/bs_repserver
/usr/lib/obs/server/bs_sched
/usr/lib/obs/server/bs_serverstatus
/usr/lib/obs/server/bs_srcserver
/usr/lib/obs/server/bs_worker
/usr/lib/obs/server/bs_signer
/usr/lib/obs/server/bs_warden
/usr/lib/obs/server/worker
/usr/lib/obs/server/worker-deltagen.spec
%config(noreplace) /usr/lib/obs/server/BSConfig.pm
%config(noreplace) /etc/slp.reg.d/*
%dir %attr(-,obsrun,obsrun) %{_sharedstatedir}/obs
%dir %attr(-,obsrun,obsrun) %{_localstatedir}/log/obs
%attr(-,obsrun,obsrun) %{_sharedstatedir}/obs/log
# created via %%post, since rpm fails otherwise while switching from
# directory to symlink
%ghost /usr/lib/obs/server/build

# formerly obs-source_service
%{_initddir}/obsservice
%config(noreplace) /etc/logrotate.d/obs-source_service
/etc/cron.d/cleanup_scm_cache
%{_sbindir}/rcobsservice
/usr/lib/obs/server/bs_service
/usr/lib/obs/server/call-service-in-docker.sh
/usr/lib/obs/server/cleanup_scm_cache

# formerly obs-productconverter
/usr/bin/obs_productconvert
/usr/lib/obs/server/bs_productconvert

# add obsservicerun user into docker group if docker
# gets installed
%triggerin -n obs-server -- docker
%{_sbindir}/usermod -a -G docker obsservicerun

%files -n obs-worker
%defattr(-,root,root)
%{_initddir}/obsworker
%{_sbindir}/rcobsworker
%dir %attr(-,obsrun,obsrun) %{_sharedstatedir}/obs
%dir %attr(-,obsrun,obsrun) %{_localstatedir}/log/obs

%files -n obs-api
%defattr(-,root,root)
%doc dist/{README.UPDATERS,README.SETUP} docs/openSUSE.org.xml ReleaseNotes-* README.md COPYING AUTHORS
%{_datadir}/obs/overview

%{_datadir}/obs-api/config/thinking_sphinx.yml.example
/etc/cron.d/obs_api_delayed_jobs_monitor
%config(noreplace) %{_datadir}/obs-api/config/thinking_sphinx.yml
%attr(-,%{apache_user},%{apache_group}) %config(noreplace) %{_datadir}/obs-api/config/production.sphinx.conf

%dir %{_datadir}/obs-api
%dir %{_datadir}/obs-api/config
%config(noreplace) %{_datadir}/obs-api/config/cable.yml
%config(noreplace) %{_datadir}/obs-api/config/feature.yml
%config(noreplace) %{_datadir}/obs-api/config/puma.rb
%config(noreplace) %{_datadir}/obs-api/config/secrets.yml
%config(noreplace) %{_datadir}/obs-api/config/spring.rb
%config(noreplace) %{_datadir}/obs-api/config/crawler-user-agents.json
%{_datadir}/obs-api/config/initializers
%dir %{_datadir}/obs-api/config/environments
%dir %{_datadir}/obs-api/files
%dir %{_datadir}/obs-api/db
%{_datadir}/obs-api/db/checker.rb
%{_datadir}/obs-api/Gemfile.in
%{_datadir}/obs-api/config.ru
%{_datadir}/obs-api/config/application.rb
%{_datadir}/obs-api/config/clock.rb
%config(noreplace) /etc/logrotate.d/obs-api
%{_initddir}/obsapidelayed
%{_initddir}/obsapisetup
%{_sbindir}/rcobsapisetup
%{_sbindir}/rcobsapidelayed
%{_datadir}/obs-api/app
%attr(-,%{apache_user},%{apache_group})  %{_datadir}/obs-api/db/structure.sql
%{_datadir}/obs-api/db/attribute_descriptions.rb
%{_datadir}/obs-api/db/data
%{_datadir}/obs-api/db/migrate
%{_datadir}/obs-api/db/seeds.rb
%{_datadir}/obs-api/files/wizardtemplate.spec
%{_datadir}/obs-api/lib
%{_datadir}/obs-api/public
%{_datadir}/obs-api/Rakefile
%{_datadir}/obs-api/script
%{_datadir}/obs-api/bin
%{_datadir}/obs-api/test
%{_datadir}/obs-api-doc


%{_datadir}/obs-api/config/locales
%{_datadir}/obs-api/vendor

#
# some files below config actually are _not_ config files
# so here we go, file by file
#

%{_datadir}/obs-api/config/boot.rb
%{_datadir}/obs-api/config/routes.rb
%{_datadir}/obs-api/config/environments/development.rb
%{_datadir}/obs-api/config/unicorn
%attr(0640,root,%apache_group) %config(noreplace) %verify(md5) %{_datadir}/obs-api/config/database.yml
%attr(0640,root,%apache_group) %{_datadir}/obs-api/config/database.yml.example
%attr(0644,root,root) %config(noreplace) %verify(md5) %{_datadir}/obs-api/config/options.yml
%attr(0644,root,root) %{_datadir}/obs-api/config/options.yml.example
%dir %attr(0755,%apache_user,%apache_group) %{_datadir}/obs-api/db/sphinx
%dir %attr(0755,%apache_user,%apache_group) %{_datadir}/obs-api/db/sphinx/production

%config %{_datadir}/obs-api/config/environment.rb
%config %{_datadir}/obs-api/config/environments/production.rb
%config %{_datadir}/obs-api/config/environments/test.rb
%config %{_datadir}/obs-api/config/environments/stage.rb

%{_datadir}/obs-api/log
%dir %attr(-,%{apache_user},%{apache_group}) %{_localstatedir}/log/obs-api
%attr(-,%{apache_user},%{apache_group}) %{_datadir}/obs-api/tmp

# these dirs primarily belong to apache2:
%if 0%{?fedora} || 0%{?rhel}
%{_httpd_confdir}/obs.conf.dist
%else
%dir /etc/apache2
%dir /etc/apache2/vhosts.d
%config(noreplace) /etc/apache2/vhosts.d/obs.conf
%endif

%if %{with selinux}
%{_datadir}/selinux/packages/obs-api/obs-api.pp
%endif

%defattr(0644,%{apache_user},%{apache_group})
%ghost %{_localstatedir}/log/obs-api/access.log
%ghost %{_localstatedir}/log/obs-api/backend_access.log
%ghost %{_localstatedir}/log/obs-api/delayed_job.log
%ghost %{_localstatedir}/log/obs-api/error.log
%ghost %{_localstatedir}/log/obs-api/lastevents.access.log
%ghost %{_localstatedir}/log/obs-api/production.log
%ghost %attr(0640,root,%{apache_group}) %secret_key_file

%files -n obs-common
%defattr(-,root,root)
%{_datadir}/obs/rc.compat
%if 0%{?suse_version:1}
/var/adm/fillup-templates/sysconfig.obs-server
%else
%config(noreplace) %{_sysconfdir}/sysconfig/obs-server
%endif
/usr/lib/obs/server/setup-appliance.sh
%{_initddir}/obsstoragesetup
%{_sbindir}/rcobsstoragesetup


%files -n obs-utils
%defattr(-,root,root)
%{_sbindir}/obs_project_update

%files -n obs-devel
%defattr(-,root,root)
%dir %_docdir/obs-devel
%_docdir/obs-devel/README.devel

%files -n obs-tests-appliance
%defattr(-,root,root)
%dir /usr/lib/obs/tests/
%dir /usr/lib/obs/tests/appliance
/usr/lib/obs/tests/appliance/*

%files -n obs-cloud-uploader
%defattr(-,root,root)
%{_initddir}/obsclouduploadworker
%{_initddir}/obsclouduploadserver
%{_sbindir}/rcobsclouduploadworker
%{_sbindir}/rcobsclouduploadserver
/usr/lib/obs/server/bs_clouduploadserver
/usr/lib/obs/server/bs_clouduploadworker
%{_bindir}/clouduploader
%dir /etc/obs
%dir /etc/obs/cloudupload
%dir /etc/obs/cloudupload/.aws
%config(noreplace) /etc/obs/cloudupload/.aws/credentials
%config /etc/obs/cloudupload/.ec2utils.conf

%package -n obs-container-registry
Summary:        The Open Build Service -- container registry
%if 0%{?suse_version} < 1210 && 0%{?suse_version:1}
Group:          Productivity/Networking/Web/Utilities
%endif
Requires:       docker-distribution-registry

%description -n obs-container-registry
The OBS Container Registry, based on the docker registry, which allows

* anonymous pulls from anywhere
* anonymous pushes from localhost.

This is done by proxying access to the registry through
apache and restricting any other http method than GET and HEAD
to localhost.


%files -n obs-container-registry
%defattr(-,root,root)
%dir %{_datadir}/obs/container-registry
%dir %{_datadir}/obs/container-registry/log
%dir %{_datadir}/obs/container-registry/htdocs
%if 0%{?fedora} || 0%{?rhel}
%config %{_httpd_confdir}/obs-container-registry.conf
%else
%config /etc/apache2/vhosts.d/obs-container-registry.conf
%endif

%files plugin-notify_hermes
%defattr(-,root,root)
%dir /usr/lib/obs/server/plugins
/usr/lib/obs/server/plugins/notify_hermes.pm

%files plugin-notify_rabbitmq
%defattr(-,root,root)
%dir /usr/lib/obs/server/plugins
/usr/lib/obs/server/plugins/notify_rabbitmq.pm


%changelog