13. そこでOhaiです
• Ohai (https://github.com/opscode/ohai)
• quot: Ohai detects data about your
operating system.
• 参考資料:Chefの心臓、Ohaiのアトリビュ
ート(以下略) http://qiita.com/items/
5ce72101f8dee906ccb4
• OS/ディストリビューションをはじめ、動作
環境を泥臭く判別
Monday, March 4, 13
14. Ohaiのソース抜粋、
プラットフォーム判別の一部
#
platform
[
and
platform_version
?
]
should
be
lower
case
to
avoid
dealing
with
RedHat/Redhat/redhat
matching
if
File.exists?("/etc/oracle-‐release")
contents
=
File.read("/etc/oracle-‐release").chomp
platform
"oracle"
platform_version
get_redhatish_version(contents)
elsif
File.exists?("/etc/enterprise-‐release")
contents
=
File.read("/etc/enterprise-‐release").chomp
platform
"oracle"
platform_version
get_redhatish_version(contents)
elsif
File.exists?("/etc/debian_version")
#
Ubuntu
and
Debian
both
have
/etc/debian_version
#
Ubuntu
should
always
have
a
working
lsb,
debian
does
not
by
default
if
lsb[:id]
=~
/Ubuntu/i
platform
"ubuntu"
platform_version
lsb[:release]
else
if
File.exists?("/usr/bin/raspi-‐config")
platform
"raspbian"
else
platform
"debian"
end
platform_version
File.read("/etc/debian_version").chomp
end
elsif
File.exists?("/etc/redhat-‐release")
contents
=
File.read("/etc/redhat-‐release").chomp
platform
get_redhatish_platform(contents)
まるで
platform_version
get_redhatish_version(contents)
elsif
File.exists?("/etc/system-‐release")
contents
=
File.read("/etc/system-‐release").chomp
platform
get_redhatish_platform(contents) 薄氷を踏むような...
platform_version
get_redhatish_version(contents)
elsif
File.exists?('/etc/gentoo-‐release')
地道な虱潰し
platform
"gentoo"
Monday, March 4, 13
15. AWS(EC2)上かどうかだって
検出するよ!
def
has_ec2_mac?
network[:interfaces].values.each
do
|iface|
unless
iface[:arp].nil?
if
iface[:arp].value?("fe:ff:ff:ff:ff:ff")
Ohai::Log.debug("has_ec2_mac?
==
true")
return
true
end
end
end
Ohai::Log.debug("has_ec2_mac?
==
false")
false
end
def
looks_like_ec2?
#
Try
non-‐blocking
connect
so
we
don't
"block"
if
#
the
Xen
environment
is
*not*
EC2
hint?('ec2')
||
has_ec2_mac?
&&
can_metadata_connect?
(EC2_METADATA_ADDR,80)
end
if
looks_like_ec2?
Ohai::Log.debug("looks_like_ec2?
==
true")
やっぱり地道に。
Monday, March 4, 13
27. Current Resouce取得例
パッケージマネージャ=PackManの場合
パッケージの状態を取得。。
Chef::Log.debug("#{@new_resource}
checking
pacman
for
#{@new_resource.package_name}")
status
=
popen4("pacman
-‐Qi
#{@new_resource.package_name}")
do
|pid,
stdin,
stdout,
stderr|
stdout.each
do
|line|
line.force_encoding(Encoding::UTF_8)
if
line.respond_to?(:force_encoding)
case
line
when
/^Version(s?)*:
(.+)$/
Chef::Log.debug("#{@new_resource}
current
version
is
#{$2}")
@current_resource.version($2)
end
コマンド叩いてパース...
Monday, March 4, 13
28. New Resource適用例
パッケージをインストール状態に変更
def
install_package(name,
version)
run_command_with_systems_locale(
:command
=>
"pacman
-‐-‐sync
-‐-‐noconfirm
-‐-‐noprogressbar#{expand_options(@new_resource.options)}
#{name}"
)
end
これもまた泥臭い。。
Cronなんかもお勧めの溝さらい検出Source
if
@cron_exists
unless
cron_different?
Chef::Log.debug("Skipping
existing
cron
entry
'#{@new_resource.name}'")
return
end
read_crontab.each_line
do
|line|
case
line.chomp
when
"#
Chef
Name:
#{@new_resource.name}"
cron_found
=
true
Monday, March 4, 13
29. Resouceと冪等性
• 地道なResouce Correction
• CookBookレシピ=New Resource
• Current Resouce を New Resourceと同じ
Statusに収束させる。
• => Client/Solo は何度実行しても同じ結果、
同じ状態になるようにして、常に実行させてお
くことが大事。
Monday, March 4, 13
30. 5.Configration
Management
(Automaticaly/
Configration First)
Monday, March 4, 13