Tuesday, December 18, 2012

Vagrant, Chef and Node - Part 2

This is a continuation from the previous blog post in learning how to set up chef script to use a local file instead of downloading the required files from the internet every time the VM is initialized.

Two scenarios:

1. have the source code locally so chef only have to compile it
2. have the package file locally so chef only have to install it using the package manager

Have the source code locally:

This was the first experiment so dont expect any code quality on it. (its added as a todo task already)
1. Create folder - cookbooks/nodejs/files
2. Create folder - cookbooks/nodejs/files/default
3. Download http://nodejs.org/dist/v0.8.6/node-v0.8.6.tar.gz (nodejs source)
4. Move the tar file to default folder
5. Comment the following lines from /cookbooks/nodejs/recipes/install_from_source.rb
#remote_file "/usr/local/src/#{nodejs_tar}" do
#  source nodejs_src_url
#  checksum node['nodejs']['checksum']
#  mode 0644
#end

6. Add the following lines instead
cookbook_file "/usr/local/src/#{nodejs_tar}" do
    action :create
    source "node-v0.8.6.tar.gz"
    mode 0644
end

7. destroy the VM and initialize it again using 'vagrant up'
8. Done!!

Todo:

1. Update the recipe to have this as a config option so I can propose the change back to the author of the recipe
2. Make the same changes on the install_from_package.rb

It still takes too long to startup (430seconds) and i suspect that most of that time is spent on downloading build-essential tools and compiling node from source. I hope this will be fixed once i introduce locally saved packaged file.

Friday, December 07, 2012

Vagrant , chef and Node

This is a note to myself, mostly to make sure I will not forget what I learned during set-up of the dev environment for NodeJs development. If you find it useful feel free to drop a comment on the post.

Steps followed:

1. installed vagrant and virtualbox using the osx package manager
2. Execute 'vagrant init' using terminal
3. This will create a sample 'Vagrantfile' config file
(as i didnt have the base VM downloaded yet, i had to download it )
4.vagrant box add ubuntu_lucid64 http://files.vagrantup.com/lucid64.box
5. Add the following line to Vagrantfile
config.vm.box = "ubuntu_lucid64"
6. Enable port forwarding to access the VM through the local IP address. (where 1337 is the port in the VM and 5001 is the port of the host machine)
config.vm.forward_port 1337, 5001
7. Create a folder named 'cookbooks' within the project folder
8. Download the nodejs recipe from chef community and put it to cookbooks folder
9. Add the following lines to the vagrantfile
config.vm.provision :chef_solo do |chef|
 chef.cookbooks_path = "cookbooks"
 chef.add_recipe("nodejs")
end
10. Execute 'vagrant up'
11. Execute 'vagrant ssh'
12. When you try to install node modules using npm within the VM you will always get error msgs saying "symlinks failure" (this is because virtualbox VMs cannot create symlinks with its default settings). so include the following line on the vagrantfile in order to change the default settings.
config.vm.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
13. Execute 'vagrant reload'
14. Write an node server which listens to 0.0.0.0:1337
var http = require('http');
var url = require('url');
http.createServer(function(req,res){
}).listen(1337,'0.0.0.0');

15. Test the connection by visiting localhost:5001 from your host machine browser.
16. Done!!

Points to remember when writing the node scripts:

1. If you are writing a server listening to a port using NodeJs make sure its listening to the IP address '0.0.0.0:1337' (otherwise you will not be able to access the site using hostmachineip:5001)

Todo:

1. Write a custom receipe for installing required packages using npm within the VM
2. find out how to install node using a binary saved in the local machine than rebuilding from the source - already completed. http://rarepraveen.blogspot.com/2012/12/vagrant-chef-and-node-part-2.html
3. write a separate blog post on how I used 'node dbox' (node library for dropbox APIs) to write a sample application

Okay that's it for the moment. If you know any tips that could help another dev's life easier feel free to share them as comments.