Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough
Oct 13 2010
Oct 13

Drupal without drush is like a burger without cheese. So when, in the course of human events, it becomes necessary to ensure that a specific version of drush is installed on a number of systems in a standard location, falling back to your distribution's package management system is probably a good idea.

Thus, I've hacked together a quick script that'll grab a version of drush and turn it into a noarch binary RPM, suitable for use on RHEL/CentOS (and probably Fedora, for that matter).

Drush ends up in /usr/local/drush, and for the sake of simplicity, a symbolic link (/usr/bin/drush) is created and pointed at /usr/local/drush/drush.

The script (which is attached below) has few requirements - basic utilities such as tar and wget, and your distribution's rpm building package (rpm-build for RHEL/CentOS).

Invocation is simple - you merely need to pass the script a URL to grab drush from. Example:

rpm_build-drush http://ftp.drupal.org/files/projects/drush-6.x-3.3.tar.gz

This happily spits out a lovely little package:

drush-6.x_3.3-1.noarch.rpm

Note that the generated RPM will not have dependencies of the rational sort - theoretically, one could and probably should have a PHP dependency (given that you're probably not running drush without it ;)) - however, internally, we're not using standard RHEL/CentOS PHP packages. Our dependencies would likely not be your dependencies. ;)

The script:

#!/usr/bin/perl
# 20101012-1
 
# This script packages Drush in binary .rpm form for easy installation.  It
# will automagically set up a proper environment for building RPMs in the
# invoker's home directory.  Note that the proper rpm building package
# (rpm-build for RHEL or CentOS for example) must be installed.  This script
# also depends on basic utilities being available (tar, wget).
#
# Note that the resulting package has no actual dependencies; theoretically,
# it should probably have dependencies on PHP.  We're using non-standard PHP
# packages and I'd like to make this script public, though. ;)
#
# Drush itself ends up in /usr/local/drush; a symlink for 'drush' is created
# in /usr/bin.  As per standard RPM building convention, this script should NOT
# be run as the root user.
#
# Syntax is: rpm_build-drush (URL for drush version to package)
#
# Example: rpm_build-drush http://ftp.drupal.org/files/projects/drush-6.x-3.3.tar.gz
 
### Global Variables
# Change this to your organization/etc.
my $packager = "Workhabit Systems";
 
my $home = "$ENV{HOME}";
my ($tarball, $version, $outrider);
my @files;
 
### Check for arguments
if(@ARGV[0] eq "")
{
        print "Error: You must specify the URL of the drush package to download.\n";
        exit 1;
}
 
### Grab pertinent version info
# Get our actual package filename
@ARGV[0] =~ m/(drush.+\.gz)$/;
$tarball = $1;
 
# Get our version
$version = $tarball;
$version =~ s/^drush-//;
$version =~ s/\.tar\.gz$//;
# - is invalid in RPM versioning; replace with _
$version =~ s/-/_/g;
 
### Set up our rpm building structure
print "...Setting up build directories...\n";
# Some of this is unnecessary given that we're creating a binary-only
# package through unconventional means, but we might as well set up the
# whole shebang.
system("/bin/mkdir -p $home/rpmbuild/{BUILD,RPMS,S{OURCE,PEC,RPM}S}");
# If .rpmmacros exists, back it up.
if(-e "$home/.rpmmacros")
{
        system("cp $home/.rpmmacros $home/.rpmmacros.old");
}
system("echo -e \"\%_topdir\\t\$HOME/rpmbuild\" > $home/.rpmmacros");
if((system("mkdir -p /tmp/drush-$version/usr/local") >> 8) != 0)
{
        print "Error: Could not create temporary build root\n";
        exit 2;
}
 
### Retrieve drush
print "...Retrieving and unpacking drush...\n";
# Retrieve
if((system("wget -m -nd @ARGV[0] -P /tmp") >> 8) != 0)
{
        print "Error: Could not retrieve drush from @ARGV[0]\n";
        exit 3;
}
# Unpackage
if((system("tar -zxf /tmp/$tarball -C /tmp/drush-$version/usr/local/") >> 8) != 0)
{
        print "Error: Could not unpackage $tarball\n";
        exit 4;
}
 
### Retrieve list of files included in the drush package
@files = `find /tmp/drush-$version/usr/local/drush -type f`;
 
### Write out our .spec file
print "...Creating .spec file...\n";
# Open the .spec file for writing
open(SPEC, ">$home/rpmbuild/SPECS/drush-$version.spec") || die "Error: Could not open .spec file for writing.";
print SPEC "Summary: drush\n";
print SPEC "Name: drush\n";
print SPEC "Version: $version\n";
print SPEC "Release: 1\n";
print SPEC "License: GPL v2\n";
print SPEC "Vendor: http://drush.ws/about\n";
print SPEC "Packager: $packager\n";
print SPEC "Group: Development/Tools\n";
print SPEC "BuildArch: noarch\n";
print SPEC "BuildRoot: /tmp/drush-$version\n\n";
print SPEC '%description' . "\n";
print SPEC "drush - a command line shell and scripting interface for Drupal.\n\n";
print SPEC '%prep' . "\n\n";
print SPEC '%build' . "\n\n";
print SPEC '%install' . "\n\n";
print SPEC '%post' . "\n";
print SPEC 'ln -s /usr/local/drush/drush /usr/bin/drush' . "\n\n";
print SPEC '%postun' . "\n";
print SPEC 'rm -f /usr/bin/drush' . "\n\n";
print SPEC '%clean' . "\n\n";
print SPEC '%files' . "\n";
print SPEC '%defattr(-,root,root)' . "\n";
# Now, drop our files in.
for $outrider ( @files )
{
        $outrider =~ s/\/tmp\/drush-$version//;
        print SPEC $outrider;
}
print SPEC "\n";
print SPEC '%changelog' . "\n";
close(SPEC);
 
### Create our binary rpm.
print "...Building binary RPM...\n";
if((system("rpmbuild -bb $home/rpmbuild/SPECS/drush-$version.spec") >> 8) != 0)
{
        print "Error: Failed to create binary .rpm\n";
        exit 5;
}
 
### Exit cleanly.
print "\n\nBuild complete.\n";
print "Your package should be available at $home/rpmbuild/RPMS/noarch/drush-$version-1.noarch.rpm\n\n";
exit 0;

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web