63
Gain productivity with Drush - Emmanuel Milou 1 Gain productivity with Drush

Gain productivity with Drush (Drupal Camp, Montreal 2013)

Embed Size (px)

DESCRIPTION

This presentation is an overview on how using the command-line tool Drush to gain productivity working with Drupal.

Citation preview

Page 1: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 1

Gain productivity with Drush

Page 2: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 2

Introduction

Page 3: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 3

Emmanuel Milou @[email protected]

Drupal Technical Leader at Savoir-faire Linux

Based in Montreal

http://sflx.ca/love

introduction

Page 4: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 4

What is drush ?

Page 5: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 5

“ Drush (DRUpal Shell) is a command line shell and scripting interface for

Drupal “

what is drush ?

Page 6: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 6

Running Drupal commands through the shell

3 main parts:

● The drush project manager:• Manage modules and themes

● Drush core:• Manage site configuration

● Drush SQL • Issue query to database

what is drush ?

Page 7: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 7

$ drush [options] command ... 

what is drush ?

Page 8: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 8

Short and long version for every command

● site-install (si)● variable-get (vget)● variable-set (vset)● pm-download (dl)● enable (en)● ...

what is drush ?

Page 9: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 9

Example:

$ drush ­­yes vset maintenance_mode 1$ drush vset ­­yes maintenance_mode 1$ drush ­­yes variable­set maintenance_mode 1

what is drush ?

Page 10: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 10

The help command

$ drush help

$ drush help [command]

what is drush ?

Page 11: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 11

Examples:

● Installing sites● Updating sites● Downloading and enabling modules / themes● Clearing caches● Administring sites configuration● Archiving sites● ...

what is drush ?

Page 12: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 12

Do I need to use drush ?

Page 13: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 13

Yes :-)

Page 14: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 14

why do I need to use drush

Swiss-knife

Work faster

Automate tasks

Handle multiple sites

Create your own command

Page 15: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 15

Installing drush

Page 16: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 16

https://github.com/drush-ops/drush

installing drush

Page 17: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 17

Multi plateform:

● Unix-like OS● Windows

Specific instructions on different platforms:

https://drupal.org/node/1791676

installing drush

Page 18: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 18

Example on Ubuntu (need php-pear library)

pear channel­discover \ pear.drush.orgpear install drush/drushdrush version

installing drush

Page 19: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 19

Installing a site and introspection

Page 20: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 20

site installation and instrospection

Download the latest stable release:

$ drush pm­download drupal­7.x

Run installation procedure:

$ drush site­install ­­db­url=mysql://user:pass@localhost:port/dbname

Page 21: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 21

site installation and instrospection

More on site-install command:

$ drush site­install [profile] ­­db­url=mysql://user:pass@hostname[:port]/database­­site­name=”Dev site”­­account­[email protected]­­account­name=admin­­account­pass=admin

Page 22: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 22

site installation and instrospection

After a successful bootstrap

$ drush status

General view of the current installation:

● Database credentials● Drupal root● Files directory path● Admin theme

Page 23: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 23

Downloading and managing modules

Page 24: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 24

downloading and managing modules

Available commands to:

● List all modules: $ drush pm­list

● Download a module / theme:$ drush pm­download <modulename>

● Enable a module / theme:$ drush pm­enable <modulename>

Page 25: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 25

downloading and managing modules

Installing and enabling a module used to look like this:

Page 26: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 26

downloading and managing modules

1. Find the module's exact name

2. Look up for the module on drupal.org

3. Download the archive module from project page

4. Go on your disk and extract the archive

5. Copy the module directory in the drupal installation

6. Go to module admin page

7. Realize the module needs an other module to be enabled

8. Repeat from step 2

Page 27: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 27

downloading and managing modules

Now with drush, it is:

Page 28: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 28

downloading and managing modules

Find the module's exact name

Run:

$ drush dl <modulename>

$ drush enable <modulename>

Page 29: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 29

downloading and managing modules

Download modules in sites/all/modules

­­destination=<path> to override Specify specific version to download:

● Latest dev release: $ drush dl views ­­dev

● List of recent releases: $ drush dl views ­­select

● Specific version:$ drush dl 7.x­3.x­dev

Page 30: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 30

downloading and managing modules

$ drush en views

Handles module's dependencies by:

● Downloading the required modules● Enabling them

Page 31: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 31

downloading and managing modules

Disable a module:

$ drush pm­disable <modulename>

Uninstall a module:

$ drush pm­uninstall <modulename>

Page 32: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 32

Updating a site

Page 33: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 33

updating a site

One of the most tedious task ever ...

Gets a lot easier with drush:

$ drush pm­update

Core, non-core, modules, security, ...

Page 34: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 34

Database management

Page 35: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 35

database management

Open a SQL cli, using Drupal's credentials

$ drush sql­cli

Database synchronisation between sites

Run a custom query

$ drush sql­query “SELECT title from node where nid=1”

Import SQL statements from a file

$ drush sql­query –file=example.sql

Page 36: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 36

database management

Database backup

$ drush sql­dump –result­file=dbname.sql –gzip 

● Save to a file● Compress the dump

Page 37: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 37

database management

Shortcut for accessing variables table:

$ drush variable­get clean_url

Write is supported:

$ drush variable­set clean_url 0

Page 38: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 38

Controlling the cache and cron

Page 39: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 39

controlling the cache and cron

Clear database cache tables

$ drush cc all

Clear a specific cache

● views, menu, block, theme-registry, ...

$ drush cc views

Page 40: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 40

Run cron

$ drush cron

controlling the cache and cron

Page 41: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 41

Archiving and restoring site

Page 42: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 42

archiving and restoring site

Backup the code, files and database in a single archive

$ drush archive­dump –tar­options="­­exclude=.git –exclude=sites/default/files"                                 

Page 43: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 43

archiving and restoring site

Restore an archive

$ drush archive­restore file                                 

Page 44: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 44

Drush aliases

Page 45: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 45

drush aliases

Run drush commands on other local or remote Drupal installations

Site aliases:

● @self: current installation● Custom:

• @dev• @staging• @prod

Page 46: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 46

drush aliases

Shortcut:

$ drush ­­uri=”camp2013.drushdemo.staging” ­­root=”/home/manu/dev/drupal/camp2013/staging/src/drupal” status

becomes:

$ drush @dev status

Page 47: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 47

<?php

/*** Drush aliases definition */

$aliases['staging'] = array('root' => '/var/www/staging','uri' => 'staging.bibliopoly.sfl.net','remote-host' =>'staging.bibliopoly.sfl.net','remote-user' => 'webmaster', 'path-aliases' => array(

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

);

$aliases['prod'] = array('root' => '/var/www/prod','uri' => 'live.bibliopoly.sfl.net','remote-host' =>'live.bibliopoly.sfl.net','remote-user' => 'webmaster', 'path-aliases' => array(

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

);

drush aliases

Page 48: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 48

drush status == drush @self status

The simplest example:

$ drush @prod status

More advanced:

$ drush sql­sync @prod @dev$ drush rsync @staging @prod$ drush rsync @prod:%files        @staging:%files

drush aliases

Page 50: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 50

Third-party integration

Page 51: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 51

Third-party integration

3 easy steps to create a custom drush command:

● Create a command file called COMMANDFILE.drush.inc

● Implement the function COMMANDFILE_drush_command()

● Implement the functions that your command will call

Page 52: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 52

Third-party integration

Drush searches for commandfiles in the following location:

● The “/path/to/drush/command” folder● Folders listed in the “include” folder● The system-wide drush commands folder, e.g.

/usr/share/drush/commands● The ".drush" folder in the user's HOME folder● /drush and /sites/all/drush in the current Drupal

installation● All enabled modules in the current Drupal installation

Page 53: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 53

Third-party integration

Complete documentation:

$ drush topic docs­commands

Page 54: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 54

Third-party integration

Custom module integration

● Create a file called MODULENAME.drush.inc in the module directory

● Add the reference in MODULENAME.info• files[] = MODULENAME.drush.inc

● Implement hook_drush_command()• MODULENAME_drush_command()

Page 55: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 55

Third-party integration

devel_generate module

● Generate content, users, terms, ... views module

● Enable, disable, list, revert views, ... features module

● Create, export, revert features, ... coder_review module

● Run code reviews

Page 56: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 56

Example: deploying a site

Page 57: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 57

Example: deploying a site

(1) Put production site in maintenance

(2) Sync files from the staging env to production

(3) Run any pending database updates on the production site

(4) Clear the cache on production

(5) Put production site back online

Page 58: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 58

Example: deploying a site

The perfect setup:

Continuous-integration server: Jenkins Source code management: git A development environment A staging environment A production environment

Page 59: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 59

Example: deploying a site

Source code changes are pushed from dev into git repository

Jenkins polls the git repository for new commits

Jenkins checkout latest revision

Jenkins runs automatic tests

Jenkins deploys latest revision to the staging environment if tests passed

Page 60: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 60

$ drush ­­yes @prod vset maintenance_mode 1

$ drush ­­yes rsync –verbose @staging @prod

$ drush ­­yes @prod updatedb

$ drush @prod clear­cache all

$ drush ­­yes @prod vset maintenance_mode 0

Example: deploying a site

Page 61: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 61

Code synchronisation:

✗ @prod -> @staging✔ @staging -> @prod

Database synchronisation:

✗ @staging -> @prod✔ @prod -> @staging

Files synchronisation:

✗ @staging -> @prod✔ @prod -> @staging

Example: deploying a site

Page 62: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 62

Thank you :-)

And don't forget to get a badge ...

Page 63: Gain productivity with Drush (Drupal Camp, Montreal 2013)

Gain productivity with Drush - Emmanuel Milou 63