From 4a9ebc4ba4221cb614fb3b862e86edeec0fe55a9 Mon Sep 17 00:00:00 2001
From: Todd Zullinger <tmz@pobox.com>
Date: Fri, 30 Apr 2010 09:39:15 -0400
Subject: [PATCH] packaging: Misc. improvements to rpm spec file

- Rework %%install for better FHS compliance
- Misc. adjustments to match Fedora/EPEL packaging guidelines
- Update License field to GPLv3+ to match README
---
 extras/spec/foreman.spec |  131 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 94 insertions(+), 37 deletions(-)

diff --git a/extras/spec/foreman.spec b/extras/spec/foreman.spec
index 643535b..5959ad3 100644
--- a/extras/spec/foreman.spec
+++ b/extras/spec/foreman.spec
@@ -1,28 +1,37 @@
-%define name foreman
-%define homedir /usr/share/%{name}
-%define confdir extras/spec
+%global homedir %{_datadir}/%{name}
+%global confdir extras/spec
 
-Name:           %{name}
+Name:           foreman
 Version:        0.1.4
-Release:        3%{?dist}
+Release:        4%{?dist}
 Summary:        Systems Management web application
-Group:          Administration Tools
-License:        GPLv2+
+
+Group:          Applications/System
+License:        GPLv3+
 URL:            http://theforeman.org
-Source0:        http://github.com/ohadlevy/foreman/tarball/foreman-0.1-4.tar.bz2
+Source0:        http://github.com/ohadlevy/%{name}/tarball/%{name}-0.1-4.tar.bz2
 BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
+
 BuildArch:      noarch
+
 Requires:       ruby(abi) = 1.8
 Requires:       rubygems
 Requires:       rubygem(rack) >= 1.0.1
 Requires:       rubygem(rake) >= 0.8.3
 Requires:       puppet >= 0.24.4
 Requires:       rubygem(sqlite3-ruby)
+Requires(pre):  shadow-utils
+Requires(post): chkconfig
+Requires(preun): chkconfig
+Requires(preun): initscripts
+Requires(postun): initscripts
+
 Packager:       Ohad Levy <ohadlevy@gmail.com>
 
 %description
 Foreman is aimed to be a Single Address For All Machines Life Cycle Management.
-Foreman is based on Ruby on Rails, and this package bundle all Rails and plugins required for Foreman to work
+Foreman is based on Ruby on Rails, and this package bundles Rails and all
+plugins required for Foreman to work.
 
 %prep
 %setup -q -n %{name}
@@ -30,54 +39,102 @@ Foreman is based on Ruby on Rails, and this package bundle all Rails and plugins
 %build
 
 %install
-%{__rm} -rf %{buildroot}
-%{__install} -p -d -m0755 %{buildroot}%{_datadir}/%{name}
-%{__install} -p -d -m0755 %{buildroot}%{_defaultdocdir}/%{name}-%{version}
-%{__install} -Dp -m0644 %{confdir}/%{name}.sysconfig %{buildroot}%{_sysconfdir}/sysconfig/%{name}
-%{__install} -Dp -m0755 %{confdir}/%{name}.init %{buildroot}%{_initrddir}/%{name}
-%{__cp} -p -r app config db extras lib public Rakefile script vendor %{buildroot}%{_datadir}/%{name}
-%{__chmod} a+x %{buildroot}%{_datadir}/%{name}/script/{console,dbconsole,runner}
-%{__rm} -rf %{buildroot}%{_datadir}/%{name}/extras/{jumpstart,spec,puppet}
-%{__rm} -rf %{buildroot}%{_datadir}/%{name}/VERSION
-%{__mkdir} %{buildroot}%{_datadir}/%{name}/{tmp,log}
+rm -rf %{buildroot}
+install -d -m0755 %{buildroot}%{_datadir}/%{name}
+install -d -m0755 %{buildroot}%{_sysconfdir}/%{name}
+install -d -m0755 %{buildroot}%{_localstatedir}/lib/%{name}
+install -d -m0755 %{buildroot}%{_localstatedir}/run/%{name}
+install -d -m0750 %{buildroot}%{_localstatedir}/log/%{name}
+
+install -Dp -m0644 %{confdir}/%{name}.sysconfig %{buildroot}%{_sysconfdir}/sysconfig/%{name}
+install -Dp -m0755 %{confdir}/%{name}.init %{buildroot}%{_initrddir}/%{name}
+cp -p -r app config extras lib Rakefile script vendor %{buildroot}%{_datadir}/%{name}
+chmod a+x %{buildroot}%{_datadir}/%{name}/script/{console,dbconsole,runner}
+rm -rf %{buildroot}%{_datadir}/%{name}/extras/{jumpstart,spec,puppet}
+rm -rf %{buildroot}%{_datadir}/%{name}/VERSION
+
+# Move config files to %{_sysconfdir}
+for i in database.yml environment.rb settings.yaml; do
+    mv %{buildroot}%{_datadir}/%{name}/config/$i %{buildroot}%{_sysconfdir}/%{name}
+    ln -sv %{_sysconfdir}/%{name}/$i %{buildroot}%{_datadir}/%{name}/config/$i
+done
+
+# Put db in %{_localstatedir}/lib/%{name}/db
+cp -pr db/migrate %{buildroot}%{_datadir}/%{name}
+mkdir %{buildroot}%{_localstatedir}/lib/%{name}/db
+ln -sv %{_localstatedir}/lib/%{name}/db %{buildroot}%{_datadir}/%{name}/db
+ln -sv %{_datadir}/%{name}/migrate %{buildroot}%{_localstatedir}/lib/%{name}/db/migrate
+
+# Put HTML %{_localstatedir}/lib/%{name}/public
+cp -pr public %{buildroot}%{_localstatedir}/lib/%{name}/
+ln -sv %{_localstatedir}/lib/%{name}/public %{buildroot}%{_datadir}/%{name}/public
+
+# Put logs in %{_localstatedir}/log/%{name}
+ln -sv %{_localstatedir}/log/%{name} %{buildroot}%{_datadir}/%{name}/log
+
+# Put tmp files in %{_localstatedir}/run/%{name}
+ln -sv %{_localstatedir}/run/%{name} %{buildroot}%{_datadir}/%{name}/tmp
+
+# Create a script for migrating the SQLite db
+cat << \EOF > %{buildroot}%{_datadir}/%{name}/extras/dbmigrate
+#!/bin/sh
+cd && /usr/bin/rake db:migrate RAILS_ENV=production
+EOF
 
 %clean
-%{__rm} -rf %{buildroot}
+rm -rf %{buildroot}
 
 %files
 %defattr(-,root,root,0755)
-%{_datadir}/%{name}
 %doc README
-%{_initrddir}/foreman
-%config(noreplace) %{homedir}/config/settings.yaml
-%config(noreplace) %{homedir}/config/database.yml
+%{_datadir}/%{name}
+%{_initrddir}/%{name}
+%config(noreplace) %{_sysconfdir}/%{name}
 %config(noreplace) %{_sysconfdir}/sysconfig/%{name}
+%attr(-,%{name},%{name}) %{_localstatedir}/lib/%{name}
+%attr(-,%{name},%{name}) %{_localstatedir}/log/%{name}
+%attr(-,%{name},%{name}) %{_localstatedir}/run/%{name}
 
 %pre
 # Add the "foreman" user and group
-/usr/sbin/useradd -c "Foreman" -s /sbin/nologin -r -d %{homedir} -G puppet %{name} 2> /dev/null || :
+getent group %{name} >/dev/null || groupadd -r %{name}
+getent passwd %{name} >/dev/null || \
+    useradd -r -g %{name} -G puppet -d %{homedir} -s /sbin/nologin -c "Foreman" %{name}
+exit 0
 
 %post
-# fixing some permissions issues and file duplication warning while creating the RPM.
-for dir in db tmp log public config/environment.rb; do
-  /bin/chown -R foreman:root %{homedir}/$dir
-done
-/bin/chown foreman:root %{homedir}
-/bin/chown -R root:root %{homedir}/db/migrate
+/sbin/chkconfig --add %{name} || ::
 
-# install foreman (but don't activate)
-/sbin/chkconfig --add %{name}
+# initialize/migrate the SQLite database
+su - foreman -s /bin/bash -c %{_datadir}/%{name}/extras/dbmigrate >/dev/null 2>&1 || :
 
-# migrate the SQLite database
-su - foreman -s /bin/bash -c 'cd ; /usr/bin/rake db:migrate RAILS_ENV=production > /dev/null'
+%preun
+if [ $1 -eq 0 ] ; then
+    /sbin/service %{name} stop >/dev/null 2>&1
+    /sbin/chkconfig --del %{name} || :
+fi
+
+%postun
+if [ $1 -ge 1 ] ; then
+    # Restart the service
+    /sbin/service %{name} condrestart >/dev/null 2>&1 || :
+fi
 
 %changelog
+* Fri Apr 30 2010 Todd Zullinger <tmz@pobox.com> - 0.1.4-4
+- Rework %%install for better FHS compliance
+- Misc. adjustments to match Fedora/EPEL packaging guidelines
+- Update License field to GPLv3+ to match README
+- Use foreman as the primary group for the foreman user instead of puppet
+
 * Thu Apr 19 2010 Ohad Levy <ohadlevy@gmail.com> - 0.1-4-3
 - added status to startup script
 - removed puppet module from the RPM
-* Thu Apr 12 2010 Ohad Levy <ohadlevy@gmail.com> - 0.1-4-2
+
+* Thu Apr 12 2010 Ohad Levy <ohadlevy@gmail.com> - 0.1.4-2
 - Added startup script for built in webrick server
 - Changed foreman user default shell to /sbin/nologin and is now part of the puppet group
 - defaults to sqlite database
-* Thu Apr 6 2010 Ohad Levy <ohadlevy@gmail.com> - 0.1-4-1
+
+* Thu Apr 6 2010 Ohad Levy <ohadlevy@gmail.com> - 0.1.4-1
 - Initial release.
-- 
1.7.0

