Installing Drupal 6 using the Command Line with Drush and Twill
While working on code for Drupal 7, I've been creating and dropping databases all over the place. And each time I drop a database, I have to go to my browser and fill out all the site information again.
So this got me thinking... does it really need to be this way? (Spoiler: it doesn't)
A few weeks ago I found Byte Craft kamal's awesome post about twill, a really simple language that lets you fill out HTML forms from the command line, and how it can be used to install your Drupal site without using your browser at all!
In this post, I will expand that solution to show you how you can create scripts to download and install Drupal and then show you a script you can use to automate the recreation of your site when you drop the database.
Before We Start
So, for this to work, you will need:
- Drush
- Python
- twill
Drush
Drush can be downloaded from drupal.org. Place the drush folder alongside your Drupal sites. On my Mac, this is in the Sites folder.
You will want to create an alias for drush so you can use it on the command line. Just to be safe, you may want to tell it what version of php5 to use (for instance, I would tell it to use the php5 that came with MAMP).
alias drush='/Applications/MAMP/bin/php5/bin/php ~/Sites/drush/drush.php'
Once this alias is created, you should be able to type drush and get a list of drush commands.
Python
Python comes standard with the Mac. It seems that Macs ship with a stale version that may not have been updated in 1-2 years, but this shouldn't be a problem for most people. If your version is older than 2.4, you will want to update.
Twill
twill can be downloaded from the twill author's site. To install it, change directory to the twill directory. You can only run the installer from inside the twill directory, though it does not matter where that directory is in your file system. Then run this command:
sudo python setup.py install
The Scripts
Here is an example shell script to get things started... you could save this as drupalinstall.sh and run the command source drupalinstall.sh. You will probably want to run this script from the same folder where you have your Drupal sites and the drush folder.
Shell Script
drush dl drupalecho -n "Enter a directory name: "
read -e DIR
mv drupal-6.* $DIR
cd $DIR
install -m 777 sites/default/default.settings.php sites/default/settings.php
twill-sh ../drupal_install.py
Super Easy Explanation
This script downloads Drupal, asks you what you want to call the Drupal folder and renames it with the mv command. I am including the assertion that this is Drupal version 6 because in Drupal version 7 you will be able to use Drush to install Drupal without any of this.
The script then changes directory to your new directory and copies the settings file. It then calls your twill script, which we will create right now.
Twill Script—First Install
getinput 'Site directory '
setglobal directory __input__
getinput 'Database name '
setglobal db_path __input__
getinput 'Database username '
setglobal db_user __input__
getinput 'Database user password '
setglobal db_pass __input__
getinput 'Site email '
setglobal site_mail __input__
getinput 'Admin username '
setglobal admin __input__
getinput 'Admin email '
setglobal mail __input__
getinput 'Admin password '
setglobal password __input__
go http://localhost/${directory}/install.php?profile=default&locale=en
code 200
find "To set up your"
fv 1 db_path ${db_path}
fv 1 db_user ${db_user}
fv 1 db_pass ${db_pass}
submit op
find "All necessary changes to"
fv 1 site_mail ${site_mail}
fv 1 account[name] ${admin}
fv 1 account[mail] ${mail}
fv 1 account[pass][pass1] ${password}
fv 1 account[pass][pass2] ${password}
submit op
find "Congratulations"
clear_cookies
go http://localhost/${directory}/
fv 1 name ${admin}
fv 1 pass ${password}
submit op
find "Log out"
Super Easy Explanation
The first chunk gets input from the command line to set variables. Then we go to the install page, make sure we found the page and that it returned code 200, and that we found some of the expected text ("To set up your"...) on the page.
We would now be at the Database Information page, so we use the command fv to set the field values for database name, database user, and database password. We submit the form and check to make sure we find the expected text on the next page ("All necessary changes to"...).
We are now at the site information page, so we fill in the site email address, the admin user name, the admin email address, and the admin password. We submit and check for the correct text ("Congratulations"...).
Now we clear the cookies to get rid of the session and use the admin name and password to make sure that we can log in. If we find the Log out button on the page, we have successfully logged in.
Twill Script—Re-Install
If we have a sandbox and we've mucked about in the database, we may want to get a fresh database. We can reinstall the site using this script. The values are hard coded in the file to make it a very fast reinstall of your site.
First you want to drop your database. Then run the command twill-sh drupalsite_reinstall.py.
Note that you will need to replace 'directory' with the name of your Drupal directory.
go http://localhost:8888/directory/install.php?profile=default&locale=en
find "All necessary changes to"
fv 1 site_mail your-email@example.com
fv 1 account[name] Admin
fv 1 account[mail] admins-email@example.com
fv 1 account[pass][pass1] mypassword
fv 1 account[pass][pass2] mypassword
submit op
find "Congratulations"
clear_cookies
go http://localhost:8888/directory/
fv 1 name Admin
fv 1 pass mypassword
submit op
find "Log out"
Comments
Hi, there's a python version
Hi, there's a python version of the script I created short while ago. You can find it at my bitbucket repo:-
http://bitbucket.org/k4ml/pulut2/src/tip/scripts/
Good explanation, updated my post to refer here.
The Demo Module
You could also just use the demo module, which would give you other improvements. You just install drupal and enable the demo module. Then you tell the demo module to save your current place. Next, go have whatever fun you want on your drupal site.
When you decide you want to return the the initial setup, just go to the demo module and restore your spot in a couple clicks.
It also has the advantage of allowing you to save various versions along the way if you are developing a module and want to have a config that you are testing the upgrade of, just create a config that includes the old config, then enable the module with the upgrade stuff in it, found out it failed, revert to the old config using the demo module and ... well, I think you get the point.
http://drupal.org/project/demo