Your SlideShare is downloading. ×
Optimize Site Deployments with Drush (DrupalCamp WNY 2011)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Optimize Site Deployments with Drush (DrupalCamp WNY 2011)

2,808
views

Published on

When a site goes live, are you crossing your fingers or are you confident that everything is configured? Are you looking to manage and optimize site deployments like any other operational process? Do …

When a site goes live, are you crossing your fingers or are you confident that everything is configured? Are you looking to manage and optimize site deployments like any other operational process? Do you find it impossible to create development, test and production environments that act the same every time? Do you have a custom set of modules or configurations that you rely on for all your sites?

This session will teach you how to optimize your site deployments with open tools such as drush, drush make, features, leveraging software versioning systems such as subversion and git. Beyond these projects, the session will train you to develop your own custom modules for consistent and precise deployments including variables, users, content types, nodes, imagecache presets, menus, blocks, theme configuration, and more.

Using these techniques you can automate and optimize your deployment procedures, giving you technical flexibility and saving valuable time.

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,808
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
27
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • PHP 4 & 5 Senior developer, development manager and systems administrator Started using Drupal for sites on the side
  • This presentation is a result of working on multiple sites and trying to reign in both the entropy associated with deployments and out of frustration from having to keep reconfiguring details. The script examples I will be showing use bash, but you can probably do something very similar with Windows if you were so inclined. There are three primary modules that I will highlight today; drush, drush make, and features.   The features module is an extremely powerful tool, but it is in no means the final solution.  I'll explore both the good and the bad of features.   There's many ways to deploy sites; I'll discuss the way I currently do it based on my experience, both working with a in-house, distributed and one-person team. No size fits all, some sizes fit most, and every client is unique.  With that said, I hope that these strategies can be incorporated into your deployments and make your life easier.
  • When you deploy a site, are you crossing your fingers?  Do you know exactly what's going to happen?  Did you remember every last variable, setting, configuration, module and theme? Leverage software versioning systems to store your configurations.  How many people here already use a system like SVN or git? Separate configurations for development, staging and production You can track how long it takes to do things, document why something was done, and constantly improve the process
  • Drush is a back end tool - a command line shell and scripting interface for Drupal. Easy, fast interface for administrative tasks Originally designed for Drupal 4, it has been maintained and expanded to support Drupal 5, 6 and 7 Many modules include support for Drush This is an example of the status command
  • Download, enable, disable modules Create, block or cancel users, set passwords, add or remove roles Clear cache, flush images, index search items, update database... Very useful for building custom modules that configure your site Excellent for debugging Notice how it also automatically downloaded the dependency, FirePHP
  • drush make is an extension that uses a parsed configuration to create a ready-to-use drupal site, downloading from sources as needed Core and contrib from Drupal.org Checking out code from CVS, SVN, git, and bzr repos Get archives for libraries Does not require database It can play nice with installation profiles, but I have not had a need to develop my own using this strategy. It is compatible with Acquia Drupal and Pressflow.   Using drush, you can export a drush make configuration from your existing site.  drush generate -makefile   The website, drushmake.me is a graphical user interface that generates drush make files
  • Step 1- get the files using drush make.  It uses a configuration file; this is same for all environments. Step 2 - Install and configure.  You can specify a particular configuration specific to an environment Step 3 - Enable modules using drush
  • Much like many of you, I have to deal with a large number of sites on a regular basis.  With that comes the overhead of keeping track of configurations, variables, tweaks and the such. In response, I developed the Drupal Deployment Skeleton to facilitate the rapid deployment and development of commercial sites.  I've open-sourced it to go along with this session so you can leverage it to allow you to create your own scriptable deployments. You must customize these scripts to your environment to be able to use it; this is not a turnkey package.   drush.*.php - Site configurations for drush drush.make - drush make configuration config.ini - For each environment, defines where config is and where the site will go install.sh - Performs the actual installation, similar to simplest example setup.sh - Gets path, loads configuration, verifies configuration rebuild.sh - After prompt, destroys database and removes site, then reinstalls
  • Features is a module that captures and manages a collection of Drupal entities It includes both a graphical user interface and API Bundles exportable components into a single module Very Easy to use
  • Within your site: Site content Content types Users Roles Module dependencies imagecache presets Menus Permissions   Using drush :   List all available features Update code of existing feature to include DB changes - drush fu Revert to feature defaults Show a diff  
  • Publishing preference, for example - told it not to publish on the front page, does it anyway You have to keep going over the same changes time and time again.  Strongarm exposes some variables, but not all. Themes and blocks have to be manually changed and updated. It's a blunt force instrument, and therefore doesn't handle logic or precedence, like do not do X if Y exists   It's a great tool, but if you can't do it in the GUI, your hands are going to get dirty. Fortunately, there is another way.
  • Beyond the site administrator - the first user, you often will have roles that you want to place your client's users in. For brochure websites in particular, I disable the login block.  Depending on the design needs of the client, I will also remove the "Powered by Drupal" block. While you want your client to maintain their own content, often you have static content, pages usually, that you want to populate the site with. Content Construction Kit is a great and easy way to create new content types; you can export them with the UI and import them programatically. Do you use taxonomies to categorize your content?  You can easily create vocabularies and terms out of the box. Leverage drush's vget, variable get command to keep track of before and after configuration changes with a quick diff Create WYSIWYG profiles - use them across multiple sites! Imagecache profiles Rather than making you suffer through a bunch of dry examples, they are at the end of the presentation and you can download the slides at your leisure. Store your module in your favorite repository
  • This is one I learned from hard experience - Some operations require a user to be associated, so enable your custom module separately, explicitly specifying UID 1.
  • Even if you're a one man shop Backup - especially in production, before making any changes Tiered: Development - working environment for individual developers or small teams.  Can also include integration, where the entire team's work is combined and validated. Staging - identical to production as possible, simulating actual release.  Also good for demonstration and training. Production - Public facing deployment. Tiering - this can dovetail into a larger discussion of software versioning and branching  Make sure everything works the way you want it to You ultimately want someone else to be able to do this for you
  • The Drush manual is a bit overwhelming, but just search in the page and you'll get the hang of it. Drush make is also well documented; their examples are well worth looking at. What exactly are you trying to accomplish?  Are you putting a skeleton in place for your client to fill, or do you have all content already? Cluster it by purpose.Ex: I need imagecache, galleria, cck, file, image, and jquery_update for an image gallery. Get files, install and configure, enable modules
  • Transcript

    • 1. Optimize Site Deployments with Drush Friday, 2011.10.14 Jon Peck, Owner, FluxSauce.com Audience: Intermediate. Revision 1.  Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License .
    • 2. Who am I?
        • Jon Peck
        • Founded web application development firm FluxSauce
        • Zend Certified PHP Engineer
        • Web development since 1998
        • Used Drupal for the last three years
        • Worked at ProZ.com for four
    • 3. Overview
        • Why should you script site deployments?
        • What are drush and drush make , and how can you use them?
        • What is features , and does fit your needs?
        • What are some recommended site deployment strategies?
        • How can you develop your own strategy?
    • 4. Why should I script site deployments?
        • Reliability
        • Track changes over time (including blame)
        • Support multiple tiers
        • Manage and optimize your operations
      "Aaahhh!!!", CC BY 2.0 by Evil Erin @ flickr
    • 5. What is drush ?
      • drush is a command line shell and scripting interface for Drupal.
      Site installation status report using drush.
    • 6. What can you use drush for?
        • Manage modules
        • Manage users
        • Execute Drupal commands
        • Manipulate and extract site variables
        • Execute MySQL queries using Drupal config
      Downloading and installing devel in 1.359 seconds.
    • 7. What is drush make ? A standard recipe for downloading everything you need for a site installation. "Mom's new pumpkin pie recipe with gingersnaps", CC BY-SA 2.0 by Ann Larie Valentine @ flickr
    • 8. Scripting a deployment - simplest
      • 1. Get the files ( drush make )
      •  
      • drush -y make --no-patch-txt "PATH/drush.make" FOLDER
      2. Install and configure ( drush )   cd FOLDER drush -y site-install --config="PATH/drushrc .ENV.php" 3. Enable modules ( drush )   drush -y en   admin_menu   ctools ...
    • 9. Scripting a deployment - flexible
      • /projects/NAME/config - stored in repository
      •  
        • drush.*.php
        • drush.make
        • config.ini
        • install.sh
        • setup.sh
        • rebuild.sh
      • Drupal Deployment Skeleton (MIT license)
        • http://goo.gl/09JkE
        • https://github.com/fluxsauce/DrupalDeploySkel
    • 10. What is features ?
    • 11. What can you use features for?
        • Within your site
        • Using drush
    • 12. What are features' limitations?
        • Imperfect exports
        • No site variables without third-party modules
        • No theme, block support
        • No logic
        • If you can't do it in the GUI...
      "40+296 Hello?", CC BY 2.0 by Bark @ flickr
    • 13. Use a custom module for complete control.
      • With hook_install( ), you can programmatically:
      •  
        • Create users, roles, permissions
        • Create and hide blocks
        • Create content (nodes)
        • Import content types / CCK
        • Create vocabularies and terms
        • Set site variables ( drush vget )
    • 14. Custom module gotchas
        • Clear all caches after installing your custom module
      •  
      • drush cc all
        • Custom modules used like this need a user associated
      •  
      • drush -u 1 -y en custom_module
        • Changes and updates need to go in hook_update_N within your module
    • 15. Deployment strategies
        • Store everything in a versioning system
        • Always backup both your scripts and database first!
        • Tiered development strategy
        • Unique database credentials for each tier
        • When in doubt, rebuild on dev first
        • Instead of repeating steps, do it in code
        • Document the process
    • 16. How can I do this?
        • Get drush
        • Get drush_make
        • Define your needs.
        • Build and document your module toolbox
        • Start small, then scale up
    • 17. Questions?
      • "Question mark in Esbjerg", CC BY-SA 2.0 by Alexander Drachmann @ flickr
    • 18. Thanks!
      • drush
        • http://drupal.org/project/drush
      • drush_make
        • http://drupal.org/project/drush_make
      • Drupal Deployment Skeleton (DDS)
        • http://goo.gl/09JkE (gitHub)
      • Drush Make Generator
        • http://drushmake.me
      • permissions_api
        • http://drupal.org/project/permissions_api
      • Traditional Development... Practice for Software Development
        • http://dltj.org/article/software-development-practice/
      • Contact
        • [email_address]
        • @FluxSauce
        • http://fluxsauce.com
    • 19. Appendix - code examples
      • Use these snippets to programmatically create:
        • Creating a user
        • Roles and permissions
        • Manipulating blocks
        • Create content (nodes)
        • CCK Content Types
        • Vocabulary
        • Terms
        • Site variables
        • imagecache presets
    • 20. Creating a user
      • $roles = user_roles();
      • $user_data = array(   'name' => 'steve',   'pass' => 'steve',   'mail' => 'jpeck+steve@fluxsauce.com',   'init' => 'jpeck+steve@fluxsauce.com',   'status' => 1,   'roles' => array(array_search('Manager', $roles) => 1), );
      • $user = user_save('', $user_data);
    • 21. Roles and permissions
      • Requires permissions_api module
      •  
      • // Administrative role $result = permissions_create_role('Manager'); // Permissions $permissions_grant = array(   'access administration menu',   'administer nodes', ...   'administer taxonomy', ); permissions_grant_permissions('Manager', $permissions_grant);
    • 22. Manipulating blocks
      • // Disable "User login" $sql_query  = 'UPDATE {blocks} '; $sql_query .= 'SET status = 0 '; $sql_query .= 'WHERE module = "user" '; $sql_query .= 'AND region = "left" '; $sql_query .= 'AND status = 1 '; $sql_query .= 'AND delta = 0 '; $sql_query .= 'AND theme = "fourseasons" '; $sql_query .= 'LIMIT 1 ';
      • $result = db_query($sql_query);
    • 23. Create content (nodes)
      • if (!function_exists('node_object_prepare')) {   include_once(drupal_get_path('module', 'node') . '/node.pages.inc'); } // Home $node = new stdClass(); $node->type = 'page'; node_object_prepare($node); $node->title = 'An excellent title'; $node->body = '<p>Hello, world!</p>'; $node->teaser = node_teaser($node->body); $node->uid = 1; $node->format = 2; // Full HTML $node->path = 'home'; $node->pathauto_perform_alias = FALSE; node_save($node);
    • 24. CCK Content Types
      • Export the content type using the GUI and paste the content into a file in module/content_types/
      • $filename = drupal_get_path('module', $module_name) . '/content_types/' . $content_type . '.cck'; // Build form state $form_state = array(   'values' => array(     'type_name' => '<create>',     'macro' => implode('', file($filename)),   ), ; drupal_execute('content_copy_import_form', $form_state);
    • 25. Vocabulary
      • module_load_include('module', 'taxonomy', 'taxonomy'); $t = get_t(); // Create the vocabulary programmatically. $vocab = array(   'name' => 'Medium',   'description' => $t('Storage and/or transmission tools used to store and deliver information or data.'),   'help' => '',   'relations' => 0,   'hierarchy' => 0,   'multiple' => 0,   'required' => 1,   'tags' => 0,   'module' => 'taxonomy',   'weight' => 0,   'nodes' => array('portfolio_item' => 1), ); taxonomy_save_vocabulary($vocab);
    • 26. Terms
      • // Get the vid of the vocabulary we just created. $vid = db_result(db_query('SELECT vid FROM {vocabulary} WHERE name = &quot;%s&quot;', 'Medium')); // Create the term programmatically. $terms = array(   'Videos' => 'Full-motion visual presentations of information.', ); foreach ($terms as $name => $definition) {     $term = array(       'name' => $name,       'vid' => $vid,       'description' => $t($definition),     );     taxonomy_save_term($term); }
    • 27. Site variables
      • drush vget --pipe site_frontpage
      • $variables['site_frontpage'] = 'node/1';
      • Then in code...
      • $variables = array();
      • // Front page $variables['site_frontpage'] = 'node/1';
      • foreach ($variables as $name => $value) {   variable_set($name, $value); }
    • 28. imagecache presets
      • External file:
      •  
      • $_presets = array(); $_presets['galleria_gallery'] = array (...);
      • return $_presets;
      • Module:
      • /**  * Implementation of hook_imagecache_default_presets().  */ function MODULE_imagecache_default_presets() {   global $_presets;   $module_name = 'MODULE';   $filename = drupal_get_path('module', $module_name) . '/defaults/imagecache_presets.inc.php';   include_once($filename);   return $_presets; }

    ×