LoginSignup
16
16

More than 5 years have passed since last update.

Gitをsourceからインストールするchefのレシピ

Last updated at Posted at 2013-09-04

chefではレシピを汎用的に書くためにattributeというものを作っておくといいそうな。
opscodeに上がっているレシピでよくみかけるnode[hoge][fuga]みたいなものです。

今回は早速覚えたattriburteの機能を使ってGitをソースコードからインストールするレシピを書いてみた。
Gitはバージョンアップのスピードが早いので、パッケージでインストールではちょっと古めのバージョンがインストールされる。
centOSに限っては問題多い1.7.1のバージョンが未だに入ってくる!!
このレシピはきっと大いに役に立つに違いない(とっくに他の誰かがもっと良いレシピを作って公開しているとは思うけど...)。

レシピ

レシピのディレクトリはcookbooks/git/で作成したことにします。

まずは、gitのattributeを定義しておくためのファイルを作成しよう、cookbooks/git/attributes/default.rbで作成する。
versionの他にconfigureオプションやGitのインストールに必要なpackageをデフォルト値として設定した。

cookbooks/git/attributes/default.rb
default['git']['version']    = '1.8.4'
default['git']['source_uri'] = 'https://git-core.googlecode.com/files/git-1.8.4.tar.gz'
default['git']['configure']  = './configure'
default['git']['packages']   = %w{gettext gettext-devel zlib-devel openssl-devel libcurl-devel perl-ExtUtils-MakeMaker}

次に、Gitをインストールするレシピを書く。
レシピはcookbooks/git/recepies/default.rbで作成する。

cookbooks/git/recepies/default.rb
install_dir = '/usr/local/src'
version     = node['git']['version']
source_uri  = node['git']['source_uri']
configure   = node['git']['configure']

node['git']['packages'].each do |package_name|
  package "#{package_name}" do
    :install
  end
end

remote_file "/tmp/git-#{version}.tar.gz" do
  not_if 'which git'
  source "#{source_uri}"
end

bash 'install_git' do
  not_if 'which git'
  user 'root'

  code <<-EOL
    install -d #{install_dir}
    tar xfz /tmp/git-#{version}.tar.gz -C #{install_dir}
    cd #{install_dir}/git-#{version}
    #{configure} && make && make install
  EOL
end

実行するときに以下のようなjsonファイルを用意してchefコマンドから指定してあげればインストールバージョンが変更出来る。

{
    "git": {
        "version": "1.8.3.4",
        "source_uri": "https://git-core.googlecode.com/files/git-1.8.3.4.tar.gz"
    }
}

vagrantではjsonファイルを用意しないで、chef.jsonというプロパティに連想配列で設定するみたい。

Vagrantfile
config.vm.provision :chef_solo do |chef|
  chef.cookbooks_path = './cookbooks'
  chef.add_recipe 'git'
  # jsonファイル作る代わりにこう書く
  chef.json = {
    :git => {
      :version    => '1.8.3.4',
      :source_uri => 'https://git-core.googlecode.com/files/git-1.8.3.4.tar.gz'
    }
  }
end

まとめ

かなりスッキリレシピが書けた。

以上

16
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16