この記事はDrupal Advent Calendar 2015の14日目の記事です。
11月19日にリリースされたばかりのDrupal8は、GUIでの画面操作やディレクトリ構成も刷新されていて、これまで以上に利便性が向上しています。
また、DrupalをCUIから操作するDrushもD8に対応しています。
コマンドラインで実行可能なDrushを既に利用して、データ管理の自動化を行っている方も居ると思いますが、改めてこの記事ではDrupal8@CentOS7の動作環境下にcomposer経由でDrushの8.0.1を入れ、実際動作するまでを記して行きたいと思います。
Drupal8@CentOS7の構築方法については、Drupal Advent Calendar 2015の4日目の記事として自分が投稿した記事「CentOS7に D8を入れてみた」
http://qiita.com/m_46/items/2acd238253e9fb1a45fd
をご参考頂ければと思います。
以後は、構築が完了したCentOS7@Drupal8の環境下でのdrush導入手順です。
#composerの導入
まずはcurl経由でcomposerを導入します。
[root@cent7d8 ~]# curl -sS https://getcomposer.org/installer | php
動作確認をします。
[root@cent7d8 ~]# php composer.phar
composerが展開されるディレクトリを作成します。
[root@cent7d8 ~]# mkdir /root/.composer
composer.pharをディレクトリ内に格納します。
[root@cent7d8 ~]# mv ./composer.phar .composer/
環境変数を設定します。
alias composer='php /root/.composer/composer.phar'
その後、「.bash_profile」に記述した環境変数を適用します。
[root@cent7d8 ~]# source ~/.bash_profile
helpコマンド等を実行してみて問題無く実行できたらcomposerのインストールは完了です。
[root@cent7d8 ~]# composer help
#composer経由のdrushの導入
composer経由でdrush8をインストールします。
[root@cent7d8 ~]# composer global require drush/drush:8.*
Changed current directory to /root/.composer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing pear/console_table (1.2.1)
Downloading: 100%
- Installing symfony/polyfill-mbstring (v1.0.0)
Downloading: 100%
- Installing symfony/var-dumper (v2.8.0)
Downloading: 100%
- Installing symfony/yaml (v2.7.7)
Downloading: 100%
- Installing jakub-onderka/php-console-color (0.1)
Downloading: 100%
- Installing jakub-onderka/php-console-highlighter (v0.3.2)
Downloading: 100%
- Installing dnoegel/php-xdg-base-dir (0.1)
Downloading: 100%
- Installing nikic/php-parser (v2.0.0)
Downloading: 100%
- Installing symfony/console (v3.0.0)
Downloading: 100%
- Installing psy/psysh (v0.6.1)
Downloading: 100%
- Installing drush/drush (8.0.1)
Downloading: 100%
pear/console_table suggests installing pear/Console_Color2 (>=0.1.2)
symfony/var-dumper suggests installing ext-symfony_debug ()
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/process ()
symfony/console suggests installing psr/log (For using the console logger)
psy/psysh suggests installing ext-posix (If you have PCNTL, you'll want the POSIX extension as well.)
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
drush/drush suggests installing drush/config-extra (Provides configuration workflow commands, such as config-merge.)
Writing lock file
Generating autoload files
ここまで画面が出ればインストールが完了です。
次にdrushの環境変数を登録します。
PATH="/root/.composer/vendor/bin:$PATH"
再度環境変数を適用します。
[root@cent7d8 ~]# source ~/.bash_profile
drushのバージョン確認をします。8.0.1がcomposer経由でインストール出来る現時点での最新版となります。
[root@cent7d8 ~]# drush --version
Drush Version : 8.0.1
#drushコマンドの実行
早速drushコマンドを実行してみます。
[root@cent7d8 ~]# drush
Execute a drush command. Run `drush help [command]` to view command-specific help. Run `drush topic` to read even more documentation.
Global options (see `drush topic core-global-options` for the full list):
-d, --debug Display even more information, including internal messages.
-h, --help This help system.
-n, --no Assume 'no' as answer to all prompts.
-r <path>, --root=<path> Drupal root directory to use (default: current directory).
-s, --simulate Simulate all relevant actions (don't actually change the system).
-l <http://example.com:8888>, URI of the drupal site to use (only needed in multisite environments or when running on an alternate
--uri=<http://example.com:8888> port).
-v, --verbose Display extra information about the command.
-y, --yes Assume 'yes' as answer to all prompts.
Core Drush commands: (core)
archive-dump (ard, Backup your code, files, and database into a single file.
archive-backup, arb)
archive-restore Expand a site archive into a Drupal web site.
(arr)
browse Display a link to a given path or open link in a browser.
core-cli (php) Open an interactive shell on a Drupal site.
core-config (conf, Edit drushrc, site alias, and Drupal settings.php files.
config)
core-cron (cron) Run all cron hooks in all active modules for specified site.
core-execute (exec, Execute a shell command. Usually used with a site alias.
execute)
core-init (init) Enrich the bash startup file with completion and aliases. Copy .drushrc file to ~/.drush
core-quick-drupal Download, install, serve and login to Drupal with minimal configuration and dependencies.
(qd, cutie)
core-requirements Provides information about things that may be wrong in your Drupal installation, if any.
(status-report, rq)
core-rsync (rsync) Rsync the Drupal tree to/from another server using ssh.
core-status (status, Provides a birds-eye view of the current Drupal installation, if any.
st)
core-topic (topic) Read detailed documentation on a given topic.
drupal-directory Return the filesystem path for modules/themes and other key folders.
(dd)
entity-updates Apply pending entity schema updates.
(entup)
help Print this help message. See `drush help help` for more options.
image-derive (id) Create an image derivative.
image-flush (if) Flush all derived images for a given style.
php-eval (eval, ev) Evaluate arbitrary php code after bootstrapping Drupal (if available).
php-script (scr) Run php script(s).
queue-list Returns a list of all defined queues
queue-run Run a specific queue by name
shell-alias (sha) Print all known shell alias records.
site-alias (sa) Print site alias records for all known site aliases and local sites.
site-install (si) Install Drupal along with modules/themes/configuration using the specified install profile.
site-set (use) Set a site alias to work on that will persist for the current session.
site-ssh (ssh) Connect to a Drupal site's server via SSH for an interactive session or to run a shell command
twig-compile (twigc) Compile all Twig template(s).
updatedb (updb) Apply any database updates required (as with running update.php).
updatedb-status List any pending database updates.
(updbst)
variable-delete Delete a variable.
(vdel)
variable-get (vget) Get a list of some or all site variables and values.
variable-set (vset) Set a variable.
version Show drush version.
Cache commands: (cache)
cache-clear (cc) Clear a specific cache, or all drupal caches.
cache-get (cg) Fetch a cached object and display it.
cache-rebuild (cr, Rebuild a Drupal 8 site and clear all its caches.
rebuild)
cache-set (cs) Cache an object expressed in JSON or var_export() format.
Config commands: (config)
config-edit (cedit) Open a config file in a text editor. Edits are imported into active configration after closing editor.
config-export (cex) Export configuration to a directory.
config-get (cget) Display a config value, or a whole configuration object.
config-import (cim) Import config from a config directory.
config-list (cli) List config names by prefix.
config-pull (cpull) Export and transfer config from one environment to another.
config-set (cset) Set config value directly.
Field commands: (field)
field-clone Clone a field and all its instances.
field-create Create fields and instances. Returns urls for field editing.
field-delete Delete a field and its instances.
field-info View information about fields, field_types, and widgets.
field-update Return URL for field editing web page.
Make commands: (make)
make Turns a makefile into a working Drupal codebase.
make-convert Convert a legacy makefile into YAML format.
make-generate Generate a makefile from the current Drupal site.
(generate-makefile)
make-lock Process a makefile and outputs an equivalent makefile with projects version *resolved*. Respects pinned versions.
make-update Process a makefile and outputs an equivalent makefile with projects version resolved to latest available.
Project manager commands: (pm)
pm-disable (dis) Disable one or more extensions (modules or themes).
pm-download (dl) Download projects from drupal.org or other sources.
pm-enable (en) Enable one or more extensions (modules or themes).
pm-info (pmi) Show detailed info for one or more extensions (modules or themes).
pm-list (pml) Show a list of available extensions (modules and themes).
pm-projectinfo Show a report of available projects and their extensions.
(pmpi)
pm-refresh (rf) Refresh update status information.
pm-releasenotes Print release notes for given projects.
(rln)
pm-releases (rl) Print release information for given projects.
pm-uninstall (pmu) Uninstall one or more modules.
pm-update (up) Update Drupal core and contrib projects and apply any pending database updates (Same as pm-updatecode + updatedb).
pm-updatecode (upc) Update Drupal core and contrib projects to latest recommended releases.
pm-updatestatus Show a report of available minor updates to Drupal core and contrib projects.
(ups)
Role commands: (role)
role-add-perm (rap) Grant specified permission(s) to a role.
role-create (rcrt) Create a new role.
role-delete (rdel) Delete a role.
role-list (rls) Display a list of all roles defined on the system. If a role name is provided as an argument, then all of the permissions of
that role will be listed. If a permission name is provided as an option, then all of the roles that have been granted that
permission will be listed.
role-remove-perm Remove specified permission(s) from a role.
(rmp)
Runserver commands: (runserver)
runserver (rs) Runs PHP's built-in http server for development.
SQL commands: (sql)
sql-cli (sqlc) Open a SQL command-line interface using Drupal's credentials.
sql-connect A string for connecting to the DB.
sql-create Create a database.
sql-drop Drop all tables in a given database.
sql-dump Exports the Drupal DB as SQL using mysqldump or equivalent.
sql-query (sqlq) Execute a query against a database.
sql-sanitize Run sanitization operations on the current database.
(sqlsan)
sql-sync Copies the database contents from a source site to a target site. Transfers the database dump via rsync.
Search commands: (search)
search-index Index the remaining search items without wiping the index.
search-reindex Force the search index to be rebuilt.
search-status Show how many items remain to be indexed out of the total.
State commands: (state)
state-delete (sdel) Delete a state value.
state-get (sget) Display a state value.
state-set (sset) Set a state value.
User commands: (user)
user-add-role (urol) Add a role to the specified user accounts.
user-block (ublk) Block the specified user(s).
user-cancel (ucan) Cancel a user account with the specified name.
user-create (ucrt) Create a user account with the specified name.
user-information Print information about the specified user(s).
(uinf)
user-login (uli) Display a one time login link for the given user account (defaults to uid 1).
user-password (upwd) (Re)Set the password for the user account with the specified name.
user-remove-role Remove a role from the specified user accounts.
(urrol)
user-unblock (uublk) Unblock the specified user(s).
Watchdog commands: (watchdog)
watchdog-delete Delete watchdog messages.
(wd-del, wd-delete)
watchdog-list Show available message types and severity levels. A prompt will ask for a choice to show watchdog messages.
(wd-list)
watchdog-show Show watchdog messages.
(wd-show, ws)
#drushのsh化
その後、実行するバックアップコマンドをshに書き出してみましょう。
ここでは6時間おきにDrupalのキャッシュをクリアするコマンドをsh化してみます。
#!/bin/sh
cd {drupalのルートパス}
drush cc all
shの実行権限追加
[root@cent7d8 ~]# chmod +x /root/cache-clear.sh
以上でshの作成は完了です。
#shのcrontabへの登録
その後crontabに登録します。
[root@cent7d8 ~]# crontab -e
* */6 * * * sh /root/cache-clear.sh >/dev/null 2>&1
上記の設定で6時間間隔でキャッシュのクリアが実行されます。
#その他のdrushコマンド
その他、以下は使用頻度が高いであろうコマンドです。
- core のアップデート
drush pm-update
- バックアップ
drush archive-dump
- メンテナンスモード
drush vset maintenance_mode 1 --yes
- メンテナンスモード解除
drush vset maintenance_mode 0 --yes
メンテナンスモードからアップデートやバックアップ、キャッシュのクリアまでは自動化を実装する事は可能ですが、事前に実行するコマンドを「-d」でデバッグしてみたり「-s」でシミュレートしてみたり「-v」でログ出力をして実行結果が正常終了するなどを確かめた後で自動化を行えば、より精度の高い自動化を実装出来るのではないかと考えます。
#まとめ
ケースによっては自動化は厳しい場合もあるかも知れませんが、これまで手動で行ってきた処理を定期自動実行する事で、コンテンツ運営の自動化を実現させるという意味では、この機能を活用しない手はないと思います。
これにて私の「Drupal Advent Calendar 2015」の12月14日分の投稿を終了します。
最後までの閲覧、ありがとうございました。