Chef cheat sheet: Difference between revisions

 
(80 intermediate revisions by the same user not shown)
Line 1: Line 1:
Opscode chef はまだまだ OSS の初期段階で、パワーユーザの時代であるらしい。hack, patch, push 的な感じっぽい。なのでお作法に則って、 Let's enjoy!!!
Opscode chef はまだまだ OSS の初期段階で、パワーユーザの時代であるらしい。hack, patch, push 的な感じっぽい。十分に使い物になるレベルにはあるし、使えばたくさん良いことがある。なのでお作法に則って、 Let's enjoy!!!


= メモ =
= メモ =
Line 14: Line 14:


cookbook に汎用性を持たせようとする動きはあるらしいが、現状では無理っぽい。というか、使うことによってベストプラクティスを模索している段階かも。
cookbook に汎用性を持たせようとする動きはあるらしいが、現状では無理っぽい。というか、使うことによってベストプラクティスを模索している段階かも。
[http://acrmp.github.com/foodcritic/ Foodcritic - A lint tool for your Opscode Chef cookbooks]
[https://github.com/etsy/foodcritic-rules Etsy's foodcritic rules]
ここで定義されているスタイルに合わせる。(最近 FC001 が違うという議論もあって面白い ;)
[http://wiki.opscode.com/display/chef/Opscode+Community+Summit+2 Opscode Community Summit 2 Session Documentation]
コミュニティサミットのドキュメント いろいろ参考になるかも。


== 導入 ==
== 導入 ==
Line 21: Line 28:


=== Workstation ===
=== Workstation ===
Workstation は事実上 gem で手管理でないとだめっぽい
Workstation は事実上 gem で手管理のほうが関連するツール(knife-ec2やFoodcritic)を考えるといいかも


  [http://wiki.opscode.com/display/chef/Workstation+Setup+for+Debian+and+Ubuntu Workstation Setup for Debian and Ubuntu]
  [http://wiki.opscode.com/display/chef/Workstation+Setup+for+Debian+and+Ubuntu Workstation Setup for Debian and Ubuntu]
しかも ruby 1.9.2 以上のほうがよさげ
[http://wiki.brightbox.co.uk/docs:ruby-ng Next Generation Ubuntu Ruby Packages]


<syntaxhighlight>
<syntaxhighlight>
sudo apt-add-repository ppa:brightbox/ruby-ng
sudo aptitude update
sudo aptitude install ruby ruby-dev libopenssl-ruby rdoc ri irb build-essential wget ssl-cert curl
sudo aptitude install ruby ruby-dev libopenssl-ruby rdoc ri irb build-essential wget ssl-cert curl
cd /tmp
curl -O http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz
tar zxf rubygems-1.8.10.tgz
cd rubygems-1.8.10
sudo ruby setup.rb --no-format-executable
sudo gem install chef --no-ri --no-rdoc
sudo gem install chef --no-ri --no-rdoc
sudo gem install knife-ec2 --no-ri --no-rdoc
sudo gem install knife-ec2 --no-ri --no-rdoc
sudo gem install foodcritic --no-ri --no-rdoc
sudo aptitude -y install git-core
sudo aptitude -y install git-core
</syntaxhighlight>
</syntaxhighlight>


=== Client ===
=== Node ===
client は opscode の リポジトリ の配下が楽
最近は omnibus install を推奨らしい
 
[http://wiki.opscode.com/display/chef/Installing+Omnibus+Chef+Client+on+Linux+and+Mac Omnibus Installation]
 
<del>sudo true && curl -L https://www.opscode.com/chef/install.sh | sudo bash</del>


[http://wiki.opscode.com/display/chef/Installing+Chef+Client+on+Ubuntu+or+Debian Installing Chef Client on Ubuntu or Debian]
upgrade も ↑ で OK


<syntaxhighlight>
<del>sudo true && curl -L https://www.opscode.com/chef/install.sh | sudo bash -s -- -v 10.12.0</del>
echo 'deb http://apt.opscode.com/ lucid-0.10 main' | sudo tee /etc/apt/sources.list.d/opscode.list
 
wget -qO - http://apt.opscode.com/packages@opscode.com.gpg.key | sudo apt-key add -
とか バージョン指定も可能。(今は 10.16.2)
sudo aptitude update
sudo aptitude -y install chef
</syntaxhighlight>


knife bootstrap で distro を ubuntu10.04-apt 的に指定するのが良い
(2015/06/25 追記: 最近は curl | sudo bash はさすがにマズイという風潮が... なので公式サイトの表面からは消えてるようです。[https://downloads.chef.io/chef-client/])


== my recipes ==
== my recipes ==
[http://www.egrep.jp/wiki/index.php/Ubuntu_10.04_LTS_%28Lucid_Lynx%29_AMI 普段やっている設定]を cookbook にまとめ中
[http://www.egrep.jp/wiki/index.php/Ubuntu_10.04_LTS_%28Lucid_Lynx%29_AMI 普段やっている設定]を cookbook にまとめ中
  [https://github.com/nxhack/chef nxhack / chef My recipes (opscode chef cookbooks) : Bootstrap - Configuration - Orchestration]
  [https://github.com/nxhack/chef nxhack / chef My recipes (opscode chef cookbooks) : Bootstrap - Configuration - Orchestration]
== ★ 料理のコツ ★ ==
*一度だけ処理するとか、纏めて処理するとかは、action :nothing と notifies をうまく組み合わせると出来る
*cookbook_file とか template とか file で create_if_missing の組み合わせと、さっきの action :nothing と notifies でいろいろ出来そう
*cookbook_file と template は 出来た対象ファイルの内容をチェックして、同じなら処理しないという挙動になる。また file は content の組み合わせでは、 backup は取ってくれるが、内容のチェックはしてくれないようだ。
*execute, script, ruby_block とかの Resource に付ける名前は、同じ名前(重なる名前)は付けてはいけない
*secure_password を使う場合は、cookbook openssl を upload して recipe['openssl'] を run_list に追加する事


== knife-ec2 ==
== knife-ec2 ==
Line 59: Line 81:
knife.rb
knife.rb
<pre>
<pre>
# EC2:                                                                                                                    
# EC2:
knife[:aws_access_key_id]    = "---"
knife[:aws_access_key_id]    = "---"
knife[:aws_secret_access_key] = "---"
knife[:aws_secret_access_key] = "---"
knife[:flavor]                = "t1.micro"
knife[:flavor]                = "t1.micro"
knife[:image]                = "ami-0baf7662"
knife[:image]                = "ami-c7b202ae"
knife[:aws_ssh_key_id]        = "EC2_Key_Pairs_Name"
knife[:aws_ssh_key_id]        = "EC2_Key_Pairs_Name"
</pre>
</pre>
Line 84: Line 106:


<pre>
<pre>
knife ec2 server create -N node01.egrep.jp -x ubuntu -d ubuntu10.04-apt-ec2
knife ec2 server create -N node01.egrep.jp -x ubuntu -r "role[node01],role[ec2-common]" -Z us-east-1d
</pre>
 
<pre>
knife ec2 server create -N node01.egrep.jp -x ubuntu -r "role[node01],role[ec2-common]" -Z us-east-1d --bootstrap-version 10.12.0
</pre>
 
 
[http://wiki.opscode.com/display/chef/Custom+Knife+Bootstrap+Script Custom Knife Bootstrap Script]
distro で指定するのもあり。(今は、OmniBus Installer を使った標準のままの chef-full.erb でやるようがよさげ)
<pre>
knife ec2 server create -N node01.egrep.jp -x ubuntu -d ubuntu10.04-omnibus-ec2
</pre>
 
<pre>
knife ec2 server create -N node01.egrep.jp -x ubuntu -r "role[node01],role[ec2-common]" -d ubuntu10.04-omnibus-ec2 -Z us-east-1d
</pre>
</pre>


node name を指定して起動したインスタンスを停止する際に、chef-server に自動登録した node と client も同時に消したい場合
<pre>
<pre>
knife ec2 server create -N node01.egrep.jp -x ubuntu -r "role[node01],role[ec2-common]" -d ubuntu10.04-apt-ec2
knife ec2 server delete i-xxxxxxxx -N node01.egrep.jp -P
</pre>
</pre>


Line 94: Line 132:
すでに稼働しているインスタンスに opscode の リポジトリから chef client を node 名を指定してインストールする。
すでに稼働しているインスタンスに opscode の リポジトリから chef client を node 名を指定してインストールする。
<pre>
<pre>
knife bootstrap IPADDRESS -N node01.egrep.jp -i ~/.ssh/ssh_key.pem -x ubuntu --sudo -d ubuntu10.04-apt
knife bootstrap IPADDRESS -N node01.egrep.jp -i ~/.ssh/ssh_key.pem -x ubuntu --sudo -d chef-full
</pre>
</pre>


Line 101: Line 139:
Bootstrap => Configuration => Orchestration の区別をして考える事が大事
Bootstrap => Configuration => Orchestration の区別をして考える事が大事


== role ==
== knife role ==
<pre>
<pre>
knife role from file ec2-common.json
knife role from file ec2-common.json
Line 109: Line 147:
knife role show ec2-common -F json
knife role show ec2-common -F json
</pre>
</pre>
(-Fj 使わない)


== node ==
== knife node ==
<pre>
<pre>
knife node run_list add node01.egrep.jp 'role[node01],role[ec2-common]'
knife node run_list add node01.egrep.jp 'role[node01],role[ec2-common]'
</pre>
== knife search ==
<pre>
knife search node '*:*' -a lsb.codename
</pre>
== chef-client ==
run_list を指定して起動する場合
<pre>
chef-client -o 'role[node01],role[ec2-common]'
</pre>
</pre>


Line 118: Line 168:
=== node object ===
=== node object ===
  [http://wiki.opscode.com/display/chef/Recipes#Recipes-CommonAutomaticAttributes Common Automatic Attributes]
  [http://wiki.opscode.com/display/chef/Recipes#Recipes-CommonAutomaticAttributes Common Automatic Attributes]
== knife-essentials ==
[https://github.com/jkeiser/knife-essentials Knife Essentials]
これは便利なので入れる方がよいそうです。
knife diff | fgrep diff
== berkshelf ==
[http://berkshelf.com/ Berkshelf - Manage a Cookbook or an Application's Cookbook dependencies]
便利っぽい
== spiceweasel ==
[http://wiki.opscode.com/display/chef/Spiceweasel Spiceweasel]
便利っぽい
== cookbook motd-tail ==
[https://github.com/opscode-cookbooks/motd-tail Updates motd.tail with Chef Roles]
node に login したときに、role を表示させる。
== WP CLI (WordPress Command Line Interface) ==
WordPress の configration にコレは便利そう。(テスト中)
[https://github.com/wp-cli/wp-cli A command line interface for WordPress]
wp-cli の chef recipe をナンチャッテで書いてみた。(推敲中)
[https://github.com/nxhack/chef/tree/master/cookbooks/conf-WordPress-cli https://github.com/nxhack/chef/tree/master/cookbooks/conf-WordPress-cli]
<pre>
wp core install --url='http://www.example.com/blog' --title='MY SITE' --admin_email='admin@example.com' --admin_password='pass'
wp plugin install apc
wp plugin install 001-prime-strategy-translate-accelerator
wp plugin install backwpup
## wp plugin install db-cache-reloaded-fix
wp plugin install easy-fancybox
wp plugin install pushpress
wp plugin install syntaxhighlighter
wp plugin install tinymce-advanced
wp plugin install wp-crontrol
wp plugin install wp-page-numbers
wp plugin install wp-social-bookmarking-Light
wp plugin install wptouch
wp plugin activate akismet
wp plugin activate wp-multibyte-patch
## wp plugin activate apc  (!!! mv ./object-cache.php ../../ )
wp plugin activate 001-prime-strategy-translate-accelerator
wp plugin activate backwpup
## wp plugin activate db-cache-reloaded-fix
wp plugin activate easy-fancybox
wp plugin activate pushpress
wp plugin activate syntaxhighlighter
wp plugin activate tinymce-advanced
wp plugin activate wp-crontrol
wp plugin activate wp-page-numbers
wp plugin activate wp-social-bookmarking-Light
wp plugin activate wptouch
</pre>