Drush for Developers - Second Edition - Juan Novillo Requena - E-Book

Drush for Developers - Second Edition E-Book

Juan Novillo Requena

0,0
20,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 198

Veröffentlichungsjahr: 2015

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



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 <[email protected]> for more details.

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.

https://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 PacktCopy and paste, print, and bookmark contentOn 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.comMAC OS X (any version)
Software:
PHP 5.2 or higher, available at http://www.php.netMySQL 5.0 or higher, available at http://www.mysql.comApache 2.0 or higher, available at http://www.apache.orgDrupal 7, available at http://drupal.orgGit, available at http://git-scm.comJenkins, 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.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you really get the most out of.

To send us general feedback, simply e-mail to <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata SubmissionForm link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem..

Chapter 1. Introduction, Installation, and Basic Usage

Drush is a command-line interface for Drupal. It can also serve as an alternative to write scripts using PHP instead of BASH. The Drush ecosystem is vast. Every year, at DrupalCon, the Drush core team gives an update on the bleeding edge features being developed by them and by contributors all over the world.

Tasks such as clearing caches, running database updates, executing batch scripts, and managing remote websites are just a glimpse of what you can do with Drush.

Here is an example. Imagine that you have pushed new code for your website and need to run database updates. Normally this would involve the following steps:

Back up your database.Open your web browser and navigate to http://example.com/user.Authenticate as administrator.Navigate to http://example.com/update.php.Run database updates and wait for a confirmation message.

Now, here is how you can accomplish the preceding steps with Drush:

$ drush @example.prod sql-dump > dump.sql$ drush @example.prod updatedb --yes

That's it. We did not even have to open an SSH connection or a web browser. The first command created a database backup and the second one executed pending database updates. In both these commands, we used @example.prod, which is a Drush site alias used to load configuration details about a particular site. We will see Drush site aliases in detail in Chapter 5, Managing Local and Remote Environments.

Drush is highly customizable. You can adjust it to fit a specific workflow. This is especially helpful when working on a Drupal project within a team; you can define security policies, wrap commands with sensible defaults, sanitize a copy of the production database automatically, and so on. This is the area that this book will focus on. We will go through some common processes during a Drupal project and discover how we can automate or simplify them using Drush. Let's start!

This chapter is an introduction and will cover the following topics to get you up to speed:

Installation requirementsDrush command structureUnderstanding Drush's context system

Installation requirements

The following are the installation requirements for Drush. If you have already installed it, simply make sure that you are running version 7.0.0-alpha5 (https://github.com/drush-ops/drush/releases/tag/7.0.0-alpha5) or higher by executing drush --version in the command line, and skip forward to the next section of this chapter.

Operating system

Drush works on Unix-like operating systems (such as Ubuntu and OSX) and Windows operating systems.

If you use Windows, consider using something like VirtualBox (https://www.virtualbox.org) to install a virtual machine that runs, for example, Ubuntu (http://www.ubuntu.com). If you still want to use Drush on Windows, there is an installer available at http://www.drush.org/drush_windows_installer. Note, however, that the installer installs an older version of Drush, so some of the contents of this book won't work.

PHP

Let's start by making sure that you have PHP 5.3.0 or greater installed. To do so, open a terminal and run the following command:

$ php -v

The output should look something like the following code screenshot:

As you can see, I am using PHP 5.5.9. If you get a Command not found message or your version is lower than 5.3.0, you will need to install or upgrade PHP. Refer to your vendor documentation to do this as the steps will vary.

Installing Composer

On Linux and OSX platforms, the recommended way to install Drush is through Composer (https://getcomposer.org), a dependency manager that has become the standard in the PHP world. Installing Composer can be accomplished with the following commands:

$ cd $HOME$ curl -sS https://getcomposer.org/installer | php$ sudo mv composer.phar /usr/local/bin/composer

If you find any issues while running the preceding commands or while installing it through a packaging system such as homebrew, then take a look at the official installation instructions for Composer (https://getcomposer.org/doc/00-intro.md#globally-on-osx-via-homebrew). Once you have completed the installation, you can verify that it works by running the following command:

$ composer about Composer - Package Management for PHP Composer is a dependency manager tracking local dependencies of your projects and libraries. See http://getcomposer.org/ for more information.

Note

If you have already installed Composer, make sure that it is up to date by runningcomposer self-update (https://getcomposer.org/doc/03-cli.md#self-update).

Drush installation on Linux and OSX

At the time of writing this book, the latest available version of Drush is 7.0.0-alpha5 (https://github.com/drush-ops/drush/releases/tag/7.0.0-alpha5). This is the version that we will use. The Drush core team does a fantastic job of keeping backwards compatibility between major versions, so if you have already installed a more recent version of Drush, you should be okay as practically all the examples in the book will work.

Let's go ahead and install Drush. Once Composer has been installed (see the previous section on installing Composer), you can install Drush with the following command:

$ composer global require drush/drush:7.0.0-alpha5 -vChanged current directory to /home/juampy/.composer./composer.json has been updatedLoading composer repositories with package informationUpdating dependencies (including require-dev) - Installing drush/drush (7.0.0-alpha5) Downloading: 100% Extracting archivedrush/drush suggests installing youngj/httpserverWriting lock fileGenerating autoload files

The preceding command has downloaded Drush 7.0.0-alpha5 into $HOME/.composer/vendor/bin/drush. In order to use Drush from anywhere in the system, we need to make sure that Composer's bin directory is present at our $PATH environment variable. We can do so with the following commands:

$ sed -i '1i export PATH="$HOME/.composer/vendor/bin:$PATH"' \ $HOME/.bashrc$ source $HOME/.bashrc

Note the use of $HOME and $PATH, which are environment variables. $HOME contains the location of your home directory, while $PATH represents a list of directories to look for executable files. You can view the contents of these variables by executing echo $HOME or echo $PATH. Take a look at your home directory to check whether there is .bash_profile, .bash_login, or .profile file at $HOME. If you find them, adjust the preceding commands, so the $PATH variable is adjusted in these files as well.

Finally, we can test that Drush has been installed successfully and contains the right version:

$ cd $HOME$ drush --version Drush Version : 7.0.0-alpha5

Manual installation

If you prefer to install Drush manually, then follow these steps:

Start by opening a web browser, and download and uncompress the contents of Drush 7.0.0-alpha5 (https://github.com/drush-ops/drush/releases/tag/7.0.0-alpha5) into your home directory.Open a terminal and move the drush directory into your system's shared directory:
$ sudo mv $HOME/drush /usr/share
Set proper permissions to the drush executable file:
$ sudo chmod u+x /usr/share/drush/drush
Create a symbolic link of the Drush executable to any of the directories listed at your $PATH environment variable so that you do not have to type /usr/share/drush/drush every time you use it.
$ echo $PATH/home/juampy/.composer/vendor/bin:/usr/local/sbin: /usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games$ sudo ln -s /usr/share/drush/drush /usr/local/bin/drush
The next step consists of installing Composer dependencies for Drush:
$ cd /usr/share/drush$ composer installLoading composer repositories with package informationInstalling dependencies (including require-dev) from lock file - Installing d11wtq/boris (v1.0.8) - Installing pear/console_table (1.1.5) - Installing phpunit/php-token-stream (1.2.2) - Installing symfony/yaml (v2.2.1) - Installing sebastian/version (1.0.3) - Installing sebastian/exporter (1.0.1) - Installing sebastian/environment (1.0.0) - Installing sebastian/diff (1.1.0) - Installing sebastian/comparator (1.0.0) - Installing phpunit/php-text-template (1.2.0) - Installing phpunit/phpunit-mock-objects (2.1.5) - Installing phpunit/php-timer (1.0.5) - Installing phpunit/php-file-iterator (1.3.4) - Installing phpunit/php-code-coverage (2.0.9) - Installing phpunit/phpunit (4.1.3) - Installing symfony/process (v2.4.5) pear/console_table suggests installing pear/Console_Color (>=0.0.4) phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)Generating autoload files
Finally, verify the installation:
$ cd $HOME$ which drush /usr/local/bin/drush$ drush --version Drush Version : 7.0.0-alpha5

The main README file at the Drush repository has a great section on POST-INSTALL tasks (https://github.com/drush-ops/drush#post-install) with additional information on configuring PHP and extra settings for environments such as MAMP. It's worth taking a look at it.