Drupal-Entwicklung mit Drush und Shell-Skripten vereinfachen
Bei der Entwicklung mit mehreren Team-Mitgliedern passiert häufig Neues in der Code-Basis. Einmal muss vielleicht die update.php ausgeführt werden, weil ein Modul in neuerer Version eingesetzt wird, Features müssen bei Änderungen manuell zurückgesetzt werden, ein Feld war vielleicht überflüssig geworden und muss gelöscht werden. Wir lösen dieses Problem mit Update-Scripts. Nach jedem git pull führen die Entwickler einfach die Datei update.sh aus und alles, was getan werden muss, passiert automatisch. Typischerweise ist das das Leeren vom Cache, das Aktivieren von neuen Modulen, das Zurücksetzen von Features. Was auch häufig vorkommt ist das Löschen von Feldern, die nicht mehr benötigt werden oder das Indizieren von Nodes für Solr.
Hier ein paar Beispiele, was man in Kombination mit Drush im Script machen kann:
- Module oder Themes aktivieren und deinstallieren
- Features zurücksetzen
- Den Cache leeren
- Datenbankaktualisierungen durchführen (update.php)
- Inhaltstypen und Felder löschen
- Inhalte mit Apache Solr indizieren
- Alles, was Drush sonst noch kann
Die Skripte liegen in sites/all/scripts. Hier ein einfaches und verständliches Beispiel für eine solche Datei, die in sites/all/scripts liegen kann:
- ################################################################################
- # Shell and drush commands to update the given installation
- #
- # Go to sites/all/scripts (e.g. cd sites/all/scripts) and type "sh update.sh"
- # in your console/shell/Terminal.
- #
- # The commands have to be functional for any case after the initial installation
- # installation. So on any set up after calling that script, the configuration
- # must be the same.
- #
- # For example:
- # ------------
- # When the script enabled a module in an earlier revision and that module shall
- # not be enabled in the current revision, we have to add a "drush dis" and a
- # "drush pm-uninstall" command for that module to this script.
- #
- ################################################################################
- # Change directory to drupal root
- cd ../../..
- # pwd
- # Check drupal status
- drush status
- # Disable these themes to make sure they are never enabled.
- drush --yes pm-disable seven
- drush --yes pm-disable bartik
- # Enable some modules that must be enabled.
- drush --yes pm-enable features
- drush --yes pm-enable strongarm
- # Enable project features.
- drush --yes pm-enable ok_base
- drush --yes pm-enable ok_event
- drush --yes pm-enable ok_i18n
- drush --yes pm-enable ok_menus
- drush --yes pm-enable ok_news
- drush --yes pm-enable ok_page
- drush --yes pm-enable ok_search
- drush --yes pm-enable ok_slideshow
- drush --yes pm-enable ok_sidebar
- drush --yes pm-enable ok_theme
- drush --yes pm-enable ok_user
- drush --yes pm-enable ok_wysiwyg
- # Enable project theme.
- drush --yes pm-enable ourprettytheme
- # Update
- drush --yes updb
- # Disable unused modules and features.
- drush --yes pm-disable features_override
- drush --yes pm-uninstall features_override
- # Remove an old content type and some fields.
- drush --yes php-eval "node_type_delete('page');"
- drush field-delete field_news_tags --bundle=news
- drush field-delete field_news_link --bundle=news
- # Index content for solr
- drush sapi-i ok_sitewide_index 10000 25
- drush sapi-s
- # Revert all features and clear cache.
- drush --yes features-revert-all
- drush cache-clear all
- # Display list of features to check status manually.
- drush features
- # Check for available notification system
- # Checks in preferred order and only uses one: Notification Center, Growl, Audio
- # For OS X 10.8 use terminal notifier "sudo gem install terminal-notifier" http://rubygems.org/gems/terminal-notifier
- # For Growl, download and install growlnotifier http://growl.info/downloads#generaldownloads
- if [ $( find /Library/Ruby/Gems/ -name 'terminal-notifier*' -type d | wc -l) -ge 1 ]; then
- # Notification center output for finished update.sh
- terminal-notifier -message "$USER, the update script run is complete" -title "undpaul Update Script"
- elif [ -f /usr/local/bin/growlnotify ]; then
- # Growl notification
- growlnotify -n "undpaul Update Script" -m "$USER, the update script run is complete"
- elif [ $(command -v say) ]; then
- # Vocal output for finished update.sh
- say "$USER, the update script run is complete"
- fi
Das Skript gibt am Ende eine Benachrichtigung aus und teilt uns mit, wenn es fertig ist. In der Zwischenzeit können wir etwas anders machen.
Außerdem haben wir ein Installations-Skript, mit dem jeder im Team schnell ins Projekt starten kann. Gemeinsam mit dem Update-Skript kann man also jederzeit frisch ins Projekt starten, indem man einfach nur diese beiden Dateien ausführt. Hier ein Beispiel für die Datei install.sh:
- ################################################################################
- # Shell and drush commands to set up (or some part of it) the new site.
- #
- # Go to sites/all/scripts (e.g. cd sites/all/scripts) and type "sh install.sh"
- # in your console/shell/Terminal.
- #
- # The commands are built to run once, e.g. to set some symlinks, but shall be
- # designed to not break other commands on the second run.
- #
- ################################################################################
- # Change directory to drupal root
- cd ../../..
- # Build symlinks for local environment.
- echo "Create symlinks:"
- ln -s .htaccess.local .htaccess
- ln -s settings.local.php sites/default/settings.php
- # Install Drupal
- drush site-install minimal -y
- # Check drupal status
- drush status
- # Create Login
- echo "Use the following link to log in"
- drush uli
Was haltet ihr von unseren Scripts? Verwendet ihr in eurem Team etwas ähnliches?