Drush for Developers - Second Edition
()
About this ebook
- Install Drush on different systems, and download and install a full Drupal site in seconds
- Automate, optimize and monitor periodic and one-off tasks
- Manage multiple environments and define commands to simplify and automate processes within a team
This book is a best fit for backend developers with a basic knowledge of Drupal's APIs and some experience using the command line. Perhaps you already worked on one or two Drupal projects, but have never dived deep into Drush's toolset. In any case, this book will give you a lot of advice by covering real-world challenges in Drupal projects that can be solved using Drush.
Related to Drush for Developers - Second Edition
Related ebooks
Drupal 8 Configuration Management Rating: 0 out of 5 stars0 ratingsDrupal 8 SEO: The Visual, Step-By-Step Guide to Drupal Search Engine Optimization Rating: 3 out of 5 stars3/5CodeIgniter Web Application Blueprints Rating: 0 out of 5 stars0 ratingsBeginning JavaScript Rating: 0 out of 5 stars0 ratingsPHP & MYSQL: 200 Solved Exercises and Projects for Practice Rating: 0 out of 5 stars0 ratingsHTML5 for Flash Developers Rating: 5 out of 5 stars5/5Mastering KnockoutJS Rating: 0 out of 5 stars0 ratingsjQuery 2.0 Development Cookbook Rating: 0 out of 5 stars0 ratingsUnix Unleashed: From Rookie to Rock Star in the World of Command Lines Rating: 0 out of 5 stars0 ratingsDrupal 7 First Look Rating: 0 out of 5 stars0 ratingsLearning Drupal 6 Module Development Rating: 3 out of 5 stars3/5Mastering Linux: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsBasic Drupal: How to create, administer and maintain a Drupal Site Rating: 0 out of 5 stars0 ratingsLinux Shell Scripting: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsPHP & MySQL Practice It Learn It Rating: 3 out of 5 stars3/5Linux Shell Scripting Simplified: A Practical Guide with Examples Rating: 0 out of 5 stars0 ratingsMastering Linux System Administration Rating: 0 out of 5 stars0 ratingsDrupal 6 Content Administration Rating: 0 out of 5 stars0 ratingsBuilding Web Applications with Flask Rating: 0 out of 5 stars0 ratingsDrupal 6 Theming Cookbook Rating: 0 out of 5 stars0 ratingsCentOS High Performance: Create high availability clusters to enhance system performance using CentOS 7 Rating: 0 out of 5 stars0 ratingsLearning Yii Testing Rating: 1 out of 5 stars1/5Bash Command Line Pro Tips Rating: 5 out of 5 stars5/5Getting Started with Gulp – Second Edition Rating: 0 out of 5 stars0 ratingsLinux Command Line for New Users: A Practical Guide with Examples Rating: 0 out of 5 stars0 ratingsTerraform CLI Boss: Novice To Command Line Guru Rating: 0 out of 5 stars0 ratingsMastering Linux Administration: A Comprehensive Guide: The IT Collection Rating: 5 out of 5 stars5/5Drupal 5 Themes Rating: 0 out of 5 stars0 ratingsLinux Unveiled: From Novice to Guru Rating: 0 out of 5 stars0 ratings
Internet & Web For You
Coding For Dummies Rating: 5 out of 5 stars5/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5Get Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Canva Tips and Tricks Beyond The Limits Rating: 3 out of 5 stars3/5UX/UI Design Playbook Rating: 4 out of 5 stars4/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5From Nothing Rating: 5 out of 5 stars5/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Learn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/52022 Adobe® Premiere Pro Guide For Filmmakers and YouTubers Rating: 5 out of 5 stars5/5Web Design For Dummies Rating: 4 out of 5 stars4/5200+ Ways to Protect Your Privacy: Simple Ways to Prevent Hacks and Protect Your Privacy--On and Offline Rating: 4 out of 5 stars4/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/548 Really Useful Web Sites Rating: 5 out of 5 stars5/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsStop Asking Questions: How to Lead High-Impact Interviews and Learn Anything from Anyone Rating: 5 out of 5 stars5/5Blogging For Dummies Rating: 0 out of 5 stars0 ratings
Reviews for Drush for Developers - Second Edition
0 ratings0 reviews
Book preview
Drush for Developers - Second Edition - Juampy Novillo Requena
Table of Contents
Drush for Developers Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introduction, Installation, and Basic Usage
Installation requirements
Operating system
PHP
Installing Composer
Drush installation on Linux and OSX
Manual installation
The Drush command structure
Executing a command
Providing arguments to a command
Altering a command's behavior through options
Structuring command invocations
Command aliases
Understanding Drush's context system
Setting the context manually
Summary
2. Keeping Database Configuration and Code Together
Meeting the update path
Rebuilding the registry
Preparing the trap
Breaking the registry
Rebuilding Drupal's registry
Running database updates
Managing features
Exporting configuration into code
Running the update path on a different environment
Analyzing results
Reverting the feature components programmatically
Summary
3. Running and Monitoring Tasks in Drupal Projects
Running periodic tasks with cron
Disabling Drupal's cron
Verifying the current cron frequency
Overriding cron frequency and exporting it to code
Running cron with Drush
Scheduling cron runs with Jenkins
Installing Jenkins
Creating a job through the web interface
Monitoring cron runs
Running a task outside cron
Example – moving a Feeds importer from Drupal's cron to Drush
Exporting the Feeds importer into code
Writing a Drush command to trigger the Feeds importer
Running long tasks in batches
A sample Drush command using the Batch API
Batch API operations
Running the command and verifying the output
Evaluating code on the fly and running scripts
The php-eval command
The php-script command
A script to create nodes and revisions
Logging messages in Drush
The verbose and quiet modes
Redirecting Drush output into a file
Implementing your own logging mechanism
Running a command in the background
Summary
4. Error Handling and Debugging
Validating input
Validating an argument
Validating options
Ignoring options after the command name
Allowing additional options
Adding custom validation to a command
Rolling back when an error happens
Turning the update path into a single command
Browsing hook implementations
Inspecting the bootstrapping process
Inspecting hook and function implementations
Browsing and navigating hook implementations
Viewing source code of a function or method
Summary
5. Managing Local and Remote Environments
Managing local environments
Managing remote environments
Verifying requirements
Accessing a remote server through a public key
Defining a group of remote site aliases for our project
Using site aliases in commands
Special site aliases
Running a command on all site aliases of a group
Avoiding a Drupal bootstrap with @none
Referencing the current project with @self
Adding site alias support to the update path
Inspecting the command implementation and hooks
Running the update path with a site alias
Copying database and files between environments
Defining Drush shell aliases for a team
Blocking the execution of certain commands
Ignoring tables on sql-sync
Summary
6. Setting Up a Development Workflow
Moving configuration, commands, and site aliases out of Drupal
Installing Drupal Boilerplate
Relocating Drush files
Testing the new setup
Configuring the development database for the team
Configuring Jenkins to sync production to development
Fine-tuning the development database
Recreating the database on sql-sync
Excluding table data from production
Ignoring tables from production
Sanitizing data
Preventing e-mails from being sent
Running post sql-sync tasks in local environments
Summary
Index
Drush for Developers Second Edition
Drush for Developers Second Edition
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2012
Second edition: January 2015
Production reference: 1240115
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-378-6
www.packtpub.com
Credits
Author
Juampy Novillo Requena
Reviewers
Greg Anderson
Chris Burgess
Jonathan Araña Cruz
Jeremy French
Todd Zebert
Commissioning Editor
Dipika Gaonkar
Acquisition Editor
Meeta Rajani
Content Development Editor
Anila Vincent
Technical Editor
Arvind Koul
Copy Editor
Relin Hedly
Project Coordinator
Neha Bhatnagar
Proofreaders
Bridget Braund
Ameesha Green
Indexer
Tejal Soni
Production Coordinator
Alwin Roy
Cover Work
Alwin Roy
About the Author
Juampy Novillo Requena started working as a web developer in London. After spending a few years developing with plain PHP, Symfony, and Ruby on Rails, he discovered Drupal. Drawn by the Drupal community and the mind-blowing effect of getting a project done 10 times faster than before, Juampy has never looked back.
Since then, he's become more and more involved in the issue queues, which in turn led him to become a maintainer of core and contributed modules. He organizes events, gives sessions at national and international conferences, and has written the book Drush User's Guide, Packt Publishing. He feels privileged to experiment, have fun, and be challenged every day. He is known as juampy on Drupal.org and IRC. His Twitter account is @juampy72.
This book is the result of my two years working at Lullabot. Most of the contents explained here were originated by discussions or contributions within the team. I am very thankful to the team who worked on the MSNBC project, where we collaboratively developed and implemented best practices that are represented in this book.
I also want to thank the technical reviewers; their suggestions and corrections leveraged this book to a higher level.
Finally, a personal acknowledgement to the city of Niamey, Niger, where I did most of the writing.
About the Reviewers
Greg Anderson is an open source contributions engineer working on Drupal and WordPress at Pantheon in San Francisco. He has been contributing to Drush since just before the release of version 2, and remains an active co-maintainer to this day.
Chris Burgess is currently making the world better by building open source tools for activist and nonprofit organizations to campaign and communicate. He has been developing with Drupal since 2006, and he is immensely grateful to the Drupal and wider open source communities for the learning and sharing environment that they foster. Chris is based in Dunedin, New Zealand, with his two sons, Hunter and Rowan, and partner, Saira. He works for Fuzion Aotearoa, and you can reach him at @xurizaemon on Twitter, xur1z on IRC, or his Drupal.org profile at https://www.drupal.org/u/xurizaemon.
Jonathan Araña Cruz is a co-maintainer of Drush. He combines both sysadmin and Drupal development work. Jonathan has contributed several modules to Drupal, and as a sysadmin, he manages Infrastructure as Code with Puppet.
Jonathan's Drupal profile can be found at https://www.drupal.org/u/jonhattan.
Jeremy French has worked in web development for over a decade, floating through a wasteland of bespoke Content Management Systems, before finding Drupal. He has developed sites for a number of household names and blue chips, as well as a few interesting start-ups. Currently, he is working for a small agency, living the dream of distributed working.
Todd Zebert has been involved with Drupal since early version 6. He creates websites and web apps with a variety of technologies. Currently, Todd works as a lead web developer for Miles.
Todd has a diverse background in technology, including infrastructure, network engineering, project management, and IT leadership. His experience with web development started with the original Mosaic graphical web browser, SHTML/CGI, and Perl. His fondness for Drupal and his interest in workflow, efficiency, repeatable best practices, and DevOps drives his interest in Drush.
Todd is an entrepreneur involved with the start-up community. He's a believer in volunteering, open source, and contributing back. He's an advocate for Science, Technology, Engineering, Art, and Math (STEAM) education.
I'd like to thank the Drupal community, which is like no other.
Finally, I'd like to thank my pre-teen son with whom I get to share my interest in technology and program video games together.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
Support files, eBooks, discount offers, and morehttps://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Preface
In this book, I share with you how I use Drush in my day-to-day work. When working on Drupal projects, Drush is omnipresent. It is a key tool to debug code, run small scripts, and discover APIs. However, this is just the beginning; Drush's real potential comes when teams use it to define a development workflow.
What this book covers
Chapter 1, Introduction, Installation, and Basic Usage, begins with Drush's requirements and installation and then shows its basic usage through examples.
Chapter 2, Keeping Database Configuration and Code Together, explains how to export configuration from the database into code in order to share it with the rest of the team and other environments.
Chapter 3, Running and Monitoring Tasks in Drupal Projects, gives different options to run tasks in Drupal projects such as cron, Batch API, and custom scripts.
Chapter 4, Error Handling and Debugging, explores tools that help us catch and process errors, so as to navigate through the available hooks and functions in our project.
Chapter 5, Managing Local and Remote Environments, unveils all the magic behind site aliases using a typical Drupal project that involves production and development environments.
Chapter 6, Setting Up a Development Workflow, leverages all the concepts covered in the book by defining a development workflow for a team.
What you need for this book
Here are the system requirements to run the examples in the book:
Operating system: Any Unix-based system such as:
Ubuntu (any version), available at http://www.ubuntu.com
MAC OS X (any version)
Software:
PHP 5.2 or higher, available at http://www.php.net
MySQL 5.0 or higher, available at http://www.mysql.com
Apache 2.0 or higher, available at http://www.apache.org
Drupal 7, available at http://drupal.org
Git, available at http://git-scm.com
Jenkins, available at https://wiki.jenkins-ci.org
Who this book is for
This book will fit best to backend developers with a basic knowledge of Drupal's APIs and some experience using the command line. Perhaps, you already worked on one or two Drupal projects, but have never dived deep into Drush's toolset. In any case, this book will give you a lot of advice by covering real-world challenges in Drupal projects that can be solved using Drush.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Drush runs using a different PHP.ini configuration than the web server that does not have a request timeout.
A block of code is set as follows:
/**
* Callback to delete revisions using Batch API.
*/
function node_revision_delete_batch_process($content_type, $max_revisions, &$context) {
if (!isset($context['sandbox']['nids'])) {
// Set initial values.
$context['sandbox']['nids'] = node_revision_delete_candidates($content_type, $max_revisions);
$context['sandbox']['current'] = 0;
$context['sandbox']['total'] = count($context['sandbox']['nids']);
}
}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
/**
* Callback to delete revisions using Batch API.
*/
function node_revision_delete_batch_process($content_type, $max_revisions, &$context) {
if (!isset($context['sandbox']['nids'])) {
// Set initial values. $context['sandbox']['nids'] = node_revision_delete_candidates($content_type, $max_revisions); $context['sandbox']['current'] = 0; $context['sandbox']['total'] = count($context['sandbox']['nids']);
}
}
Any command-line input or output is written as follows:
$ drush php-script logging.php
success: marks a successful message. [success]
error: reports an error message. [error]
warning: is used to alert about something. [warning]
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes for example, appear in the text like this: "You can test it by