massの日記

日々の薪

年の瀬にJenkinsの訃報が届いても、悲嘆にくれないために #vgadvent2013

この記事はVOYAGE GROUP エンジニアブログ : Advent Calendar 2013 の11日目の記事になります。

adingoでDevOpsやっている@_zooです。
今年から、弊社が提供しているSSPサービスのFluctでは、AWSを利用した広告配信を開始しました。

AWSは手軽に構築できる分、いつインスタスの訃報が飛んできてもいいように準備をしておく必要があります。

今回は普段元気なJenkinsおじいちゃんが年の瀬にお亡くなりになった場合に、悲嘆にくれないためのお話です。

まずは、おじいちゃんの訃報に備えての対策

急な訃報で普段きないスーツやら、黒い靴やら、年末でぐでーと酒のんでいる時に探したくはないです。

  • Puppetによる構成管理
  • ServerSpecによるサーバの状態テスト
  • Jenkinsのジョブbackupをs3に定期的に同期

以下、JenkinsのPuppetとServerSpecのコードを抜粋します。

# Puppet

class jenkins::install {
  file {
    "/ebs1/jenkins":
      owner   => "jenkins",
      group   => "contentsuser",
      mode    => 755,
      ensure  => directory;

    "/ebs1/jenkins_backup":
      owner   => "jenkins",
      group   => "contentsuser",
      mode    => 755,
      ensure  => directory;
  }

  package {
    "jenkins":
      ensure  => installed,
      require => File["/var/lib/jenkins"];

    "java-1.6.0-openjdk":
      ensure => installed;

    "rubygem-bundler":
      ensure => installed;
  }
}

class jenkins::prepare( $jenkins_https_port = 8443,
                        $jenkins_cert_file,
                        $jenkins_key_file,
                        $jenkins_java_options ) {

  file {
      "/etc/pki/tls/certs/jenkins.ec2.crt":
        owner   => "jenkins",
        group   => "contentsuser",
        mode    => 440,
        source  => "puppet:///jenkins/etc/pki/tls/certs/dev.fluct.ec2.crt";

      "/etc/pki/tls/certs/jenkins.ec2.key":
        owner   => "jenkins",
        group   => "contentsuser",
        mode    => 400,
        source  => "puppet:///jenkins/etc/pki/tls/certs/dev.fluct.ec2.key";
    }
  }

  file {
    "/etc/sysconfig/jenkins":
      owner   => "root",
      group   => "root",
      mode    => 644,
      content => template("jenkins/etc/sysconfig/jenkins.erb");
  }
}

class jenkins( $jenkins_https_port   = 8443,
               $jenkins_cert_file    = "/etc/pki/tls/certs/jenkins.ec2.crt",
               $jenkins_key_file     = "/etc/pki/tls/certs/jenkins.ec2.key",
               $jenkins_java_options = "-Djava.awt.headless=true" ) {

  Class["jenkins::install"]
  -> Class["jenkins::prepare"]
  -> Class["jenkins"]

  include "jenkins::install"
  class {
    "jenkins::prepare":
      jenkins_https_port   => $jenkins_https_port,
      jenkins_cert_file    => $jenkins_cert_file,
      jenkins_key_file     => $jenkins_key_file,
      jenkins_java_options => $jenkins_java_options;
  }

  service {
    "jenkins":
      ensure  => running,
      enable  => true;
  }
}

# ServerSpec

require 'spec_helper'

describe package('jenkins') do
  it { should be_installed }
end

describe package('java-1.6.0-openjdk') do
  it { should be_installed }
end

describe package('rubygem-bundler') do
  it { should be_installed }
end

describe package('dejavu-sans-fonts') do
  it { should be_installed }
end

describe service('jenkins') do
  it { should be_enabled   }
  it { should be_running   }
end

describe port(8443) do
  it { should be_listening }
end

describe file('/etc/sysconfig/jenkins') do
  it { should be_file }
  it { should be_mode 644 }
  it { should be_owned_by 'root' }
  it { should be_grouped_into 'root' }
end

おじいちゃんは自分の健康状態を維持するため、このServerSpecを30分おきに実行しています。

さて、そんな日々の健康管理があっても急な病にかかることもあります。

そんな時に備えて、おじいちゃんは自分の設定ファイルをS3に置いておきます。
これでいつ倒れても、次のおじいちゃんが引き継げます。

JenkinsのバックアップはPluginで提供されているthinBackupで作成しています。
f:id:johnsmith0707:20131210224758p:plain

このバックアップそのものもJenkinsのジョブで取っています。
とはいっても、ワンライナーです。ぶっちゃけ、s3cmdのsync一行です。

s3cmd -c s3cfg sync /ebs1/jenkins_backup s3://jenkins-backup/

次に、おじいちゃんを殺してみます

やはりAWSとはいえ殺すというのは怖いですね、手が震えます。
インスタンスはCloudFormationで作成しているので、Stackを消せば終了です。

いざっ!

aws cloudformation delete-stack --stack-name JENKINS

やってしまいました。
ついカッとなって、やってしまった。怖いですね、これが人間というものです。

最後に、次のおじいちゃんを作ります

先ほど同様、CloudFormationを利用します。

aws cloudformation create-stack --stack-name JENKINS --template-body file://template_jenkins.json

次にできあがったインスタンスsshでログインしてPuppetをあてます。

sudo /etc/init.d/puppet start

次にデータをリストアします。

s3cmd -c s3cfg sync s3://jenkins-backup/ /ebs1/jenkins_backup

後はthinBackupのリストア画面から、リストアするだけです。
f:id:johnsmith0707:20131210224802p:plain

リストアしたら、元気なおじーちゃんが生まれました。良かったです。

かなりはしょりましたが、こんな感じで訃報がとんできても悲嘆にくれないようにしています。
ひよっこDevOpsエンジニアで、まだまだPuppetやServerSpecを使いこなせてないです。
そんなひよっこDevOpsエンジニアがいる弊社は絶賛DevOps系エンジニアを募集中です!

明日のアドベントカレンダー@tadasyさんです。お楽しみに!

DevOpsDays Tokyo 2013に行ってきた

今日は久しぶりに一日イベント、DevOpsDays Tokyo 2013に参加してきた。

参加した理由

DevOpsのひよっこエンジニアとしてエキスパートな方々が参加している場に行き、ノウハウや最新のツールを学んでくることが目的。

Blogを書いた理由

技術的な部分は実際に検証してみないとかけないけれど、今日はすごく刺激を受けた部分が多かったので、熱がさめないうちにイベントの感想をまとめてみることにした。

イベントのトピック

個人的に、今回のDevOpsDaysでのトピックは”Operationsの可視化”と”クラウド環境に適応したOperations”、この2つだと感じた。
派生して、Operationsの可視化ツールやクラウド環境に適応したOperationツールの紹介や実例の説明、実際にDevOpsエンジニアとして動いている人には非常にありがたいお話だったと思う。

Operationsの可視化

動くapplicationと違い、operation、いわゆる運用と言われる部分は目につきにくい。
だけど、今は簡単に可視化できるツールがたくさんある。1人に1台のマシンがあれば、1日で可視化できるくらいに簡単。

単純にOperationsの可視化だけでなく、他のデータと一緒に可視化する事でOperationsがApplicationだけでなくビジネス的にも影響を与えているもわかるようになり、Operator=運用者も今まで以上に開発者とビジネスそのものに歩み寄れるというのが印象的だった。

クラウド環境に適応したOperations

クラウドサービスの普及によりシステム的な柔軟性が高まった結果、静的な環境ではなく、動的な環境に適応できる監視ツール、システム構成管理のツールの需要が高まってきていると感じた。
PuppetやChefなどのシステム構成管理ツールが普及したように、動的な環境に対応できるように監視ツールも進化していて、これから様々な分野でどんどん発展していく勢いがあり非常に楽しみ。

クラウド環境に適応したツール

正直色々とツールがですぎて咀嚼しきれていない部分が多々ある。
でも、ここにまとめておけば後でぐぐるさいに自分が助かるので、記憶をたよりにまとめてみた。

構成管理

いわずとしれた構成管理ツールの二台巨頭。

監視

Pure rubyのMonitoring Framework。

node.jsのデータ収集ツール、udpでデータのやり取りを行う。

その他

デプロイツール。

botによる通知ツール。

環境の自動化ツール

まとめ

今回はDevOpsとは何?というものではなく、DevOpsの立ち位置にいるエンジニアに向けたお話が多かったと思う。
後は、DevOpsで重要なこととして複数のセッションであげられていたのが『コミュニケーション』というのが印象的だった。
いかにコミュニケーションを頻繁に行うか、いかにお互いを尊重しあうか、そういった部分が重要であると改めて思った。

AWSを利用してサービスの拡張環境を構築してみた

AWSを真面目に使ってみた

今月からAWSを本格的に業務で使い初めた。
環境構築の方針や導入した技術の振り返りもかねて、まとめてみた。

自分の立ち位置

昨年からアドネットワークサービスの開発業務に携わっているひよっこDevOpsエンジニア。
昨年の主な業務はデプロイシステム改善、今年の初夏からの主な業務は配信システムのインフラ部分やサーバ構成の改善。
インフラ部分は以前から興味があったので、ジェダイの騎士達(弊社インフラチームを心の中で勝手にこう呼んでいる)に教えをこいつつ日々勉強。

AWSを利用することになった背景

広告配信システムのうち、配信サーバの増設コストが課題となっていたため、より柔軟にスケールアップ・スケールダウン/スケールアウト・スケールインができる環境としてAWSを利用することにした。

利用したサービス・ツール

CI

監視

設計の方針

AWS環境の構築するにあたって、ご近所さんに相談したり、勉強会に参加して、環境構築の方針をざっくり決めた。
まず、環境構築を自動化する。
自動化として、Bootstrapping、Configuration、Orchestration、の三段階にわける。
Bootstrapingは環境構築の自動化、Configurationはサーバ構築の自動化、Orchestrationはアプリケーションのリリース作業の自動化、とそれぞれ自動化する部分での役割を分担させることにした。

Bootstrapingは何を行うか?

BootstrapingではCloudFormationとAWS CLIを利用し、JSONファイルを定義ファイルとし再現性のある環境構築を行う。
配置場所(VPC)、配置物(ELB、EC2)、出入口(SecurityGroup)、を作成する役割を持つ。

Configurationは何を行うか?

ConfigurationではPuppetを利用し、サーバのあるべき状態をManifestで定義し、定義を元に構成管理を行う。
パッケージのインストール、ディレクトリの構成、サービスの起動状態などを管理する役割を持つ。

Orchestrationは何を行うか?

OrchestrationではJenkinsを利用し、継続的なビルド&テスト、各環境へのデプロイ作業を行う。
アプリケーションのリリースを管理する役割を持つ。

結果

どこまで自動化できたか?

手動での設定が3ファイル残ってしまったが、それ以外はほぼ自動で環境構築を行えるようになった。

どの程度の柔軟性を得たか?

数十台のサーバを半日以内で、スケールアウト・スケールイン/スケールアップ・スケールダウンできる柔軟性を得た。

振り返り

一番悩んだのが、環境構築の自動化部分での役割分担。
三段階にわけるというのが果たしてよいのか、単一化した方がよいのかも、とか、色々悩んだ。
手探り状態で行ったので、運用していくと色々と問題点がみつかるかもしれないが、そこは適時対応する。

DevOpsのエンジニア業を意識した初めての業務で、中々やりがいのあるお仕事だった。
周囲の先輩エンジニアやチームメンバーに支えられつつ、今月リリースとあいなった。良かった。

2012年の振り返り

昨年に引き続き、今年も一年を振り返る。
一年を振り返るといっても、長過ぎるので今年に入ってから興味を持つようになったことを振り返る。

  • 今年興味を持ったこと

開発チームについて

  • 興味をもったきっかけ

今年は、社内で新卒研修や内定者向けの研修でバージョン管理やデプロイについての講義というか、セッションを担当した。
これらの研修では後半戦にグループによるアプリ開発を行い、そこで色々なチームを目にすることになったことが興味を持つきっかけだった。
また、自分自身も去年の11月に転職してから、社内で3回ほどチームを転々としてそれぞれのチームの違いを感じたことも、今思うとチームを意識する要因の一つだったと思う。

  • 興味をもってから考えたこと

いわゆる、『いいチーム』ってなんなんだろう、ということ。
これまで興味を持っていなかった部分なので、改めて考えてみた。
まずは、自分が今までいて『いいチーム』と感じたチームの、特徴点を考えてみた。

  • 目標以上の成果を出し続けている
  • チームメンバーのスキルがバランス良く整っている
  • 問題をたらい回しにしない

メンバー同士の円滑なコミュニケーションとかは横に置いておく。
自分自身がさほど興味を持っている部分ではないのと、おおよそ、上記の特徴があるチームではディスコミュニケーションはほとんどなかったからだ。
正直、仲の良いチームもあれば、仲は良くないかなーと思えるチームにいたこともある。
個人としては、コミュニケーションが円滑にできる=仲が良い、では決してないと思っている。コミュニケーションが円滑にできる=仲はそれほど良くない、状態もあると思う。
極論いえば、言うべきことをキチンといい、聞くべきことをキチンときけば、それでいい。
もちろん、仲が良いのが悪いことじゃないと思うが、必要以上にチームの仲の良さアピールというか、仲良し向上策はいらないと思っている。

  • 興味をもってからやりはじめたこと

自分自身が興味を持っていると気づいたのが11月という師走間近のことだった。
チームメンバーの構成が変わったり、リリースプロセスが変わったりして、KPTをやり始めるいいタイミングだったのもある。また、KPTのをやり初めたのがきっかけで別のチームのKPTに参加してみたりした。
後は、チームメンバーの構成を見たりとか、開発じゃないけれど、スポーツとかの試合を見たり、とかをしていた。

KPTをやっていて面白いと思ったのが、チームによってKeep,Problem,Tryのうち、最多となるものが違うところ。さらに面白いのが、どれが最多になるかは、おおよそチームによって毎回同じ。

例えば、今いるチームだとTryが毎回一番多い。
KPTの参加メンバーを見ると、ああ、このメンバーならTryが多そう!と確かに納得する。
もちろん、一週間に一度のKPTなので、週によってはばらつきが出るけれど、それでも最多となる項目はほぼ毎回同じなのが面白い。

チーム別にKPTの最多項目はどれか!とかやると、ああ、あのチームはそうだよね、と納得できるんじゃないかな。

  • 何故ブログを書いたのか

去年も書いたけれど、自分が興味をもったことを振り返って、何故そこに興味をもったのか、どういったことがきっかけで何をやりたいと考えたのか、そういったことを一度整理したかったというのが理由の一つ。
もう一つは、振り返りというものを定期的にする習慣をつけたかったのが理由。

今年は書いてて感じたのが、去年興味を持ったことに対して、今年何を行ったか、何を残せたかも書きたかった。来年は振り返りとあわせてこちらも書こう。

SinatraからPadrinoへのお引越し

ここ最近久しぶりにRubyと戯れている@_zooです。

今回のお話はVOYAGE GROUP エンジニアブログ : Advent Calendar 2012の12/22のエントリーとなります。

Advent Calendarを書くのは初めてですが、大掃除正月をゆっくり過ごすためのお話は既に出されていたので、年末らしい話!と考えるのはやめました。

じゃあ、なんの話にするかと悩みましたが、ここ最近久しぶりに戯れているRubyのお話にします。

Rubyといっても広いので、今回はSinatraと今年になって知ったPadrinoという2つのフレームワークについてのお話にします。

Sinatraは知っている方も多いかもしれませんが、Rubyの軽量フレームワークです。

PadrinoSinatraをベースにしつつも、LoggerやORMをとりこんだ、こちらも軽量フレームワークです。

実のところ、SinatraRubyで一番最初に触ったフレームワークですが、Padrinoを知ったのは今年になってからです。きっかけは、@bash0C7 からの"Padrinoいいですよー"という一言でした。

Shibuya.rbでも何回か話にあがり、VGでは実際にプロダクトの開発でも使用されています。

既存のSinatraアプリをPadrinoアプリに置き換えてみたいけど、できるのかな?という単純な興味が発端となり試してみたらできたので、そのやり方を紹介させていただきます。

まず、Sinatraアプリを作ります。今回はBundlerを利用します。

gem install bundler
mkdir voyage_advent_calendar_2012
cd voyage_advent_calendar_2012

ここからがアプリの作成です。 まずはGemfileを用意します。

# Gemfile
source :rubygems
gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"

Gemfileに記入されているSinatraを入れます。

bundle install --path gems

次はmy_app.rbの準備です。

# my_app.rb
require 'sinatra/base'
class MyApp < Sinatra::Base
  get '/' do
    'Hello Sinatra!'
  end
  run! if app_file == $0
end

これでもう準備は整いました。

後はたった一行のコマンドを打つだけで動きます!

bundle exec ruby my_app.rb

http://localhost:4567/ にアクセスしてHello Sinatra!が見えたら、作成成功です。

さて、ここまでだとSinatraREADMEの最初の数行とかわりありません。

よくある話ですがアプリは動かし始めると、だんだん要求が増えてきます。

SInatraは最初に書いた通り軽量フレームワークなので、もしかしたら物足りなくなるかもしれません。

自分で不足分だけ補っていくのはそれはそれで楽しいですが、 Sinatraでは物足りない部分をさくっと補いたくなります。

そんな時に便利なのがPadrinoです。

Padrino is a ruby framework built upon the Sinatra web library. Sinatra is a DSL for creating simple web applications in Ruby.

という説明の通り、ベースにはSinatraがあり、ちょっとアプリが複雑になってきて、 Sinatraだと物足りたくなったら、こちらに乗り換えると、物足りない部分がさくっと補われます。

え、フレームワークの乗り換えなんて面倒、と思いきや、そうでもないです。 SinatraのREADMEの最初の数行を読むのと同等の気力でどうにかなります。

まずはPadrinoを入れる準備をします。 先程同じようにGemfileを編集します。この際、Sinatraの部分は削除してしまいます。

# Gemfile
source :rubygems
gem 'padrino', :git => "git://github.com/padrino/padrino-framework.git"

不用となったgemをばさっと削除して、必要なものだけ入れ直します。

rm -rf gems
bundle

Padrinoを入れたところで、次はPadrinoプロジェクトの作成です。 generateタスクが用意されているので、コマンド一行でプロジェクトのテンプレートが作成されます。

bundle exec padrino g project ../voyage_advent_calendar_2012 -n my_app
#conflict  Gemfile
#Overwrite /your_app_path/Gemfile? (enter "h" for help) [Ynaqdh] Y

Gemfileがコンフリクトした!というメッセージが出てきます。 手元にあったGemfileとPadrinoが作成しようとしているGemfileが同じファイルだからです。 ここでは何も考えずYを押します。

さて、Padrinoは自分で必要となるgemが記入されたGemfileを用意してくれました。 先ほど同様、追記されたであろうgemを入れます。

bundle

これでPadrinoを動かす準備はできました。 後は、自分が作ったアプリケーションのコアファイルを移動し、 Sinatraを呼び出している部分をPadrinoに変えるだけです。

mv my_app.rb app/app.rb

# my_app.rb
class MyApp < Padrino::Application
  get '/' do
    'Hello Padrino!'
  end
end

これでPadrinoへの載せ替えは終了です。

最後に、Padrinoを起動させます。

bundle exec padrino start

http://localhost:3000/ にアクセスしてHello Padrino!が見えたら、引越し成功です。

ということで、年末とは関係ないSinatraからPadrinoへのお引越し話でした。

明日は@hironomiuのエントリーです。

s3cmdを使ってみた

サービスで利用している画像ファイルをAWSに移行することになった。

今までS3そのものは使ったことがあったけれど、s3cmdは使ったことがなかったが、今回はサーバ側にs3cmdを入れてもらい、それを利用することになった。s3cmdを使うことになった理由として、下記の点がある。

  • アプリ側とAWSの鍵情報を切り離して考えやすい
  • ライブラリの選定などしなくてよい

以前はアプリ側の設定ファイルに秘密鍵とトークンを書いていたけれど、今回は外部サービスにアプリケーションのソースを配置しているので、できるだけ設定関係のファイルはアプリケーションの中から排除したかった。そこで、コマンドベースにすれば、管理上アプリから切り離しやすいといった流れになり、s3cmdを使うことになった。

s3cmdの使い方はとてもシンプルで、ファイルの追加、取得、削除は簡単にできる。

#ファイルの追加
s3cmd -c #{config_file} put #{path_from} s3://#{bucket_name}/#{s3_path_to} 
#ファイルの取得
s3cmd -c #{config_file} get s3://#{bucket_name}/#{s3_path_to} 
#ファイルの削除
s3cmd -c #{config_file} del s3://#{bucket_name}/#{s3_path_to} 
#ファイルの確認
s3cmd -c #{config_file} info s3://#{bucket_name}/#{s3_path_to} 
#bucketの確認
s3cmd -c #{config_file} ls s3://#{bucket_name}/
#bucketとの同期
s3cmd -c #{config_file} sync #{path_from} s3://#{bucket_name}/#{s3_path_to} 

※オプションとして任意のファイルのみ、任意のファイルを除外といった指定もできる。例えば、svnファイルは除外したいというときは、下記のようにすればよい。
s3cmd -c #{config_file} put --exclude '.svn*' #{path_from} s3://#{bucket_name}/#{s3_path_to}

内部的にはHTTPの通信が行われて、pythonで作られている。ソースコードは公開されているので、内部でどんな処理が行われているかみればわかる。ソースコードはさほど多くないので、サクサク読める。
今回設定ファイルで制御したのは、socket_timeoutの設定だった。デフォルトは300秒=5分となっており、Webアプリケーションとしてはそれはないよね?というtimeout設定だった。また、timeoutの設定値はリトライの待機時間としても使用されているようなので、ここを300秒にしておくとリトライが走った場合に5分以上の待ち時間が発生する、多分。
S3へのアップロード時間を毎日1時間おきに測定したところ1.5Mの1ファイルをアップするのにおおよそ1秒かかっていた。しかし、1週間に3回ほどは5秒くらいになっていたので、timeout設定は5秒にしておくことにした。

他にも、今回はS3をwebsite化したり、bucketをロギングしたりと、使ったことのない機能をいくつか使ってみた。
またconfigファイルに設定できる情報がwebに見当たらなかったので次のブログで各設定値をまとめてみたい。今回はs3cmdの備忘録としてシンプルな使い方だけ書いておく。

るびまの記事を読んでChefを使ってみた

先月から新しい開発チームに所属し、個人開発環境はローカルのVMという状況になった。
開発マシンのセットアップ手順はRedmineのwikiに書かれているから、それに従って環境を構築していくというよくあるケース。

環境を構築しようとしたけど、wikiに手順がまとめられているならその手順を自動化しとけばいいじゃないと考え、最近色々なところでみたChefというツールを使ってみることを決意。

るびまChef でサーバ管理を楽チンにしよう! (第 1 回)という記事を見つけたので、
これを読んでみたところ、下記のような理解に落ち着いた。

Chefはサーバの構成管理ツール、サーバを構築する際に、まずは構成の設定ファイルを作成し、設定ファイルに従ってサーバを構築してくれ、サーバ内の構成を保ってくれる。

複数台のサーバはもちろん、個人開発環境など、繰り返し同じことを行う場合は、こういったツールで一度設定ファイルを作成し、一度作ったら後はツールの方でよしなに構築してくれる方が嬉しい。

同じようなツールでPuppetがあるけれど、これとどちらを使うか悩んでいたところ上述のるびまの記事にあった、PuppetとChefの比較をみて設定ファイルの書きやすさからChefを使うことにした。
f:id:johnsmith0707:20120502130133p:plain

そんなわけで、Chefを使う方向で固まり、るびまの記事とオフィシャルのドキュメントを読み進めていく。
るびまの記事を読んでからChefのオフィシャルページを読んだところ、Chefでは3つの役割が内部で分担されていることがわかった。一つ目はChefサーバ、二つ目はNodeサーバ、三つ目はWorkstation、という役割。
f:id:johnsmith0707:20120502131049p:plain

Chefサーバは構成設定ファイルが配置されているサーバ、Nodeサーバは構成設定ファイルにより構築されたサーバ、WorkStationは構成設定ファイルの変更をChefサーバに反映する環境。Chefサーバは1台、NodeサーバはX台、Workstationは最低1台、となる形。この形がChefの基本形となる。

この3つ役割分担はChefを使う上での基本であるが、今回自分が作ろうとした開発環境のように1台のセットアップを自動化する場合にはChefサーバを立てる必要はなく、Chef-Soloという1台構成用の使い方が存在する。るびまの記事にあった図を拝借すると下記のような形になる。

f:id:johnsmith0707:20120502130959p:plain


さて、今までさんざん出てきた構成設定ファイルだが、どんな構成になっているのかというと、Cookbookという名前で構成設定ファイルのセットが用意されている。
Cookbookのセットは下記のようなファイル構成となっている。

f:id:johnsmith0707:20120502133403p:plain

※オフィシャルを見たところ若干違う?以下抜粋。
A cookbook is a collection of recipe, resource definition, attribute, library, cookbook file and template files that chef uses to configure a system, plus metadata.

実際Cookbookを作ってみたところCookbookの構成はおおむねオフィシャルにかかれている通りだった。

vagrant@ubuntu-oneiric:~/chef-repo/cookbooks/php$ pwd /home/vagrant/chef-repo/cookbooks/php
vagrant@ubuntu-oneiric:~/chef-repo/cookbooks/php$ ls
attributes  definitions  files  libraries  metadata.rb  providers  README.md  recipes  resources  templates

ファイルの関係性をみると↓のような形になる。
f:id:johnsmith0707:20120502133729p:plain


最初のcookbookはるびまの記事写経ということで、rakeタスクでcookbookを作成したが、今ではknifeといわれるツールが主流の模様。長くなったが、ここから環境の構築が始まる。

環境:VirtualBox with Vagrant /OS:ubuntu 11.10 server amd64
まずは今回の作業で必要となるgit、rakeをgemでいれる。chef自身は既にインストールされているのでインストールの必要はないのでupdateのみとする。

$ sudo apt-get install git
$ sudo gem install rake
$ sudo gem update chef
$ sudo apt-get install emacs

まずはchef-repo(ひな形ディレクトリ)をcloneして、rakeタスクからcookbookを作成する。目標としては今回手動でいれたもの(git,rake,emacs)を自動でさくっと入れられるようにする。このためホストマシンとの共有ディレクトリ(/vagrant_data)にディレクトリを作成する。

$ cd /vagrant_data
$ git clone git://github.com/opscode/chef-repo.git
$ cd chef-repo
$ rake new_cookbook COOKBOOK=git
$ emacs cookbooks/git/recipes/default.rb

ファイルの中身は下記のような感じ。オフィシャルの説明を参考に書いてみた。今回はpackageから入れるので下記の形。

apt_package 'git' do
  action :install
end

続けてrakeも。

$ rake new_cookbook COOKBOOK=rake
$ emacs cookbooks/rake/recipes/default.rb
gem_package 'rake' do
  action :install
end

次はchef。

$ rake new_cookbook COOKBOOK=chef
$ emacs cookbooks/chef/recipes/default.rb
gem_package 'chef' do
  action :upgrade
end

最後にemacs.

$ rake new_cookbook COOKBOOK=emacs
$ emacs cookbooks/chef/recipes/default.rb
apt_package 'emacs' do
  action :install
end

とここまでは作成したのが、最後にcookbookへのパスを通すのとchefのログレベルの設定を行う必要がある。solo.rb、chef.jsonの2ファイルを用意する。これらのどこに置くかというと、自分でパスを設定できるので、どこでもいいが、個人的にはchefを使うユーザの$HOMEが良さそうだと感じた。
しかし、今回はvagrantのホストマシンとの共有ディレクトリに配置したいため、仮想マシンとホストマシンの共有ディレクトリである/vagrant_dataに配置。
どうせならこのファイルの作成も自動化したい!なので、先ほど作成したchefのレシピに下記を追記。

template "/vagrant_data/solo.rb" do
  owner "vagrant"
  group "vagrant"
  mode "0744"
end

template "/vagrant_data/chef.json" do
  owner "vagrant"
  group "vagrant"
  mode "0744"
end

ファイルの中身は↓のような形。

# solo.rb
file_cache_path "/tmp/chef-solo"
cookbook_path "/vagrant_data/chef-repo/cookbooks"
log_level :debug
# chef.json
{
    "run_list": [
       "recipe[git]","recipe[rake]","recipe[chef]","recipe[emacs]"
    ]
}

各cookbookとsolo.rbとchef.jsonができたのでいよいよ別のマシンでやってみよう!ということで、ホストマシンから新しいvirtualboxを立てて上記で作成したレシピファイルを移動。solo.rbとchef.jsonはそもそもchef-soloを叩く時に必要なのでtemplatesに入れておいても意味がないことにここで気づいた。死にたい。

# 新しいマシン
sudo chef-solo -c /vagrant_data/solo.rb -j /vagrant_data/chef.json

ずらずらーっとでてきた。

vagrant@ubuntu-oneiric:~$ sudo chef-solo -c /vagrant_data/solo.rb -j /vagrant_data/chef.json
[Wed, 09 May 2012 04:15:13 +0100] INFO: *** Chef 0.10.8 ***
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Building node object for ubuntu-oneiric
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Extracting run list from JSON attributes provided on command line
[Wed, 09 May 2012 04:15:13 +0100] INFO: Setting the run_list to ["recipe[git]", "recipe[rake]", "recipe[chef]", "recipe[emacs]"] from JSON
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Applying attributes from json file
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Platform is ubuntu version 11.10
[Wed, 09 May 2012 04:15:13 +0100] INFO: Run List is [recipe[git], recipe[rake], recipe[chef], recipe[emacs]]
[Wed, 09 May 2012 04:15:13 +0100] INFO: Run List expands to [git, rake, chef, emacs]
[Wed, 09 May 2012 04:15:13 +0100] INFO: Starting Chef Run for ubuntu-oneiric
[Wed, 09 May 2012 04:15:13 +0100] INFO: Running start handlers
[Wed, 09 May 2012 04:15:13 +0100] INFO: Start handlers complete.
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: No chefignore file found at /vagrant_data/chef-repo/cookbooks/chefignore no files will be ignored
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Loading Recipe git via include_recipe
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Found recipe default in cookbook git
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Loading Recipe rake via include_recipe
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Found recipe default in cookbook rake
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Loading Recipe chef via include_recipe
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Found recipe default in cookbook chef
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Loading Recipe emacs via include_recipe
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Found recipe default in cookbook emacs
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Loading from cookbook_path: /vagrant_data/chef-repo/cookbooks
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Converging node ubuntu-oneiric
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: Processing apt_package[git] on ubuntu-oneiric
[Wed, 09 May 2012 04:15:13 +0100] INFO: Processing apt_package[git] action install (git::default line 19)
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: apt_package[git] checking package status for git
[Wed, 09 May 2012 04:15:13 +0100] DEBUG: sh(apt-cache policy git)
git:
  Installed: (none)
  Candidate: 1:1.7.5.4-1
  Version table:
     1:1.7.5.4-1 0
        500 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main amd64 Packages
[Wed, 09 May 2012 04:15:15 +0100] DEBUG: apt_package[git] current version is nil
[Wed, 09 May 2012 04:15:15 +0100] DEBUG: apt_package[git] candidate version is 1:1.7.5.4-1
[Wed, 09 May 2012 04:15:15 +0100] DEBUG: Executing apt-get -q -y install git=1:1.7.5.4-1
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: ---- Begin output of apt-get -q -y install git=1:1.7.5.4-1 ----
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: STDOUT: Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  git-man liberror-perl
Suggested packages:
  git-doc git-el git-arch git-cvs git-svn git-email git-daemon-run git-gui
  gitk gitweb
The following NEW packages will be installed
  git git-man liberror-perl
0 upgraded, 3 newly installed, 0 to remove and 2 not upgraded.
Need to get 5,347 kB of archives.
After this operation, 12.6 MB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main liberror-perl all 0.17-1 [23.8 kB]
Get:2 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main git-man all 1:1.7.5.4-1 [571 kB]
Get:3 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main git amd64 1:1.7.5.4-1 [4,752 kB]
Fetched 5,347 kB in 5s (1,034 kB/s)
Selecting previously deselected package liberror-perl.
(Reading database ... 56417 files and directories currently installed.)
Unpacking liberror-perl (from .../liberror-perl_0.17-1_all.deb) ...
Selecting previously deselected package git-man.
Unpacking git-man (from .../git-man_1%3a1.7.5.4-1_all.deb) ...
Selecting previously deselected package git.
Unpacking git (from .../git_1%3a1.7.5.4-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up liberror-perl (0.17-1) ...
Setting up git-man (1:1.7.5.4-1) ...
Setting up git (1:1.7.5.4-1) ...
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: STDERR: 
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: ---- End output of apt-get -q -y install git=1:1.7.5.4-1 ----
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: Ran apt-get -q -y install git=1:1.7.5.4-1 returned 0
[Wed, 09 May 2012 04:15:26 +0100] INFO: apt_package[git] installed version 1:1.7.5.4-1
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: Processing gem_package[rake] on ubuntu-oneiric
[Wed, 09 May 2012 04:15:26 +0100] INFO: Processing gem_package[rake] action install (rake::default line 19)
[Wed, 09 May 2012 04:15:26 +0100] DEBUG: gem_package[rake] no installed version found for rake (>= 0)
[Wed, 09 May 2012 04:15:32 +0100] DEBUG:  found gem rake version 0.9.2.2 for platform ruby from http://rubygems.org/
Fetching: rake-0.9.2.2.gem (100%)
[Wed, 09 May 2012 04:15:33 +0100] INFO: gem_package[rake] installed version 0.9.2.2
[Wed, 09 May 2012 04:15:33 +0100] DEBUG: Processing gem_package[chef] on ubuntu-oneiric
[Wed, 09 May 2012 04:15:33 +0100] INFO: Processing gem_package[chef] action upgrade (chef::default line 19)
[Wed, 09 May 2012 04:15:33 +0100] DEBUG: gem_package[chef] found installed gem chef version 0.10.8 matching chef (>= 0)
Building native extensions.  This could take a while...
Fetching: ipaddress-0.8.0.gem (100%)
[Wed, 09 May 2012 04:15:57 +0100] INFO: gem_package[chef] upgraded from 0.10.8 to 
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Processing template[/vagrant_data/solo.rb] on ubuntu-oneiric
[Wed, 09 May 2012 04:15:57 +0100] INFO: Processing template[/vagrant_data/solo.rb] action create (chef::default line 23)
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Current content's checksum:  3b1ab7375d1a452432b9e667df95b0ac8710c13f01c9f0bfbac756c7866550e6
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Rendered content's checksum: 3b1ab7375d1a452432b9e667df95b0ac8710c13f01c9f0bfbac756c7866550e6
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: template[/vagrant_data/solo.rb] content has not changed.
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Processing template[/vagrant_data/chef.json] on ubuntu-oneiric
[Wed, 09 May 2012 04:15:57 +0100] INFO: Processing template[/vagrant_data/chef.json] action create (chef::default line 29)
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Current content's checksum:  37ed952a3f583412c467103721d213b06caf363df9355137353c3950072a5f33
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Rendered content's checksum: 37ed952a3f583412c467103721d213b06caf363df9355137353c3950072a5f33
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: template[/vagrant_data/chef.json] content has not changed.
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: Processing apt_package[emacs] on ubuntu-oneiric
[Wed, 09 May 2012 04:15:57 +0100] INFO: Processing apt_package[emacs] action install (emacs::default line 19)
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: apt_package[emacs] checking package status for emacs
[Wed, 09 May 2012 04:15:57 +0100] DEBUG: sh(apt-cache policy emacs)
emacs:
  Installed: (none)
  Candidate: 23.3+1-1ubuntu4
  Version table:
     23.3+1-1ubuntu4 0
        500 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main amd64 Packages
[Wed, 09 May 2012 04:15:58 +0100] DEBUG: apt_package[emacs] current version is nil
[Wed, 09 May 2012 04:15:58 +0100] DEBUG: apt_package[emacs] candidate version is 23.3+1-1ubuntu4
[Wed, 09 May 2012 04:15:58 +0100] DEBUG: Executing apt-get -q -y install emacs=23.3+1-1ubuntu4
[Wed, 09 May 2012 04:16:45 +0100] DEBUG: ---- Begin output of apt-get -q -y install emacs=23.3+1-1ubuntu4 ----
[Wed, 09 May 2012 04:16:45 +0100] DEBUG: STDOUT: Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  anthy anthy-common defoma dictionaries-common emacs23 emacs23-bin-common
  emacs23-common emacsen-common fontconfig gconf2-common hicolor-icon-theme
  iamerican ienglish-common ispell libanthy0 libasound2 libatk1.0-0
  libatk1.0-data libavahi-client3 libavahi-common-data libavahi-common3
  libcroco3 libcups2 libdatrie1 libfontenc1 libgconf2-4 libgd2-noxpm
  libgdk-pixbuf2.0-0 libgif4 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common
  libice6 libjasper1 libjpeg62 libm17n-0 libotf0 libpango1.0-0 librsvg2-2
  libsm6 libthai-data libthai0 libtiff4 libxcomposite1 libxcursor1 libxdamage1
  libxfixes3 libxfont1 libxft2 libxi6 libxinerama1 libxpm4 libxrandr2 libxt6
  m17n-contrib m17n-db shared-mime-info wamerican x-ttcidfont-conf x11-common
  xfonts-encodings xfonts-utils
Suggested packages:
  defoma-doc psfontmgr libfont-freetype-perl jed-extra emacs23-el spell
  libasound2-plugins libasound2-python cups-common libgd-tools librsvg2-common
  gvfs libjasper-runtime m17n-docs ttf-japanese-gothic ttf-japanese-mincho
  ttf-thryomanes ttf-baekmuk ttf-arphic-gbsn00lp ttf-arphic-bsmi00lp
  ttf-arphic-gkai00mp ttf-arphic-bkai00mp librsvg2-bin gawk
The following NEW packages will be installed
  anthy anthy-common defoma dictionaries-common emacs emacs23
  emacs23-bin-common emacs23-common emacsen-common fontconfig gconf2-common
  hicolor-icon-theme iamerican ienglish-common ispell libanthy0 libasound2
  libatk1.0-0 libatk1.0-data libavahi-client3 libavahi-common-data
  libavahi-common3 libcroco3 libcups2 libdatrie1 libfontenc1 libgconf2-4
  libgd2-noxpm libgdk-pixbuf2.0-0 libgif4 libgtk2.0-0 libgtk2.0-bin
  libgtk2.0-common libice6 libjasper1 libjpeg62 libm17n-0 libotf0
  libpango1.0-0 librsvg2-2 libsm6 libthai-data libthai0 libtiff4
  libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxfont1 libxft2 libxi6
  libxinerama1 libxpm4 libxrandr2 libxt6 m17n-contrib m17n-db shared-mime-info
  wamerican x-ttcidfont-conf x11-common xfonts-encodings xfonts-utils
0 upgraded, 63 newly installed, 0 to remove and 2 not upgraded.
Need to get 40.1 MB of archives.
After this operation, 137 MB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libasound2 amd64 1.0.24.1-0ubuntu10 [417 kB]
Get:2 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libatk1.0-data amd64 2.2.0-0ubuntu1 [12.6 kB]
Get:3 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libatk1.0-0 amd64 2.2.0-0ubuntu1 [59.9 kB]
Get:4 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libavahi-common-data amd64 0.6.30-4ubuntu1 [22.4 kB]
Get:5 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libavahi-common3 amd64 0.6.30-4ubuntu1 [25.0 kB]
Get:6 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libavahi-client3 amd64 0.6.30-4ubuntu1 [43.2 kB]
Get:7 http://gb.archive.ubuntu.com/ubuntu/ oneiric-updates/main libcups2 amd64 1.5.0-8ubuntu6 [173 kB]
Get:8 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libdatrie1 amd64 0.2.4-3 [15.9 kB]
Get:9 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libjpeg62 amd64 6b1-1ubuntu2 [88.3 kB]
Get:10 http://gb.archive.ubuntu.com/ubuntu/ oneiric-updates/main libjasper1 amd64 1.900.1-7ubuntu2.11.10.1 [155 kB]
Get:11 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libtiff4 amd64 3.9.5-1ubuntu1 [144 kB]
Get:12 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libgdk-pixbuf2.0-0 amd64 2.24.0-1ubuntu1 [198 kB]
Get:13 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libgtk2.0-common amd64 2.24.6-0ubuntu5 [114 kB]
Get:14 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libthai-data amd64 0.1.15-2 [177 kB]
Get:15 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libthai0 amd64 0.1.15-2 [19.3 kB]
Get:16 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxft2 amd64 2.2.0-3ubuntu1 [42.3 kB]
Get:17 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main fontconfig amd64 2.8.0-3ubuntu2 [157 kB]
Get:18 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libpango1.0-0 amd64 1.29.3+git20110916-0ubuntu1 [365 kB]
Get:19 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxcomposite1 amd64 1:0.4.3-2 [7,806 B]
Get:20 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxfixes3 amd64 1:5.0-4 [12.9 kB]
Get:21 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxcursor1 amd64 1:1.1.12-1 [22.6 kB]
Get:22 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxdamage1 amd64 1:1.1.3-2 [7,572 B]
Get:23 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxi6 amd64 2:1.4.3-3ubuntu1 [30.6 kB]
Get:24 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxinerama1 amd64 2:1.1.1-3 [8,044 B]
Get:25 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxrandr2 amd64 2:1.3.2-2 [17.9 kB]
Get:26 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main shared-mime-info amd64 0.90-1ubuntu4 [459 kB]
Get:27 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libgtk2.0-0 amd64 2.24.6-0ubuntu5 [2,250 kB]
Get:28 http://gb.archive.ubuntu.com/ubuntu/ oneiric-updates/main x11-common amd64 1:7.6+7ubuntu7.1 [56.8 kB]
Get:29 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libice6 amd64 2:1.0.7-2 [46.2 kB]
Get:30 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libcroco3 amd64 0.6.2-1 [101 kB]
Get:31 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main librsvg2-2 amd64 2.34.1-2 [105 kB]
Get:32 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libsm6 amd64 2:1.2.0-2 [18.1 kB]
Get:33 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxt6 amd64 1:1.1.1-2 [185 kB]
Get:34 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main anthy-common all 9100h-9ubuntu1 [3,358 kB]
Get:35 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libanthy0 amd64 9100h-9ubuntu1 [171 kB]
Get:36 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main anthy amd64 9100h-9ubuntu1 [148 kB]
Get:37 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main defoma all 0.11.12ubuntu1 [91.2 kB]
Get:38 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main dictionaries-common all 1.11.5ubuntu1 [247 kB]
Get:39 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main emacsen-common all 1.4.19ubuntu2 [17.7 kB]
Get:40 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main emacs23-common all 23.3+1-1ubuntu4 [22.5 MB]
Get:41 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main emacs23-bin-common amd64 23.3+1-1ubuntu4 [176 kB]
Get:42 http://gb.archive.ubuntu.com/ubuntu/ oneiric-updates/main gconf2-common all 3.2.3-0ubuntu0.1 [20.4 kB]
Get:43 http://gb.archive.ubuntu.com/ubuntu/ oneiric-updates/main libgconf2-4 amd64 3.2.3-0ubuntu0.1 [171 kB]
Get:44 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libgif4 amd64 4.1.6-9 [41.4 kB]
Get:45 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libgd2-noxpm amd64 2.0.36~rc1~dfsg-5.1ubuntu1 [198 kB]
Get:46 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libotf0 amd64 0.9.12-1 [49.5 kB]
Get:47 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main m17n-db all 1.6.2-2 [1,684 kB]
Get:48 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main m17n-contrib all 1.1.12-2 [504 kB]
Get:49 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libm17n-0 amd64 1.6.2-3 [300 kB]
Get:50 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxpm4 amd64 1:3.5.9-1ubuntu1 [37.8 kB]
Get:51 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main emacs23 amd64 23.3+1-1ubuntu4 [3,334 kB]
Get:52 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main emacs all 23.3+1-1ubuntu4 [5,736 B]
Get:53 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main hicolor-icon-theme amd64 0.12-1ubuntu1 [10.0 kB]
Get:54 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main ispell amd64 3.3.02-5 [162 kB]
Get:55 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main ienglish-common all 3.3.02-5 [11.2 kB]
Get:56 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main iamerican all 3.3.02-5 [187 kB]
Get:57 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libfontenc1 amd64 1:1.1.0-1 [15.4 kB]
Get:58 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libgtk2.0-bin amd64 2.24.6-0ubuntu5 [10.3 kB]
Get:59 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main libxfont1 amd64 1:1.4.4-1 [133 kB]
Get:60 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main wamerican all 6-3 [269 kB]
Get:61 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main xfonts-encodings all 1:1.0.4-1 [583 kB]
Get:62 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main xfonts-utils amd64 1:7.6+1 [96.4 kB]
Get:63 http://gb.archive.ubuntu.com/ubuntu/ oneiric/main x-ttcidfont-conf all 32+nmu2 [20.2 kB]
Preconfiguring packages ...
Fetched 40.1 MB in 22s (1,775 kB/s)
Selecting previously deselected package libasound2.
(Reading database ... 56992 files and directories currently installed.)
Unpacking libasound2 (from .../libasound2_1.0.24.1-0ubuntu10_amd64.deb) ...
Selecting previously deselected package libatk1.0-data.
Unpacking libatk1.0-data (from .../libatk1.0-data_2.2.0-0ubuntu1_amd64.deb) ...
Selecting previously deselected package libatk1.0-0.
Unpacking libatk1.0-0 (from .../libatk1.0-0_2.2.0-0ubuntu1_amd64.deb) ...
Selecting previously deselected package libavahi-common-data.
Unpacking libavahi-common-data (from .../libavahi-common-data_0.6.30-4ubuntu1_amd64.deb) ...
Selecting previously deselected package libavahi-common3.
Unpacking libavahi-common3 (from .../libavahi-common3_0.6.30-4ubuntu1_amd64.deb) ...
Selecting previously deselected package libavahi-client3.
Unpacking libavahi-client3 (from .../libavahi-client3_0.6.30-4ubuntu1_amd64.deb) ...
Selecting previously deselected package libcups2.
Unpacking libcups2 (from .../libcups2_1.5.0-8ubuntu6_amd64.deb) ...
Selecting previously deselected package libdatrie1.
Unpacking libdatrie1 (from .../libdatrie1_0.2.4-3_amd64.deb) ...
Selecting previously deselected package libjpeg62.
Unpacking libjpeg62 (from .../libjpeg62_6b1-1ubuntu2_amd64.deb) ...
Selecting previously deselected package libjasper1.
Unpacking libjasper1 (from .../libjasper1_1.900.1-7ubuntu2.11.10.1_amd64.deb) ...
Selecting previously deselected package libtiff4.
Unpacking libtiff4 (from .../libtiff4_3.9.5-1ubuntu1_amd64.deb) ...
Selecting previously deselected package libgdk-pixbuf2.0-0.
Unpacking libgdk-pixbuf2.0-0 (from .../libgdk-pixbuf2.0-0_2.24.0-1ubuntu1_amd64.deb) ...
Selecting previously deselected package libgtk2.0-common.
Unpacking libgtk2.0-common (from .../libgtk2.0-common_2.24.6-0ubuntu5_amd64.deb) ...
Selecting previously deselected package libthai-data.
Unpacking libthai-data (from .../libthai-data_0.1.15-2_amd64.deb) ...
Selecting previously deselected package libthai0.
Unpacking libthai0 (from .../libthai0_0.1.15-2_amd64.deb) ...
Selecting previously deselected package libxft2.
Unpacking libxft2 (from .../libxft2_2.2.0-3ubuntu1_amd64.deb) ...
Selecting previously deselected package fontconfig.
Unpacking fontconfig (from .../fontconfig_2.8.0-3ubuntu2_amd64.deb) ...
Selecting previously deselected package libpango1.0-0.
Unpacking libpango1.0-0 (from .../libpango1.0-0_1.29.3+git20110916-0ubuntu1_amd64.deb) ...
Selecting previously deselected package libxcomposite1.
Unpacking libxcomposite1 (from .../libxcomposite1_1%3a0.4.3-2_amd64.deb) ...
Selecting previously deselected package libxfixes3.
Unpacking libxfixes3 (from .../libxfixes3_1%3a5.0-4_amd64.deb) ...
Selecting previously deselected package libxcursor1.
Unpacking libxcursor1 (from .../libxcursor1_1%3a1.1.12-1_amd64.deb) ...
Selecting previously deselected package libxdamage1.
Unpacking libxdamage1 (from .../libxdamage1_1%3a1.1.3-2_amd64.deb) ...
Selecting previously deselected package libxi6.
Unpacking libxi6 (from .../libxi6_2%3a1.4.3-3ubuntu1_amd64.deb) ...
Selecting previously deselected package libxinerama1.
Unpacking libxinerama1 (from .../libxinerama1_2%3a1.1.1-3_amd64.deb) ...
Selecting previously deselected package libxrandr2.
Unpacking libxrandr2 (from .../libxrandr2_2%3a1.3.2-2_amd64.deb) ...
Selecting previously deselected package shared-mime-info.
Unpacking shared-mime-info (from .../shared-mime-info_0.90-1ubuntu4_amd64.deb) ...
Selecting previously deselected package libgtk2.0-0.
Unpacking libgtk2.0-0 (from .../libgtk2.0-0_2.24.6-0ubuntu5_amd64.deb) ...
Selecting previously deselected package x11-common.
Unpacking x11-common (from .../x11-common_1%3a7.6+7ubuntu7.1_amd64.deb) ...
Selecting previously deselected package libice6.
Unpacking libice6 (from .../libice6_2%3a1.0.7-2_amd64.deb) ...
Selecting previously deselected package libcroco3.
Unpacking libcroco3 (from .../libcroco3_0.6.2-1_amd64.deb) ...
Selecting previously deselected package librsvg2-2.
Unpacking librsvg2-2 (from .../librsvg2-2_2.34.1-2_amd64.deb) ...
Selecting previously deselected package libsm6.
Unpacking libsm6 (from .../libsm6_2%3a1.2.0-2_amd64.deb) ...
Selecting previously deselected package libxt6.
Unpacking libxt6 (from .../libxt6_1%3a1.1.1-2_amd64.deb) ...
Selecting previously deselected package anthy-common.
Unpacking anthy-common (from .../anthy-common_9100h-9ubuntu1_all.deb) ...
Selecting previously deselected package libanthy0.
Unpacking libanthy0 (from .../libanthy0_9100h-9ubuntu1_amd64.deb) ...
Selecting previously deselected package anthy.
Unpacking anthy (from .../anthy_9100h-9ubuntu1_amd64.deb) ...
Selecting previously deselected package defoma.
Unpacking defoma (from .../defoma_0.11.12ubuntu1_all.deb) ...
Selecting previously deselected package dictionaries-common.
Unpacking dictionaries-common (from .../dictionaries-common_1.11.5ubuntu1_all.deb) ...
Adding 'diversion of /usr/share/dict/words to /usr/share/dict/words.pre-dictionaries-common by dictionaries-common'
Selecting previously deselected package emacsen-common.
Unpacking emacsen-common (from .../emacsen-common_1.4.19ubuntu2_all.deb) ...
Selecting previously deselected package emacs23-common.
Unpacking emacs23-common (from .../emacs23-common_23.3+1-1ubuntu4_all.deb) ...
Selecting previously deselected package emacs23-bin-common.
Unpacking emacs23-bin-common (from .../emacs23-bin-common_23.3+1-1ubuntu4_amd64.deb) ...
Selecting previously deselected package gconf2-common.
Unpacking gconf2-common (from .../gconf2-common_3.2.3-0ubuntu0.1_all.deb) ...
Selecting previously deselected package libgconf2-4.
Unpacking libgconf2-4 (from .../libgconf2-4_3.2.3-0ubuntu0.1_amd64.deb) ...
Selecting previously deselected package libgif4.
Unpacking libgif4 (from .../libgif4_4.1.6-9_amd64.deb) ...
Selecting previously deselected package libgd2-noxpm.
Unpacking libgd2-noxpm (from .../libgd2-noxpm_2.0.36~rc1~dfsg-5.1ubuntu1_amd64.deb) ...
Selecting previously deselected package libotf0.
Unpacking libotf0 (from .../libotf0_0.9.12-1_amd64.deb) ...
Selecting previously deselected package m17n-db.
Unpacking m17n-db (from .../m17n-db_1.6.2-2_all.deb) ...
Selecting previously deselected package m17n-contrib.
Unpacking m17n-contrib (from .../m17n-contrib_1.1.12-2_all.deb) ...
Selecting previously deselected package libm17n-0.
Unpacking libm17n-0 (from .../libm17n-0_1.6.2-3_amd64.deb) ...
Selecting previously deselected package libxpm4.
Unpacking libxpm4 (from .../libxpm4_1%3a3.5.9-1ubuntu1_amd64.deb) ...
Selecting previously deselected package emacs23.
Unpacking emacs23 (from .../emacs23_23.3+1-1ubuntu4_amd64.deb) ...
Selecting previously deselected package emacs.
Unpacking emacs (from .../emacs_23.3+1-1ubuntu4_all.deb) ...
Selecting previously deselected package hicolor-icon-theme.
Unpacking hicolor-icon-theme (from .../hicolor-icon-theme_0.12-1ubuntu1_amd64.deb) ...
Selecting previously deselected package ispell.
Unpacking ispell (from .../ispell_3.3.02-5_amd64.deb) ...
Selecting previously deselected package ienglish-common.
Unpacking ienglish-common (from .../ienglish-common_3.3.02-5_all.deb) ...
Selecting previously deselected package iamerican.
Unpacking iamerican (from .../iamerican_3.3.02-5_all.deb) ...
Selecting previously deselected package libfontenc1.
Unpacking libfontenc1 (from .../libfontenc1_1%3a1.1.0-1_amd64.deb) ...
Selecting previously deselected package libgtk2.0-bin.
Unpacking libgtk2.0-bin (from .../libgtk2.0-bin_2.24.6-0ubuntu5_amd64.deb) ...
Selecting previously deselected package libxfont1.
Unpacking libxfont1 (from .../libxfont1_1%3a1.4.4-1_amd64.deb) ...
Selecting previously deselected package wamerican.
Unpacking wamerican (from .../archives/wamerican_6-3_all.deb) ...
Selecting previously deselected package xfonts-encodings.
Unpacking xfonts-encodings (from .../xfonts-encodings_1%3a1.0.4-1_all.deb) ...
Selecting previously deselected package xfonts-utils.
Unpacking xfonts-utils (from .../xfonts-utils_1%3a7.6+1_amd64.deb) ...
Selecting previously deselected package x-ttcidfont-conf.
Unpacking x-ttcidfont-conf (from .../x-ttcidfont-conf_32+nmu2_all.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Processing triggers for install-info ...
Setting up libasound2 (1.0.24.1-0ubuntu10) ...
Setting up libatk1.0-data (2.2.0-0ubuntu1) ...
Setting up libatk1.0-0 (2.2.0-0ubuntu1) ...
Setting up libavahi-common-data (0.6.30-4ubuntu1) ...
Setting up libavahi-common3 (0.6.30-4ubuntu1) ...
Setting up libavahi-client3 (0.6.30-4ubuntu1) ...
Setting up libcups2 (1.5.0-8ubuntu6) ...
Setting up libdatrie1 (0.2.4-3) ...
Setting up libjpeg62 (6b1-1ubuntu2) ...
Setting up libjasper1 (1.900.1-7ubuntu2.11.10.1) ...
Setting up libtiff4 (3.9.5-1ubuntu1) ...
Setting up libgdk-pixbuf2.0-0 (2.24.0-1ubuntu1) ...
Setting up libgtk2.0-common (2.24.6-0ubuntu5) ...
Setting up libthai-data (0.1.15-2) ...
Setting up libthai0 (0.1.15-2) ...
Setting up libxft2 (2.2.0-3ubuntu1) ...
Setting up fontconfig (2.8.0-3ubuntu2) ...
Cleaning up old fontconfig caches... done.
Regenerating fonts cache... done.
Setting up libpango1.0-0 (1.29.3+git20110916-0ubuntu1) ...
Setting up libxcomposite1 (1:0.4.3-2) ...
Setting up libxfixes3 (1:5.0-4) ...
Setting up libxcursor1 (1:1.1.12-1) ...
Setting up libxdamage1 (1:1.1.3-2) ...
Setting up libxi6 (2:1.4.3-3ubuntu1) ...
Setting up libxinerama1 (2:1.1.1-3) ...
Setting up libxrandr2 (2:1.3.2-2) ...
Setting up shared-mime-info (0.90-1ubuntu4) ...
Setting up libgtk2.0-0 (2.24.6-0ubuntu5) ...
Setting up x11-common (1:7.6+7ubuntu7.1) ...
Setting up libice6 (2:1.0.7-2) ...
Setting up libcroco3 (0.6.2-1) ...
Setting up librsvg2-2 (2.34.1-2) ...
Setting up libsm6 (2:1.2.0-2) ...
Setting up libxt6 (1:1.1.1-2) ...
Setting up anthy-common (9100h-9ubuntu1) ...
Setting up libanthy0 (9100h-9ubuntu1) ...
Setting up anthy (9100h-9ubuntu1) ...
Setting up defoma (0.11.12ubuntu1) ...
Setting up dictionaries-common (1.11.5ubuntu1) ...
Setting up emacsen-common (1.4.19ubuntu2) ...
emacsen-common: Handling install of emacsen flavor emacs
Setting up emacs23-common (23.3+1-1ubuntu4) ...
Setting up emacs23-bin-common (23.3+1-1ubuntu4) ...
update-alternatives: using /usr/bin/b2m.emacs23 to provide /usr/bin/b2m (b2m) in auto mode.
update-alternatives: using /usr/bin/ctags.emacs23 to provide /usr/bin/ctags (ctags) in auto mode.
update-alternatives: using /usr/bin/ebrowse.emacs23 to provide /usr/bin/ebrowse (ebrowse) in auto mode.
update-alternatives: using /usr/bin/emacsclient.emacs23 to provide /usr/bin/emacsclient (emacsclient) in auto mode.
update-alternatives: using /usr/bin/etags.emacs23 to provide /usr/bin/etags (etags) in auto mode.
update-alternatives: using /usr/bin/grep-changelog.emacs23 to provide /usr/bin/grep-changelog (grep-changelog) in auto mode.
update-alternatives: using /usr/bin/rcs-checkin.emacs23 to provide /usr/bin/rcs-checkin (rcs-checkin) in auto mode.
Setting up gconf2-common (3.2.3-0ubuntu0.1) ...
Setting up libgconf2-4 (3.2.3-0ubuntu0.1) ...
Setting up libgif4 (4.1.6-9) ...
Setting up libgd2-noxpm (2.0.36~rc1~dfsg-5.1ubuntu1) ...
Setting up libotf0 (0.9.12-1) ...
Setting up m17n-db (1.6.2-2) ...
Setting up m17n-contrib (1.1.12-2) ...
Setting up libm17n-0 (1.6.2-3) ...
Setting up libxpm4 (1:3.5.9-1ubuntu1) ...
Setting up emacs23 (23.3+1-1ubuntu4) ...
update-alternatives: using /usr/bin/emacs23-x to provide /usr/bin/emacs (emacs) in auto mode.
emacs-install emacs23
install/dictionaries-common: Byte-compiling for emacsen flavour emacs23
emacsen-common: Handling install of emacsen flavor emacs23
emacsen-common: byte-compiling for emacs23
Setting up emacs (23.3+1-1ubuntu4) ...
Setting up hicolor-icon-theme (0.12-1ubuntu1) ...
Setting up libfontenc1 (1:1.1.0-1) ...
Setting up libgtk2.0-bin (2.24.6-0ubuntu5) ...
Setting up libxfont1 (1:1.4.4-1) ...
Setting up wamerican (6-3) ...
Setting up xfonts-encodings (1:1.0.4-1) ...
Setting up xfonts-utils (1:7.6+1) ...
Setting up x-ttcidfont-conf (32+nmu2) ...
Processing triggers for dictionaries-common ...
Setting up ispell (3.3.02-5) ...
Processing triggers for dictionaries-common ...
Setting up ienglish-common (3.3.02-5) ...
Setting up iamerican (3.3.02-5) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
Processing triggers for dictionaries-common ...
Extract templates from packages: 100%UG: STDERR: 
Updating font configuration of fontconfig...
Cleaning up category cid..
Cleaning up category truetype..
Cleaning up category type1..
Updating category type1..
Updating category truetype..
Updating category cid..
Cannot load module /usr/lib/gtk-2.0/2.10.0/immodules/*.so: /usr/lib/gtk-2.0/2.10.0/immodules/*.so: cannot open shared object file: No such file or directory
/usr/lib/gtk-2.0/2.10.0/immodules/*.so does not export GTK+ IM module API: /usr/lib/gtk-2.0/2.10.0/immodules/*.so: cannot open shared object file: No such file or directory
Updating anthy.dic...file name prefix=[./] you can change this by -p option.
  copying .///mkworddic/anthy.wdic (word_dic)
  copying .///depgraph/anthy.dep (dep_dic)
  copying .///calctrans/anthy.trans_info (trans_info)
  copying .///calctrans/anthy.cand_info (cand_info)
  copying .///calctrans/anthy.weak_words (weak_words)
  copying .///calctrans/anthy.corpus_bucket (corpus_bucket)
  copying .///calctrans/anthy.corpus_array (corpus_array)
/usr/bin/mkfiledic done.
done.
ispell-autobuildhash: Processing 'american' dict.

Creating config file /etc/gconf/2/path with new version
Wrote /usr/share/emacs23/site-lisp/dictionaries-common/debian-ispell.elc
Wrote /usr/share/emacs23/site-lisp/dictionaries-common/ispell.elc
Wrote /usr/share/emacs23/site-lisp/dictionaries-common/flyspell.elc
Wrote /etc/emacs23/site-start.d/00debian-vars.elc
Wrote /usr/share/emacs23/site-lisp/debian-startup.elc
Updating font configuration of x-ttcidfont-conf...
Cleaning up category cmap..
Cleaning up category cid..
Cleaning up category truetype..
Updating category truetype..
Updating category cid..
Updating category cmap..
[Wed, 09 May 2012 04:16:45 +0100] DEBUG: ---- End output of apt-get -q -y install emacs=23.3+1-1ubuntu4 ----
[Wed, 09 May 2012 04:16:45 +0100] DEBUG: Ran apt-get -q -y install emacs=23.3+1-1ubuntu4 returned 0
[Wed, 09 May 2012 04:16:45 +0100] INFO: apt_package[emacs] installed version 23.3+1-1ubuntu4
[Wed, 09 May 2012 04:16:45 +0100] INFO: Chef Run complete in 91.96998 seconds
[Wed, 09 May 2012 04:16:45 +0100] DEBUG: Cleaning the checksum cache
[Wed, 09 May 2012 04:16:45 +0100] INFO: Running report handlers
[Wed, 09 May 2012 04:16:45 +0100] INFO: Report handlers complete
[Wed, 09 May 2012 04:16:45 +0100] DEBUG: Exiting

細かいログの流れは後で追うとして、入れたかったgit,rake,emacsが入っているか確認。

$ git --version
git version 1.7.5.4
$ rake --version
Invalid gemspec in [/var/lib/gems/1.8/specifications/json-1.6.1.gemspec]: invalid date format in specification: "2011-09-18 00:00:00.000000000Z"
rake, version 0.9.2.2
$ emacs --version
GNU Emacs 23.3.1
Copyright (C) 2011 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

おお、入ってる。だけど、rakeの方でInvalid gemspecと言われている。これはgemspecの問題・・?
そして、意味のなかったsolo.rbとchef.jsonも一応うまく入った模様。権限がchefで指定した744になっている。

$ ls -la /vagrant_data/
total 12
drwxr-xr-x  1 vagrant vagrant  170 2012-05-09 04:10 .
drwxr-xr-x 25 root    root    4096 2012-05-09 04:13 ..
  • rwxr--r-- 1 vagrant vagrant 94 2012-05-09 04:10 chef.json
drwxr-xr-x 1 vagrant vagrant 442 2012-05-09 04:10 chef-repo
  • rwxr--r-- 1 vagrant vagrant 100 2012-05-09 04:10 solo.rb

ということで、chefと戯れた一週間だった。これは最初の一台を作る時に使えば、後がぐっと楽になる。
レシピファイルをバージョン管理しておけば、各ミドルウェアのupgrade,downgradeもすごい簡単。そして設定ファイルとかもtemplatesに押し込めるのがいい。http.confとかmy.confとかのあたりも、ここで管理してしまえば各サーバで設定が違うんだよーといったことはなくなる、はず。
puppetでも同じようなことができるけど、各ディレクトリの名前が直感的でなによりrubyでかけるのが素敵。

自分が好きな環境、例えば”Rails×Nginx×Unicorn”の環境一式とかを作る際に一度cookbookを作ってしまえば、
その後は仮想だろうと物理だろうと同じ構成が手軽に作成できる。開発環境、ステージング環境、本番環境でディレクトリ構成が微妙に違う・・・?といった問題は、同じcookbookを使えば回避できる。

また、Railsのバージョンをあげたいけど共有の環境でやる前のステップとして、一度ローカルで同じcookbookを使い、Railsのバージョンだけあげておき、仮想マシンを作成してみるといった使い方もできる。バージョンをあげるのは、chefの.rbファイルを1ついじるだけで検証できる。

Chefのメリット

各サーバの構成管理はChefにおまかせし、開発者は設定ファイルを管理すればよい(理論的には)

名前がわかりやすい

参考にさせていただいたサイト
Chef でサーバ管理を楽チンにしよう! (第 1 回)
Chef