複数ホストにコマンドを撒けるpdshをUbuntuでビルドする手順

複数ホストにコマンドを撒けるpdshをUbuntuでビルドする手順
Page content

複数のホストに対してコマンドを撒いて並列実行できるpdsh (参考サイト [1] に詳しい) は便利なので、手元のUbuntu 20.04の環境で使いたくなった。ちなみにpdsh自体は公式リポジトリに用意されていてaptで即導入可能。

しかし、~/.dsh/group/でのpdshのホストのグループ定義を行いたくてそのためのモジュールpdsh-mod-dshgroupもaptで探すと、Ubuntu環境にはパッケージが無いとわかった。えっ、そうなの?

ということで以下は、Ubuntu環境で、pdsh-mod-dshgroupモジュールによるグループ定義を利用できるpdshを自力ビルドする手順のメモです。

Ubuntuでのpdshビルド手順

  1. GitHubからソースを取得する
cd ~/tmp
git clone https://github.com/chaos/pdsh
cd pdsh
  1. ここではたと気づく。よくある実行形式のconfigureが入っていない。このソースはどうやってbuildするのだ?
  2. それは落ち着いてINSTALLを読めば自明だった。bootstrapを実行せよとある。(autoreconfというものを使っているらしい)
$ ./bootstrap 
Running libtoolize --automake --copy ... 
Running autoreconf --verbose --install -I config
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal -I config -I config
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy
autoreconf: running: /usr/bin/autoconf --include=config
autoreconf: running: /usr/bin/autoheader --include=config
autoreconf: running: automake --add-missing --copy --no-force
configure.ac:36: installing 'config/compile'
configure.ac:16: installing 'config/config.guess'
configure.ac:16: installing 'config/config.sub'
configure.ac:27: installing 'config/install-sh'
configure.ac:27: installing 'config/missing'
autoreconf: Leaving directory `.'
Cleaning up ...
Now run ./configure.
  1. 実行形式のconfigureが生成されたので、参考サイト [2] のように以下を実施。
./configure --with-ssh --with-dshgroups --prefix=/usr/local/pdsh
make
./src/pdsh/pdsh --help # pdshが出来ている
sudo make install
  1. 指定のpathへ、pdshとともにdshbakなど一式がインストールされた。
$ ls -al /usr/local/pdsh/bin/
total 452
drwxr-xr-x 2 root root   4096 Jun 11 01:15 .
drwxr-xr-x 5 root root   4096 Jun 11 00:52 ..
-rwxr-xr-x 1 root root   8663 Jun 11 01:15 dshbak
-rwxr-xr-x 1 root root 145824 Jun 11 01:15 pdcp
-rwxr-xr-x 1 root root 145824 Jun 11 01:15 pdsh
-rwxr-xr-x 1 root root 145824 Jun 11 01:15 rpdcp

pdshの利用例

  1. 参考サイト [2] に習って、次の環境変数をシェルに設定しておく。
PATH=$PATH:/usr/local/pdsh/bin
MANPATH=$MANPATH:/opt/local/pdsh/man
PDSH_RCMD_TYPE=ssh
export PATH MANPATH PDSH_RCMD_TYPE
  1. 次のディレクトリに任意の名前のファイルを作成して、ホストのグループを定義する。
$ cat ~/.dsh/group/server 
gce-1
ocaf-1
ocaf-2
ocaf-3
webarena-1
  1. pdshを用いて、たとえば上記serverに定義した複数ホストの「Kernelリリース番号」を調べるためにuname -rを並列実行し、標準出力をdshbak -cでまとめると次のようになる。
$ pdsh -g server 'uname -r' | dshbak -c
----------------
webarena-1
----------------
5.4.0-117-generic
----------------
gce-1
----------------
5.13.0-1031-gcp
----------------
ocaf-[1-3]
----------------
5.13.0-1034-oracle

上記の例ではたったの5台ですが、1台毎の対応では不可能なほどの大量のホストに対して調べ物を一斉に行いたいときなどに、pdshはものすごく威力を発揮するのでおすすめ。

おまけ) Slurm’s hostlistについて

上記のdshbak -cの出力結果に現れているocaf-[1-3]は、Slurm’s hostlistという表記法である (言い切っているが正式名かどうかは自信ありません)。ホスト名・計算ノード名のような、パターンに連番を含む文字列をコンパクトに表現することができる。

この表記法の文字列を扱うにはhostlistというコマンドが便利で、次のPythonモジュールで導入可能。以下のように、hostlists形式の文字列の生成や展開などの処理が行える。pdshと併せてこれもおすすめ。

$ hostlist -c a001,a002,a003
a[001-003]

$ hostlist -e b[001-003] # 集合を展開する
b001
b002
b003

$ hostlist -d c[001-100] c[012,034,056,078] # 集合の差を求める
c[001-011,013-033,035-055,057-077,079-100]

参考サイト