Sunday, 30 December 2018

AWS - Running a Spring Boot Application on AWS EC2 Instance

This post describes about how you can deploy and run a spring boot application on AWS EC2 instance.

Generate Spring Boot Application 
1. Generate spring boot application from here. Provide Group and Artifact detail and choose Generate Project. Note that this project is generated as a maven project by default . So you must ensure to have Maven in your local machine to build this project.




2. When you choose Generate Project, a zip file is downloaded with name aws-ec2-springboot.zip. Extract this file and import this maven project to your Eclipse IDE.

3. Now create the HelloController.java in this spring boot project.
package com.nv.aws.ec2.springboot.awsec2springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "AWS EC2 Springboot - Greetings from Spring Boot on AWS !!!";
    }

}


4. Open command prompt and go to the location where your project is imported. Now execute below command. This command builds and starts the spring boot application.

<YourDirectory>\aws-ec2-springboot>mvn clean install spring-boot:run

If you see below on your command prompt, this means your application is successfully started.



5. Try accessing application from your browser with below URL.
http://localhost:8080/

If this returns below page, your application is all set to be deployed on AWS EC2 instance.










Launch EC2 Instance
Now you need to launch EC2 instance on AWS. This post describes about how to launch a new Linux EC2 instance on AWS.

Java Installation on Launched EC2 Instance
To run a spring boot application on EC2 instance you need to make sure that java is installed on EC2 instance. Please refer this post to install java on linux EC2 instance.

Copy SpringBoot App to AWS EC2 Instance
Here we will use WinSCP to copy spring boot app from your local machine to AWS remote EC2 machine. We need .ppk file to connect to remote EC2 using WinSCP client. If you do not have .ppk file generated, please refer this post to have .ppk file. Now open WinSCP, connect to remote EC2 machine and copy spring boot application.















Start and Access SpringBoot App 
Access EC2 machine using Putty. This post describes about how to access Amazon EC2 Instance from Windows using Putty. Once you are able to access EC2 machine, you just need to execute below command. Since you have copied your spring boot application at location /home/ec2-user/aws-ec2-springboot-0.0.1-SNAPSHOT.jar, so make sure that you run below command from this location /home/ec2-user/

java -jar aws-ec2-springboot-0.0.1-SNAPSHOT.jar 

If you see below output, this means your spring boot application is successfully started on AWS EC2 instance.





Now finally use your EC2 IP to access web application on your browser.

http://<EC2-IP>:8080/    

http://10.205.168.99:8080/

If it returns below output on browser, you are done with running spring boot application on AWS EC2 instance.  Great Job !!.


Friday, 28 December 2018

AWS - Accessing Amazon EC2 Instance from Windows using Putty

If you have already launched AWS EC2 instance and looking for option to access that instance from Windows machine using Putty, this post may help you.In case, you have not already launched the AWS EC2 server, not a problem. Please refer this post to launch the Linux EC2 instance on AWS.

Prerequisite
1. Make sure that you have already installed Putty on your windows machine. If not please download and install.
2. While launching the EC2 instance, you must have saved .pem file (step#8 in this post). But Putty uses .ppk file instead of .pem file. You need to generate .ppk file from .pem file. Follow below steps to generate .ppk file.

A) Open PuTTYgen (<yourDirWhereYourDownloadedPutty>\putty\PUTTYGEN.EXE)


B) Choose Load on the right side
C) Set the file type to *.*
D) Browse and open your .pem file
E) PuTTY will auto-detect everything it needs, and you just need to choose Save private key and you can save your .ppk file.

Accessing Amazon EC2 Instance using Putty
1.  Open putty (<yourDirWhereYourDownloadedPutty>\putty\PUTTY.EXE)
2.  Fill the IP of EC2 box. If you are not sure about EC2 IP, refer step#11 from this post to locate the EC2 IP.

 3. Now you need to mention Auth file. Here you need to provide .ppk file that you just generated in prerequisite section above.

4. Once you choose Open, a dialog box is opened. Just select YES to connect to the server. When command prompt comes with login as, type user name as ec2-user  and enter. Now you are able to access the Linux machine you launched as EC2 on AWS. 



AWS - Launching an Amazon EC2 Instance

This post describes about how to launch a new Linux EC2 instance on AWS.

About AWS EC2 Instance 
EC2 stands for Elastic Compute Cloud. This is a virtual server/machine in Amazon's Elastic Compute Cloud where you can run applications on the Amazon Web Services (AWS) infrastructure.

How to Launch an Amazon EC2 Instance
1. First open AWS Management Console and login with your AWS account credentials.
2. Now under Services select EC2



3.
Now click on Launch Instance


4. Now choose the AMI (Amazon Machine Image) you would like to launch. In this post we would launch SUSE Linux machine. In general AMI is like templates for instance. To know more about AMI please refer this link.


5. Now you have to choose an instance type. If you are just learning, you can choose free tier eligible (marked as green below), so that you are not charged for this. After choosing instance type, choose Next: Configure Instance Details.

To know more about instance types please refer this link. In general, instance types are different combinations of CPU, memory, storage, and networking capacity. Based on your application requirement, you can choose one of them.


6. Now in this step, you need to configure instance details. Before that you must learn about what is VPC and Subnet.  Once you have your VPC and Subnet created, you can choose accordingly. Now choose Review and Launch.


Sunday, 28 January 2018

How To + SpringBoot + Embedded Tomcat + JNDI + URL + File Resources

If you are building your application in SpringBoot with Embedded Tomcat and looking for reading property files using JNDI, this post may help you.

Let say you have an environment property file which you want to keep out of your web package  and it should be referred/read in your application using JNDI URL resource. You can follow below steps to achieve same.

1. Create the property file which you want to read using JNDI URL resource.

EnvPropFile
mykey=myvalue

2. Create URLFactory class which should implement ObjectFactory interface.

URLFactory.java
package com.xxx.example;

import java.net.URL;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;

public class URLFactory implements ObjectFactory {
    public Object getObjectInstance(Object obj, Name name,
       Context nameCtx, Hashtable environment) throws Exception {
       Reference ref = (Reference) obj;
       String urlString = (String) ref.get("file").getContent();
       return new URL(urlString);
     }
}


3. Add below code in your spring boot application class that should initialize TomcatEmbeddedServletContainerFactory bean. Here in this bean, under postProcessContext() method, you can define JNDI file resources as mentioned below.  Factory class should be same that you created in step #2.

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
            
  return new TomcatEmbeddedServletContainerFactory() {

     @Override
     protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                             Tomcat tomcat) {
         tomcat.enableNaming();
         return super.getTomcatEmbeddedServletContainer(tomcat);
     }

     @Override
     protected void postProcessContext(Context context) {
       ContextResource fileRes = new ContextResource();
       fileRes.setName("url/EnvPropFile");
       fileRes.setType(URL.class.getName());
       fileRes.setProperty("factory","com.xxx.example.URLFactory.java");
       fileRes.setProperty("file","file:///C:/your-path/EnvPropFile");
       context.getNamingResources().addResource(evolutionEngineConfigResource);                                         
      }
   };
}

4. Define the URL bean which should return URL using resource name lookup. Keep 'java:comp/env' as it is and add your resource name as mentioned above.


import java.net.MalformedURLException;
import java.net.URL;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Configuration
public class ConfigFileConfiguration {

 @Bean(name="envPropFile", destroyMethod = "")
  public URL envPropFile() throws MalformedURLException, NamingException{
                              InitialContext context = new InitialContext();
                              return (URL) context.lookup("java:comp/env/url/EnvPropFile");
   }
}


5. Now autowire this URL bean in other spring component where you want to access the properties. Note that, in actual production code, file initialization should happen only once. 

@Component
public class MyService {
  
    @Autowired
    private URL envPropFile;

    public   void pringEnvPropFileKey() throws Exception{
    
      Properties configHolder = new Properties();
      
      final URLConnection conn = envPropFile.openConnection();
      final InputStream inputStream = conn.getInputStream();
      configHolder.load(inputStream);
   
      String value = configHolder.getProperty("mykey"); // This is the key mentioned in prop file
   
      System.out.println(value);

   }
}

I think that's pretty much what you need to do to read file using JNDI in spring boot embedded tomcat application.To get more detail specific to tomcat and it's JNDI resources you can refer this link.

Tuesday, 26 December 2017

TechTip - SVN to GIT Migration - Renaming/Refactoring Projects

During SVN to GIT Migration, I have faced one challenge that is renaming/restructuring projects while migration.

Let say, you have following maven project in SVN

old-project
-- src
-- pom.xml

Now you want this project to be renamed while migrating to GIT and it should look like below:

new-project
-- src
-- pom.xml

If you try changing this name while cloning projects from SVN to GIT just like below. You will loose history after migration. But you must think about solution where history remain as it is but your project can be renamed or restructured.

git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --prefix=origin/ --authors-file= svn-committer.txt  svn://xyz.com/SRC/Trunk/old-project new-project

How to do it? 

Actually, there is no rocket science I have applied here. Its pretty simple. You just need to refactor/rename your projects in SVN itself. The good part is, SVN still maintains full history even after renaming the folder names while copying from one place to another. This is really one of the great features in SVN that I will miss in GIT for sure.

First copy your project in SVN itself with required name using below command

svn copy <source-svn-url>   <destination-svn-url> -m "Renamed project name"


Example:

svn copy svn://xyz.com/SRC/Trunk/old-project  svn://xyz.com/SRC/GIT-SRC/new-project -m "Renamed project name from old-project to new-project"

Do whatever refactoring you want to do with your project in SVN itself. Now once you are ready with structure of your  project which must be there in GIT as repository, you can use your refactored location to clone project.

git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --prefix=origin/ --authors-file= svn-committer.txt  svn://xyz.com/SRC/GIT-SRC/new-project new-project

After migration in GIT, you can see full change history of old-project including message 'Renamed project name from old-project to new-project' that you just did while renaming the project in SVN itself from old to new location.

Renaming/Refactoring was a big challenge but with this simple approach I could successfully renamed/refactored projects while SVN to GIT migration.

If you are looking for post about how to migrate from SVN to GIT. You can refer my another post.


TechTip - How to embed html report within the Jenkins email notifications?

If you would like to send html report through Jenkins email notifications and looking for how to embed html report within the Jenkins email notifications, you can follow below steps:

Prerequisite

Ensure that your Jenkins has email-ext-plugin installed. If not, refer this and install it first.

Steps

Follow below steps to embed html file in email content:
  1. Go to your jenkins job and click on Configure.
  2. Go to Post Build Actions and then select Editable Email Notifications
  3. Select Content Type as HTML(text/plain)
  4. In the default content section replace $DEFAULT_CONTENT with following
           ${FILE,path=”relative path to html file”}

Example

For example, below is the absolute path for your html file. So you just need to pick bold marked as relative path and mention in path.

/app/jenkins/jobs/myjob/workspace/my-maven-module/target/site/surefire-report.html


${FILE,path="my-maven-module/target/site/surefire-report.html”}


This will place the surefire-report.html content in your email body.


Hope this tip is helpful for you.




Monday, 18 December 2017

SVN to GIT Migration

In this post, I will be describing about how to migrate projects from SVN to GIT. You can follow below steps to migrate your projects into GIT from SVN.

Prerequisites 
You need following softwars to be available on your local machine before proceeding further:
  • Java runtime, version 6 or newer
  • Git, with git-svn included, version 1.7.7.5 or newer
  • Subversion, version 1.6.17 or newer.
Instructions 
Step-1: First download svn-migration-scripts.jar from Atlassian’s bitbucket account.

Step-2: You need to verify required software on your local machine. Execute below command to do the same:

java -jar svn-migration-scripts.jar verify

If you are moving your projects from SVN to GIT, you must want to maintain SVN history in GIT too. But it must be possible where committer names in SVN and GIT are different. For this you need to map committer's names with GIT committer email.

Step-3: Extract SVN committer’s names and store in text file. Execute below command to do same: 
java -jar svn-migration-scripts.jar authors <SVN-URL> > svn-committer.txt 
Step-4: Map every SVN committer’s information with their respective mail id's on GIT. 
For example, while extracting we got below file:

NarendraV = NarendraV < NarendraV@mycompany.com>
AbcX = AbcX <AbcX@mycompany.com>


Now change this file like below. Here, we need to ensure that given email id is configured in GIT. 

NarendraV = NarendraV <NarendraV@abc.com>
AbcX = AbcX AbcX@abc.com

Step-5: Execute below command for converting SVN project to GIT in local:

git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --prefix=origin/ --authors-file= svn-committer.txt <svn-repository-url> <git-repository-name>

Ensure to replace <svn-repository-url> with your SVN repository URL and <git-repository-name> with repository name in GIT.

Step-4: Now create remote repository using BitBucket and push your local SVN to GIT converted project (done in step#5). 

First add remote repository to your local:

git remote add <GIT-Repository>

Now push project to remote repository using below command:

git push <remote-name> <remote-branch>

Example: git push origin develop

If you would like to get more detail, you can refer this link.

Challenges Faced
Here I would like to list out some challenges that we faced during SVN to GIT migration.
  • SVN branch did not follow the branch/trunk/tag standard structure
  • Restructuring/Refactoring projects in GIT. Please refer this link to get detail. 
  • Empty sub-folder migration from SVN to GIT
Hope this post helps you in SVN to GIT migration.