Skip to content

Instantly share code, notes, and snippets.

@catchamonkey
Last active April 3, 2019 08:39
Show Gist options
  • Save catchamonkey/8575619f450fe4c94acd to your computer and use it in GitHub Desktop.
Save catchamonkey/8575619f450fe4c94acd to your computer and use it in GitHub Desktop.
RPM .spec file for Symfony2 apps
Name: application-name
Summary: Application Summary
Version: %version
Release: 1
License: Application License
Group: Development/Library
URL: www.application-url.com
BuildRoot: %{_tmppath}/%{name}-root
Source0: %{name}-%{version}.tar.gz
BuildRequires: rubygem-sass, php >= 5.5, php-xml
Requires: php >= 5.5, php-xml, php-mysql, php-intl
BuildArch: noarch
%description
Description of the application this spec file relates to
%prep
%setup -q
# Install the vendors (optimised autoloader, and no interaction)
./composer.phar install --prefer-dist -o -n --no-dev --no-progress --no-scripts
%build
export SYMFONY_ENV=prod
# Run post-install scripts defined in composer config (build bootstrap, asset install)
./composer.phar run-script post-install-cmd --no-dev --no-interaction
# Update the version used in asset output scheme
sed -i -e 's/\(assets_version: v=[ ]*\)\([a-zA-Z0-9_]*\)\(.*\)$/\1%{version}\3/g' app/config/config.yml
# dump the application assets
app/console assetic:dump --env=prod
# Remove;
# non production controllers (app._{env}.php)
# build files
# test config files
# Remove the parameters.yml file, needed for the above install_prod_assets, but not needed on production
# as the 'real' file is managed by puppet
rm web/app_*.php composer.* behat.yml phpspec.yml .travis.yml app/config/parameters.yml
# Move the generated cache dir, into a 'build' dir
mv app/cache/prod app/cache/build
# Update the cache directory from the one used during generation
find app/cache/ -type f | xargs sed -i "s#%{_builddir}/%{name}-%{version}#/home/sites/%{name}#ig"
%install
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT
export INSTALL_ROOT=$RPM_BUILD_ROOT
# Install vhost file
install -m 755 -d $RPM_BUILD_ROOT/home/vhosts
install -m 644 app/config/vhost.conf $RPM_BUILD_ROOT/home/vhosts/%{name}.conf
# Install application files
install -d -m 755 $RPM_BUILD_ROOT/home/sites/%{name}
cp -R * $RPM_BUILD_ROOT/home/sites/%{name}
%post
# If this is the first installation, then move the cache created during build into place
# if not, and it's an upgrade; we wait until old versions are uninstalled (see %triggerpostun)
if [ $1 -eq 1 ]; then
# Grant apache access to it so remaining items can be cached (doctrine annotations)
chown -R apache:apache /home/sites/%{name}/app/cache/build
# move the built cache into place
mv /home/sites/%{name}/app/cache/build /home/sites/%{name}/app/cache/prod
fi
%triggerpostun -- %{name}
# Only moved the created cache into place if at least 1 version will be left behind from the uninstall
# $2 is the number of versions left after an uninstall of %{name} has been performed
# >=1 means this is an upgrade, anything smaller means a complete removal
if [ $2 -ge 1 ]; then
# Grant apache access to it so remaining items can be cached (doctrine annotations)
chown -R apache:apache /home/sites/%{name}/app/cache/build
# move the built cache into place
mv /home/sites/%{name}/app/cache/build /home/sites/%{name}/app/cache/prod
fi
%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
/home/sites/%{name}
/home/vhosts/%{name}.conf
%attr(755, apache, apache) %dir /home/sites/%{name}/app/cache
%attr(755, apache, apache) %dir /home/sites/%{name}/app/logs
@codemedic
Copy link

You can get rid of the BUILD path using the below formula

find app/cache/ -type f | xargs sed -i "s#%{_builddir}/%{name}-%{version}#/home/sites/%{name}#ig"

@catchamonkey
Copy link
Author

Nice, that makes it more portable, cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment