Ruby on windows with mongodb

setting up ruby for windows, like anything windows these days, is more annoying then the other environments, especially Linux. Here are some key point I hope will save you some time:

  1. Ruby installer for windows, is only the start. it's located here. while you're there, don't stop with the installer: make sure to download the development kit (Development Kit section). You'll need it later on. make sure to extract it in a simple path, e.g. c:\devkit or alike. don't use spaces or special characters.
  2. Once ruby is installed, lets check the gem operation: if you can run gem update –system without an error – great, but if not, here is what you need to do: download the pem file here and save it in your rubygems/ssl_certs/ folder. now, the command should be executed correctly.
  3. lets update the system with

     

     

    gem update

     

  4. To install mongo, lets run

     

     

    gem install mongo
    
    gem install bson_ext

     

  5. The last one will install bson in C which is much faster. great? sure, but it's not going to work (Windows). so now what? first, lets go to the install folder of this gem (..lib/ruby/gems/[version]/gems/bson_ext[xxx]/ using cmd.
  6. once there open the cbson.c file that is located inside of ext/cbson folder. make sure that you have a reference to winsock2 and not arpa/inet. note that it is existed in more advanced versions, so if it's there, you don't need to change it. This is how it should looks like:

     

    #ifdef _WIN32
    #include <winsock2.h>
    #else
    #include <arpa/inet.h>
    #include <sys/types.h>
    #endif

    note that if you already have this file, your installation might actually work, so you can skip directly to the test code below.

  7. next, you need to setup your devkit installment, so go to your devkit folder, and run

    ruby dk.rb init

    . this will generate the config.yml file in that folder.

  8. edit this file, making sure that it contains the ruby path at its end, like this: – c:/ruby. note the spaces and the backslash. these are not typos.
  9. next run ruby

    dk.rb install

    .

  10. in your command window that is in the gem folder, run gem build bson_ext.gemspec.
  11. move the new gem c
  12. delete the entire bson_ext gem folder
  13. Run:

    gem install bson_ext-1.11.1.gem --local

     

  from within the folder you've saved that gem.

Starter code:

require 'rubygems'
require('mongo')
puts('testing mongo...')

if you can run this code without an error or a mongo warning claiming that you are not using bson_ext, you are good to go!

 

Multiple chef environments

Well, this is a little bit strange, but in opscode website, it’s not really clear how to configure your chef client to access multiple servers.

Not only that this is possible and more then just nice to have, in many cases, it’s essential:

  1. You might manage multiple servers
  2. You don’t want to create a knife file in a privileged directory
  3. You want to test a client configuration

To make it happen:

In any folder that you wish to create chef access, just create a .chef sub folder. ingredients for this folder are:

  1. knife.rb file.
  2. public and private key files (you get these from the chef server).

note that you need to set your permissions in the server, and most likely to have an admin access if you want the new user to be able to create/delete nodes.

 

Static constructor in NodeJS objects

It's not hard to create an object in JavaScript and therefore in NodeJS. However, if you're an advanced user, you probably devil a little with static objects (Factories will be the definite example). Since JavaScript doesn't really comes with static constructors, we need to take advantage of NodeJS require feature.

About require

require is a nodejs specific. The cool thing about it, is that it is only called once. so, every code that executed there, is executed only once, what's making it a perfect place for static initialization.  Lets take a look at some code:

function Counter()
{
this.counter=0;
Counter.__counters++;
}

What we have here is a simple increase of the number of objects created. However, since Counter.__counters is not defined, we will get an error. sure, we can check that this variable exists in the object constructor, and currently this is not a big issue, but if the test is a timely manner or a costly one, we have a problem. Using NodeJS feature, we can solve it easily:


Counter.__counters=0;

function Counter() 
{ 
this.counter=0;
Counter.__counters++; 
}

The first line will be called only once, and thus make it a static constructor. This line can be replace in a function call, if we wish to make it neater, and the effect will remain the same.

Installing chef gems

This might sounds strange, but your regular gem location is not the location that chef is using. This is important in case you are trying to install any knife plugins, where regular gem just won’t work.

The usual location is:/opt/chef/embedded/bin/gem , but you might have it some place else. Generally, look for gem|chef|more, and this should do the trick.

Add menu entry in ubuntu

Sometimes, you might want to download a program without installing it system wide. Or, you might not have an installer, but still, you would like to have a shortcut in the application menu.

To achieve that, here is what needed to be done:

  1. Create a desktop entry of the application you want to insert
  2. Copy this entry to ~/.local/share/applications.

The second part is pretty simple. but how to create a desktop entry file? Here:

Desktop entries are actually simple text file that follow some guidelines.

  1. The file postfix is "desktop" (same as txt for text file and so on)
  2. The file contains several key-value pairs, most important are Exec, Terminal and probably Icon.
  3. You don't have to create it from scratch. Here is a desktop entry for robomongo I've created. Note that you need to remove the txt postfix part for it to work. You should change the other parameters as well (Name for example or comment).

If you wish to copy a shortcut that  was installed through a debian package to your desktop, these are usually located in /usr/share/applications.

 

 

Downloads:

  1. robomongo.desktop