Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 044b5b50d388c80fec71b238012cb83c7435de12 @fujiwara committed Jul 4, 2012
2 Capfile
@@ -0,0 +1,2 @@
+Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
+load 'config/deploy'
80 README.md
@@ -0,0 +1,80 @@
+# chef-solo with capistrano
+
+chef-solo を capistorano で実行する例
+
+## 前提
+
+* chef-solo が各ホストにインストール済み
+* capistrano をするホストから各ホストに ssh でログインできる
+
+## 設定
+
+config/deploy.rb でファイルを配置するディレクトリを指定。
+
+ set :chef_dir, "/root/chef"
+
+roles/base.json で定義された hosts に対して実行される。
+
+ {
+ "hosts": {
+ "web01": "192.168.1.101",
+ "app01": "192.168.1.102"
+ },
+ "nameservers": [ "192.168.1.1", "8.8.8.8" ]
+ "run_list": [
+ "os-defaults"
+ ]
+ }
+
+roles/{hostname}.json が、base.json に上書きされてそれぞれのホストで chef-solo が実行される。
+
+* run_list は base.json + {hostname}.json の内容が連結される
+* それ以外の key が重複した場合は {hostname}.json の内容が使用される
+
+## 実行
+
+ # cd /root/chef
+ # cap -T
+ cap chef:run_chef # run chef-solo
+ cap chef:sync # rsync /root/chef
+ cap invoke # Invoke a single command on the remote servers.
+ cap shell # Begin an interactive Capistrano session.
+
+ # cap chef
+ * executing `chef'
+ * executing `chef:init_config'
+ * executing `chef:sync'
+ * executing `chef:merge_json'
+ * executing "cd /root/chef && export HOST=`hostname -s`; ./bin/merge_json roles/base.json roles/${HOST}.json > config/self.json"
+ servers: ["web01", "app01"]
+ [web01] executing command
+ [app01] executing command
+ command finished in 89ms
+ * executing `chef:run_chef'
+ * executing "chef-solo -c /root/chef/config/solo.rb -j /root/chef/config/self.json"
+ servers: ["web01", "app01"]
+ [app01] executing command
+ [web01] executing command
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: *** Chef 10.12.0 ***
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: *** Chef 10.12.0 ***
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Setting the run_list to ["os-defaults"] from JSON
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Run List is [recipe[os-defaults]]
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Run List expands to [os-defaults]
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Starting Chef Run for app01
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Running start handlers
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Start handlers complete.
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Processing template[/etc/resolv.conf] action create (os-defaults::default line 2)
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Chef Run complete in 0.006611 seconds
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Running report handlers
+ ** [out :: app01] [2012-07-04T19:33:46+09:00] INFO: Report handlers complete
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Setting the run_list to ["os-defaults"] from JSON
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Run List is [recipe[os-defaults]]
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Run List expands to [os-defaults]
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Starting Chef Run for web01
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Running start handlers
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Start handlers complete.
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Processing template[/etc/resolv.conf] action create (os-defaults::default line 2)
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Chef Run complete in 0.010748 seconds
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Running report handlers
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Report handlers complete
+ command finished in 861ms
16 bin/merge_json
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+
+require "rubygems"
+require "json"
+
+base = JSON.parse( open(ARGV[0]).read )
+host = JSON.parse( open(ARGV[1]).read )
+
+merged = base.merge(host) { |key, self_val, other_val|
+ if key.to_s == "run_list"
+ (self_val + other_val).uniq
+ else
+ other_val
+ end
+}
+puts JSON.pretty_generate(merged)
44 config/deploy.rb
@@ -0,0 +1,44 @@
+set :application, "chef-solo"
+set :chef_dir, "/root/chef"
+set :hostname, `hostname -s`.chomp
+
+require "json"
+JSON.parse( open("#{chef_dir}/roles/base.json").read )["hosts"].keys.each do |host|
+ role :host, host
+end
+
+namespace :chef do
+ task :default do
+ init_config
+ sync
+ merge_json
+ run_chef
+ end
+
+ task :init_config do
+ File::open("#{chef_dir}/config/solo.rb", "w") {|f|
+ f.puts "file_cache_path '/tmp/chef-solo'"
+ f.puts "cookbook_path '#{chef_dir}/cookbooks'"
+ f.puts "node_name `hostname -s`.chomp"
+ }
+ end
+
+ task :merge_json, :roles => :host do
+ run "cd #{chef_dir} && export HOST=`hostname -s`; ./bin/merge_json roles/base.json roles/${HOST}.json > config/self.json"
+ end
+
+ desc "run chef-solo"
+ task :run_chef, :roles => :host do
+ run "chef-solo -c #{chef_dir}/config/solo.rb -j #{chef_dir}/config/self.json"
+ end
+
+ desc "rsync #{chef_dir}"
+ task :sync do
+ find_servers_for_task(current_task).each do |server|
+ next if server.host == hostname
+ `rsync -avC --delete -e ssh --exclude config/self.json #{chef_dir}/ #{server.host}:#{chef_dir}/`
+ end
+ end
+
+end
+
7 cookbooks/os-defaults/recipes/default.rb
@@ -0,0 +1,7 @@
+
+template "/etc/resolv.conf" do
+ source "resolv.conf.erb"
+ owner "root"
+ mode 0644
+end
+
4 cookbooks/os-defaults/templates/default/resolv.conf.erb
@@ -0,0 +1,4 @@
+; generated by /sbin/dhclient-script
+<% node[:nameservers].each do |server| %>
+nameserver <%= server %>
+<% end %>
87 index.html
@@ -0,0 +1,87 @@
+<h1>chef-solo with capistrano</h1>
+
+<p>chef-solo を capistorano で実行する例</p>
+
+<h2>前提</h2>
+
+<ul>
+<li>chef-solo が各ホストにインストール済み</li>
+<li>capistrano をするホストから各ホストに ssh でログインできる</li>
+</ul>
+
+<h2>設定</h2>
+
+<p>config/deploy.rb でファイルを配置するディレクトリを指定。</p>
+
+<pre><code>set :chef_dir, "/root/chef"
+</code></pre>
+
+<p>roles/base.json で定義された hosts に対して実行される。</p>
+
+<pre><code>{
+ "hosts": {
+ "web01": "192.168.1.101",
+ "web02": "192.168.1.102"
+ },
+ "nameservers": [ "192.168.1.1", "8.8.8.8" ]
+ "run_list": [
+ "os-defaults"
+ ]
+}
+</code></pre>
+
+<p>roles/{hostname}.json が、base.json に上書きされてそれぞれのホストで chef-solo が実行される。</p>
+
+<ul>
+<li>run_list は base.json + {hostname}.json の内容が連結される</li>
+<li>それ以外の key が重複した場合は {hostname}.json の内容が使用される</li>
+</ul>
+
+<h2>実行</h2>
+
+<pre><code># cd /root/chef
+# cap -T
+cap chef:run_chef # run chef-solo
+cap chef:sync # rsync /root/chef
+cap invoke # Invoke a single command on the remote servers.
+cap shell # Begin an interactive Capistrano session.
+
+# cap chef
+ * executing `chef'
+ * executing `chef:init_config'
+ * executing `chef:sync'
+ * executing `chef:merge_json'
+ * executing "cd /root/chef &amp;&amp; export HOST=`hostname -s`; ./bin/merge_json roles/base.json roles/${HOST}.json &gt; config/self.json"
+ servers: ["sl6", "centos5"]
+ [sl6] executing command
+ [centos5] executing command
+ command finished in 89ms
+ * executing `chef:run_chef'
+ * executing "chef-solo -c /root/chef/config/solo.rb -j /root/chef/config/self.json"
+ servers: ["web01", "web02"]
+ [web02] executing command
+ [web01] executing command
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: *** Chef 10.12.0 ***
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: *** Chef 10.12.0 ***
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Setting the run_list to ["os-defaults"] from JSON
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Run List is [recipe[os-defaults]]
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Run List expands to [os-defaults]
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Starting Chef Run for web02
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Running start handlers
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Start handlers complete.
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Processing template[/etc/resolv.conf] action create (os-defaults::default line 2)
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Chef Run complete in 0.006611 seconds
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Running report handlers
+ ** [out :: web02] [2012-07-04T19:33:46+09:00] INFO: Report handlers complete
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Setting the run_list to ["os-defaults"] from JSON
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Run List is [recipe[os-defaults]]
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Run List expands to [os-defaults]
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Starting Chef Run for web01
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Running start handlers
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Start handlers complete.
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Processing template[/etc/resolv.conf] action create (os-defaults::default line 2)
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Chef Run complete in 0.010748 seconds
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Running report handlers
+ ** [out :: web01] [2012-07-04T19:33:46+09:00] INFO: Report handlers complete
+ command finished in 861ms
+</code></pre>
11 roles/base.json
@@ -0,0 +1,11 @@
+{
+ "hosts": {
+ "web01": "192.168.0.101",
+ "app01": "192.168.0.102"
+ },
+ "nameservers": ["192.168.0.1", "8.8.8.8", "8.8.4.4"],
+ "run_list": [
+ "os-defaults"
+ ]
+}
+
3 roles/web01.json
@@ -0,0 +1,3 @@
+{
+ "nameservers": ["192.168.1.1", "8.8.4.4", "8.8.8.8"]
+}
2 roles/web02.json
@@ -0,0 +1,2 @@
+{
+}

0 comments on commit 044b5b5

Please sign in to comment.