%global _hardened_build 1
%{!?_pkgdocdir: %global _pkgdocdir %{_docdir}/%{name}-%{version}}

Name:             glusterfs
Version:          7.0
Release:          4
License:          GPLv2 and LGPLv3+
Summary:          Aggregating distributed file system
URL:              http://docs.gluster.org/
Source0:          https://download.gluster.org/pub/gluster/glusterfs/7/7.0/glusterfs-7.0.tar.gz
Source7:          glusterfsd.service

Patch0:		  0000-core-fix-memory-pool-management-races.patch

BuildRequires:    systemd bison flex gcc make libtool ncurses-devel readline-devel libattr-devel
BuildRequires:    libxml2-devel openssl-devel libaio-devel libacl-devel python3-devel git perl
BuildRequires:    libtirpc-devel rpcgen libuuid-devel sqlite-devel lvm2-devel firewalld
BuildRequires:    fuse-devel libibverbs-devel libcurl-devel
BuildRequires:    userspace-rcu-devel >= 0.7
BuildRequires:    libcmocka-devel >= 1.0.1
BuildRequires:    librdmacm-devel >= 1.0.15
Obsoletes:        glusterfs-regression-tests hekafs glusterfs-ufo glusterfs-ganesha
Obsoletes:        %{name}-common < %{version}-%{release}
Obsoletes:        %{name}-core < %{version}-%{release}
Provides:         %{name}-common = %{version}-%{release}
Provides:         %{name}-core = %{version}-%{release}
Requires:         python3-gluster = %{version}-%{release}
Requires:         python3  python3-prettytable python3-requests attr psmisc rsync
Requires:         rpcbind lvm2 nfs-utils
Requires(pre):    shadow-utils
Requires(post):   systemd /sbin/ldconfig
Requires(preun):  systemd
Requires(postun): systemd /sbin/ldconfig

Provides:         %{name}-api
Obsoletes:        %{name}-api
Provides:         %{name}-cli
Obsoletes:        %{name}-cli
Provides:         %{name}-events
Obsoletes:        %{name}-events
Provides:         %{name}-client-xlators
Obsoletes:        %{name}-client-xlators
Provides:         %{name}-extra-xlators
Obsoletes:        %{name}-extra-xlators
Provides:         %{name}-fuse
Obsoletes:        %{name}-fuse
Obsoletes:        %{name}-client < %{version}-%{release}
Provides:         %{name}-client = %{version}-%{release}
Provides:         %{name}-gnfs
Obsoletes:        %{name}-gnfs
Provides:         %{name}-geo-replication
Obsoletes:        %{name}-geo-replication
Provides:         %{name}-libs
Obsoletes:        %{name}-libs
Provides:         %{name}-rdma
Obsoletes:        %{name}-rdma
Provides:         %{name}-server
Obsoletes:        %{name}-server
Provides:         %{name}-cloudsync-plugins
Obsoletes:        %{name}-cloudsync-plugins
Provides:         %{name}-thin-arbiter
Obsoletes:        %{name}-thin-arbiter

%description
GlusterFS is a scalable network filesystem suitable for data-intensive tasks such as cloud storage
and media streaming.

%package devel
Summary:          Development files for glusterfs
Provides:         %{name}-api-devel
Obsoletes:        %{name}-api-devel
Requires:         %{name} = %{version}-%{release}
Requires:         %{name}-extra-xlators = %{version}-%{release}
Requires:         libacl-devel

%description devel
This package provides the development libraries and include files.

%package -n python3-gluster
Summary:          GlusterFS python library
Requires:         python3

%description -n python3-gluster
This package contains the python2 modules of GlusterFS and own gluster namespace.

%package help
Summary: Including man files for glusterfs.
Requires: man

%package resource-agents
Summary:          OCF-compliant resource agents for GlusterFS
License:          GPLv3+
BuildArch:        noarch
Requires:         %{name}-server = %{version}-%{release}
Requires:         %{_prefix}/lib/ocf/resource.d

%description resource-agents
Resource agents for monitoring and managing glusterd daemons
under Open Cluster Framework (OCF) compliant resource
managers such as Pacemaker.

%description    help
This contains man files for the using of glusterfs.

%prep
%autosetup -Sgit -n %{name}-%{version}

%build
sed -i -e 's/--quiet//' configure.ac
./autogen.sh && %configure \
        --enable-firewalld \
        --enable-gnfs \
        --with-tmpfilesdir=%{_tmpfilesdir}

#hardening and rpath
sed -i 's| \\\$compiler_flags |&\\\$LDFLAGS |' libtool
%disable_rpath

%make_build V=1

%check
make check

%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot} docdir="%_docdir/%name"

%delete_la_and_a

mkdir -p %{buildroot}%{_localstatedir}/log/{glusterd,glusterfs,glusterfsd}
mkdir -p %{buildroot}%{_rundir}/gluster/metrics

#new working directory
mkdir -p %{buildroot}%{_localstatedir}/lib/glusterd

# change the configuration file for the new working directory
perl -i -pe \
    's{^(\s*option working-directory )\S+}{$1 %{_localstatedir}/lib/glusterd}g' \
    "%{buildroot}/%{_sysconfdir}/%name/glusterd.vol"

# glusterfsd.service
install -D -p -m 0644 %{_sourcedir}/glusterfsd.service %{buildroot}%{_unitdir}/glusterfsd.service

install -D -p -m 0644 extras/glusterfs-logrotate %{buildroot}%{_sysconfdir}/logrotate.d/glusterfs

#ghosts
install -d %{buildroot}%{_localstatedir}/lib/glusterd/geo-replication
touch %{buildroot}%{_localstatedir}/lib/glusterd/geo-replication/gsyncd_template.conf
install -D -p -m 0644 extras/glusterfs-georep-logrotate %{buildroot}%{_sysconfdir}/logrotate.d/glusterfs-georep
touch %{buildroot}%{_localstatedir}/lib/glusterd/glusterd.info
touch %{buildroot}%{_localstatedir}/lib/glusterd/options

subdirs=(add-brick create copy-file delete gsync-create remove-brick reset set start stop)
for dir in ${subdirs[@]}; do
    mkdir -p %{buildroot}%{_localstatedir}/lib/glusterd/hooks/1/"$dir"/{pre,post}
done

install -d %{buildroot}%{_localstatedir}/lib/glusterd/{glustershd,peers,vols,nfs/run,bitd,quotad,scrub,snaps,ss_brick}
touch %{buildroot}%{_localstatedir}/lib/glusterd/nfs/nfs-server.vol
touch %{buildroot}%{_localstatedir}/lib/glusterd/nfs/run/nfs.pid

#cli
install -p -m 0755 -D extras/command-completion/gluster.bash  %{buildroot}%{_sysconfdir}/bash_completion.d/gluster

%post
/sbin/ldconfig
%systemd_postun_with_restart rsyslog
exit 0

%systemd_post glustereventsd
exit 0

if [ $1 -ge 1 ]; then
    %systemd_postun_with_restart glusterd
fi
exit 0

%systemd_post glusterd
%systemd_post glusterfsd

if [ -f %{_localstatedir}/log/glusterfs/.cmd_log_history ]; then
    mv %{_localstatedir}/log/glusterfs/.cmd_log_history \
       %{_localstatedir}/log/glusterfs/cmd_history.log
fi

if [ -d /etc/glusterd -a ! -h %{_localstatedir}/lib/glusterd ]; then
    install -d %{_localstatedir}/lib/glusterd
    cp -a /etc/glusterd %{_localstatedir}/lib/glusterd
    rm -rf /etc/glusterd
    ln -sf %{_localstatedir}/lib/glusterd /etc/glusterd
fi

if [ -d %{_localstatedir}/lib/glusterd/vols ]; then
    for file in $(find %{_localstatedir}/lib/glusterd/vols -name '*.vol'); do
        newfile=${file}.rpmsave
        echo "warning: ${file} saved as ${newfile}"
        cp ${file} ${newfile}
    done
fi

if [ -e /etc/ld.so.conf.d/glusterfs.conf ]; then
    rm -f /etc/ld.so.conf.d/glusterfs.conf
fi

%firewalld_reload

pidof -c -o %PPID -x glusterd &> /dev/null
if [ $? -eq 0 ]; then
    kill -9 `pgrep -f gsyncd.py` &> /dev/null

    killall --wait glusterd &> /dev/null
    glusterd --xlator-option *.upgrade=on -N

    rm -f %{_rundir}/glusterd.socket

    /bin/systemctl --quiet start glusterd.service
else
    glusterd --xlator-option *.upgrade=on -N

    rm -f %{_rundir}/glusterd.socket
fi

exit 0

%pre
getent group gluster > /dev/null || groupadd -r gluster
getent passwd gluster > /dev/null || useradd -r -g gluster -d %{_rundir}/gluster -s /sbin/nologin -c "GlusterFS daemons" gluster
exit 0

%preun
if [ $1 -eq 0 ]; then
    if [ -f %{_unitdir}/glustereventsd.service ]; then
        /bin/systemctl --quiet stop glustereventsd.service
        %systemd_preun glustereventsd
    fi
fi
exit 0

if [ $1 -eq 0 ]; then
    if [ -f %{_unitdir}/glusterfsd.service ]; then
        /bin/systemctl --quiet stop glusterfsd.service
    fi
    /bin/systemctl --quiet stop glusterd.service

    if [ -f %{_unitdir}/glusterfsd.service ]; then
        %systemd_preun glusterfsd
    fi
    %systemd_preun glusterd
fi

if [ $1 -ge 1 ]; then
    if [ -f %{_unitdir}/glusterfsd.service ]; then
        %systemd_postun_with_restart glusterfsd
    fi
    %systemd_postun_with_restart glusterd
fi

if [ $1 -eq 0 ]; then
    if [ -f %{_unitdir}/gluster-ta-volume.service ]; then
        /bin/systemctl --quiet stop gluster-ta-volume
        %systemd_preun gluster-ta-volume
    fi
fi

exit 0

%postun
/sbin/ldconfig
%systemd_postun_with_restart rsyslog
%firewalld_reload
exit 0

%files
%license COPYING-GPLV2 COPYING-LGPLV3
%doc extras/clear_xattrs.sh
%_docdir/%name
%dir %{_localstatedir}/log/glusterfs
%dir %{_datadir}/glusterfs
%dir %{_datadir}/glusterfs/scripts
     %{_datadir}/glusterfs/scripts/*
%dir %{_libdir}/glusterfs
%dir %{_libdir}/glusterfs/%{version}
%dir %{_libdir}/glusterfs/%{version}/auth
     %{_libdir}/glusterfs/%{version}/auth/*
%dir %{_libdir}/glusterfs/%{version}/rpc-transport
     %{_libdir}/glusterfs/%{version}/rpc-transport/*
%dir %{_libdir}/glusterfs/%{version}/xlator
%dir %{_libdir}/glusterfs/%{version}/xlator/debug
     %{_libdir}/glusterfs/%{version}/xlator/debug/*
%dir %{_libdir}/glusterfs/%{version}/xlator/features
     %{_libdir}/glusterfs/%{version}/xlator/features/*.so
     %{_libdir}/glusterfs/%{version}/xlator/meta.so
%dir %{_libdir}/glusterfs/%{version}/xlator/performance
     %{_libdir}/glusterfs/%{version}/xlator/performance/*.so
%dir %{_libdir}/glusterfs/%{version}/xlator/system
     %{_libdir}/glusterfs/%{version}/xlator/system/*
%dir %attr(0775,gluster,gluster) %{_rundir}/gluster
%dir %attr(0775,gluster,gluster) %{_rundir}/gluster/metrics
     %{_tmpfilesdir}/gluster.conf
%exclude %{_libdir}/*.so
%{_libdir}/libgfapi.*
%dir %{_libdir}/glusterfs/%{version}/xlator/mount
     %{_libdir}/glusterfs/%{version}/xlator/mount/*.so
     %{_sbindir}/gluster
     %{_sysconfdir}/bash_completion.d/gluster
%dir %{_libdir}/glusterfs/%{version}/xlator/cluster
     %{_libdir}/glusterfs/%{version}/xlator/cluster/*.so
%dir %{_libdir}/glusterfs/%{version}/cloudsync-plugins
     %{_libdir}/glusterfs/%{version}/cloudsync-plugins/cloudsyncs3.so
     %{_libdir}/glusterfs/%{version}/cloudsync-plugins/cloudsynccvlt.so
%dir %{_libdir}/glusterfs/%{version}%{?dev}/xlator/playground
     %{_libdir}/glusterfs/%{version}%{?dev}/xlator/playground/template.so

# files fuse
%{_sbindir}/glusterfs
%{_sbindir}/glusterfsd
%{_bindir}/fusermount-glusterfs
/sbin/mount.glusterfs
%config(noreplace) %{_sysconfdir}/logrotate.d/glusterfs

# file gnfs
%dir %{_libdir}/glusterfs/%{version}/xlator/nfs
     %{_libdir}/glusterfs/%{version}/xlator/nfs/*
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/nfs
%ghost      %attr(0600,-,-) %{_localstatedir}/lib/glusterd/nfs/nfs-server.vol
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/nfs/run
%ghost      %attr(0600,-,-) %{_localstatedir}/lib/glusterd/nfs/run/nfs.pid

%config(noreplace) %{_sysconfdir}/logrotate.d/glusterfs-georep
%{_sbindir}/gfind_missing_files
%{_sbindir}/gluster-mountbroker
%dir %{_libexecdir}/glusterfs
%dir %{_libexecdir}/glusterfs/python
%dir %{_libexecdir}/glusterfs/python/syncdaemon
     %{_libexecdir}/glusterfs/python/syncdaemon/*
%dir %{_libexecdir}/glusterfs/gfevents
     %{_libexecdir}/glusterfs/gfevents/*
     %{_libexecdir}/glusterfs/gsyncd
     %{_libexecdir}/glusterfs/gverify.sh
     %{_libexecdir}/glusterfs/set_geo_rep_pem_keys.sh
     %{_libexecdir}/glusterfs/peer_gsec_create
     %{_libexecdir}/glusterfs/peer_mountbroker
     %{_libexecdir}/glusterfs/peer_mountbroker.py*
     %{_libexecdir}/glusterfs/gfind_missing_files
     %{_libexecdir}/glusterfs/peer_georep-sshkey.py*
     %{_libexecdir}/glusterfs/mount-shared-storage.sh
     %{_libexecdir}/glusterfs/glusterfind
     %{_libexecdir}/glusterfs/peer_add_secret_pub
     %{_libexecdir}/glusterfs/peer_eventsapi.py*
     %{_sbindir}/gluster-georep-sshkey
%dir   %attr(0755,-,-) %{_localstatedir}/lib/glusterd/geo-replication
%ghost %attr(0644,-,-) %{_localstatedir}/lib/glusterd/geo-replication/gsyncd_template.conf
%dir   %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/gsync-create
%dir   %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/gsync-create/post
       %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/gsync-create/post/S56glusterd-geo-rep-create-post.sh
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/gsync-create/pre
       %{_libdir}/*.so.*
# sysconf
%config(noreplace) %{_sysconfdir}/glusterfs

# init files
%{_unitdir}/glusterd.service
%{_unitdir}/glusterfsd.service
%{_unitdir}/glusterfssharedstorage.service
%{_unitdir}/gluster-ta-volume.service
# binaries
%{_sbindir}/glusterd
%{_sbindir}/glfsheal
%{_sbindir}/gf_attach
%{_sbindir}/gluster-setgfid2path

%dir %{_libdir}/glusterfs/%{version}/xlator/mgmt
     %{_libdir}/glusterfs/%{version}/xlator/mgmt/*.so
%dir %{_libdir}/glusterfs/%{version}/xlator/protocol
     %{_libdir}/glusterfs/%{version}/xlator/protocol/*.so
%dir %{_libdir}/glusterfs/%{version}/xlator/storage
     %{_libdir}/glusterfs/%{version}/xlator/storage/posix.so
# snap_scheduler
%{_sbindir}/snap_scheduler.py
%{_sbindir}/gcron.py
%{_sbindir}/conf.py
#/var/lib/glusterd,hookscript,etc.
%ghost      %attr(0644,-,-) %config(noreplace) %{_localstatedir}/lib/glusterd/glusterd.info
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/bitd
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/groups
            %attr(0644,-,-) %{_localstatedir}/lib/glusterd/groups/*
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/glusterfind
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/glusterfind/.keys
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/glustershd
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/add-brick
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/add-brick/post
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/add-brick/post/*
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/add-brick/pre
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/add-brick/pre/*
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/create
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/create/post
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/create/post/*
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/create/pre
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/copy-file
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/copy-file/post
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/copy-file/pre
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/delete
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/delete/post
                            %{_localstatedir}/lib/glusterd/hooks/1/delete/post/*
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/delete/pre
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/delete/pre/*
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/remove-brick
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/remove-brick/post
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/remove-brick/pre
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/reset
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/reset/post
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/reset/pre
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/set
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/set/post
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/set/post/*
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/set/pre
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/start
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/start/post
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/start/post/*
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/start/pre
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/stop
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/stop/post
       %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/stop/pre
            %attr(0755,-,-) %{_localstatedir}/lib/glusterd/hooks/1/stop/pre/*
%config(noreplace) %ghost %attr(0600,-,-) %{_localstatedir}/lib/glusterd/options
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/peers
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/quotad
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/scrub
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/snaps
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/ss_brick
%ghost %dir %attr(0755,-,-) %{_localstatedir}/lib/glusterd/vols
       %{_bindir}/glusterfind
       %{_prefix}/lib/firewalld/services/glusterfs.xml
%dir %{_localstatedir}/lib/glusterd/events
%{_sbindir}/glustereventsd
%{_sbindir}/gluster-eventsapi
%{_unitdir}/glustereventsd.service

%files -n python3-gluster
%dir %{python3_sitelib}/gluster
     %{python3_sitelib}/gluster/__init__.*
     %{python3_sitelib}/gluster/__pycache__
     %{python3_sitelib}/gluster/cliutils

%files devel
%dir %{_includedir}/glusterfs
     %{_includedir}/glusterfs/*
     %{_libdir}/*.so
%dir %{_libdir}/glusterfs/%{version}/xlator/features
%{_libdir}/pkgconfig/*.pc

%files resource-agents
%{_prefix}/lib/ocf/resource.d/glusterfs

%files help
%{_mandir}/man8/*gluster*.8*

%changelog
* Mon Feb 24 2020 renxudong<renxudong1@huawei.com> - 7.0-4
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:fix glfs_new memory leak

* Wed Feb 12 2020 Ruijun Ge<geruijun@huawei.com> - 7.0-3
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:fix install error

* Tue Jan 14 2020 renxudong<renxudong1@huawei.com> - 7.0-2
- Type:enhancement
- ID:NA
- SUG:NA
- DESC:delete useless files

* Sat Jan 11 2020 renxudong<renxudong1@huawei.com> - 7.0-1
- Type:Upgrade
- ID:NA
- SUG:NA
- DESC:upgrade to 7.0

* Sat Sep 21 2019 zoujing<zoujing13@huawei.com> - 4.1.5-3
- Type:enhancemnet
- ID:NA
- SUG:NA
- DESC:split the glusterfs-resource-agents

* Mon Sep 16 2019 zoujing<zoujing13@huawei.com> - 4.1.5-2
- Package init
