Friday, October 9, 2015

Build your first HTTP Server using NODE.JS

In this blog, I will be talking about how you can create a HTTP server using Node.js. Later in the blog I will talk about creating "Hello World" app using Express.js

I will start by creating an empty file named Server.js and write the following code in it.


Detailed Explanation of the Above Code


Loading the HTTP module

Node.js has built-in modules to create HTTP server. The first line uses the require function to load a built-in module called http.




Creating the server

We need to create a HTTP Server object to create a server. So, the variable app takes a function that will listen for requests.
The writeHead() is called to write the header of the response. In our case, Http Status is 200 and Content-Type is text/plain.
The end() method sends the response to the client.



Starting the Server

To start a s server we call listen method on the server object.



Run the Server

In the terminal, write the following command:

node FILE-NAME.js

Create a simple application using Express.js


- Express.js is a node.js web application framework.

- Before you start using Express, don't forget to install it.

npm install Express




- We require express and node's http module.
- The express() function is a top level function exported by the express module.
- In the above example we are calling HTTP Get method.

You can use a browser to do a GET request just by entering http://localhost:9090 or you can use Advanced REST client.

After successful execution of the above program, output would look like: 


Now, you have a HTTP server up and running.

Wednesday, May 27, 2015

ESXi and VM Operations using vSphere Java API


In this blog, I will give a detailed description about various VMware technologies such as ESXi, vSphere Client, Hosted Architecture and how to install ESXi, vSphere Client on your machine. 
Also, I will talk about how to use vSphere Java API to perform VM operations.

  • ESXi: It is a Type-1 hypervisor in which the virtualization layer runs directly on hardware.
  • Hosted Architecture: It is a Type-2 hypervisor in which virtualization layer runs on top of a host OS.
    Players: VMware Fusion (Mac), VMware Player (Windows, Linux), VMware Workstation (Windows, Linux), Oracle VirtualBox (Windows, Linux and Mac).
  • vSphere Client: It is an interface for administering ESXi and vCenter Server.
  • vCenter Server: Centralized Management of entire vSphere infrastructure. It has multiple ESXi hosts.

Step 1: Download and install ESXi.
  • Register yourself at my.vmware.com
  • You can download ESXi from: http://www.vmwarearena.com/2013/10/vsphere-55-download-free-esxi-55.html
  • For installing ESXi, you have to first install any one of the above mentioned hosted architecture's players. Since, I am a Mac user I would suggest you to download VMware Fusion and for windows I would suggest Oracle VirtualBox.
  • Once VMware fusion is successfully downloaded and installed, we will then setup ESXi.

    Install ESXi
  • In VMware fusion create a virtual machine
  • If your machine only has 4GB RAM, use ESXi 5.1 instead of using  ESXi 5.5
  • VMware Fusion Settings:
    Select 2 cores, for ESXi 5.5: 4GB RAM; ESXi 5.1: 2.5GB RAM, Disk: Thin Provisioned, Network Adapter: Bridged, Guest OS: VMware ESXi 5.
    Note: Change the network type to bridge to connect vSphere client to ESXi.






Step 2: Download and install vSphere Client.

Mac users cannot directly install vSphere Client on Mac OS. First install Windows 7 in VMware Fusion and then install vSphere Client on Windows 7.

  • You can download vSphere Client from:
http://vsphereclient.vmware.com/vsphereclient/1/9/9/3/0/7/2/VMware-viclient-all-5.5.0-1993072.exehttp://vsphereclient.vmware.com/vsphereclient/1/2/3/5/2/3/3/VMware-viclient-all-5.1.0-1235233.exe


  • Once vSphere Client is downloaded, install it on Windows and invoke it.



Use IP address, User name and Password of ESXi to access ESXi from vSphere Client.


Step 3: Creating VM on ESXi from vSphere Client.

  • Upload ISO files of different OS to ESXi before creating VM. You can select any of the below options to upload ISO file.

                 – Putty
                 – VI client: datastore browser.
                 – VI client: mount external ISO file.
  • Create new VM and Resource Pool.

  • List of VMs within ESXi
  1. VM1: xphome-656-1
  2. VM2: ubuntu1410-656-2 

    Step 4: Perform VM operations using vSphere Java API (vijava)

    In this blog, I will explain only 2 VM operations i.e Power On  and Power Off VM. There are many other VM operations such as snapshot, clone, migrate which I will be talking about in my next blog.

    Power On and Power Off operations can be performed using vSphere Client. But in this blog I will show how to perform these operations using Java code.

    In Eclipse IDE, add the following external JARs to perform VM operations using java code:
    • dom4j-1.6.1.jar
    • vijava55b20130927.jar


     Step 5: Code Snippet

    I will only give code snippets which will help you in understanding how to connect to ESXi by writing java code and perform different VM operations.
    • Connect to ESXi
      ServiceInstance si = new ServiceInstance(new URL("https://IP OF ESXi/sdk"), "username", "password", true);
    • Power On VM
                    if(vm1.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOff)
    {

                     Task task = vm1.powerOnVM_Task(null);
    task.waitForTask();
    System.out.println("Power On VM: status = " + task.getTaskInfo().state);
                   }
    • Power Off VM
                if(vm1.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOn)
                { 
                         Task task = vm1.powerOffVM_Task(); 
                         task.waitForTask(); 
                        System.out.println("Power off VM: status = " + task.getTaskInfo().state);
                }


    Here is the console output and vSphere Client recent task output to show that Power On and Power Off VM operations are successfully performed.





    In the above displayed output, I have also extracted Host and VM information using vijava.

    Please explore vijava api more so that you can get crystal clear understanding of how host and vm information can be extracted.

    --------------------------------------------------------------------------------------------------------------------------

    So, we have seen how to use vSphere Java API to perform VM operations.