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: 1################################################################################
2# Shell and drush commands to update the given installation
3#
4# Go to sites/all/scripts (e.g. cd sites/all/scripts) and type "sh update.sh"
5# in your console/shell/Terminal.
6#
7# The commands have to be functional for any case after the initial installation
8# installation. So on any set up after calling that script, the configuration
9# must be the same.
10#
11# For example:
12# ------------
13# When the script enabled a module in an earlier revision and that module shall
14# not be enabled in the current revision, we have to add a "drush dis" and a
15# "drush pm-uninstall" command for that module to this script.
16#
17################################################################################
18
19# Change directory to drupal root
20cd ../../..
21# pwd
22
23# Check drupal status
24drush status
25
26# Disable these themes to make sure they are never enabled.
27drush --yes pm-disable seven
28drush --yes pm-disable bartik
29
30# Enable some modules that must be enabled.
31drush --yes pm-enable features
32drush --yes pm-enable strongarm
33
34# Enable project features.
35drush --yes pm-enable ok_base
36drush --yes pm-enable ok_event
37drush --yes pm-enable ok_i18n
38drush --yes pm-enable ok_menus
39drush --yes pm-enable ok_news
40drush --yes pm-enable ok_page
41drush --yes pm-enable ok_search
42drush --yes pm-enable ok_slideshow
43drush --yes pm-enable ok_sidebar
44drush --yes pm-enable ok_theme
45drush --yes pm-enable ok_user
46drush --yes pm-enable ok_wysiwyg
47
48# Enable project theme.
49drush --yes pm-enable ourprettytheme
50
51# Update
52drush --yes updb
53
54# Disable unused modules and features.
55drush --yes pm-disable features_override
56drush --yes pm-uninstall features_override
57
58# Remove an old content type and some fields.
59drush --yes php-eval "node_type_delete('page');"
60drush field-delete field_news_tags --bundle=news
61drush field-delete field_news_link --bundle=news
62
63# Index content for solr
64drush sapi-i ok_sitewide_index 10000 25
65drush sapi-s
66
67# Revert all features and clear cache.
68drush --yes features-revert-all
69drush cache-clear all
70
71# Display list of features to check status manually.
72drush features
73
74# Check for available notification system
75# Checks in preferred order and only uses one: Notification Center, Growl, Audio
76# For OS X 10.8 use terminal notifier "sudo gem install terminal-notifier" http://rubygems.org/gems/terminal-notifier
77# For Growl, download and install growlnotifier http://growl.info/downloads#generaldownloads
78if [ $( find /Library/Ruby/Gems/ -name 'terminal-notifier*' -type d | wc -l) -ge 1 ]; then
79 # Notification center output for finished update.sh
80 terminal-notifier -message "$USER, the update script run is complete" -title "undpaul Update Script"
81elif [ -f /usr/local/bin/growlnotify ]; then
82 # Growl notification
83 growlnotify -n "undpaul Update Script" -m "$USER, the update script run is complete"
84elif [ $(command -v say) ]; then
85 # Vocal output for finished update.sh
86 say "$USER, the update script run is complete"
87fi
88
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: 1################################################################################
2# Shell and drush commands to set up (or some part of it) the new site.
3#
4# Go to sites/all/scripts (e.g. cd sites/all/scripts) and type "sh install.sh"
5# in your console/shell/Terminal.
6#
7# The commands are built to run once, e.g. to set some symlinks, but shall be
8# designed to not break other commands on the second run.
9#
10################################################################################
11
12# Change directory to drupal root
13cd ../../..
14
15# Build symlinks for local environment.
16echo "Create symlinks:"
17ln -s .htaccess.local .htaccess
18ln -s settings.local.php sites/default/settings.php
19
20# Install Drupal
21drush site-install minimal -y
22
23# Check drupal status
24drush status
25
26# Create Login
27echo "Use the following link to log in"
28drush uli
29
Was haltet ihr von unseren Scripts? Verwendet ihr in eurem Team etwas ähnliches?