drush: the best tool ever - drupalcamp michigan 2016

19

Click here to load reader

Upload: commercial-progression

Post on 22-Jan-2018

262 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Drush: the best tool ever - DrupalCamp Michigan 2016

Drush: the Best Tool Ever

Hillary Lewandowski

Page 2: Drush: the best tool ever - DrupalCamp Michigan 2016

Drupal Shell

Drush is a command line shell and Unix scripting interface for Drupal.

Drush core comes with useful commands for interacting with modules, themes,

and profiles. It can clear caches and run update.php. It also interacts with the

database for common functions such as executing queries and migrating

databases.

What is Drush?

Page 3: Drush: the best tool ever - DrupalCamp Michigan 2016

Why is Drush the Best Tool Ever?

Skip the UI for common operations

Install a site with two commands

Local development is easier

Make files facilitate install profiles

Deployment is much easier

Policy files to avoid catastrophe

Make your own tools

Resources:

Drush Docs

Drush Github

Drush Commands

Page 4: Drush: the best tool ever - DrupalCamp Michigan 2016

Drush is a command line tool

You need to be somewhat comfortable with the command line. The easiest way to

install drush is through composer: composer global require drush/drush

You must run drush from the Drupal site folder (ie /var/www/html)

Different drush versions

can be installed in

parallel, based on which

version of Drupal you

are using

Drush Version Drush Branch PHP Compatible Drupal versions Code Status

Drush 9 master 5.5+ D7, D8

Drush 8 8.x 5.4.5+ D6, D7, D8

Drush 7 7.x 5.3.0+ D6, D7

Drush 6 6.x 5.3.0+ D6, D7 Unsupported

Drush 5 5.x 5.2.0+ D6, D7 Unsupported

Page 5: Drush: the best tool ever - DrupalCamp Michigan 2016

Skip the UI for common operations

Drush makes it easy to

Clear the caches: drush cc all

Download a module: drush dl views

Enable a module: drush en views

Disable a module: drush dis views

Uninstall a module: drush pmu views

Get a one-time login link for user #1: drush uli

Get a one-time login link for any user: drush uli username

Page 6: Drush: the best tool ever - DrupalCamp Michigan 2016

Install a site from two commands

# drush dl drupal --drupal-project-rename=example

# cd example

# drush site-install standard

--db-url='mysql://[db_user]:[db_pass]@localhost/[db_name]'

--site-name=Example

Page 7: Drush: the best tool ever - DrupalCamp Michigan 2016

Local development is easier

Check your configuration: drush status

Update your local site: drush up --security-only

Run database updates: drush updatedb -y

List all the features with status: drush fl

Recreate all features: drush fua -y

Recreate a specific feature : drush fu feature_name -y

Revert all features: drush fra -y

Revert a specific feature: drush fr feature_name -y

Page 8: Drush: the best tool ever - DrupalCamp Michigan 2016

Make files facilitate install profiles

A drush make file can define modules and other sources that need to be

downloaded, and put them in the correct place.

A make file is a core part of an install file, which will enable and configure all of the

resources downloaded by the make file. See the compro_install_profile for an

example of a make file inside of an install profile.

Page 9: Drush: the best tool ever - DrupalCamp Michigan 2016

Calling a make file and installing a profile

Now we can install a site from scratch with these few lines to download our

resources with drush make and then do a drush site-install compro to use

our install profile named compro

# drush make --no-cache compro.make example

# cd example

# drush site-install compro

--db-url='mysql://[db_user]:[db_pass]@localhost/[db_name]'

--site-name=Example

Page 10: Drush: the best tool ever - DrupalCamp Michigan 2016

Deployment is much easier

These commands will bring the database and files down to your local

drush sql-sync @prod @dev-hillary -y

drush rsync @prod:%files @dev-hillary:%files -y

After making your code changes and pulling them into production, you can run

these commands to run updates, revert all features, and clear the caches

drush updatedb -y

drush fra -y

drush cc all

Page 11: Drush: the best tool ever - DrupalCamp Michigan 2016

Define site aliases

Site aliases are defined in sites/all/drush/example.aliases.drushrc.php

$aliases['dev-hillary'] = array(

'uri' => hillarycp.dev',

'root' => '/home/hillary/htdocs/hillarycp,

'path-aliases' => array(

'%files' => 'sites/default/files',

),

);

$aliases['prod'] = array(

'uri' => 'dev-hillarycp.pantheonsite.io',

'remote-host' => 'appserver.dev.3df39394-5977-4ed1-9b59-52e24e85c5e7.drush.in',

'remote-user' => 'dev.3df39394-5977-4ed1-9b59-52e24e85c5e7',

'path-aliases' => array(

'%files' => 'code/sites/default/files',

),

);

Page 12: Drush: the best tool ever - DrupalCamp Michigan 2016

Define shell aliases

You can execute bash commands with drush, or chain multiple commands

together with one command by defining drush shell aliases in

sites/all/drush/drushrc.php

$options['shell-aliases'] = array(

'sync-live' => '!drush sql-sync {{#prod}} {{#self}} && drush rsync {{#prod}}:%files {{#self}}:%files'

);

This command chains the drush sql-sync command immediately followed by a

call to drush rsync

@self is an assumed alias to the current drupal instance

Page 13: Drush: the best tool ever - DrupalCamp Michigan 2016

Policy files to avoid catastrophe

It’s really useful to pull down your database, but if you mistakenly name @prod for

your destination you could accidentally overwrite your production database!

Luckily you can add a policy.drush.php file into sites/all/drush to prevent that

function drush_policy_sql_sync_validate($source = NULL, $destination = NULL) {

if ($destination == '@prod') {

return drush_set_error('POLICY_DENY', t('You are not allowed to overwrite the production database.'));

}

if (($destination == '@stage') and ($source != '@prod')) {

return drush_set_error('POLICY_DENY', t('You are only allow to overwrite the stage db with the prod

db'));

}

}

Page 14: Drush: the best tool ever - DrupalCamp Michigan 2016

Make your own tools

We can create a custom drush command that enables devel, field_ui, and

views_ui when we run drush dev-mode. This is a very simple drush command

that doesn’t require any further information or input from the user, but we could

make our commands more powerful with arguments and options, for example

drush fu feature_name

feature_name is the argument

drush sql-dump --result-file=my.sql result-file is the option

Page 15: Drush: the best tool ever - DrupalCamp Michigan 2016

custom.drush.inc

function custom_drush_command() {

$items = array();

$items['dev-mode'] = array(

'description' => "Enables development-specific modules.",

'aliases' => array('devm'),

);

return $items;

}

function drush_custom_dev_mode() {

module_enable(array('devel', 'field_ui', 'views_ui'));

drush_print(t("Dev mode enabled successfully."));

}

Page 16: Drush: the best tool ever - DrupalCamp Michigan 2016

drush rr is very useful when come across a PHP file not found error

To use this command, first download it, clear the drush caches, then run the

command!

drush @none dl registry_rebuild

drush cc drush

drush rr

Download and usage instructions can be followed on the registry_rebuild project

page.

drush rr

Page 17: Drush: the best tool ever - DrupalCamp Michigan 2016

Demonstration

Page 18: Drush: the best tool ever - DrupalCamp Michigan 2016

Questions?

Page 19: Drush: the best tool ever - DrupalCamp Michigan 2016

Thank you!

Check out my Object Oriented Programming blog posts at commercialprogression.com