2017年5月12日金曜日

Drupalを管理するのにDrushてのを使ってみた

職場でDrupalとWordpressを使っていて、Wordpressの自動アップデートがうらやましくなって調べたところ、Drupalだとrushを使えばできるってことだったのでやってみた。



対象の環境はこんな感じ。

  • FreeBSD 11.0
  • Nginx 1.10
  • PHP 5.6
  • Drupal 7.X

まずはdrushを持ってくる

drushはDrupal公式にも書いてあるようにGitHubにリポジトリが移動したということなので、GitHubから7.X用のブランチを指定してダウンロードする。

普通に公式にアクセスするとlatestブランチが表示される(8.X用)が、今欲しいのは7.X用なので、Branchドロップダウンから7.xを選択し、Clone or downloadからzipをダウンロードし、サーバ上の適当なディレクトリに展開する。

Composerのインストール

drushを実行するにはPHPの依存性管理ツールであるComposerが必要。
で、さらに、composerの実行にはPHPのOpenSSLモジュールが必要になるので、入っていないなら入れておく。

準備ができたら、インストール方法に記載されているようにインストールスクリプトをダウンロードして実行する。

% curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer (version 1.4.1) successfully installed to: /usr/home/yatsu/composer.phar
Use it: php composer.phar

ここまでやって気が付いたけど、pkgにphp-composer-1.4.1てのがあるのでこっちを使うことにして、自分で入れたcomposerはカレントディレクトリ以下にしか影響が出ないので、さくっと忘れることに。

drushが依存するパッケージのインストール

composerを使ってdrushが依存するパッケージをインストールする。

drushのzip内にcomposer用のcomposer.jsonがあるので、特に何か設定ファイルをゴリゴリする必要はない。

また、composerはデフォルトでカレントディレクトリの下にvendorてディレクトリを作り、この下に依存パッケージを入れていくので、他のディレクトリが汚れることもなく安心。

% pwd
/home/hoge/drupal/d7/drush-7.x
% ls
commands/               drush*                  drush.complete.sh*      includes/               README.md
composer.json           drush_logo-black.png    drush.info              lib/                    tests/
CONTRIBUTING.md         drush.api.php           drush.php*              misc/                   unish.sh*
docs/                   drush.bat*              examples/               mkdocs.yml
%
% composer.phar install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 27 installs, 0 updates, 0 removals
  - Installing pear/console_table (v1.2.1): Downloading (100%)
  - Installing symfony/process (v2.4.5): Downloading (100%)
  - Installing psr/log (1.0.2): Downloading (100%)
  - Installing d11wtq/boris (v1.0.8): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.3.0): Downloading (100%)
  - Installing symfony/var-dumper (v2.8.20): Downloading (100%)
  - Installing symfony/yaml (v2.8.20): Downloading (100%)
  - Installing sebastian/version (1.0.6): Downloading (100%)
  - Installing sebastian/global-state (1.1.1): Downloading (100%)
  - Installing sebastian/recursion-context (1.0.5): Downloading (100%)
  - Installing sebastian/exporter (1.2.2): Downloading (100%)
  - Installing sebastian/environment (1.3.8): Downloading (100%)
  - Installing sebastian/diff (1.4.1): Downloading (100%)
  - Installing sebastian/comparator (1.2.4): Downloading (100%)
  - Installing doctrine/instantiator (1.0.5): Downloading (100%)
  - Installing phpunit/php-text-template (1.2.1): Downloading (100%)
  - Installing phpunit/phpunit-mock-objects (2.3.8): Downloading (100%)
  - Installing phpunit/php-timer (1.0.9): Downloading (100%)
  - Installing phpunit/php-file-iterator (1.4.2): Downloading (100%)
  - Installing phpunit/php-token-stream (1.4.11): Downloading (100%)
  - Installing phpunit/php-code-coverage (2.2.4): Downloading (100%)
  - Installing webmozart/assert (1.2.0): Downloading (100%)
  - Installing phpdocumentor/reflection-common (1.0): Downloading (100%)
  - Installing phpdocumentor/type-resolver (0.2.1): Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (3.1.1): Downloading (100%)
  - Installing phpspec/prophecy (v1.7.0): Downloading (100%)
  - Installing phpunit/phpunit (4.8.35): Downloading (100%)
pear/console_table suggests installing pear/Console_Color2 (>=0.1.2)
d11wtq/boris suggests installing ext-readline (*)
d11wtq/boris suggests installing ext-pcntl (*)
symfony/var-dumper suggests installing ext-symfony_debug ()
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.2.1)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files
% ls
commands/               drush*                  drush.info              misc/                   vendor/
composer.json           drush_logo-black.png    drush.php*              mkdocs.yml
composer.lock           drush.api.php           examples/               README.md
CONTRIBUTING.md         drush.bat*              includes/               tests/
docs/                   drush.complete.sh*      lib/                    unish.sh*

drushを使ってみる

やっと準備が整ったのでdrushを使ってみる。

この辺を読めば、なにができるか一通りわかりそう。

元々やりたかったのはDrupalのアップデートなので、今回はこれをやってみる。

まず、ステータス情報を更新。

% ./drush -r /usr/home/www/www.example.jp rf
Refreshing update status information ...
Done.

ステータスの確認。

% ./drush -r /usr/home/www/www.example.jp status
 Drupal version                  :  7.53
 Site URI                        :  http://default
 Database driver                 :  mysql
 Database hostname               :  localhost
 Database username               :  foo
 Database name                   :  bar
 Drupal bootstrap                :  Successful
 Drupal user                     :
 Default theme                   :  skeletontheme
 Administration theme            :  seven
 PHP executable                  :  /usr/local/bin/php
 PHP configuration               :  /usr/local/etc/php.ini
 PHP OS                          :  FreeBSD
 Drush script                    :  /usr/home/hoge/drupal/d7/drush-7.x/drush.php
 Drush version                   :  7.4-dev
 Drush temp directory            :  /tmp
 Drush configuration             :
 Drush alias files               :
 Install profile                 :  Japanese
 Drupal root                     :  /usr/home/www/www.example.jp
 Site path                       :  sites/default
 File directory path             :  sites/default/files
 Temporary file directory path   :  /usr/home/www/drupal.tmp

最新の7.54にしないとダメだよねー、ということで、-sを付けて、一旦ドライランしてみる。

% ./drush -s -r /usr/home/www/www.example.jp up
Executing: mysql --defaults-extra-file=/tmp/drush_gEAFWi --database=library --host=localhost --silent  < /tmp/drush_xyrCU5
Executing: wget --version
Simulating backend invoke: /usr/local/bin/php /usr/home/hoge/drupal/d7/drush-7.x/drush.php --php=/usr/local/bin/php  --backend=2 --root=/usr/home/www/www.example.jp --uri=http://default  pm-updatestatus 2>&1
pm-updatestatus failed.

wget使うのかな? 入れてないから入れておく。
最後にしくじってるのは書き込み権限ないからかな? バックアップあるから気にしないで実行しちゃう。

% sudo ./drush -r /usr/home/www/www.example.jp up
Update information last refreshed: 水, 2017-05-10 21:11
 Name                           Installed Version  Proposed version  Message
 Drupal                         7.53               7.54              アップデートが利用可能です
 jQuery Update (jquery_update)  7.x-3.0-alpha3     7.x-3.0-alpha5    アップデートが利用可能です
 Bootstrap (bootstrap)          7.x-3.10           7.x-3.13          アップデートが利用可能です


NOTE: A code update for the Drupal core is available.
Drupal core will be updated after all of the non-core projects are updated.

Code updates will be made to the following projects: jQuery Update [jquery_update-7.x-3.0-alpha5], Bootstrap [bootstrap-7.x-3.13]

Note: A backup of your project will be stored to backups directory if it is not managed by a supported version control system.
Note: If you have made any modifications to any file that belongs to one of these projects, you will have to migrate those modifications after updating.
Do you really want to continue with the update process? (y/n): y
Project jquery_update was updated successfully. Installed version is now 7.x-3.0-alpha5.
Backups were saved into the directory /home/hoge/drush-backups/bar/20170510121112/modules/jquery_update.[ok]
Project bootstrap was updated successfully. Installed version is now 7.x-3.13.
Backups were saved into the directory /home/hoge/drush-backups/bar/20170510121112/themes/bootstrap.     [ok]

Code updates will be made to drupal core.
WARNING:  Updating core will discard any modifications made to Drupal core files, most noteworthy among these are .htaccess and robots.txt.  If you have made any modifications to these files, please back them up before updating so that you can re-create your modifications in the updated version of the file.
Note: Updating core can potentially break your site. It is NOT recommended to update production sites without prior testing.

Do you really want to continue? (y/n): y
Project drupal was updated successfully. Installed version is now 7.54.
Backups were saved into the directory /home/hoge/drush-backups/library/20170510121112/drupal.               [ok]
The following module is missing from the file system: <em class="placeholder">Japanese</em>. For information [warning]
about how to fix this, see <a href="https://www.drupal.org/node/2487215">the documentation page</a>.
bootstrap.inc:1138
No database updates required
[success]
'all' cache was cleared.
[success]
Finished performing updates.

うまくいったかな。
最後のwarningはほっといても問題なさそうなので、そのうち対処しよう。

アップデートできたようなので、Drupalが持っているキャッシュをクリアしておく。

% ./drush -r /usr/home/www/www.example.jp cc all
'all' cache was cleared.

再度ステータスを更新。

% ./drush -r /usr/home/www/www.example.jp rf
Refreshing update status information ...
Done.

ステータスの確認。

% ./drush -r /usr/home/www/www.example.jp status
 Drupal version                  :  7.54
 Site URI                        :  http://default
 Database driver                 :  mysql
 Database hostname               :  localhost
 Database username               :  foo
 Database name                   :  bar
 Drupal bootstrap                :  Successful
 Drupal user                     :
 Default theme                   :  skeletontheme
 Administration theme            :  seven
 PHP executable                  :  /usr/local/bin/php
 PHP configuration               :  /usr/local/etc/php.ini
 PHP OS                          :  FreeBSD
 Drush script                    :  /usr/home/hoge/drupal/d7/drush-7.x/drush.php
 Drush version                   :  7.4-dev
 Drush temp directory            :  /tmp
 Drush configuration             :
 Drush alias files               :
 Install profile                 :  Japanese
 Drupal root                     :  /usr/home/www/www.example.jp
 Site path                       :  sites/default
 File directory path             :  sites/default/files
 Temporary file directory path   :  /usr/home/www/drupal.tmp

大丈夫そう。Webの管理画面からみてもアップデートされているのでこれでOKでしょ。