Drush. Secrets come out.

29
Drush. Secrets come out. Alexander Schedrov DrupalCafe 5 , Donetsk, 2013

description

How to use Drush at full power. Как использовать Drush на полную мощность.

Transcript of Drush. Secrets come out.

Page 1: Drush. Secrets come out.

Drush. Secrets come out.

Alexander Schedrov

DrupalCafe №5, Donetsk, 2013

Page 2: Drush. Secrets come out.

DRUPAL SHELL

Page 3: Drush. Secrets come out.

Most helpful commandsCore • cc - clear-cache

• sql-dump

• sql-cli

• make

• updb, up, upc

• vget, vset

• image-flush

• dl, en, dis, pm-uninstall

• upwd

• uli

Contrib Apache Solr

• solr-index, solr-mark-all, solr-delete-index

Registry Rebuild

• registry-rebuild(rr)

Devel

• devel-reinstall (dre)

• fn-hook(fnh, hook)

Localization Update

• l10n-update, l10n-update-refresh, l10n-update-status

Features

• features-list(fl)

• features-diff(fd)

• features-revert(fr)

• etc.

Page 4: Drush. Secrets come out.

Installation on Windows• download drush. Using c:\drush as location in this example

• install gzip, tar, wget command line binaries

• set PATH environment variables

c:\drush;d:\xampp\php\;d:\xampp\mysql\bin\;C:\Program Files\GnuWin32\bin\

• MsysGit

https://drupal.org/node/594744

Page 5: Drush. Secrets come out.

Installation on *nix

• download drush. Using home folder as location in this example

• create symlink

!

https://drupal.org/node/1791676

sudo ln -s ~/drush/drush /usr/local/bin/drush

Page 6: Drush. Secrets come out.

Configuration, code sync, sql sync.

Page 7: Drush. Secrets come out.

aliases.drushrc.php

drush -r ~/Sites/drupal.dev/ status !drush ~/Sites/drupal.dev/#default status !

aliases.drush.php: $aliases['dev'] = array( 'root' => '~/Sites/drupal.dev', ); !!drush @dev status

Page 8: Drush. Secrets come out.

blog.aliases.drushrc.php: $aliases['dev'] = array( 'root' => '~/Sites/blog', 'uri' => 'blog.dev', ); $aliases['live'] = array( 'root' => '~/Sites/blog-live', 'uri' => 'blog-live.dev', ); !drush @blog status drush @blog.dev status drush @blog.live status !$aliases['all-blog'] = array( 'site-list' => array('@dev', '@live'), ); !

drush site-alias(sa)

Page 9: Drush. Secrets come out.

SSHImportant: remote server must have Drush !$aliases['live'] = array( 'remote-host' => 'bitbucket.org', 'remote-user' => 'sanchiz', 'root' => '/var/www', 'uri' => 'default', ); !ssh-keygen ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

equals !drush dl drush_extras drush pushkey @live

Page 10: Drush. Secrets come out.

Remote host with special configuration

!$aliases['live'] = array( 'remote-host' => 'dev.project.de', 'remote-user' => 'dev_alex_sh', 'root' => '/home/dev_alex_sh/www', 'php' => '/usr/bin/php', 'path-aliases' => array( '%drush-script' => '/home/dev_alex_sh/drush/drush.php', ), );

Page 11: Drush. Secrets come out.

Code and database syncIf you do not use Git, this section for you =)

Page 12: Drush. Secrets come out.

Code sync!drush rsync(core-rsync) @live @dev --include-conf !drush rsync @live:sites @dev:sites !$aliases['live'] = array( 'command-specific' => array( 'core-rsync' => array( 'exclude' => '*.sql', 'exclude' => '.svn', 'exclude' => '.git', 'exclude' => "sites/default/files/", ), ), );

Page 13: Drush. Secrets come out.

Sql-syncdrush sql-sync @live @dev !drush sql-sync @live @dev --create-db !!$aliases['live'] = array( 'path-aliases' => array( '%dump-dir' => ‘/home/dev_alex_sh/dump’, ), ); !!

Page 14: Drush. Secrets come out.

Selective syncdrush sql-sync @live @dev --structure-tables-key=common

drushrc.php: !$options['structure-tables']['custom'] = array( 'cache', 'cache_*', 'search_*', 'watchdog' ); !drush sql-sync @live @dev --structure-tables-key=custom --no-cache=true !drush sql-sync @live @dev --sanitize !

emails => user+%uid@localhost

passwords => password

Page 15: Drush. Secrets come out.

Sql-sync without Drush on server

drush @live sql-conf --all --show=passwords !$aliases['live'] = array( 'databases' => array( 'default' => array( 'default' => array( 'driver' => 'mysql', 'username' => 'username', 'password' => 'password', 'port' => '', 'host' => '127.0.0.1', 'database' => 'live_database', ), ), ), ); !

Page 16: Drush. Secrets come out.

$aliases['live'] = array( 'remote-host' => 'dev.project.de', 'remote-user' => 'default_alex_sh', 'root' => '/home/default_alex_sh/www', 'php' => '/usr/bin/php', 'php-options' => '-d error_reporting="E_ALL^E_DEPRECATED"', 'variables' => array('mail_system' => array('default-system' => 'DevelMailLog')), 'databases' => array( 'default' => array( 'default' => array( 'driver' => 'mysql', 'username' => 'username', 'password' => 'password', 'port' => '', 'host' => '127.0.0.1', 'database' => 'live_database', ), ), ), 'path-aliases' => array( '%drush-script' => '/home/default_alex_sh/drush/drush.php', '%files' => '/home/default_alex_sh/www/sites/default/files', '%drush' => '/home/default_alex_sh/drush', '%dump-dir' => '/home/default_alex_sh/www/dump', ), ! 'command-specific' => array( 'core-rsync' => array( 'exclude' => '*.sql', 'exclude' => '.svn', 'exclude' => '.git', 'exclude' => "sites/default/files/", ), ), ! 'ssh-options' => '-p 2222', );

Page 17: Drush. Secrets come out.

Development

Page 18: Drush. Secrets come out.

DRUSH make What can drush make do?

• Download modules

• Clone modules from git

• Apply patches

• Download libraries

• Download core

Page 19: Drush. Secrets come out.

core = 7.x api = 2 !; core projects[drupal][version] = 7.23 !; modules projects[views][version] = 3.7 projects[views][subdir] = "contrib" !projects[views][version] = 3.7 projects[views][subdir] = "contrib" projects[views][patch][] = "https://drupal.org/files/views_join_condition_increment_5.patch" !!projects[views][type] = module projects[views][download][type] = git projects[views][download][revision] = 6920ff71c0bf3a75fc769658ba0dabb42ffcd357 projects[views][download][branch] = 7.x-3.x projects[views][subdir] = "contrib" !; libraries libraries[ckeditor][download][type] = get libraries[ckeditor][download][url] = "http://download.cksource.com/CKEditor/CKEditor/CKEditor 4.3/ckeditor_4.3_full.zip" !libraries[colorbox][download][type] = get libraries[colorbox][download][url] = "https://github.com/jackmoore/colorbox/archive/master.zip"

drush make make_file.make --no-core

Page 20: Drush. Secrets come out.

http://drushmake.me

Page 21: Drush. Secrets come out.

Extensions Drush

• A .drush folder in your HOME folder.

• Anywhere in module

• Drupal's /drush or /sites/all/drush folders.

Page 22: Drush. Secrets come out.

function usertools_drush_command() { $items = array(); ! $items['last-registration'] = array( 'description' => 'Show last registration.', 'arguments' => array( 'role' => 'Specific role.', ), 'options' => array( 'count' => array( 'description' => 'Count rows.', 'example-value' => '5', ), ), 'examples' => array( 'drush utl admin --count=5' => 'Show the last 5 registrations of admin.', ), 'aliases' => array('utl'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, ); ! return $items; } !function usertools_drush_help($section) { switch ($section) { case 'drush:last-registration': return dt('This command will show you last registrations.'); } }

Page 23: Drush. Secrets come out.

function drush_usertools_last_registration_validate($role = 'all') { $count = drush_get_option('count'); if (!empty($count) && !is_numeric($count)) { return drush_set_error('Count should be numeric.'); } } !function drush_usertools_last_registration($role = 'all') { $count = drush_get_option('count'); if (empty($count)) { $count = 10; } ! $query = db_select('users', 'u'); ! // Condition by role. if ($role != 'all') { $rid = drush_usertools_get_role_by_name($role); $query->innerJoin('users_roles', 'ur', 'u.uid = ur.uid'); $query->condition('ur.rid', $rid); } ! $query->fields('u', array('uid', 'name', 'mail', 'access', 'created')); $query->condition('u.uid', 0, '<>'); $query->range(0, $count); $query->orderBy('u.created', 'DESC'); $result = $query->execute(); ! $items[] = array('uid', dt('Name'), dt('email'), dt('Last login')); foreach ($result as $user) { $items[] = array( $user->uid, $user->name, $user->mail, $user->access == 0 ? dt('Never') : format_interval(time() - $user->access) . ' ago', ); } drush_print_table($items, TRUE); }

Page 24: Drush. Secrets come out.

Usagedrush utl --count=5

drush utl administrator --count=10

Page 25: Drush. Secrets come out.

function usertools_drush_command() { $items = array(); $items['wipe-users'] = array( 'description' => 'Wipe user tables.', 'examples' => array( 'drush utw' => 'Wipe users.' ), 'aliases' => array('utw'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, ); return $items; } !function drush_usertools_wipe_users() { if (drush_confirm(dt('You want to delete all users? Are you sure?'))) { $query = db_select('users', 'u'); $query->fields('u', array('uid', 'name')); $query->condition('u.uid', 1, '>'); $result = $query->execute(); foreach ($result as $user) { drush_log(dt('Deleting user') . ' ' . $user->name . '...', 'warning'); user_delete($user->uid); drush_log(dt('Succes deleting') . ' ' . $user->name, 'success'); } } else { drush_user_abort(); } }

Page 26: Drush. Secrets come out.

Usagedrush utw

Page 27: Drush. Secrets come out.

function drush_usertools_disable_role() { $query = db_select('role', 'r'); $query->fields('r', array('rid', ‘name')); $query->condition('r.rid', 1, '<>'); $query->orderBy('r.rid'); $result = $query->execute(); ! $options = array(); foreach ($result as $role) { $options[$role->rid] = array($role->name); } ! $rid = drush_choice($options, dt('Select role:')); ! // SQL queries. ! drush_log('All users of role ' . $options[$rid][0] . ' disabled.', 'success'); } !!!!!!!!!

Page 28: Drush. Secrets come out.

Links

• https://drupal.org/node/594744 - Drush on Windows

• https://drupal.org/node/1791676 - Drush on *nix

• http://drushmake.me - Drush make

• http://drush.ws

• https://gist.github.com/Sanchiz/7627657

Page 29: Drush. Secrets come out.

Thank you!

!

Email: [email protected]

Twitter: @alexschedrov

FB: schedrov

http://sanchiz.net