WP-CLIアドベントカレンダー2014の14日目です。Yay!
今日はローカルのWP-CLIコマンドをリモートで実行するコマンドのwp-cli-sshの紹介です。
これも前回同様にWP-CLIにもとから備わっているコマンドではなく、コミュニティパッケージという形の追加のコマンドです。
ほんとはCompoerでインストールするほうが楽かと思うんですが、どうもCompoerのインデックスが壊れているとのこと(宮さん、情報ありがとうございます!)なので、前回同様にCompoerじゃない方法でインストールしてみましょう。
~/.wp-cli配下に追加コマンド用のディレクトリがなければ作り、GitHubからそのディレクトリに追加コマンドのスクリプトをクローンします:
mkdir -p ~/.wp-cli/commands git clone git@github.com:xwp/wp-cli-ssh.git ~/.wp-cli/commands/wp-cli-ss
次に ~/.wp-cli/config.yml
ファイルを作成、もしくはすでにあれば編集して以下のようにします:
require:
- commands/wp-cli-ssh/wp-cli-ssh.php
そんでもってローカルにある適当なWordPressのディレクトリに wp-cli.local.yml
もしくは wp-cli.yml
ファイルを作成し(あるいは、すでにあれば編集して)以下のようにsshの項目を追加します:
ssh:
host-name-A:
cmd: ssh %pseudotty% host-name-B %cmd%
path: /path/to/the/wp-root/
- host-name-Aはローカルのwpコマンドでホスト指定するときの名称
- host-name-Bはリモートサーバに接続するときにsshコマンドに渡すホスト名
/path/to/the/wp-root/
はリモートにあるWordPressのパス
ここでは分かりやすいようにhost-name-Aとhost-name-Bと別にしてますが、同じでもかまいません。
これでローカルで、
wp ssh plugin status --host=host-name-A
とすると
$ wp ssh plugin status --host=host-name-A
Connecting via ssh to host: host-name-B
17 installed plugins:
A automatic-updater 1.0.2
A acf-repeater 1.1.1
UI akismet 3.0.2
I custom-fields-search 1.2.1
I dc-admin
A front-end-editor 2.3.1
I hotfix 1.0
UI jetpack 3.1.1
UI nginx-champuru 2.9.0
I relevanssi 3.3.7.1
A search-filter 1.2.5
A user-switching 1.0.1
UI wp-front-end-editor 0.10
I wordpress-importer 0.6.1
I wp-basic-auth 1.1.1
A wp-multibyte-patch 2.1.1
I wp-remote-manager-client 0.7.0.2
Legend: A = Active, I = Inactive, U = Update Available
Connection to XXX.XXX.XXX.XXX closed.
のように、リモートに接続、WP-CLIコマンド実行、リモート接続解除までを行います。