Virtual Machine

The complete steps to having a virtual box up and running on your computer

18 Apr , 2015  

There are a lot of benefits of using a virtual box for your development. You can test your installs with different configurations and easily go back to different snapshots of this box. You can clone a snapshot to create another box in no time. You can also use this to create a network environment on your box with a few virtual boxes if you need more than one computer talking to each other for your setup.

There are few softwares available to create your virtual environment, but it does take a bit of work to start with.

This is a quick walkthrough on setting up a new virtual box with a Linux Centos 7.0 OS on your computer. It has some essential additional steps like enabling graphical user interface, adding vbox additions, connecting it to the network with a bridged connection, sharing folders with the virtual box and taking snaphshots. I ran into quite a few hiccups during my first install. So I have laid out thes exact steps to get it right the first time, the next time someone tries it. I did this on a Windows 7 host system. But most of it should apply very well on a Mac too.

Step 1: Download and Install virtual box software

Download and install from here: https://www.virtualbox.org/wiki/Downloads
Note that the install file is the same for both the 32 version and 64 version of windows.
download virtual box software

Step 2: Create a new virtual box

Open Oracle VM VirtualBox Manager. Click on the New button on the top left to specify a new name for your virtual box and then hit Next.
Specify a name for your virtual box

The RAM specification selection window shows. The default settings works for the most part, so we hit next.
select memory for your virtual box

Leave the radio button for “Create a virtual hard drive now” checked and hit Create.
add a virtual hard drive option

Leave the default option for Hard drive file type and hit Next
hard drive type selection for virtual box

Keep the “Dynamically allocated” button checked and hit Next
choose storage type for virtual box

Centos recommends 8 GB of Virtual Hard Drive space. But I ended up needing more. It is a pain to increase the size later on, so I bumped up the storage space to 20 GB on this screen. Hit the Create button.
virtual box file location and size

The Virtual Box will be created and display in the list of virtual boxes on the VM VirtualBox Manager screen
virtual box created

Step 3: Start your virtual box and add VBox additions

Right click on your virtual box and hit the start button
start a virtual box

It will ask for the OS file to install from. Make sure you have the Centos OS File downloaded with the iso extension from the Centos website: http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1503-01.iso

Click on the explorer icon to select that file dowloaded on your computer.
After the file selection, click on the Start button:
select the os installation iso file

It should start install of the OS on the new virtual box.
virtual box post os installation

You might see this prompt when you click inside the virtual box space. Remember the “Right Ctrl” option and click on the Capture button.
Every time you want your mouse pointer to go outside the virtual box, hit the Ctrl button on the right side of the keyboard, and your mouse pointer will then be used by the host box instead of the virtual box.
virtual box prompt to capture mouse

Select the language when prompted and hit the Next button
vbox choose a language

If you need to change your timezone, hit the Date and time option. Otherwise, hit the Installation Destination option to proceed
vbox choose time zone

Leave the default selected or add drives if you want to. Then hit Done
vbox select harddisk

Hit the Begin Installation Button
vbox centos 7 begin installation

Change the root password by clicking on “Root Password”. Also make sure you create another user by clicking on “User Creation”.
I used my name and checked the checkbox for administrator for this user.
centos 7 change root password

Once the installation is complete, it will ask you to Reboot. Click on the button to reboot.
centos 7 reboot after installation

The machine will then reboot. Enter the login and password once it reboots to access your VM
centos 7 console login

Step 4: Update the version of Linux to use and clipboard share settings

Go to Machine -> Settings
centos 7 select machine settings

In the general section, select the OS to be Other Linux (64 bit)
centos settings general option

Then move to the Advanced tab, and set the “Shared Clipboard” and “Drag ‘n’ Drop” values to “Bidirectional”.
centos settings clipboard share

Step 5: Connect this computer to the network

Enable internet access for the virtual box by making a bridged connection Using the following steps. Go to Devices, Network Settings.
centos network settings

Under Network, select “Bridged Adapter” in the attached to field.
centos bridge network connection

Step 6: Enable your graphical interface

The centos box will show the Linux console based UI to work with by default. Had to go through some hiccups before I could make the graphical interface working.

Will need to do a few installs on the box for it using yum commands.

If it gives an error “cannot find a valid baseurl for repo” during the below steps installation, run this command
$ dhclient
Reference: http://unix.stackexchange.com/questions/22924/how-can-i-fix-cannot-find-a-valid-baseurl-for-repo-errors-on-centos

$ yum clean all #cleans up the cache
$yum update
$ yum groupinstall “GNOME Desktop” “Graphical Administration Tools”
Reference: http://www.itzgeek.com/how-tos/linux/centos-how-tos/install-gnome-gui-on-centos-7-rhel-7.html#axzz3XUgIBpsm

$ systemctl set-default graphical.target (We are telling the system to startup in graphical mode)

Reboot the system. It should now open in graphical mode.
centos 7 graphical interface

This post covers running centos in different types of desktop environments (http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7)

Step 7: Install Guest Additions on your virtual box

Select Devices-> Insert Guest Additions CD image
centos 7 insert guest additions cd

Open terminal. When in the GUI interface, terminal can now be opened by going to Applications-> Terminal

Run the following command to go to the folder containing the guest additions files
$ cd “/run/media//VBOXADDITIONS 4.3.26 98988″
As an alternative, you should see this disk on your desktop. You can right click on it and select “Open in Terminal”
virtual box open guest additions cd in terminal

Then run the following command to install the guest additions
$ sh VBGuestAdditions.run

I got the below error when I ran it the first time.
centos failed guest additons

If you get the same error, try the below command
$ yum install -y gcc kernel-devel

If it gives the error about unable to download package, try the below command first
$ dhclient
Then run the same command again
$ yum install -y gcc kernel-devel

After the above command succeeds, try running the below again.
$ sh VBGuestAdditions.run

You should see the below success messages:
centos successful installation of vbox additions

Reboot the machine. Now the guest additions will work. You should now be able to mount folders. Also, you would no longer need to hit the right Ctrl key to move your mouse pointer control from the virtual box to your host box. It should all work seamlessly as you move your mouse from inside the virtual box space to outside.

References
http://wiki.centos.org/HowTos/Virtualization/VirtualBox/CentOSguest

Step 8: Mount a shared Folder

Go to Machine -> Settings
centos machine settings

Go to the Shared Folders Section
virtual box settings shared folders

Right click and add a shared folder
virtual box add shared folder

Select a folder on your host box that you want to share with this virtual machine.
Check the “Auto-mount” and “Make Permanent” option and then hit Ok.
virtual box select folder to share

You should see the shared folder like this:
virtual box shared folder

Open the terminal
Create a new directory with the following command
$ sudo mkdir temp

Then mount the host shared folder on it with the below command.
$sudo mount -t vboxsf temp temp

The folder temp is now a shared folder between the host and the virtual box

Step 9: Create a snapshot of your machine

Your virtual box is in a pristine shape. Before you start working with it, it would be good to keep a snapshot of this original state. This way, you will not need to go through the same steps to create a brand new virtual box again. You can use this snapshot again.

Power off your virtual box (by closing it’s window).

On the Oracle VM VirtualBox Manager, click on Snapshots on the top right.
virtual box manager snapshots option

Right click on the Current State of the virtual box, and select “Take Snapshot”
virtual box manager take snapshot

Give a name of the snapshot and a description. Then hit ok. You now have a snapshot that you can always go back to if things go wrong.
virtual box saving a snapshot

 

Conclusion

It does take a little bit of work as outlined above to get your virtual environment going. But once you have it installed and working, you don’t have to do it again. You can always use the snapshot of this base install for creating all virtual boxes in future and keep reaping the benefits of your setup henceforth.

Wordpress

Adding a new blog post to your wordpress site

20 Sep , 2014  

I created a short video on how to add a blog post to your wordpress site. It’s pretty easy and does not need any prior experience to add a blog post. Here is the link to the video:

ExtJs,Javascript

How to find out the ExtJs component behind a specific element on your html page

6 Jun , 2014  

How to find out the ExtJs component behind a specific element on your html page

The Problem

While creating web pages using the ExtJs framework, we do not directly create the html markup. ExtJs is responsible for creating the markup based on the components we define.

But after everything is rendered and you are looking at a particular element on the page, it’s hard to tell which extjs component has rendered that markup. This becomes more apparent when we are dealing with a complex web application with dozens of components defined in different js files.

If we are looking at the html output and wondering which component needs to be modified to change the look of it, it’s not that easy.

The Solution

Here is an easy way to find that out. Browse to your webpage in Chrome. Then right click on the element in question, and click on “Inspect Element”.

Untitled_Clipping_060514_114502_PM

This will open up the Elements tab in the Chrome debugging Tool with that particular element markup highlighted.

Screenshot_060514_114712_PM

Now go to the console tab and type this command: Ext.getCmp($0.id). It will reveal the ExtJs component in the console.

Screenshot_060514_114900_PM

You can expand the object to see all it’s properties and easily identify it. Note that a typical component renders a bunch of html elements. This trick will not work if you are on a child level element. So if this command does not give you any result, click on the parent html element to highlight it on the elements tab. And run this command in the console again. You might have to keep traversing the markup this way a few times as most component render a deeply nested markup.

How it works

$0 is a chrome trick to get the javascript object for a given html element. When you do $0.id, that reveals the id of the element. Now, ExtJs provides a way to select an element by it’s markup id: Ext.getCmp(‘idofelement’);

So running Ext.getCmp($0.id) means select the ext component whose id is the same as the id of the selected html element.

So there you go. Using this trick you no longer have to second guess where your markup is coming from. Hopefully, this will save you a bunch of time like it does for me. Happy debugging!!

 

Javascript

Javascript: Add more functionality to an existing method of a javascript object

16 Apr , 2014  

Let’s say we have a class with an existing method.

var MyClass = function() { };

MyClass.prototype = { myMethod: function(parameter1) { console.log(‘called from original method ‘ + parameter1); }}

We can create an instance of the class and call it’s method like this:

var obj = new MyClass();

obj.myMethod(‘p’);

>called from original method p

Now, suppose that for this instance obj, we want to modify the definition of myMethod in such a way that some additional code is executed. But at the same time, we don’t want to lose the original code. In the end, we want that code to be executed as well.

We can achieve this by first storing the original method definition in a variable before modifying the method. Then in the modified method, we can call the original code as well by referring to this variable.

var origFunction = obj.myMethod;

obj.myMethod = function() { console.log(‘called from the modified function’); if(origFunction){origFunction.apply(this, arguments); }}

Now, when we execute myMethod, we get the following output:

obj.myMethod(‘p’);

> called from the modified function

> called from original method p

Note: This was about modifying the object instance. Not the original class. The original class is still intact as you can verify below:

var obj1 = new MyClass();

obj1.myMethod(‘p’);

> called from original method p

Startup

Is advanced payment from customer enough for idea validation

8 Jan , 2014  

When it comes to customer validation, the idea of getting them to pay in advance is mostly touted as the best strategy. Some recommend putting out a landing page with a fake buy or sign up link. But if we actually talk the customer into making an upfront payment even before the product is ready, that speaks volume on how much the product is solving their pain point(s).

While I do not disagree with this, and will possibly try to do the same thing too when validating my business idea, I also wanna know if this is the best we can do. What more can I possibly do? It’s an important question to ask before we go down the route of investing our time and money building something.

I had the privilege of attending a info session at Flashpoint out here in Atlanta. Flashpoint is a well known management and education program here, that works closely with founders to create companies. Merrick who happens to be the biggest pillar at Flashpoint shared some interesting perspectives at this session.

He definitely doesn’t believe getting upfront payments is a guarantee your product will be successful.

Having helped start many startup companies, he makes his student founders go through a rigorous process of customer discovery and validation which is pretty intensive. He didn’t go through the full details of the program in the info session. But the basic idea is in trying to understand what is the thing that the customer cannot do without.

Quoting his exact words: “You can rely on people to do what they cannot not do. If they can avoid it, they would. The opposite of this double negative is not true. Customers are immune to Change.”

So if they can avoid a product that will force them to change, they will avoid using it. So next time we build a product, let’s build something so compelling that the customer is compelled to make the change in their business to use it. Let’s build something so good, he does not have a choice!

OData

Adding Authorization to OData Web Api Service using Visual Studio 2013 Provided Templates

3 Nov , 2013  

In previous posts I have show how we can easily expose a database table on the web using OData capabilities, by creating a OData Web Api Service. In this post I am going to demonstrate an easy way to make this service secure so that only properly authenticated users can access it.

If you have noticed the new features in Visual Studio 2013, they have done a lot to make Authentication and Authorization a snap. There are built in features to Register and LogIn users through site specific account as well as through a Google, Facebook or Microsoft Account.

I wanted to be able to reuse all this functionality in my OData Service Stack so that I can easily secure my service without reinventing the wheel again. Turns out, it wasn’t too difficult.

The Microsoft implementation of service stack for authentication is not through OData. So I decided to just add the built in authentication side by side to my OData Controllers.

The steps to create a Web Api with Authentication are pretty simple.

It actually gives a dialog window with four different options for Authentication. I chose the Individual Account one which is sufficient for my purpose. Go here to learn more on how to add authentication as well as what the four different options mean.

authentication_options

After that, all I had to do to my OData Controller was put an Authorize attribute on top of it’s class definition. Now this attribute takes care of returning a 401 Unauthorized message if the user tries to access it’s methods without first authenticating himself.
adding_authorize_attribute

One more step that I decided to do was to move around the authentication related tables. When Visual Studio creates the Authentication tables and service layer, the tables are created in an mdf file in the App_Data folder by default. I grabbed the schema of those tables and recreated them in my SQL Server database where the rest of my tables resided. This ensured that all my tables are in one place.

Here is a 15 minute video detailing all the steps I took to create a service stack with Authorization enabled and then test it using fiddler:

Javascript

4 Amazing Chrome Hacks for Developers

31 Oct , 2013  

In this age of rich and highly interactive web pages, cutting edge client side scripting skills are in high demand. All modern browsers provide a rich set of developer tools to aid us. I like Google Chrome the most. Here are some cool things that you can do with Chrome.

And first off, if you haven’t seen the chrome dev tools before, just hit F12 while your chrome browser is open and in focus and voilla!! You would be happily surprised to see all the information about the currently loaded page. Anyways, this blog is about some cool hacks for devs who are already using the tool, so let me jump right into it.

1. The Network tabs on Chrome shows the list of requests being sent to the server. We usually monitor errors or logged statuses on the console tab. But if you want to see the network requests here itself, just right click and select the first option “Log HttpXmlRequests”.

log http requests

2. Have you ever got lost trying to find a particular piece of code in the plethora of js files and resources that is loaded. Next time you need to do this, just go to the Chrome Console. Do Ctrl+Shift+F and a search in all files dialog will appear. Put in the search term and hit enter to get the list of matching results across all files. Clicking on them will take you to the specific file.

3. Do you have to read returned json data in the browser window. Have you struggled navigating through the json string. Install this Chrome extension to have the json data presented to you in a nice format: https://chrome.google.com/webstore/detail/json-prettifier/kccpfgilgmgbipamhohknpokhibinhhj

4. A lot of times we change our js files and reload browser to test the change. You need to make sure that the browser does download the latest file. To avoid caching of the files, press F12 in chrome to open the developer tools. Then go to the Network tab, then click on the settings icon on the bottom right. Check the first checkbox (Disable cache (while DevTools is open). Then whenever we access the site, make sure dev tools is open. The load times for each page might increase but you can be certain you are debugging with the latest versions of the file.

disable cache

What are some of the Chrome Dev Tool Features that you like? Please share it in the comments…

Javascript

Understanding Context in Javascript

31 Oct , 2013  

The ‘this’ keyword (also referred to as the context) in a javascript function is used heavily to access properties and methods that are defined within the same object.

Code:
function myObject(){
this.property1 = “Something”;
this.alertProperty1 = function(){
alert(this.property1);
}
};

In the above code alertProperty1 function uses the this keyword to get the value of property1 which is defined in the same context.

Now let’s execute this code.

var obj1 = new myObject();
obj1.alertProperty1();

As you would guess, the call obj1.alertProperty1() alerts the text “Something” since property1 value within obj1 is “Something”

Now here comes the tricky part. There are ways to call alertProperty1 in which the context might not be the context of obj1.

One example is when you are calling the function asynchronously. In the below code, we are using setTimeout to call this function after 5 seconds. Interestingly, the alert window shows undefined instead of “Something”.

That’s because setTimeout sets up a new asynchronous thread. When the function is called, the context is not of myObject anymore. It is now the window itself. Since window does not have a property by the name property1this.property1 gives us undefined.

window.setTimeout(obj1.alertProperty1, 5000);

How to fix this?
Thankfully there is a way to force the execution to be in the context of obj1 at all times. This can be done by passing the context explicitly when calling the function. Below is one way to do that. When you run this code, the alert value will be “Something”, as we are using the bind method to call the alertProperty1 while binding the context explicitly to obj1.

window.setTimeout(obj1.alertProperty1.bind(obj1), 10000);

See it in action here: http://jsfiddle.net/uqRzT/2/

Conclusion
A good understanding of how the this keyword or context works in javascript is important if you code regulary in javascript. Context related errors happen frequently and are sometimes hard to debug. Hopefully this article gave you a basic understanding of context and a quick tip on how to avoid such errors.

OData

Creating Service Stack for my database using Entity Framework and OData

31 Oct , 2013  

Taking the Database First approach, if you already have your database defined with all the tables in place, how soon can you provide the functionality to read and write to it over the web?

I just looked at the latest offerings from Microsoft and pretty impressed by how easy it is to do this using Entity Framework, OData and some code generation wizards.

This is a 15 min video of how I was able to create a service from scratch that would be ready to deploy over the web fulfilling all my basic requirements to access my database and run my CRUD (Create, Read, Update and Delete) operations.

The example is using .Net Framework version 4.5, Entity Framework 6.0 and the OData protocol. I used the Visual Studio 2013 as my development tool.

Entity Framework

Create Entity Framework Mappings for tables using a wizard

31 Oct , 2013  

It always feels exciting when you don’t have to do redundant work. I am currently looking at the best ways to create an ORM layer from an existing database without much overhead. Reading on Entity Framework, I like how easy it is to create the simple mapping classes for my tables.

These classes can then be used to fire CRUD (Create, Read, Update and Delete) operations on the tables. The detailed article on the approach I am demonstrating here is at this microsoft article: http://msdn.microsoft.com/en-us/library/vstudio/cc716703(v=vs.100).aspx but here is a quick 5 minute video demo on how to create the mappings from the database.

Here are the steps:
1. Open or create the project for which you want to create an .edmx file.

2. Right-click the project name in Solution Explorer, point to Add, and then click New Item.

3. Select ADO.NET Entity Data Model in the Templates pane.

4. Enter the name for the file (<project name>.edmx), and then click Add.
The first page of the Entity Data Model Wizard appears.

5. Select Generate from database in the Choose Model Contents dialog box, and then click Next.

6. Click the New Connection button.
The Connection Properties dialog box appears.

7. Enter the server name, select the authentication method, and enter the name of the database for which the model is being created. Click OK.
The Choose Your Data Connections dialog box is updated with the database connection settings.

8. Click Next to continue.
The Choose Your Database Objects dialog box appears. By default, no objects in the database are selected for inclusion in the .edmx file.

9. Expand the nodes for Tables, Views, and Stored Procedures. Cancel the selection of any tables, views, and stored procedures that you do not want included in the .edmx file.

10. Click Finish to create the .edmx file.

The Entity Data Model Wizard does the following:

* Adds references to the System.Data, System.Data.Entity, System.Core, System.Security, and System.Runtime.Serialization assemblies to the project, if they do not already exist.

* Generates an .edmx file that encapsulates the storage model, the conceptual model, and mappings.

* Creates a source code file that contains classes generated from the conceptual model. You can view the source code file by expanding the .edmx node in Solution Explorer.