System « Intelligrape Groovy & Grails Blogs

Archive for the ‘ System ’ Category

Using git diff feature on Github

Posted by Abhishek Tejpaul on June 7th, 2011

Hi Folks,

Recently I came across a cool way to compare differences between two branches, two tags, or between two commits on Github. Many a times in our project we have to thoroughly see what has been a specific change in the code base before we push it on our production branch.

Here is how you can view the differences in commits:

On the Github, go to the Source view of your project. You will see a link named ‘Branch List’. Once the page opens you can see a list of all the remote branches. Hit on the Compare button in front of any of the available branches to see the difference between two branches.

Now note the URL in the address bar. It should end with something like ‘…/compare/<x>…<y>’ where x and y are separated by three dots(…) and their values could be project’s branch names

Isn’t it good?

Well, Git’s (read about git diff) and Github’s goodness does not stop just here. Instead of branch names as the values of x and y, you can also put two different commit hashes or tag names to view the differences in the code-base. More so, the commit hashes do not have to belong to the same branch. So you can pretty much compare your code’s current snap-shot with any of its past snap-shot irrespective of branch or a tag or a commit hash.

Hope this helps someone.

Abhishek Tejpaul
abhishek@intelligrape.com
[IntelliGrape Software Pvt. Ltd.]

  • Share/Bookmark
Posted in System

Mounting an EBS Volume to an Instance and Soft Linking a Growing Directory to it

Posted by Vivek Krishna on May 5th, 2011

We were having a crisis on our project the other day. The VPS on which we were running our application had some issues with kernel and Tomcat, for that matter, any java process was running unpredictably. Tomcat would explode the WAR file once in a while and even if it did, it would just pause at “Deploying app.war” forever. After spending some time troubleshooting (we didn’t know that it was an issue with the kernel), we decided to move our infrastructure to Amazon EC2. We picked up an EBS based large instance, running Ubuntu and copied the setup from our production server to this new machine. The setup was a breeze, thanks to the scripts we already had in place for migrating our production setup to our QA machines for testing purposes.

However, we realized that Image Magick, which was being used from our application was not working as expected. In fact, it wasn’t working at all. We noticed that we were able to perform it as root, but not as any other user. The error which showed up was that there was lack of disk space. Static documents generated by our application were stored on the file system and it was meant to be an ever expanding directory. The EBS instance we had chosen had a capacity of just 8GB and about 6 GB of that was being occupied by the OS and the rest of our infrastructure. We decided to move this particular directory to a new EBS volume.

This could be accomplished smoothly, thanks to the ease of attaching a new EBS volume to an instance. We added a new volume and then, performed the following steps to move this particular expanding directory.


mke2fs -F -j /dev/sdh #This is to create an ext3 file system for the device attached at /dev/sdh

mkdir /path_to_new_file_system

mount /dev/sdh /path_to_new_file_system #mount the new file system at this directory

cp -r static_documents_location /path_to_new_file_system #Copy the files from the existing directory to the new directory

mv static_documents_location static_documents_location_backup #Backup the existing documents, just in case anything goes wrong

ln -s /path_to_new_file_system/static_documents_location static_documents_location #Create a soft link to refer to the new file location with the same name as the previous one

With these steps, we were good to go on the Amazon machine without even requiring a Server restart. We were expecting a downtime of at least 10 minutes but this happened so flawlessly.

Hope this helps someone.

  • Share/Bookmark
Posted in Database, Linux, System

Generating EAN-8 standard barcode using Barcode4J

Posted by Vishal Sahu on April 14th, 2011

Hi,

In one of my project i needed to generate EAN-8 standard bar-code for identifying the various products. I searched for various libraries available for generating bar-code and found a library to do so. The library i used for generating the bar-code is Barcode4J.
You can download it from here

Barcode4J is a flexible generator for barcodes written in Java. It’s free, available under the Apache License, version 2.0.
The bar-code generated with the help of of this library uses eight digit number hence EAN-8.

To generate bar-code image from this library is quite simple. Just put the barcode4j.jar in the lib folder of the application.


Code to generate image is:-

public File generateBarcode(Long codeDigits){
try {
      //'codeDigits' is the code for which we want to generate bar-code image
     EAN8Bean bean = new EAN8Bean();
     final int dpi = 150;
     bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
     bean.setFontSize(2.0)
     bean.doQuietZone(true)
     File outputFile = new File(filepath) // existing file in the file system
     OutputStream out = new FileOutputStream(outputFile)

     // class to convert provided image into barcode image
     BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi,   BufferedImage.TYPE_BYTE_BINARY, false, 0)
     bean.generateBarcode(canvas, codeDigits)
     canvas.finish()
     return outputFile
}

So, here it takes a file from the file system and convert it into the required barcode image. Now, the user can simply display this image on the items.

In my case, i needed to generate a bar-code image for each product, so i create new file for every item, and stored it on the file system with unique name.

This works in my case.
Hope it helps


Cheers..!!!
Vishal Sahu
vishal@intelligrape.com

  • Share/Bookmark
Tags:

byobu: screen sessions in Linux

Posted by Salil on April 13th, 2011

This post is just to talk about Screen Sessions in Linux (esp. ubuntu) using command “byobu”.

 

What is Byobu?
Byobu is a Japanese term for decorative, multi-panel screens. As an open source project, Byobu is an elegant enhancement of plain GNU Screen.

 

Where can it be used?
You SSH to some remote machine and Run some commands (application, service, etc). Now you want to logout of ssh session. But want your screen turned-on, so that you just resume it, the next time you access the machine.

 

How to use it?
It’s as simple as 2 steps process.

Step 1: Enter command "byobu"
Step 2: Press F2 (to create a new screen)

here you go!

 

Now you are in a detachable screen session. Enter your commands. And once you done –

Press F6 (to detach the screen)

If you are already logged-into some remote machine (using ssh), now you can logout. And your screen will remain there.
Then, when you come back — you can resume your screen back by following commmand

$ screen -r

It’s good to read about other options (like, screen -x)

 

Your comments are always Welcome. Please post if you have any!

 

Cheers!
Salil Kalia
Salil [at] IntelliGrape [dot] com
Twitter LinkedIn

  • Share/Bookmark
Tags: , , ,
Posted in Linux, System

Handling Instance Based Security

Posted by Imran Mir on April 6th, 2011

In my current project, we were required to implement Instance Based Security. The idea was to find a clean solution separate from the main business logic of the application. We took a clue from the Spring Security Plugin to use the Annotations to do our job. All we wanted to do was to develop annotations for actions, which could help to decide what type of access verification needs to be done on a particular request, inside some Filter. So we proceeded like this :

We created an Abstract Class from which our User domain Class extended. All our Access Verifying functions would remain in this class. The class provides a single entry function (by the name evaluateExpression) to all other Access Verifying functions. This helped us to keep the Filter class clean. The Class looks like :

abstract class MyAppSecure {

  public Boolean evaluateExpression(String methodName, Map params = [:]) {
    this."$methodName"(params.id?.toLong())
  }

  // One of the many functions to be used to verify valid access
  public Boolean hasUserAccessById(Long id) {
    return id ? (this.id == id.toLong()) : true
  }

}

So our User domain class looks like:

class User extends MyAppSecure {

}

Now, the idea is to:

  1. Intercept a request in the filter.
  2. Get the annotation expression placed on top of the respective Controller Action. (The expression(s) would be name of the MyAppSecure method(s) used to verify the access)
  3. Call a corresponding method in MyAppSecure to verify access
  4. Take appropriate action corresponding to a result

So let us delve into the implementation:

First step would be to enable the annotations. To do this create an interface in src/java (or you can do it in src/groovy as well):

@Documented
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MySecured {
    String[] expressions();
}

Now we are good to go for using annotations. Let us assume an action edit inside AccountController:

class AccountController{

@MySecured(expressions = ["hasUserAccessToAccount"])
 def edit = {
    User user = params.id ? User.get(params.id) : user
    render(view: "edit", model: [user: user])
  }
}

Note: We can also give multiple expressions (to evaluate multiple access rules), seperated by a commas inside the list.

Now we need to implement a method by the name hasUserAccessToAccount in MyAppSecure:

abstract class MyAppSecure {

public Boolean evaluateExpression(String methodName, Map params = [:]) {
    this."$methodName"(params)
}

public Boolean hasUserAccessToAccount(Map params) {
// SAMPLE LOGIC.
    Integer count = Account.createCriteria().get {
      projections {
        count("id")
      }
      eq('id', params.id.toLong())
      eq('user', this)
    }
    return (count > 0)
  }
 }
}

We would also need some logic to get and parse annotations in MyAppSecure. We wrote something like this :

public Boolean hasAccess(Class controllerClazz, String actionName,  Map params) { def field = controllerClazz.declaredFields.find {it.toString().indexOf(controllerClazz.name + '.' + actionName) != -1}
 // get the annotation on a Controller action (account/edit in our case)
    def securedAnnotation = field.getAnnotation(AdlSecured)
    return (securedAnnotation ? this.evaluateEveryExpression (params, securedAnnotation)
  }

 // Evaluate every expression and combine the result using AND (or we can use OR as well)
  private Boolean evaluateEveryExpression(Map params, def securedAnnotation) {
    Boolean hasAccess = securedAnnotation.expressions().every {String securedExpression ->
      this.hasAccessForExpression(securedExpression, params)
    }
    return hasAccess
  }

  //Evaluate expression
  private Boolean hasAccessForExpression(String expressionToBeEvaluated, Map params) {
    return this.evaluateExpression(expressionToBeEvaluated, params)
  }

Now in you Filter you can write something like this:

mySecureFilter(controller: "*", action: "*") {
      before = {
          User user = someSecurityService.currentUser
          if (user) {
          // gets the controller class for a controllerName
            Class controllerClazz = getControllerClass(controllerName)

            if (!user.hasAccess(controllerClazz, actionName, params)) {
                redirect(controller: 'accessController', action: 'unauthorized')
                }
              }
              return false
            }
          }

That is all that we need to do. I hope you would find this useful. Any suggestions will be welcomed.

  • Share/Bookmark

GIT –pretty

Posted by Hitesh Bhatia on February 18th, 2011

git log is one of most useful commands to see information about commit , author , date and subject(comment)  while using GIT.This displays information in blocks and hardly 5 – 7 can be listed on normal screen size. But git provides –pretty option , so that we can format the output of git log;

Here are some ways to do that

git log --pretty=oneline

Displays just a single line log ,which includes a commit-hash and Subject(Comment).something like this

d1fc23c1edfd2d4ffe8ef5a873adff9321cde909 comment 3
a8560c814ece4e03d39164885f44e0c5cb749928 comment 2
db6195c635179b3556c41e2a974829c7e101ef51 comment 1

some other predefined options are raw,fuller,full,medium and short. Since Git is Cool we can also use some custom formats.Like

git log --pretty=format:"$h"

will give abbreviated hash of committs(Where %h defines abbreviated hash).And it would be something like

d1fc23c
a8560c8
db6195c

Which is not quite useful , to make it better lets first see other options

  • %H  – Commit hash
  • %s  – Subject
  • %h  – Abbreviated hash
  • %an – Author name
  • %ae – Author e-mail
  • %ad – Author date
  • %ar – Author date, relative



While using “format” with option “–pretty” we can also specify string inside quotes.Like

git log --pretty=format:"%h was committed by %an , %ar"

Would give something like

d1fc23c was committed by hiteshBhatia , 5 months ago a8560c8 was committed by hiteshBhatia , 5 months ago db6195c was committed by hiteshBhatia , 5 months ago

To make this more useful we can make slight changes to string passed to “format”

git log -3 --pretty=format:"%h ||  %an  ||   %s  (%ar)"

Which will give us commit hash , author name , subject  and relative date inside paranthesis.And -3 here is number of logs .So that his whole screen is not cluttered.This is the way I like to use it.

d1fc23c ||  hiteshBhatia  ||   comment 3  (5 months ago)
a8560c8 ||  hiteshBhatia  ||   comment 2  (5 months ago)
db6195c ||  hiteshBhatia  ||   comment 1  (5 months ago)



To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon

_________________________________
Hitesh Bhatia
Mail,LinkedIn,Facebook,Twitter
_________________________________
  • Share/Bookmark
Posted in System

GNOME GUI Integration w.r.t IDEA GUI Launcher

Posted by Kushal Likhi on February 13th, 2011

One of the important part of any Installer is integrating and defining launchers for GUI. This post will demonstrate adding entries in menu bar and panel, using scripts in context of creating a GUI launcher for IDEA IDE.

currently IDEA does not come with any GUI Launcher but this does not mean that we cant have one. How cool it would be if we had an icon on the top panel to launch IDEA in Terminal mode.

IDEA LAUNCHER IN PANEL ->


Similarly an entry is also created in menu: application->programming->Idea
Download: idea_desktop_Integration.tar.gz Check the installation guide below to see how to install

PROBLEM STATEMENT:
A program will be created to do IDEA GNOME GUI Integration, which launches IDEA in TERMINAL such that we can see whats happening in the background, that too with the ease of GUI one click quick launch. It requires following subtasks:

  1. Building Launch Environment for idea.sh: idea.sh is designed to launch in terminal with all PATH variables set for various SDK’s. if we try to launch it in a standalone terminal it will not work as environment is required. In this step we will generate the launch environment
  2. Adding Menu Bar Entry: in this we will add an entry to menu: application->programming->Idea using scripts, automated.
  3. Adding entry to panel: here we will add an entry for launcher in the quickLaunch panel on top of the screen.

Building Launch Environment
whenever terminal is launched it runs the .bashrc file which have the PATH variables to be set.
but we cant execute .bashrc in standalone terminals i.e terminals with no command point as GUI always launches applications in standalone terminals.
so the other option is:- extract all the PATH variables from .bashrc and create your own setup script for the terminal.
this can be achieved by simple commands:-

grep "export" ~/.bashrc |tee ~/.IDEALauncher

this will extract all exported variables and make a file containing all of them in the home folder with the name .IDEALauncher
and we can execute this script to set the path via command:

chmod 777 ~/.IDEALauncher
. ~/.IDEALauncher

Now it has set all the path variables needed for the launch.

the above mentioned task is stored in a file klaunch.sh and is the launcher start point. file is as follows:
Note: UI and echo’s have been removed here to make it more readable, in attached file lot other stuff is there.

# IDEA Launcher Script for gnome desktop integration.
# By Kushal Likhi

#GET settings of enviornment variables and create a shell script for those
grep "export" ~/.bashrc |tee ~/.IDEALauncher

#set enviornment variables
chmod 777 ~/.IDEALauncher
. ~/.IDEALauncher

#launch IDEA
`idea.sh`

Setup Process
adding menu bar entries and panel entries is a part of setup process and is as follows:

“Everything in LINUX is a file”

yes, and using the above saying all we have to do is: create/edit/add some files
FOR Menu Entry we have to create a file: /usr/share/applications/idea.desktop and update cache to be safe
the Script is as follows for the setup file:

#IDEA Desktop integration setup file
# (c) Kushal Likhi
#Ver 1.0

#clear screen for good clean looks
clear

#welcome message
echo "-------------------WELCOME----------------------"
echo ""
echo " SETUP Program for IDEA GNOME Integration"
echo " by: kushal likhi"
echo ""
echo "------------------------------------------------"

#read IDEA installation dir path
echo ""
read -p "Enter IDEA Bin Location, press enter to keep default(/opt/idea/bin/), note: end path with '/':" ideahome

if [ -z "$ideahome"]
then
	ideahome=/opt/idea/bin/
else
	echo "path set!"
fi

echo ""
echo "Path set to: "$ideahome 

#copy files
echo ""
echo "Generating launcher...."
sudo cp -fv klaunch.sh $ideahome
echo ""
echo "copying Icons...."
sudo cp -fv icon.png $ideahome
echo ""

#add menu entries
echo "checking for /usr/share/applications"

if [ -d "/usr/share/applications" ]
then
#found cache file
echo "found"
echo "Adding Entries...."

#NOTE: echo here is used to create files, NOT display on console, '>' creates a file, and '>>' appends to it.
sudo echo "[Desktop Entry]" > /usr/share/applications/idea.desktop
sudo echo "Type=Application" >> /usr/share/applications/idea.desktop
sudo echo "Encoding=UTF-8" >> /usr/share/applications/idea.desktop
#sudo echo "Version 1.0" >> /usr/share/applications/idea.desktop
sudo echo "Name=IDEA IDE" >> /usr/share/applications/idea.desktop
sudo echo "Comment=Launcher for IDEA by Kushal" >> /usr/share/applications/idea.desktop
sudo echo "Categories=Development;" >> /usr/share/applications/idea.desktop
sudo echo "Exec="$ideahome"klaunch.sh" >> /usr/share/applications/idea.desktop
sudo echo "Icon="$ideahome"icon.png" >> /usr/share/applications/idea.desktop
sudo echo "Terminal=true" >> /usr/share/applications/idea.desktop
echo ""
echo "updating /usr/share/applications/desktop.en_US.UTF8.cache"
sudo echo "[idea]" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Name=IDEA IDE" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Comment=Launcher for IDEA by Kushal" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Categories=Development;" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Exec="$ideahome"klaunch.sh" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Icon="$ideahome"icon.png" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Terminal=true" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Type=Application" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "" >> /usr/share/applications/desktop.en_US.UTF8.cache
echo ""
echo "adding pannel entry......"

#/usr/lib/gnome-panel/gnome-panel-add --launcher=/usr/share/applications/idea.desktop
chmod 777 panel

bash ./panel

echo ""
echo "Menu set..... Enjoy!!"
echo ""
echo "------> YOU WILL FIND ENTRY IN (Applications -> Programming) MENU"
echo ""
echo "NOTE: Run command ./panel to add panel entry explicitly. was unable to add panel in sudo mode"
else
# file not found
echo "ERROR! Does not exist! this setup works for GNOME only"
exit 1
fi

now here we have added menu entry.

Adding Panel Entry:
Ubuntu has made this task easy by providing us with a python script to manipulate panels.
so all we have to do is use that script.. :D
so the code is as follows for the panel file:-

echo "adding panel......"
/usr/lib/gnome-panel/gnome-panel-add --launcher=/usr/share/applications/idea.desktop --panel=top_panel_screen0
echo "panel entry added... "

you can also add to the bottom panel with the switch –panel=bottom_panel_screen0 , now the launch button will appear next to show desktop icon at the botton left corner.
we can also align them to left or right, all is documented in the script usage guide.

Installation Guide
installation is very simple, just follow the following steps:

  1. STEP 1: Download the files: idea_desktop_Integration.tar.gz
  2. STEP 2: Extract the files to a directory.
  3. STEP 3: go in the root of the extracted files and issue command
    sudo ./setup
    

    then when asked enter idea path if it is different from the mentioned, if its same as mentioned just press enter.

  4. STEP 4: issue command:
    ./panel
    

    this has to be issued seperate because the ubuntu script for panel does not work in sudo mode

  5. STEP 5: all set Enjoy!! now whenever you have to launch IDEA just click on the icon and it will launch gracefully

~~Kushal Likhi~~
http://www.intelligrape.com

  • Share/Bookmark
Posted in Linux, System

Converting date from one timezone to another in groovy

Posted by Vishal Sahu on February 9th, 2011

Hi,
In my recent grails project, i came across the situation where i needed to convert the date in given timezone to the date in another timezone. I searched a lot about it and got many solutions for this problem and then i came out with a simple way to do so.

Lets i have a date in TimeZone say oldTimeZone and i want to convert it to another timeZone say newTimeZone, so to convert it to another timezone, i wrote the method given below.


public Date convertToNewTimeZone(Date date, TimeZone oldTimeZone, TimeZone newTimeZone){

      long oldDateinMilliSeconds=date.time - oldtimeZone.rawOffset
      // oldtimeZone.rawOffset returns the difference(in milliSeconds) of time in that timezone with the time in GMT
      // date.time returns the milliseconds of the date

      Date dateInGMT=new Date(oldDateinMilliSeconds)

      long convertedDateInMilliSeconds = dateInGMT.time + newTimeZone.rawOffset
      Date convertedDate = new Date(convertedDateInMilliSeconds)

    return convertedDate
}

This Works for me.
Hope it helps.

Cheers..!!!
Vishal Sahu
vishal@intelligrape.com
www.intelligrape.com

  • Share/Bookmark

Git – Grep

Posted by Hitesh Bhatia on January 22nd, 2011

Still in my early days of using Git. Recently just to make sure that I have merged two branches  and have pushed changes to QA server successfully,I often used command “find | xargs grep” .But to deal with this kind of situations Git provides “grep” command. Just as name suggests this commands searches for regex pattern passed to it.

 git grep "regex" 

Example – To find usage of word “collections”  in each file.

 git grep --ignore-case "collections"

output :

ReadMe.txt:groovy Collections
ReadMe.txt:nice collections

Now this just described which of project files have word ‘list’ in it and how many times.One can also use -c option to count occurrences  of word “collections”

git grep --ignore-case -c "collections"

Output:

ReadMe.txt:2
Now a question  why I am not using command (and favoring “git grep”)
find | xargs grep -y "collections"
which will easily do the same for me and would perhaps list some extra  files (which are not exactly part of my app). Now here come the answer with “git grep” I can also search in previous versions of my project.
Example
git grep --ignore-case  "collections" ea145f1
output :
 ea145f1:ReadMe.txt:groovy Collections
It searches for word “collections” in my previous commits . (As second line in file ReadMe.txt was added after commit numbered ea145f1) .Simply awesome.And this is  not it.  To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon
_________________________________

Hitesh Bhatia

Mail,LinkedIn,Facebook,Twitter

_________________________________

  • Share/Bookmark
Tags: , ,
Posted in Linux, System

Fetching an old deleted file of a project using Git

Posted by Abhishek Tejpaul on November 15th, 2010

Recently in one of my projects, I had to bring the content of old-forgotten-deleted file back to the application code base using Git. The problem took a ‘fancy’ turn as the path to the file was no more a valid path in Git as there had been a major refactorings in our project esp. renaming of the packages as well as the folder structure since the file in question was deleted.

This is how I could see the deleted file (& its content) using the Git commands:

1. First of all, we will try to get the revision number of the git commit which committed our deleted file.
Type in, git log
on your console at the location where you have cloned/forked your Git project. This command will show a all the commits that has been done since the start of the project.
If you know at what period of time the deleted file was committed, you can use another flavor of git log command i.e.
git log –before=”Oct 01 2010″, or,
git log –after=”Oct 01 2010″
If you have an idea about who all could have or have committed the deleted file, then you can also use another option provided by git log command i.e
git log –author=”Author Name” or
git log –author=authorId
The commands above are self-explanatory.

NOTE: The step above would be helpful only if your team has followed good practices such as putting appropriate and self-explanatory comments while committing. So like if you have deleted a file & the comment while committing specifies that in some way or other it would be really easy for you to find out the probable revision number of that old commit by seeing the logs.

2. Now once we have the revision number, we can try using the

git show 

command to make sure that the deleted file was actually present in the commit. Type the following:

git show --name-only <revisionNumber> 

Once you see your sought-after file there, then you can see the contents of your file by typing

  git show <revisionNumber> 

This command will show you the change-set and the content of the file. Now you can fetch or copy the code out of the deleted file and place it in your new code or create an entirely new file out of it.

Hope this helps !!!

Reference: http://cheat.errtheblog.com/s/git

– Abhishek Tejpaul
abhishek@intelligrape.com
[IntelliGrape Software Pvt. Ltd.]

  • Share/Bookmark
Posted in System