Wednesday, July 02, 2014

fixing "npm install jshint" error on #chef #nodejs

Error output:
npm ERR! Error: No compatible version found: date-now@'^0.1.4'
npm ERR! Valid install targets:
npm ERR! ["0.1.0","0.1.1","0.1.3","0.1.2","0.1.4","1.0.0"]


If you get the above error while installing/updating your test servers with #chef, you will need to update the version of npm that is installed on your machine. (ref: https://github.com/npm/npm/issues/5298)

I use the nodejs chef cookbook (http://community.opscode.com/cookbooks/nodejs) to install npm, so updating the npm install version variable to 1.4.19 worked like a charm.

File: /cookbooks/nodejs/attributes/default.rb
Change: default['nodejs']['npm'] = '1.4.19'

While reading through the source I noticed that this cookbook isn't using the install script provided by npmjs site(https://www.npmjs.org/doc/README.html) for installing npm. sounds like an opportunity for any #chef lover to contribute and update the cookbook.

Monday, May 12, 2014

Upgrading to PHPUNIT 4.0 #CIUNIT #chef

#notetomyself

The phpunit test suite failed to execute on the newly built VM (configuration was done automatically via #chef), and that made me look into whats wrong with the test suite.

[CIUnit] PHP Error: Warning - require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory File Path: CIUnit/bootstrap_phpunit.php (line: 260)

PHP Fatal error:  require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/picsyn/application/third_party/CIUnit/bootstrap_phpunit.php on line 260

the first test was to check whether there were any changes to unit test files, but few minutes later found out that it wasn't the case.

Then I did a comparison of the phpunit versions I had installed before and now. first lead of suspect is that there have been a major version change. reading more on the changeset I learnt that phpunit installation via PEAR will be dis-continued after this year, so the most logical step to take is to update the installation process to support PHAR or Composer.

I'm going to take the PHAR approach this time.

here are the steps I took to fix the issue.

1. Download the latest phpunit cookbook from (http://community.opscode.com/cookbooks/phpunit/)
or alternatively
cd path_to_cookbooks_folder
knife cookbook site download phpunit
tar -xf phpunit_cookbook_tar_file

2. change config to mention phar install (cookbooks/phpunit/attributes/default.rb)
default[:phpunit][:install_method] = 'phar'

3. change config of the install dir (cookbooks/phpunit/attributes/phar.rb)
default[:phpunit][:install_dir] = '/usr/local/bin'

4. I had to make the following hack to ensure 'phpunit' command works from the CLI
In cookbooks/phpunit/recipes/phar.rb change remote_file section as below.
remote_file "#{phpunit_dir}/phpunit" do
  source node[:phpunit][:phar_url]
  mode 0755
end

5. On the PHPUNIT bootstrap file remove all mentions of the include of PHPUnit/Autoload.php as it isn't needed anymore. eg. there were two places in bootstrap_phpunit.php file that is used by my test project.
require_once ('PHPUnit/Autoload.php');

Thats all, and the following command works again without any modifications to how it should be called.


phpunit -c php/phpunit.xml models/AllModelsTest.php


Monday, February 24, 2014

How to setup an apache web server with PHP using vagrant and chef-solo

Note to self.
I will change this post in the future to cover the entire stack installation. but for now lets get this apache server running. and also in the next version i will write about how to do this much easily using librarian.

Steps followed:

1. create the project folder
2. create the Vagrantfile inside it using the command 'vagrant init'.
3. update your Vagrantfile to use the correct base box, share the correct folders and configure the port mapping
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu_precise64"
  config.vm.provider :virtualbox do |vb|
    vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
    vb.customize ["modifyvm", :id, "--memory", "2048"]
  end
  config.vm.network :forwarded_port, guest: 80, host: 6001
  config.vm.synced_folder "prototypev2/", "/var/www/prototypev2", :extra => "dmode=777,fmode=777"
  
  config.vm.synced_folder "chefrepo/", "/var/chefrepo", :extra => "dmode=777,fmode=777"

  config.vm.provision :shell, :inline => <<-eot font="">
    apt-get install -y build-essential
    gem install chef --version 11.4.0 --no-rdoc --no-ri --conservative
  EOT
end
4. inside the chefrepo folder make sure you have the solo.rb file, config.json file and a folder for cookbooks
5. solo.rb should read like:
file_cache_path "/var/chefrepo"
cookbook_path "/var/chefrepo/cookbooks"
6. execute 'vagrant up' from host machine and login to the box using 'vagrant ssh''
7. cd /var/chefrepo/cookbooks
8. download php and apache cookbooks using knife utility:
knife cookbook site download php
tar -xvf php-1.3.14.tar.gz
knife cookbook site download apache2
tar -xvf apache2-1.9.0.tar.gz
rm *.gz
8.1 download apt cookbook, this is needed as the first thing on the runlist so it will always execute a 'apt-get update' before the installation process begins
knife cookbook site download apt
tar -xvf apt-2.3.8.tar.gz
9. create a new cookbook for installing and configuring web server
sudo knife cookbook create appserver -o /var/chefrepo/cookbooks
10. update metadata.rb inside appserver folder, don't forget to include the dependencies
depends "apache2"
depends "php"
11. download all dependencies mentioned on php and apache cookbooks (you can get them by looking at metadata.rb of each cookbook)
build-essential
xml
mysql
yum-epel
windows
iis
iptables
logrotate
pacman
chef_handler
yum
openssl

12. create a template for apache site config in appserver/templates/default/ folder named apache_site_config.erb
        ServerAdmin ops@example.com
        DocumentRoot /var/www/prototypev2/
       
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
       
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined
        ServerSignature On

13 update cookbooks/appserver/recipes/default.rb as below:
include_recipe "apache2"
include_recipe "php"
include_recipe "apache2::mod_php5"

apache_site "default" do
  enable false
end

execute "change permission web" do
  command "chmod -R 0777 /var/www/prototypev2 "
  action :run
end

web_app 'prototypev2' do
  template 'apache_site_conf.erb'
end

14. update the runlist variable of config.json which lets chef know the recipes it need to run
{
  "run_list": ["recipe[apt]","recipe[appserver]"]
}

15. run chef solo
cd /var/chefrepo
sudo chef-solo -c solo.rb -j config.json

Monday, July 08, 2013

upgrading to vagrant 1.2.x

Its been a while since i've done any updates on my vagarant installations.

Seems like the project has improved alot for the past six months specially in supporting multiple providers easily, i decided to upgrade my vagarant installation to the latest. here are the steps i followed.

1. download the latest package from vagrant
http://downloads.vagrantup.com/tags/v1.2.2

2. install the package using the osx package manager

3. update the vagrantfile to reflect the changes they made to config
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu_precise64"
  config.vm.provider :virtualbox do |vb|
    vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]   
  end
  config.vm.network :forwarded_port, guest: 1337, host: 5001
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe("nodejs")
  end
end


4. it seems like with the latest update they require medata file within the box, so i decided to add my base box once again
vagrant box add ubuntu_precise64 {{path to .box file}}

5. Success.
vagrant up

 This should work seamlessly. I'm planning to try out #puppet instead of #chef in my setup. i will write down my notes on it on a future article. 

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.

Sunday, February 06, 2011

importing PHP projects to a fresh aptana 2.0

after long usage of notepad++ and vim for all my development needs, i wanted to try out a proper opensource IDE to see whether it could really improve my efficiency in development.

as i have worked with eclipse and netbeans before in various projects i thought i will give a try on aptana studio this time. after all it was build on eclipse eco system where you can choose to install 1000s of eclipse plugins available out there.

so i downloaded a fresh copy of aptana and installed it. the installation process was really fast. and once installed when i was trying to create a default web project i realized they don't support php by default. thats a little bit weird provided that php is one of the most widely used languages on the web development. so for their aptana studio 3 i wish they will be able to support few more different types of projects by default rather than getting the user to install them manually.

installing PDT on aptana was really a joke (seriously). both PDT 1 and 2 failed miserably on aptana studio 2. well i a little bit of googling linked me to this great article where barmus explained how to install the aptana 1.5 PHP module into aptana studio 2.

[Thanks for the great article bram, it really was a life saver]

once installed it wasnt much hard to import my previous projects into aptana, all i had to do was to follow the steps mentioned below to get it up and running like a charm.
1. goto file -> import
2. choose other -> existing folder as a new project
3. once created right click on the project and click "properties"
4. on the dialog goto "project natures" and select "PHP nature" and press ok.
5. let aptana re-open the project automatically to apply the settings

and now you have your project opened in aptana as a PHP project.

I'm going to continue to play with aptana and see what it has in it for me when it comes to my lame development habbits.

feel free to let me know if there any more sweet plugins that i should look into, or if there were any other way you guys managed to get aptana to run PHP projects.