First!

So, Gnutiken has finally come around to open its Drupal-based webshop!

It took a little longer than we expected to adopt and tame Drupal to our likings, and some hundred Drupal module installs later, we’re more or less there.

Talking about the Drupal module installations, I got kind of fed up performing the same ritual over and over; download tarball, unpack, change ownership of the newly created directory tree to www-data:www-data, mv the original tarball to a safe place, etc, so I created a small script to do the job:

#!/bin/bash

# Copyright (c) 2010 Rikard Fröberg .
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

usage(){
 echo "USAGE: `basename $0` [-v] url"
 exit 5;
}
bailout(){
 echo $1
 exit $2
}

MINARGS=1

# Sufficient no. of args?
if [ $# -lt $MINARGS ]
then
 usage
fi

if [ "$UID" != "0" ]
then
 bailout "This script needs to be run as root or sudo." 6
fi

WORKING_DIR=/the-path-to-drupal/sites/all/modules
SAVE_DIR=/the-path-to-save-dir/drupal_modules
VERBOSE=false

if [ ! -e $WORKING_DIR ]
then
 bailout "Drupal modules directory NOT found!" 7
fi

while getopts v OPTION
do
 case "$OPTION" in
  v) VERBOSE=true;;
 esac
done
# reset $1 to first argument if option was given
shift $(echo "$OPTIND - 1"|bc)

# Parse arguments
URL=$1
MODULE_DIR=$(for f in $(echo $URL|tr '/' ' ');do true;done;echo $f | cut -d '-' -f1)
TAR_BALL=$(for f in $(echo $URL|tr '/' ' ');do true;done;echo $f)

# Make sure your are in the right place
$VERBOSE && echo changing directory to: $WORKING_DIR
cd $WORKING_DIR

# Download
$VERBOSE && echo fetching $URL
wget $URL || bailout "Couldn't get file, exiting" 1

# Extract
$VERBOSE && echo extracting files from $TAR_BALL
tar xvzf $TAR_BALL || bailout "couldn't extract $TAR_BALL. Exiting." 2

# Fix ownership
$VERBOSE && echo changing ownership for $WORKING_DIR/$MODULE_DIR to www-data:www-data
chown -R www-data: $WORKING_DIR/$MODULE_DIR || bailout "Couldn't change ownership on ${WORKING_DIR/$MODULE_DIR}. Exiting." 3

# Move the tar ball
$VERBOSE && echo Cleaning up. Moving $TAR_BALL to $SAVE_DIR
mv $TAR_BALL $SAVE_DIR || bailout "Couldn't save $TAR_BALL to $SAVE_DIR" 4

bailout "Done!" 0

How does it work? It uses the fact that Drupal modules are named according to a convention. For instance, the Nodewords module is found here: http://ftp.drupal.org/files/projects/nodewords-6.x-1.11.tar.gz

The tarball extracts to “nodewords” (i.e. the word before the dash in the filename). So the script figures out the filename of the tarball and also the directory (to be chowned later on) by simply looking at the URL.

The script is to be run sudo or as root and takes the URL to the module tarball as the only argument. Of course, you still need to enable the module in Drupal’s administration tool (i.e. /admin/build/modules ) and run update.php as usual, so take it for what it’s worth ;-) .

Hope someone will find any use for it!

If you find any errors or ways to improve it, feel free to leave a comment or publish your own version of it under the same License.

Next I will bring up some alternative ways to parse the URL. Stay tuned.


Related posts:

  1. String manipulation in bash

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , , ,

One Response to “First!”

Leave a Reply