2016年8月27日土曜日

Vagrant+ChefでNodejsをバージョン指定していれる

Nodejsの案件対応が発生した為、NodejsをVagrant+Chef環境で動かそうとしました。
誰かがまとめてくれているだろうと調べましたが、意外にも情報がほとんどありませんでした。
調べた結果をご紹介します。

前提

  • CentOS7
    • ubuntuだとバイナリのインストールがうまくいきませんでした
    • 6でも問題ないとは思いますが検証していません
  • cookbook

Gemfile

 source 'https://rubygems.org'  
 gem 'berkshelf'  
 gem 'knife-solo'  

Berksfile

 source "https://supermarket.chef.io"  
 cookbook "nodejs"  


bundlerをインストール(gem install bunbler)をし、
下記コマンドでcookbookをインストールします

$ bundle exec berks vendor ./cookbooks
$ bundle exec knife cookbook create base -o site-cookbooks
これでcookboks以下に外部のcookbook、site-cookbooksに実行するcookbookの用意ができました。

レシピは `site-cookbooks/base/recipes/default.rb` に作成します.

 include_recipe "nodejs"  

上記でnodejsのレシピを読み込んで使っています

バージョン指定等の属性は以下のファイルに書きます
`site-cookbooks/base/attributes/default.rb`

 node.default['nodejs']['install_method'] = 'binary'  
 node.default['nodejs']['version'] = '5.9.0'  
 node.default['nodejs']['binary']['checksum'] = '99c4136cf61761fac5ac57f80544140a3793b63e00a65d4a0e528c9db328bf40'  

checksumやバージョンは以下でリストを見れます
https://nodejs.org/dist/
今回はCentOS 64bit、4.4.7をインストールしたいので次のバイナリが該当
https://nodejs.org/dist/v4.4.7/node-v4.4.7-linux-x64.tar.gz
チェックサムは同じディレクトリのSHASUMS256.txtを確認する
https://nodejs.org/dist/v4.4.7/

おまけ

Vagrantfile


 # -*- mode: ruby -*-  
 # vi: set ft=ruby :  
 # All Vagrant configuration is done below. The "2" in Vagrant.configure  
 # configures the configuration version (we support older styles for  
 # backwards compatibility). Please don't change it unless you know what  
 # you're doing.  
 Vagrant.configure("2") do |config|  
  # The most common configuration options are documented and commented below.  
  # For a complete reference, please see the online documentation at  
  # https://docs.vagrantup.com.  
  # Every Vagrant development environment requires a box. You can search for  
  # boxes at https://atlas.hashicorp.com/search.  
  config.vm.box = "centos/7"  
  # Disable automatic box update checking. If you disable this, then  
  # boxes will only be checked for updates when the user runs  
  # `vagrant box outdated`. This is not recommended.  
  # config.vm.box_check_update = false  
  # Create a forwarded port mapping which allows access to a specific port  
  # within the machine from a port on the host machine. In the example below,  
  # accessing "localhost:8080" will access port 80 on the guest machine.  
  # config.vm.network "forwarded_port", guest: 80, host: 8080  
  # Create a private network, which allows host-only access to the machine  
  # using a specific IP.  
  # config.vm.network "private_network", ip: "192.168.33.10"  
  config.vm.network :private_network, ip: "192.168.240.100"  
  # config.nfs.map_uid = 500  
  config.vm.synced_folder ".", "/host_mount",  
   id: "vagrant-root", :nfs => true  
  config.bindfs.bind_folder "/host_mount","/srv", :owner => "vagrant", :group => "vagrant", :'create-as-user' => true, :perms => "u=rwx:g=rwx:o=rwx", :'create-with-perms' => "u=rwx:g=rwx:o=rwx", :'chown-ignore' => true, :'chgrp-ignore' => true, :'chmod-ignore' => true  
  ## provision等の前に共有ディレクトリを解除してキャッシュが使われることを防ぐ  
  config.trigger.before [:reload, :up], stdout: true do  
   run "rm -f .vagrant/machines/default/virtualbox/synced_folders"  
  end  
  config.vm.provision :chef_solo do |chef|  
   chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]  
   chef.json = {  
   }  
   chef.run_list = %w[  
    recipe[base]  
   ]  
  end  
 end