Clogeny’s Chirag Jog to speak at #Chefconf 2013

Clogeny’s CTO Chirag Jog will be speaking at this year’s ChefConf, hosted by Opscode

#Chefconf is a gathering of foremost experts in infrastructure automation and DevOps from across the globe. #Chefconf focuses on how Chef – the leading open-source systems integration framework is automating the next wave of IT infrastructure through three days of demonstrations, interactive workshops and presentations.
In his talk, on April 25th Chirag will talk about the anatomy of knife cloud plugins which extend Chef to manage cloud resources. The talk will focus primarily on the plugin for vCloud Director – VMWare’s flagship cloud provisioning product powering several public clouds – Bluelock, AT&T and Dell as well as private clouds.

For more information, please visit #Chefconf.

Backbone.js + RequireJS + Ruby on Rails

Building and managing large scale JavaScript applications becomes cumbersome as the size of application code grows. Developers often spend lot of time figuring out the flow of application instead doing the original task. There can be various reasons causing this but, it can be prevented by correctly implementing dependency management and organising the  application code properly. In this article, we will try to address the two main issues in building large scale JS applications –

  • Code Structuring (using Backbone.js)
  • Dependency management(using RequireJS)

Backbone.js is a small JavaScript library (~ 6 kb) that allows programmers to structure your JS code by providing model, view, controller, and router classes. It is designed for the single page applications and can be customized as per the project needs. It also provides a pub-sub (publish-subscribe) mechanism to allow objects to trigger and bind the events.

RequireJS serves very different purpose than backbone.js. It provides ability to improve page performance by Asynchronous module Definition (AMD) mechanism and organize each module in single JavaScript file. The Asynchronous Module Definition (AMD) API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded. This is particularly well suited for the browser environment where synchronous loading of modules incurs performance, usability, debugging, and cross-domain access problems.
The following section describes how to take advantage of Backbone.js, RequireJS to build a Ruby on Rails based application while using CoffeeScript syntax instead of Javascript to improve readability. It also points at some of the issues you would face and provides solutions for the same. Treat this as a step by step guide and refer to library documentation for more details.

The completed code and examples can be download from the github.
1. Create a new project

2. Add Gemfiles Backbone.js & RequireJS

3. Creating the first controller
Create a home controller that will serve as home page for our sample-

4. Set the application home page

Tell Rails where your actual home page is located, edit routes.rb

5. Convert plain JavaScript to coffeescript syntax
CoffeeScript is a little language that compiles into JavaScript.
So let’s rename app/assets/javascripts/application.js  to application.coffee and add following code

Still not clean? :( , if you see console log, you see an error “Error: Module name ‘underscore’ has not been loaded yet for context: _”

javascript error

Thanks to Noah [github] for providing a fix for this.

Update Gemfile as follows

Run bundle install and restart your application, everything is clean. Great!! it works now.

alert box
6. Building basic backbone app
We will try a very simple single page application example that creates a view. It also gets the username and creates a child view on the fly within javascript, no round trip to server. This only demonstrates the usage of Backbone.js Views. We will have a look at using Models, Collections, Routers in another blog.

Let’s add a placeholder application level container tag or div, where we will append the html generated from backbone views. In a single page application, the approach we follow is, have a container tag where the backbone view generated html is appended, and every user event or operation on web page either updates existing tags or appends/removes backbone views (html).
Edit application.html.erb  with

7. Remove home.html.erb
First create following folders for backbone, app/assets/javascript/models, app/assets/javascript/views, app/assets/javascript/collections  where we can write the backbone views, models, collections.
Move home.js.coffee to app/assets/javascript/views  folder and write a basic view.
Backbone can use templates to render views, for now just add following template in home.coffee.

Here is a view using above template. Note that templates can be moved to separate files which help in better maintaining the application.

Here you see that Backbone view is used to render the text ” Hello from an Backbone app.”


8. Adding event handling

Add following tags to current template. Here we will enable button when a user enters some text and handle the button click event.

Add the event handlers to HomeView.

** Note here that we have created a new child view which appends itself inside the #child_view div within HomeView

Child view source

There you go, rerun your application and you can see that views is created on the fly.

popup_1

popup_2
9. Moving templates out from backbone views
Two options: add script tags ‘text/template’ in html, or move it the separate file.

Let’s look at first option. Let’s move the child view template to script tag in application.html.erb

Remove the tempate: _template… definition from Child view and add the following to application.html.erb

And update the child view render function as follows.

Run your app. It will fail with the following error.

Error:

undefined local variable or method username

Here you go the culprit is:

  <div> Hey <%= userName %>, you just created a new Backbone child view</div>  

The problem is that Underscore is using the same syntax for templating as ERB, so it is conflicting. You need to tell Underscore to use a different syntax.

Here is the fix – Update the script tag as follows:

Rerun your application and you can see the application working with templates moved out from your backbone views.
This post is meant to be a hands-on tutorial with setting up projects using Backbone.js, RequireJS and rails. Feel free to fork the source for samples used in this post on github – https://github.com/ClogenyTechnologies/Rails-Backbone-RequireJS

INTERESTED?

If all of this is interesting/exciting to you, and you’d like to chat more about working with us, drop us a note; we’d love to hear from you.

Integrating Stripe with Ruby on Rails Application

Payment integration in applications is always a pain!  There are lot of options available in market like Shopify, PayPal, Authorize.net, CIM etc. Although It requires lot of efforts to integrate them within the application. Stripe – simple, straight forward and easy to integrate, is transforming the way payments are handled. The post covers how easy it is to integrate Stripe into your standard Rails application.

Stripe provides API wrappers for Ruby, Python, PHP and Java. It also has third party library support for Ruby, PHP, C#, ColdFusion, Perl which makes life of the developer easy. Some of the other benefits that Stripe offers are -

  1. No need to be PCI compliant – We cannot take the user’s credit card info on our site, unless we are PCI compliant, which is not feasible at all given the costs. Stripe handles it beautifully, it does some sort of cross-domain AJAX to send data to their server. So you don’t have to actually look into these issues.
  2. Events & webhooks – One of the major pain-points while integrating with any 3rd party service like Stripe is getting to know about events that take place – recurring billing events, service outage, other errors etc. Stripe solves the problem by providing a webhook to which one can provide a URL which is invoked via POST each time an event takes place.
  3. Handles failed payment attempts - Stripe makes handling failed payments easy. Stripe can automatically retry a recurring payment after it fails, and can automatically cancel the customer’s subscription if it repeatedly fails. How long to wait, and how many times to retry, can be easily.
  4. Additionally Stripe takes care of other related things like recurring charges, pro-rating etc, removing the headaches of the programmer to maintain DB at his end.

Stripe can integrated into your Rails Application in 10 simple steps:

Before we start though -

  1. Go to https://stripe.com and create your account. Account creation will provide you API keys that will be used in communicating with the Stripe using its API.
  2. Also create one dummy plan to be used when customer subscribes:
  3. Go to Subscriptions -> Plans -> Create your first plan.

Create first subscription plan with stripe
Continue reading

Hadoop Cluster Automation Using Ironfan

Recently, we faced a unique challenge – setup DevOps and management for a relatively complex Hadoop cluster on the Amazon EC2 Cloud. The obvious choice was to use a configuration management tool. Having extensively used Opscode’s Chef and given the flexibility and extensibility Chef provides; it was an obvious choice.

While looking around for the best practices to manage a hadoop cluster using Chef, we stumbled upon: Ironfan Continue reading

Enterprisy Updates to knife-cloudstack!

Opscode’s Chef is open-source systems integration framework built specifically for automating the cloud. Knife is a powerful CLI that is used by administrators to interact with Chef. It is easily extensible to support provisioning of cloud resources. There is currently support for over 15 cloud providers including Amazon EC2, Rackspace, Openstack and Cloudstack.

Ever since the acquisition of Cloud.com by Citrix, Cloudstack (now re-christened as Citrix CloudPlatform) is being actively morphed into a more enterprise-focused product with support for Production-grade networking appliances like the Netscalar suite, F5 Big IP, Cisco Nexus 1000V and networking features like InterVLAN communication and Site-to-Site VPN. Continue reading

Understanding the Redundant Virtual Router in Citrix CloudPlatform

As we discussed in the High availability and Disaster recovery article, using redundant routers can reduce the network outage from minutes to few seconds in the event of a  single point host failure. In this post, we will talk about the mechanism and working of the redundant router while achieving high availability. 

Every account using CloudStack’s advanced mode networking has a dedicated virtual router which does all the routing, port forwarding, load balancing and other networking stuff for resources belonging to that account. In advanced networking, all the user VMs are in a private network (10.0.x.x) and communicate to the outside world using virtual router as a gateway. The failure of the virtual router will disable the network access to the user VMs for that account. Though the user instances are running but, the customer won’t have access to them. Continue reading

Automation Testing using pyUnit for Cloud ISVs

Integration and automation is very important for any cloud computing ISVs. The magic is applying automation to any activity that’s performed more than once, removing the variability and delay of human intervention to enable the quality and speed that the cloud demands. Advanced system administrators deeply skilled in data center automation concepts who treat infrastructure as code. The libraries like pyUnit, jUnit enable them to do these tasks very easily. At Clogeny, we are involved in automating test scenarios for Citrix’s CloudPlatform. Continue reading

High Availability & Disaster Recovery for Apache CloudStack

The mission critical data of big organizations and companies has to be available 24/7 and especially if the daily business revenues are in the millions of dollars. A downtime ranging from a few hours to 24 hours can result in disastrous losses in terms of business revenue. High availability solutions by Cloud providers such as CloudStack and others has enabled big organizations to achieve this aim with reduced costs on hardware and installations.

Clogeny has been working on CloudStack since past 2 years. Cloudstack has different mechanisms for the high availability (HA) and disaster recovery systems (DRS) at various levels in their product. We will discuss those in this post. I recommend newbies to read basics of CloudStack first before continuing with this post.
Continue reading

Changing the Date Time on Amazon’s EC2 Instance

Amazon Elastic Compute Cloud (EC2) provides scalable virtual private servers using Xen. The instances running on Xen sync their wall clock periodically with the underlying hypervisor. For changing the datetime settings, few extra configurations are required.

On a simple Linux Machine the date and time can be simply changed by stopping ntpd service and setting the date as:

# date -s "2 OCT 2006 18:00:00"

But on Xen server based virtual instance it’s not this simple!
Continue reading

Custom Metrics in Amazon CloudWatch

Amazon CloudWatch is an Amazon Web Services utility allowing monitoring of various components like EC2 instances, EBS volumes and the Elastic Load Balancer. For EC2 instances, we can monitor CPUUtilization, DiskReadBytes, DiskReadOps, DiskWriteBytes, NetworkIn and NetworkOut. More often than not, end-users would want to monitor more parameters than the ones available. eg. Free Memory, Free Swap and so on.

Amazon CloudWatch provides custom metrics to help circumvent the problem. One can simply define a custom metric based on each one’s need and continuously feed it with data using a simple bash or python script running a while loop. Let’s take an example of Free Memory.
Continue reading