Optimize development and deployment using CloudFoundry

March 31, 2013 1 comment

In this post I will be walking though the steps needed to deploy an application on cloudFoundry and how we can achieve Contiuous Integration and Continuous Delivery using CloudFoundry(http://www.cloudfoundry.com/) and Cloudbees(http://www.cloudbees.com/). Tough CloudBees is also a PaaS(Platform as a Service) provider, but I will be limiting to use the build feature of CloudBees

For the reason why I choose CloudFoundry over other PaaS providers please refer to my previous blog

I have a Maven based Spring Web Application, with a HTML view developed in HTML5 and Bootstrap. There are also some REST endpoints. It has a MySQL backend, and it also talks to some Google API’s. The local machine used was Ubuntu 32 bit 12.04 LTS

So, we start with requesting an invitation from Cloudfoundy by clicking on the signup link on their landing page.

  • Once registered, download VMC client from command line. VMC is a command line tool to manage applications in cloudfoundry. You can manage your apps/services through VMC. Since VMC is a ruby gem, please make sure you have ruby installed(http://docs.cloudfoundry.com/tools/vmc/installing-vmc.html). You can check if vmc is installed by issuing which vmc  on command line.
sudo apt-get install ruby-full rubygems
sudo gem install vmc
  •  Set the vmc target to http://api.cloudfoundry.com.  Target defines the base location where you application will be deployed. Login to cloudfoundry using the below mentioned commands. The credentials will be the same as created above
vmc target http://api.cloudfoundry.com
vmc login
  • Once the target is being set.  Navigate to the target folder of your application. Run vmc push <appname> . This will automatically pick the .war file present in the target folder, and will try to deploy the application. If there are multiple then it will ask you for the name of the war to be deployed. Press Enter to move to next options
    • Select the instances that are needed to host your application
    • CloudFoundy automatically detects the type of application that you are trying to upload, and configures the deployment environment accordingly. Select the application type(Spring in my case)
    • Select the java Runtime.
    • Select the memory limit. For this kind of free deployment, the memory limit is 2 GB i.e. All you deployed application can have total memory of 2GB.
    • Select the domain name. This URL used to access you application over public network.
    • Create Service for a application. A service is nothing but an external dependency that you application has eg:- MySQL, Redis, RabbitMQ. You can create those service instances here and they will be automatically be linked to you application. There is no need to update the database properties files with new credentials. CloudFoundry replaces the beans at Runtime to connect to its own service. For more read refer http://bit.ly/16q3heb and http://bit.ly/YLXXPW .
    • Save the configuration.
  • VMC will now try to upload the application war file and then deploy on the tomcat instance, then the application will be started.
  • Once the application is started, you can see the status through the following commands. Also validate the application by hitting the URL.
vmc apps - Show all running applications
vmc app <appname> - Show the specified application status
vmc info - Show the target information and user details
vmc services - Shows all the running services
vmc service <servicename> - Show the service details
  • CloudFoundry uses tunnels  to connect to the  services that are currently bound to the applications. Below example connects to the MySQL service.
vmc tunnel <service-name> mysql/mysqldump
  • To access the log files for tomcat use the file command.
vmc files <app-name> <file-path> {logs/stdout.log}

CloudFoundry does provide a clean way of deploying application from our local machine, but this is tedious and also I cannot get the test results and sonar reports. It would be better if I can achieve Continuous Integration and Continous Delivery of any changes that I push to my git repository. For this we can leverage the CloudFoundry integration with CloudBees. Cloudbees provides out of the box jenkins installation, which can be used for CI.

CloudFoundy provides a clean guidline of how cloudbees can be used to provide CI and CD on CloudFoundry. It can be found on the following blog link .

On caveat here is the link mentions coding, building and deploying all under Cloudbees umbrella. Since my code is in BitBucket, it does not make any sense to move the code from BitBucket to Cloudbees.

Assuming that the Jenkins instance is hosted on the following link http://<appname&gt;.ci.cloudbees.com . Open the jenkins job and click on Configure . 

    • Cloudbees to access the git repo and perform checkout, you need to add the CloudBees public SSH Key in you git account. The public key is present in the Configure section. How to add it to git repo, follow the given link
    • In the Source Code Management provide the ssh path git repository which hosts your code. 
    • In the Post Build Section check the CloudFoundry configuration settings.

With these step we should be able to setup and configure Jenkins. Click on Build Now to start a new build. The code will be checked from your git repo. Post build the war file will be automatically deployed to CLoudFoundry.

Using the combination of CloudBees and CloudFoundry, we can automate the whole process of build and deploy. Also with jenkins in place we can use tools like Sonar and Emma to get the health of the builds and analyze different pointers.

Cheers.

Advertisements

Choosing the right cloud

March 31, 2013 10 comments

There is a small mobile application that I am working on. It a  mobile module as well as a server module, which stores all my data. Till now, it was inhouse development where I deploy my server on tomcat and then access from the mobile app as both are on the same wireless n/w domain. This obviously wont work, when I needed to host the sever-side module to a public network, for it to access anywhere. Alas, and I was struck by the Cloud,  the buzzword I hear too often. So I though, lets deploy it on cloud. So, I went on searching for a cloud provider that would help me deploy my application.

Below were my parameters/requirements.

  1. I have a small Spring Web application which has a HTML view and some REST service endpoints. There is a MySQL database at the back-end and the application fetches data from some Google Services.
  2. It should be free. I intend not to spend a penny right now since the app is in it’s infancy.
  3. It should not be a 30 day time trial.
  4. It should be quick. Something similar to how I deploy applications on my local dev.
  5. It should not be overly complicated. I don’t want to go through API or management console and tons of external dependencies.
  6. I hate to doze through documents and installing sample apps to figure out how this thing works.
  7. It should give me good computing power and good space, so that my application has a space to breath.I am more leaned towards Platform as a Service(PaaS) providers.

Below are some of the cloud service providers that I had evaluated, and my take on them(This is personal opinion and does not indicate that the providers are bad).

  1. Amazon – Amazon is probably the first Cloud provider that anyone can think of. Its like NASA when it comes to space stuff. The ecosystem of amazon is very wast and the options provided are immense and diversified. On the flipside, hosting an app to Amazon, involves a lot of groundwork and  effort of integrating and running  application is huge. So amazon proved to be too big for a small application like mine. They should have an express setup with limited functionality so that people can get their hand dirty before jumping onto AWS Management Console.
  2. Heroku – I did  fancied the name. They have awesomely named servers(After fictitious samurai’s), but then that does not hide the fact that heroku works on the world of his own. His own git repository, different deployment process. Dynamo’s and workers are a tad complicated. I just could not figure out how I can deploy my simple spring application on heroku without making configuration changes.
  3. OpenShift – I just could not make it work. I thought it was nice interface but I wasn’t able to deploy my application to it. It’s an opaque system, and you never know how/why your application failed to deploy. Also, no MySQL or third party integration find a mention(Or may be I could not find it 😦 ).
  4. Rackspace – I doubt if they have any free hosting
  5. Jelastic – The interface seems promising and was able to get a good management console, was even able to setup Mysql and tomcat instances to run on. But then it would not allow me to fetch the code and build it from the git repo, and it also does not have a feature to upload and deploy a war file.
  6. CloudFoundry – This is a cloud hosting provided by VMWare, and is currently in beta. It provides limited functionality and a clean process to deploy you application. It’s free (for upto 2GB of RAM space) and does not have a time trial. Deploying the application was a breeze and since it can be done from command line, it’s fun to do :).

After going through the given providers CloudFoundry seems to be a cloud provider that I could use.

  1. Has a very simple and minimal learning curve. Just understand how vmc works and you are good to go. 
  2. Deploy the application as a war file rather that cloud provider checking out the code and then building it(This is error prone,fragile and not verbose). Also only the delta changes are uploaded and not the whole war file, thereby making deployment quick.
  3. It automagically recognizes application types and configures the deployment environment as per the application.
  4. External service binding to databases like MySQL, Redis and MongoDB are very simple and easy and it doe not require you make any code changes. You can switch service providers at runtime.
  5. There is a RAM limit of 2GB, so you can host multiple applications , just that the total RAM size should not exceed 2GB. So you can start/stop apps as an when needed.

CloudFoundry currently might not be as full fledged  cloud service providers as a other player but they have a easy deployment process and also provide easy access for people to setup their dev environment for infant applications. It saves you the pain of spending a day or two setting up the system or else being scared of the time trial finishing or various thresholds being reached after which you are getting charged.

CloudFoundry currently supports Spring, Java web , Ruby, Scala,node.js etc. .AppFog is one PaaS provider that is build over CloudFoundry(https://www.appfog.com/)

http://docs.cloudfoundry.com/getting-started.html

GWT with tomcat

January 10, 2013 Leave a comment

I have been working with GWT for sometime, and I always find it annoying that GWT deployment runs on an in-build jetty server. So I tried to give a shot to using tomcat and deploying GWT application on that, and see if it makes the hot deployment easier. The problem is we cannot do Run As->Run On Server and deploy the application on tomcat.

Below are the steps how you can configure your tomcat to deploy GWT application.

  1. Create a new Apache Server (I used Apache 7.0)
  2. Create a mavenized sample for GWT app(using this link)
  3. Do a clean build, package the application to generate a war file.
  4. Open server.xml defined in Servers->Tomcat Server folder present in your workspace.
  5. Edit the <Context> tag. Edit the docBase to point to the exploded war folder present in the target folder of your application.
  6. Run mvn clean package to package your application.
  7. Run the application on Tomcat(Run As->Run On Server).
  8. Open Chrome, to see if the GWT application is correctly deployed.

This will only deploy the application. In order to make any changes being reflected in the web page, you need to redeploy the application.

To run the GWT application in codesvr mode, you need to run the application as Run As->Run As Web Application(running on an external server). On the dialog box give the path of the deployed tomcat application as External Server Root and select one of the matching items. Click OK to deploy the application in debug mode.

Installing Android x86 on VirtualBox and Eclipse development

October 21, 2012 1 comment

After spending night oils and getting frustrated with the slow speed and frequent timeouts with the android emulator, I decided to host the android-x86 image on a virtual box and then use to for my development purpose. This was done with the hope to get ease of use,quick deployment and performance improvement. Please note that I am using the x86 4.0.4 (Ice Cream sandwich) deployment. Please choose correct deployment from the Android x86 download section for your purpose

Below are the things that are needed to host the android image as a virtual device

  1. Android x86 image iso (Android x86 download section)
  2. Oracle Virtual Box (Download) . I am using ubuntu, so I can issue the sudo apt-get install virtualbox command to install virtualbox.

We start with downloading the latest iso image for Android x86  from the link above. I  initially downloaded android-x86-4.0-RC2-eeepc.iso and it had a better ethernet support( I was not able to put up an ethernet connection with the asus or the IBM iso images). But then I was able to get an ethernet connection enabled android-x86-4.0-eth0-generic_x86-20120516.iso released  by tabletx86(Thanks). The file is currently hosted at this link for download.

Steps for installing and running the image can be followed from the given blog post

  • Launch New virtual machine wizard Select Linux and Linux 2.6 after giving a proper name to your virtual machine
  • Set the base memory size to an appropriate level. I have set it to 1000 MB as I have a 8 GB machine. Set it to 512 MB. This can be changed later
  • Create a new Virtual Hard Disk. Select the  Start-up Disk option and Create a new hard disk  radio buttton
  • Select the file Type as VDI
  • Select storage details as Dynamically allocated
  • Select the size for the disk space that for the virtual machine. I stayed with the default 8GB.
  • Press Create to finalize the changes and create the Android Virtual machine.

After Creating the VM , we need to configure it to install the ISO and also configure the ethernet adapter for n/w connectivity. Open the Setting window for the newly created VM.

  • Select Display  and assign the video memory to a value say 20 MB.
  • Select Storage  to specify the android iso image. In the IDE Controller, select Empty. Now to the right of it in the Attributes section Select the path of the iso image to load by clicking the CD image.
  • Select Network  and select Enable Network Adapter  check box. Also select Bridged Adapter  from the Attached To,  and eth0 or wlan0(whichever is available) from the Name dropdown lists.Expand Advanced. and select Adapter Type  as PCnet-FAST III. 

After setting up the VM, start the VM to install the android-x86 image. Refer to the link for instructions

After installation, run the VM to bring up the Android Screen. So we have successfully installed Android image on our VM. Now we need to configure the VM so that it behaves as a networked machine, and can share the internet connection and also be connected through the adb bridge.

  • Unlock the screen and press Ctrl + F1  this would open the terminal for the VM.
  • Open the file /etc/init.sh with write access (Use su to gain root access and then vi /etc/init.sh)
  • Put the following lines at the end of the file.Save and close the file
    • netcfg eth0 up
    • netcfg eth0 dhcp
    • setprop net.dns1 8.8.8.8
  • Execute netcfg on terminal. It should should eth0 marked with the a local n/w IP address (192.168.1.2 in my case).
  • Open the browser to check if internet is working. If not, then restart the VM.

Once the internet connection is established then we can connect the VM through the adb by issuing the following command adb connect <IP address>.  Start your eclipse Open Devices View and the VM should be listed as an online device.

Changing Screen resolution for the VM

In case of simulating different screen resolutions we can configure the VM to ask for the resolution with which the VM should start with (link)

  • Start the VM and from the GRUB menu select debug mode
  • Run the following commands to update menu.lstfile
    • mount -o remount,rw /mnt
    • vi /mnt/grub/menu.lst
    • Update the kernal command to add DPI=160 and vga=ask, in both the normal boot command and the debug boot command
    • Save and exit the file
  • Un-mount and restart
    • cd /
    • unmount /mnt
    • reboot -f

Now on restart it will ask you the VGA resolution that needs to be selected.

Also we can add the resolution information directly to the .vbox file to Virtual Box by issuing the following command

VBoxManage setextradata <vm_name> <CustomVideoMode1> <1200x768x16>


Running On VMWare

Running an x86 image on a VMWare is also pretty simple

  1. Download the iso image from the link above. Create a new machine in VMWare Workstation
  2. Select the installer disc image as the one just downnloaded. Select Other as a the Operating System. Then select Free BSD   from the download below.
  3. Give the name for the VM and Disk Size in the next Screens and click finish to create the Virtual machine
  4. In the network Section of the VM option select with NAT or else Bridged Adapter.
  5. Start the VM and follow the installation steps as mentioned earlier.

Virtual Machine did have pretty good benefits as compared to the emulator and hence its suggested.

  1. Running the VM is much faster as compared to the android emulator.
  2. Also,we get keyboard control
  3. We can test the app for multiple reslutions.
  4. It lacks orientation support as of now

Hope this tutorial will help you in setting up the Android image successfully.

Spring redirection

There are two way of redirection present in Spring that I have come across . Forward and Redirect.  We have been using redirect, but have been facing a problem. Each time we redirect our model attributes get attached to the URL as query String paramaeters. This is not right as it exposes the internal handling of elements in the application. So we needed to prevent the model attributes from appearing in the URL.

After digging for solution I got to know that spring itself provides us with the option for attaching or detaching the model attributes from the queryString. We needed to check ig the view is coming in as a redirectView. If it is then set the modelAttribute parameter to false. And now the model attributes will not be visible in as query String.

As I am using URLBAsedViewResolver I had to override the createView() method to provide my custom implementation.

Below is the code for the same

if(element instanceof RedirectView)
{
    RedirectView redirectView = (RedirectView)element;
    redirectView.setExposeModelAttributes(false);
}

Configuring Locale in Spring

Locale is one of the most important thing that you need in case you want to internationalize the messages. In Spring it is really very easy to configure Locale both at runtime and at application load time.

To setup locale we need 2 things

1. SessionLocaleResolver

2. LocaleChangeInterceptor

SessionValueLocale sets the param value for the Locale. the properties files will then be suffixed with the param value supplied to look for locale specific properties.

<bean id="localeResolver">
        <property name="defaultLocale" value="en_US" />
</bean>

This would setup the Locale for the application for “en_US”. Hence we need to have something like messages_en_US.properties which will house the locale specific messages

LocaleChangeInterceptor  can help users to change the locale at runtime based on the locale parameter provided with the request.

<bean id="localeInterceptor">
         <property name="paramName" value="lo" />
</bean>

After configuring this we can pass in the locale params through URL query string, with the key being the paramName specified in the bean property

EG:- for setting a UK based locale we pass ?lo=en_UK. So the messages will be picked from messages_en_UK.properties.

We also need to added the above bean as an interceptor for handler mappings so that it wired with each request directed to the application

<bean 
class="org.springframework.web.servlet.mvc.support.
ControllerClassNameHandlerMapping" >
       <property name="interceptors">
             <list>
                  <ref bean="localeInterceptor" />
             </list>
       </property>
</bean>

Setting up JBOSS AS7 with eclipse

April 26, 2012 1 comment

I recently got a chance to try out JBOSS AS7 as application server container for the upcoming project and I must say that JBOSS is no longer the monotithic,tiring,heavy and problamatic server anymore.It took me 4 seconds to run the server, which is fascinating considering the legacy.  Parallel deployment. quick startup,lightweight, powerful feature etc are some nifty features on offer

So, as part of the initial requirement, we have to setup a maven based project and then deploy that onto JBoss AS7 through Eclipse and trust it was’nt a piece of cake and henceforth I decided to  write a post about it.

  1. Download and unzip JBoss AS7 from http://www.jboss.org/jbossas/downloads/ to a convenient location
  2. Create a basic maven project from maven quickstart archtype.
  3. This is optional step if you are planning to use JBoss external to Eclipse.
  4. Download JBoss Tool for Eclipse from the following update site. You will only need to install JBoos AS Tools for registering/creating a server.This is optional step if you are planning to use JBoss external to Eclipse.
  5. Setup a JBoss Community Server within eclipse,Server->JBoss AS7 by providing the path to JBoss folder. This will help you in displaying the logs in eclipse console.
  6. AS7 currently deploys wars that are present within standalone/deployments folder.

The problem was how to deploy the war file into JBoss automatically from eclipse.  The steps follwoed with tomcat did not work and I had to come up with some different solution. I thought against making the changes in JBoss configuration.So, I thought that maven creates a war in target folder by default.We can change the o/p directory such that the war is created within the standalone/deployments folder. This way our updated war reaches JBoss each time we build the project and since JBoss is running within it would be automatically deployed, and I can track the log messages in the console.

To achieve this I used the maven-war-plugin in pom.xml. I configured the plugin to set the OutputDirectoy to the standalone/deployment folder.And Voila, it worked, so we were able to wire our project with JBoss seamlessly and also were able to reload the context automatically without going through the pains of understanding the JBoss configurations.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <outputDirectory>/path/to/standalone/deployment</outputDirectory>
  </configuration>
 </plugin>

Also through AS7 Maven Plugin
Add the following maven plugin info in pom.xml

 <pluginRepository>
   <id>jboss-public-repository-group</id>
   <name>JBoss Public Maven Repository Group</name>
   <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
 </pluginRepository>

<plugin>
 <groupId>org.jboss.as.plugins</groupId>
 <artifactId>jboss-as-maven-plugin</artifactId>
 <version>7.1.1.Final</version>
 <configuration>
  <hostname>localhost</hostname>
  <port>9999</port>
  <filename>${project.build.finalName}.${project.packaging}</filename>
  <webModule>
   <groupId>${project.groupId}</groupId>
   <artifactId>${project.artifactId}</artifactId>
   <contextRoot>/test</contextRoot>
  </webModule>
 </configuration>
</plugin>

The execution can be configured through the following entry, which says if the maven phase is package then next linked goal should be delpoy

 

<executions>
  <execution>
   <phase>package</phase>
   <goals>
      <goal>deploy</goal>
   </goals>
  </execution>
</executions>