Archive for February, 2011

h1

Building a Drupal installation profile

February 22, 2011

Setting up a new Drupal site can be a chore, especially if you need to do it a lot when doing testing or trying out new features to see if they are what you need without having to do it in a live environment or messing up existing dev environments.

There are a couple of drupal tools that can help, drush and drush_make. As someone who enjoys working on the command line, drush is a superb tool and can just be used on its own to speed up the development of building drupal sites. Drush make adds an additional make command to drush which will take an installation profile file that you’ve previously crafted and build you a drupal site out of it.

Below is both an example installation profile and a bash script I created to quickly roll out a standard drupal site with simpletest and admin_menu built in for Drupal 6 which most of our clients are still using. Assuming you’ve setup drush and drush_make If the files simpletest.make and simpletest.modsenabled exist in your web root (e.g. /var/www) then you can run the drush_make.sh script as shown below and a new site will be created at /var/www/simpletest. If you edit the drush_make.sh script to include your mysql database credentials then the script will setup the database for you as well.

./drush_make.sh simpletest

Installation profile simpletest.make

; Get Drupal 6
core = 6.x
api = 2
projects[drupal][type] = "core"
; Simpletest patch - note I'm hosting this locally, its in the simpletest zip from drupal.org/projects/simpletest
projects[drupal][patch][]="http://localhost/D6-core-simpletest.patch"
; Fix for PHP4 deprciated warnings in update/fetch.inc
projects[drupal][patch][]="http://drupal.org/files/issues/555362-1.update_parse_files.D6.patch"
; Drupal contributed modules
projects[admin_menu][type] = "module"
projects[admin_menu][version] = 1.5
projects[devel][type] = "module"
projects[devel][version] = 1.23
projects[simpletest][type] = "module"
projects[simpletest][version] = 2.11

List of modules which are enabled in new drupal site: simpletest.modsenabled


admin_menu,menu,user,devel,simpletest

drush_make.sh script

#!/bin/bash
if [ ! -n "$1" ]; then
echo "You need to provide a name which matches the name of the make file"
exit 1
fi
if [ ! -f $1.make ]; then
echo "Could not find the make file $1.make"
exit 1
fi
if [ ! -f $1.modsenabled ]; then
echo "Could not find the file $1.modsenabled";
exit 1
fi
modslist=`cat $1.modsenabled`
echo About to install $1.make
# extract the site from the make file
if [ ! -n "$2" ]; then
drush make $1.make $1
else
echo skipping because of $2
fi
# go into the new dir
if [ -d $1 ]; then
echo "Directory exists"
cd $1
else
echo "Directory does not exists"
exit 1
fi
# make the new db schema
echo DROP SCHEMA IF EXISTS $1\; CREATE SCHEMA $1\; | mysql -u root -p
# setup the new site - put your db password in here
drush site-install --db-url=mysqli://root:password@localhost/$1 --site-name=$1 -y
# Enable the modules
drush en -y $modslist
exit 0

h1

Drupal Drush: Changing the output of pm-list into something which can go into an installation profile

February 22, 2011

I like the command line and so drush is a great tool I found when building sites in drupal: http://drush.ws

It has a plugin called drush_make which can be used with an installation profile to automatically download and install a drupal and associated modules

The following command passed through sed will get all the non-core modules from a drupal installation and their version numbers and output it formatted in the way it can go into a drush make installation profile file.

drush pm-list --no-core --status=enabled | sed -r 's/(.{35})(.{63})Module 6.x-(.*)/\2|\3/g' | sed -r 's/[^(]*\(([^)]*)[^|]*\|([^ ]*)/projects[\1][version] = \2/g'

h1

All you need to remember about tar

February 10, 2011

The first thing to remember about tar is that tar is primarily for turning a directory into a file so you only have one thing to send rather than a direcotry with lots of things. Its second use is to then compress that file. So if you want to compress a single file, you first need to put it into a directory before using tar.

Most useful options:
1. Decide if you are creating or extracting to or from a tar and use one of the following…
c – create a tar from a dir
x – extract contents from a tar

2. Decide on the compression standard
z – gzip, the usual one
j – bz, makes smaller files but takes longer to process

3. Do you want lots of messgaes? Use v for verbose, usful to see that something is actually happening

4. Your options will always finish with f and after you specify the name of the tar file you are either creating or extracting from. File name convension is filename.tar.gz if using gzip or finish with bz if using the other compression option.

5. If you are creating a tar, finish with the name of the directory you want to tar.

Examples:
Turning directory mydir into a tar and compressing with gzip:
tar czvf mydir.tar.gz mydir

Extracting directory mydir from a tar which is compressed with gzip:
tar xzvf mydir.tar.gz

h1

Case sensitive find and replace in VIM

February 10, 2011

Had trouble finding this for some reason. Normal find and replace in my vim is case insenstive though must totorials I found claim it to be sensetive by default. The following will replace all case instances of book (i.e. Book and BOOK will be replaced by session as well):

:%s/book/session/g

If I only want to replace book, I need to use a capital I option like this:

:%s/book/session/gI

Another useful option is c, which asks you to confirm every instance it finds before replacing it.

:%s/book/session/gcI

h1

Search for a string in a bunch of files and sub directories

February 8, 2011

Recursivley search all files in a directory and sub directory for the presence of a string use the following grep command

grep -Rn "some string" *

h1

Generating htpasswd file from the command line

February 2, 2011

Recently found a command line utility for automagically generating a .htpasswd file:

Type this command at the prompt:

htpasswd -cmb .htpasswd username password

The arguments are [C]reate the new .htpasswd file if its not there, [m] option forces encryption and [b] allows you to include the user name and password immediately.


AuthType Basic
AuthUserFile /var/www/html/Private/.htpasswd
Require user username

Full guide here: Linux Webserver Tips