ChefでSass+Compass+Coffee+Gruntの実行環境を作る(2)
さて、前回はchefでrbenvを用いたrubyのインストール。
sassとcopassのインストールを行いました。
今回はnodeのバージョン管理とインストール、gruntやcoffeeのインストールを
行えるようにしたいと思います。
前回はこちら
参考にしたサイト
npm cookbook で npm_package "pm2" など - Qiita
node.jsの管理をhomebrewからnodebrewに変えて、npmをインストール - Qiita
Nodejsをインストールしよう!
さて、nodejsをインストールしていきたいと思います。
nodejsを簡単にインストールしてもいいんですが、
rbenvのようなバージョン管理が出来たらとても便利ですので、
そのあたりを探しながらChefに落としていこうと思います。
・・・・・・検索中・・・・・・
どうやら、nodebrewってのが簡単らしい・・・!
入れてみますか!
nodebrewを入れてみる。
nodebrewをインストールしたことも無いのに、いきなりchefで実行しちゃおうって
危険な香りがしますが、やっちゃおうかと思いますw
早速node.rbって新しいレシピを作成しました。
そちらに書いていきたいと思います。
#ファイルをクローン git "/home/vagrant/nodebrew" do repository "https://github.com/hokaccha/nodebrew.git" revision "master" user "vagrant" group "vagrant" action :sync not_if "ls /home/vagrant/nodebrew" end #nodebrewのインストール bash "nodebrew_setup" do not_if "ls /home/vagrant/.nodebrew" user "root" code <<-EOL su vagrant -l -c 'perl /home/vagrant/nodebrew/nodebrew setup' su vagrant -l -c 'echo 'export PATH="$HOME/.nodebrew/current/bin:$PATH"' >> /home/vagrant/.bash_profile' EOL end
インストール自体は数行で書けるみたいなので、これでうまくいってくれればいいなぁ・・・。
さて実行!
$ knife solo cook develop ・・・中略・・・ Starting Chef Client, version 11.12.4 Compiling Cookbooks... Converging 2 resources Recipe: develop::node * git[/home/vagrant/nodebrew] action sync - clone from https://github.com/hokaccha/nodebrew.git into /home/vagrant/nodebrew - checkout ref 8cec3cd23b38c5b95c5ceedb1dccc7eee92e5e5c branch master * bash[nodebrew_setup] action run - execute "bash" "/tmp/chef-script20140628-6770-j7ukgx"
うまく入りました!
実はログは取れなかったんですが、「https://raw.github.com/hokaccha/nodebrew/master/nodebrew」へ
なぜかアクセスできなかったので、ちょっとやり方を変えて実行しました。
一応正常に入っているか確認の為、バージョン確認をしておきます。
[vagrant@localhost ~]$ nodebrew -v nodebrew 0.7.4 Usage: nodebrew help Show this message nodebrew installDownload and install a (compile from source) nodebrew install-binary Download and install a (binary file) nodebrew uninstall Uninstall a version nodebrew use Use nodebrew list List installed versions nodebrew ls Alias for `list` nodebrew ls-remote List remote versions nodebrew ls-all List remote and installed versions nodebrew alias Set alias to version nodebrew unalias Remove alias nodebrew clean | all Remove source file nodebrew selfupdate Update nodebrew nodebrew migrate-package Install global NPM packages contained in to current version nodebrew exec -- Execute specified Example: nodebrew install v0.10.22 Install a specific version number nodebrew use v0.10.22 Use a specific version number
入っているようです!
それでは、続いてインストールをしちゃいましょう!
「nodebrew install v0.10.22」とコマンドを実行すればいいらしいんですが、
非常に時間がかかるので、バイナリインストールというのを行います。
bash "nodebrew_setup" do not_if "ls /home/vagrant/.nodebrew/node/v0.10.22/" user "root" code <<-EOL su vagrant -l -c 'nodebrew install-binary v0.10.22' su vagrant -l -c 'nodebrew use v0.10.22' EOL end
それでは実行したいと思います。
$ knife solo cook develop ・・・中略・・・ Recipe: develop::node * git[/home/vagrant/nodebrew] action sync (skipped due to not_if) * bash[nodebrew_setup] action run (skipped due to not_if) * bash[nodebrew_setup] action run - execute "bash" "/tmp/chef-script20140628-7797-1u9gzak"
正常に処理が終わったようですので、nodeがインストールされているか確認しておきます。
[vagrant@localhost ~]$ node -v v0.10.22
来ました!!!
正常にインストールされています!
これでgrunt+coffeescriptのインストールが行う準備が整いました!
CoffeeScriptをインストール
vagrantユーザーとしてインストールしてしまっていたので、
npm_packageを使用する事が出来ない状態にはまってしまいました。
今さら変更したくないので、bashでインストールしちゃおうかと思います。
bash "coffeeInstall" do not_if "ls /root/.coffeeInstall_check" user "root" code <<-EOL su vagrant -l -c 'npm install -g coffee-script' EOL end bash "coffeeInstall_check" do not_if 'ls /root/.coffeeInstall_check' user "root" code <<-EOL echo "rbenv_setup" > /root/.coffeeInstall_check chown 400 /root/.coffeeInstall_check EOL end
それでは実行したいと思います。
$ knife solo cook develop ・・・中略・・・ Starting Chef Client, version 11.12.4 Compiling Cookbooks... Converging 4 resources Recipe: develop::grunt * gem_package[sass] action install (up to date) * gem_package[compass] action install (up to date) * bash[coffeeInstall] action run - execute "bash" "/tmp/chef-script20140628-9190-15g4h38" * bash[coffeeInstall_check] action run - execute "bash" "/tmp/chef-script20140628-9190-1bjs20i" Running handlers: Running handlers complete Chef Client finished, 2/4 resources updated in 3.44067835 seconds
正常にインストール出来ているか確認しておきます。
[vagrant@localhost ~]$ coffee -v CoffeeScript version 1.7.1
はい!問題なくインストール出来ていました!
それでは最後にgruntjsをインストールしておきます。
Grunt.jsをインストール
先ほどと同様に、grunt.jsもインストールします。
bash "gruntInstall" do not_if "ls /root/.gruntInstall_check" user "root" code <<-EOL su vagrant -l -c 'npm install -g grunt-cli' EOL end #判定用 bash "gruntInstall_check" do not_if 'ls /root/.gruntInstall_check' user "root" code <<-EOL echo "rbenv_setup" > /root/.gruntInstall_check chown 400 /root/.gruntInstall_check EOL end
coffeeと同じように、問題なく実行されるはずです!
$ knife solo cook develop ・・・中略・・・ * bash[gruntInstall] action run - execute "bash" "/tmp/chef-script20140628-9680-q7vd9k" * bash[gruntInstall_check] action run - execute "bash" "/tmp/chef-script20140628-9680-xww112"
バージョンの確認をしておきます。
[vagrant@localhost ~]$ grunt grunt-cli: The grunt command line interface. (v0.1.13)
はい!正常にはいっています。
前回と今回にわたって、Chefでruby+nodeをインストールし、
目的のSass+Compass+Coffee+Gruntとインストール関係は以上で完了となりました!
次回は、実際に開発する上で便利な方法を考えながら、作成したいと思います。
vagrantのプロビジョニングをchefでやったり、
ファイル管理に関しても考えてみたいと思います。