オープンソースなシステム自動管理ツール Puppet
第15回 リソースタイプの拡張(その1)
これから数回に渡って,独自のリソースタイプを作成して,Puppetを拡張する方法について解説します。Puppetのバージョンは現在の安定版である0.24.4を前提としています。
今回はリソースタイプを拡張し,カスタムリソースを適用するための全体的な流れについて解説します。カスタムリソースタイプはRubyプログラムで作成しますが,プログラムの詳細については次回以降で解説します。
Puppetサーバ側の設定
カスタムリソースタイプはPuppetサーバ上のモジュールパスに置かれますので,カスタムリソースタイプ用のディレクトリを作成します。
# mkdir -p /etc/puppet/modules/custom/plugins/puppet/type
# mkdir -p /etc/puppet/modules/custom/plugins/puppet/provider
次に,Puppetクライアントがカスタムリソースタイプを同期できるようにするために,/etc/puppet/fileserver.confに以下のように記述し,ファイルサーバにマウントします。pathパラメータは不要です。
[plugins]
allow *.example.com
これにより,/etc/puppet/modules/*/plugins以下のファイルが,puppet://$server/pluginsで参照できるようになります。*の部分はどんな名前でも構いません。複数のディレクトリが/etc/puppet/modules以下に存在する場合でも,puppet://$server/pluginsですべて参照できます。
Puppet クライアント側の設定
Puppetクライアントでは,カスタムリソースタイプを同期するために,/etc/puppet/puppet.confに以下のように記述します。
[main]
pluginsync = true
pluginsource = puppet://$server/plugins
カスタムリソースタイプの作成
今回はサンプルとして,filesimpleというリソースタイプを作成してみます。このリソースタイプは,fileタイプに似ていますが,ensureとmodeのみがパラメータとして利用できます。
カスタムリソースタイプの作成は,タイプの作成とプロバイダの作成から構成されます。
タイプの作成
以下の内容の/etc/puppet/modules/custom/plugins/puppet/type/filesimple.rbを作成します。このファイルでは,filesimpleタイプの定義を行い,どういったパラメータが使えるか,といったことを主に定義します。
module Puppet
newtype(:filesimple) do
@doc = "Manage a file (the simple version)."
ensurable
newparam(:name) do
desc "The full path to the file."
end
newproperty(:mode) do
desc "Manage the file's mode."
defaultto "640"
end
end
end
プロバイダの作成
以下の内容の/etc/puppet/modules/custom/plugins/puppet/provider/filesimple/posix.rbを作成します。このファイルではfilesimpleタイプのリソースを適用する場合に,具体的にどのような処理を行うのかを記述します。
Puppet::Type.type(:filesimple).provide(:posix) do
desc "Normal Unix-like POSIX support for file management."
def create
File.open(@resource[:name], "w") { |f| f.puts "" } # Create an empty file
# Make sure the mode is correct
should_mode = @resource.should(:mode)
unless self.mode == should_mode
self.mode = should_mode
end
end
def destroy
File.unlink(@resource[:name])
end
def exists?
File.exists?(@resource[:name])
end
# Return the mode as an octal string, not as an integer.
def mode
if File.exists?(@resource[:name])
"%o" % (File.stat(@resource[:name]).mode & 007777)
else
:absent
end
end
# Set the file mode, converting from a string to an integer.
def mode=(value)
File.chmod(Integer("0" + value), @resource[:name])
end
end
カスタムリソースの適用
作成したfilesimpleタイプのリソースを定義し,実際にシステムに適用してみます。
Puppetサーバ側での作業
Puppetサーバ側で,以下の内容の/etc/puppet/manifests/site.ppを作成し,filesimpleリソースを定義します。
node default {
filesimple { '/tmp/file':
ensure => present,
mode => 600,
}
}
puppetmasterdを起動します。
# puppetmasterd --verbose --no-daemonize
info: Starting server for Puppet version 0.24.4
info: mount[plugins]: allowing *.example.com access
info: Listening on port 8140
notice: Starting Puppet server version 0.24.4
Puppetクライアント側での作業
puppetdを実行してPuppetサーバに接続し,カスタムリソースタイプの同期とカスタムリソースの適用を行います。
# puppetd --server puppet.example.com --verbose --no-daemonize
notice: Starting Puppet client version 0.24.4
info: Retrieving plugins
notice: /File[/var/puppet/lib]/checksum: checksum changed '{mtime}Sun Apr 27 01:34:46 +0900 2008' to '{mtime}Sun Apr 27 05:51:23 +0900 2008'
notice: /File[/var/puppet/lib/puppet]/ensure: created
notice: /File[/var/puppet/lib/puppet/type]/ensure: created
notice: /File[/var/puppet/lib/puppet/type/filesimple.rb]/ensure: created
notice: /File[/var/puppet/lib/puppet/provider]/ensure: created
notice: /File[/var/puppet/lib/puppet/provider/filesimple]/ensure: created
notice: /File[/var/puppet/lib/puppet/provider/filesimple/posix.rb]/ensure: created
info: Caching catalog at /var/puppet/state/localconfig.yaml
notice: Starting catalog run
notice: //Node[default]/Filesimple[/tmp/file]/ensure: created
notice: Finished catalog run in 0.02 seconds
カスタムリソースタイプが同期されていること,filesimpleリソースが適用されてファイルが作成されていることが見てとれます。
以上がカスタムリソースタイプを作成し,実際に適用するまでの流れとなります。次回はタイプやプロバイダのRubyプログラムについて,詳細に解説する予定です。

