WP CLI

wp-cli/find-commandをつかってみた

WP-CLIのパッケージコマンドをいろいろ探すのが最近楽しくなってる人です。 今回はwp-cli/find-commandを試してみました。 これを使うことで、サーバー内にインストールされているWordPressを探すこ […]

広告ここから
広告ここまで

WP-CLIのパッケージコマンドをいろいろ探すのが最近楽しくなってる人です。

今回はwp-cli/find-commandを試してみました。
これを使うことで、サーバー内にインストールされているWordPressを探すことができる様子です。

環境

      ___         _            __
     / _ | __ _  (_)_ _  ___  / /____
    / __ |/  ' \/ /  ' \/ _ \/ __/ _ \
   /_/ |_/_/_/_/_/_/_/_/\___/\__/\___/

https://aws.amazon.com/amazon-linux-ami/2017.03-release-notes/

 Nginx 1.13.5, PHP 7.0.24, Percona MySQL 5.6.37, WP-CLI 1.3.0

 amimoto     https://www.amimoto-ami.com/
 digitalcube https://en.digitalcube.jp/

インストール

READMEを参考にコマンドをインストールします。

$ wp package install git@github.com:wp-cli/find-command.git
Installing package wp-cli/find-command (dev-master)
Updating /home/ec2-user/.wp-cli/packages/composer.json to require the package...
Registering git@github.com:wp-cli/find-command.git as a VCS repository...
Using Composer to install the package...
---
Loading composer repositories with package information
Updating dependencies
Resolving dependencies through SAT
Dependency resolution completed in 0.106 seconds
Analyzed 4555 packages to resolve dependencies
Analyzed 189763 rules to resolve dependencies
Package operations: 1 install, 0 updates, 0 removals
Installs: wp-cli/find-command:dev-master 33aedec
 - Installing wp-cli/find-command (dev-master 33aedec)
Writing lock file
Generating autoload files
---
Success: Package installed.

ひとまずヘルプを見る

wp find <パス名> <オプション>という使い方になる様子です。

$ wp help find

NAME

  wp find

DESCRIPTION

  Find WordPress installs on the filesystem.

SYNOPSIS

  wp find <path> [--skip-ignored-paths] [--include_ignored_paths=<paths>] [--max_depth=<max-depth>] [--fields=<fields>] [--field=<field>]
  [--format=<format>] [--verbose]

  Recursively iterates subdirectories of provided `<path>` to find and
  report WordPress installs. A WordPress install is a wp-includes directory
  with a version.php file.

  Avoids recursing some known paths (e.g. /node_modules/, hidden sys dirs)
  to significantly improve performance.

  Indicates depth at which the WordPress install was found, and its alias,
  if it has one.

  ```
  $ wp find ./
  +--------------------------------------+---------------------+-------+--------+
  | version_path                         | version             | depth | alias  |
  +--------------------------------------+---------------------+-------+--------+
  | /Users/wpcli/wp-includes/version.php | 4.8-alpha-39357-src | 2     | @wpcli |
  +--------------------------------------+---------------------+-------+--------+
  ```

早速探してみる

ということで早速使ってみます。

$ wp find ./
+--------------------------------------------------------------------+---------+-------+-------+
| version_path                                                       | version | depth | alias |
+--------------------------------------------------------------------+---------+-------+-------+
| /var/www/vhosts/example.com/wp-includes/version.php                | 4.8.2   | 1     |       |
| /var/www/vhosts/example1.com/wp-includes/version.php               | 4.7.6   | 1     |       |
| /var/www/vhosts/example2.com/wp-includes/version.php               | 4.8.2   | 1     |       |
+--------------------------------------------------------------------+---------+-------+-------+

wp-includes/version.phpを探してくる様子ですね。
コアのバージョンも返してくれるのが便利です。

階層を指定する

--max_depthを指定することで、検索するディレクトリの階層を指定できます。

$ wp find /var/www --max_depth=1
+--------------+---------+-------+-------+
| version_path | version | depth | alias |
+--------------+---------+-------+-------+
+--------------+---------+-------+-------+

--max_depth=2にすることで、var/www/vhosts/配下も検索するようになりました。

$ wp find /var/www --max_depth=2
+--------------------------------------------------------------------+---------+-------+-------+
| version_path                                                       | version | depth | alias |
+--------------------------------------------------------------------+---------+-------+-------+
| /var/www/vhosts/example.com/wp-includes/version.php                | 4.8.2   | 1     |       |
| /var/www/vhosts/example1.com/wp-includes/version.php               | 4.7.6   | 1     |       |
| /var/www/vhosts/example2.com/wp-includes/version.php               | 4.8.2   | 1     |       |
+--------------------------------------------------------------------+---------+-------+-------+

フォーマット指定

--format=でフォーマットを指定できます。
jqと組み合わせるとこういうことも。

$ wp find /var/www --max_depth=2 --format=json | jq ".[].version" -r
4.8.2
4.7.6
4.8.2

使い所

cronなどで監視したい時にこのコマンド便利かなと思います。

  • wp findでサーバー内のWordPressをリストアップ
  • 各WordPressに対してwp dockerを実行
  • 実行結果をSlackなどに通知

みたいなスクリプトを組めば、サーバー内にWordPressがどれだけインストールされているかなどを特に意識する必要なく運用監視ができそうです。

今後

wp findwp doctoramimoto-ami/ssm_inventry_resources: Inventory scripts for AMIMOTO AMI intance to AWS EC2 Systems Manager.を組み合わせたら何かできないかなーと思うので、いろいろ試してみます。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts