Gain productivity with Drush (Drupal Camp, Montreal 2013)
-
Upload
emmanuel-milou -
Category
Technology
-
view
230 -
download
1
description
Transcript of Gain productivity with Drush (Drupal Camp, Montreal 2013)
Gain productivity with Drush - Emmanuel Milou 1
Gain productivity with Drush
Gain productivity with Drush - Emmanuel Milou 2
Introduction
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
Gain productivity with Drush - Emmanuel Milou 4
What is drush ?
Gain productivity with Drush - Emmanuel Milou 5
“ Drush (DRUpal Shell) is a command line shell and scripting interface for
Drupal “
what is drush ?
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 ?
Gain productivity with Drush - Emmanuel Milou 7
$ drush [options] command ...
what is drush ?
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 ?
Gain productivity with Drush - Emmanuel Milou 9
Example:
$ drush yes vset maintenance_mode 1$ drush vset yes maintenance_mode 1$ drush yes variableset maintenance_mode 1
what is drush ?
Gain productivity with Drush - Emmanuel Milou 10
The help command
$ drush help
$ drush help [command]
what is drush ?
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 ?
Gain productivity with Drush - Emmanuel Milou 12
Do I need to use drush ?
Gain productivity with Drush - Emmanuel Milou 13
Yes :-)
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
Gain productivity with Drush - Emmanuel Milou 15
Installing drush
Gain productivity with Drush - Emmanuel Milou 16
https://github.com/drush-ops/drush
installing drush
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
Gain productivity with Drush - Emmanuel Milou 18
Example on Ubuntu (need php-pear library)
pear channeldiscover \ pear.drush.orgpear install drush/drushdrush version
installing drush
Gain productivity with Drush - Emmanuel Milou 19
Installing a site and introspection
Gain productivity with Drush - Emmanuel Milou 20
site installation and instrospection
Download the latest stable release:
$ drush pmdownload drupal7.x
Run installation procedure:
$ drush siteinstall dburl=mysql://user:pass@localhost:port/dbname
Gain productivity with Drush - Emmanuel Milou 21
site installation and instrospection
More on site-install command:
$ drush siteinstall [profile] dburl=mysql://user:pass@hostname[:port]/databasesitename=”Dev site”account[email protected]accountname=adminaccountpass=admin
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
Gain productivity with Drush - Emmanuel Milou 23
Downloading and managing modules
Gain productivity with Drush - Emmanuel Milou 24
downloading and managing modules
Available commands to:
● List all modules: $ drush pmlist
● Download a module / theme:$ drush pmdownload <modulename>
● Enable a module / theme:$ drush pmenable <modulename>
Gain productivity with Drush - Emmanuel Milou 25
downloading and managing modules
Installing and enabling a module used to look like this:
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
Gain productivity with Drush - Emmanuel Milou 27
downloading and managing modules
Now with drush, it is:
Gain productivity with Drush - Emmanuel Milou 28
downloading and managing modules
Find the module's exact name
Run:
$ drush dl <modulename>
$ drush enable <modulename>
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.x3.xdev
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
Gain productivity with Drush - Emmanuel Milou 31
downloading and managing modules
Disable a module:
$ drush pmdisable <modulename>
Uninstall a module:
$ drush pmuninstall <modulename>
Gain productivity with Drush - Emmanuel Milou 32
Updating a site
Gain productivity with Drush - Emmanuel Milou 33
updating a site
One of the most tedious task ever ...
Gets a lot easier with drush:
$ drush pmupdate
Core, non-core, modules, security, ...
Gain productivity with Drush - Emmanuel Milou 34
Database management
Gain productivity with Drush - Emmanuel Milou 35
database management
Open a SQL cli, using Drupal's credentials
$ drush sqlcli
Database synchronisation between sites
Run a custom query
$ drush sqlquery “SELECT title from node where nid=1”
Import SQL statements from a file
$ drush sqlquery –file=example.sql
Gain productivity with Drush - Emmanuel Milou 36
database management
Database backup
$ drush sqldump –resultfile=dbname.sql –gzip
● Save to a file● Compress the dump
Gain productivity with Drush - Emmanuel Milou 37
database management
Shortcut for accessing variables table:
$ drush variableget clean_url
Write is supported:
$ drush variableset clean_url 0
Gain productivity with Drush - Emmanuel Milou 38
Controlling the cache and cron
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
Gain productivity with Drush - Emmanuel Milou 40
Run cron
$ drush cron
controlling the cache and cron
Gain productivity with Drush - Emmanuel Milou 41
Archiving and restoring site
Gain productivity with Drush - Emmanuel Milou 42
archiving and restoring site
Backup the code, files and database in a single archive
$ drush archivedump –taroptions="exclude=.git –exclude=sites/default/files"
Gain productivity with Drush - Emmanuel Milou 43
archiving and restoring site
Restore an archive
$ drush archiverestore file
Gain productivity with Drush - Emmanuel Milou 44
Drush aliases
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
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
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
Gain productivity with Drush - Emmanuel Milou 48
drush status == drush @self status
The simplest example:
$ drush @prod status
More advanced:
$ drush sqlsync @prod @dev$ drush rsync @staging @prod$ drush rsync @prod:%files @staging:%files
drush aliases
Gain productivity with Drush - Emmanuel Milou 49
Documentation:
http://drupalcontrib.org/api/drupal/contributions!drush!examples!example.aliases.drushrc.php/7
drush aliases
Gain productivity with Drush - Emmanuel Milou 50
Third-party integration
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
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
Gain productivity with Drush - Emmanuel Milou 53
Third-party integration
Complete documentation:
$ drush topic docscommands
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()
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
Gain productivity with Drush - Emmanuel Milou 56
Example: deploying a site
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
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
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
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 clearcache all
$ drush yes @prod vset maintenance_mode 0
Example: deploying a site
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
Gain productivity with Drush - Emmanuel Milou 62
Thank you :-)
And don't forget to get a badge ...
Gain productivity with Drush - Emmanuel Milou 63