2
votes

Hi i have a problem with code duplication while building rpm package.

I have a spec like this:

Summary     : ${product.id} ${rpmType}
Name        : ${product.id}_${rpmType}
.....


%build
%install


%files
%defattr(-,root,java,750)
%{home}

%clean
rm -rf %{buildroot}


%pre
CHECK_PAUSE=2;
echo -n "Stopping tomcat";
sh %{binary}/shutdown.sh
rc=0
while [ "$rc" == 0 ]; do
    sleep $CHECK_PAUSE;
    wget -q -O /dev/null -S http://localhost:8080/some-server/test;
    rc=$?;
done;
echo "Tomcat:  STOPPED"
mv %{home}/%{warname} %{home}/%{warname}.`date +%Y%d%m`
rm -rf %{home}/some-server
echo done.

%post
CHECK_PAUSE=2;
echo -n "Starting tomcat";
sh %{binary}/startup.sh
rc=1
while [ "$rc" -ne 0 ]; do
   sleep $CHECK_PAUSE
   wget -q -O /dev/null -S http://localhost:8080/some-server/test
   rc=$?
done
echo "Tomcat:  STARTED"

%preun
    if [ "$1" == "0" ]; then
    #STOP TOMCAT HERE SAME WAY AS IN PRE
fi


%postun
if [ "$1" == "0" ]; then
    #START TOMCAT HERE SAME WAY AS IN POST
fi

The scripts which are going to be executed in preun and postun sections are the same as in pre and post sections. But I don't want just copy/paste them. Is there some sophisticated solution to avoid code duplication here?

1

1 Answers

1
votes

Here is a possible solution:

  1. Create separate files for each piece of reusable shell script code.

  2. Create a sort of pre-processor (possibly another shell script) that inserts the files created in Step 1 above into the spec file under the appropriate scriplet label (i.e. %pre, %post, %preun, %postun).

If you are using a source control management system, you might want to track these additional files as well.