Monday, November 23, 2009

Change Proxy Settings on linux using shell commands

You can change the proxy settings for current session by executing following command. Which changes the environment variable "http_proxy". If you want, you can do it for ftp as well (ftp_proxy).
export http_proxy="http://NewProxy_address:Port"

Assume my proxy run on 192.168.0.1 machine on port 808, then this settings will be
export http_proxy="http://192.168.0.1:808"

Personal Experience


Recently, I installed Ubuntu server 8.04 and while the installation I set proxy settings wrong manner. In that case, installation guide give the format of "http://[[[user]:[passowrd]@serverIP]:[port]]" to enter proxy settings. Since I was using my proxy server in a windows machine, I had problem of giving username@server:port format. I gave it http://janaka@192.168.0.1:808, but it is only necessary to give http://192.168.0.1:808. So I needed to permanently change it. If you only used above command, it will not be there when you reboot the linux os. Because it is just changing the current environmental variable "http_proxy".
Using following changes you could permanently change the settings.
If you wanted to add it for any user add export http_proxy="http://192.168.0.1:808" on ~/.bashrc file.
 vi ~/.bashrc

If you want to do it for current user, add export http_proxy="http://192.168.0.1:808" on /etc/rc.local.
vi /etc/rc.local
The File will look like below.
And then change the execution bit to execute this script while loading.
chmod u+x /etc/rc.local

Now it should be working.

Sunday, November 22, 2009

Log4j - Logging package

Log4j is a package which supports to do logging very easily. This logging outputs can be taken in many ways.

There are many more types can be used,
AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender,
NullAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender,
SyslogAppender, TelnetAppender, DailyRollingFileAppender, RollingFileAppender.
To begin with log4j, you should first download the log4j package. Here is a sample link to download it.

Log Levels

There are some log levels defined in log4j.
  1. all - All levels including custom levels
  2. trace (since log4j 1.2.12) - Developing only, can be used to follow the program execution.
  3. debug - Developing only, for debugging purpose
  4. info - Production optionally, Course grained (rarely written information), I use it to print that a configuration is initialized, a long running import job is starting and ending.
  5. warn - Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad logging attempts, unexpected data during import jobs
  6. error - Production, application error/exception but
    application can continue. Part of the application is probably not working
  7. fatal - Production, fatal application error/exception, application cannot continue, for example database is down.
  8. no - Do not log at all.

A simple example

First you better add downloaded jar package to the project using your IDE. Then you have to setup the log4j properties through property file or, xml file.... In this case, let's check with log4j.properties file. In your IDE, as other property files add your log4j properties file. For example create new file called "log4j.properties" It should be look like this.
log4j.rootLogger=debug, file
log4j.appender.file=org.apache.log4j.RollingFileAppender #logging Type
log4j.appender.file.maxFileSize=100KB #Max file size of a log file
log4j.appender.file.maxBackupIndex=5 #keeping backups of
log4j.appender.file.File=C://myprogram_logs/test.log #Location which saving log file
log4j.appender.file.threshold=debug #Log Level
log4j.appender.file.layout=org.apache.log4j.PatternLayout #Layout of the log file
log4j.appender.file.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n #Layout of the log file
I am using RollingFileAppender here. Max file size is 100KB, kind of all the properties related to Log4j is stated here.
At your java file, load the property file and put it into the PropertyConfigurator on log4j package. Example code snipped here,
Improte packages

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import java.util.Properties;
On the relevant class, define the Logger type global variable(in my case "logger").
private static Logger logger = Logger.getLogger(MyProgram.class);
On main method,
   public static void main(String[] args){
Properties properties = new Properties();
InputStream inputStream = MyProgram.class.getClassLoader().getResourceAsStream("log4j.properties");
properties.load(inputStream);
PropertyConfigurator.configure(properties);
logger.info("My program Begins");
}
When you catch an exception, write it on log as follows
     try {
//Your code here
} catch (Exception e) {
logger.error("Found the exception here");
}

Now you will find logs on C://myprogram_logs/test.log location file.
When you need to use it in ubuntu, you just have to change the file location only from the properties file (i.e. /var/myprogram_logs/test.log).
See how easy logging you programming status with Log4j........
Here is a nice pdf to follow.
Cheers......

Tuesday, November 17, 2009

"The Twilight Saga: New Moon" for Sri Lankans...


Hay sri lankan Twilight fans, you all will be able to watch Twilight at MC soon...............
Yaaahooooooooooooo..........
You can buy tickets from TicketsLK.
Frnz itz movie time.....


Sunday, November 15, 2009

Windows TortoiseGit client for a linux Gitosis server

Tortoise git is really nice Graphical tool to handle git repository. You need to download TortoiseGit from here. First of all as we discussed earlier you need to install msysgit first. Then install the Tortoisegit and install it. While installing it Select open ssh client option for authentication method.
And completely install it and restart your computer. First of all you should add the msysgit path for this system. Tortoisegit gets the git options from msysgit. Right click on your desktop and go to settings,Set the path to the bin of the msysgit. Now setting your authentication on the server should be set. For that TortoiseGit has shipped putty Keygen for generate keys. But in this case, since we are connecting with a linux server the private keys which generate by putty key gen won't be recognize by the server. Since that you will not recognized correctly at the server end. To avoid this inconvenience you can create ssh keys through ssh-keygen. On your command prompt.

Do not use puttyGen in this case to create key pairs.

Go to your account on the windows os(i.e. C:\Documents and Settings\Janaka), check whether there is a folder called ".ssh" if not, create it using command prompts "mkdir .ssh" command. This is the place we keep your key pairs. keys you generated (id_rsa.pub, id_rsa) copy to this folder. Now follow the usual users adding mechanism on git server.Click here for more info.
Now you can go to any location on your computer and right click select option git clone... or git zync you will receive following screen. Put the remote URL as to your git repository. git@192.168.4.90:my_first_repo It will import the repository to your local folder. Like wise all the operations can be done through GUI. You don't have to type git commands on doing this.
Any comments........???

Creating gitosis repository and adding users to projects

Cont from, Previous post

Adding new repo(gitosis.conf)


Administrator is the only authenticated user for this (In according to previous posts in my case it's janaka). First administrator can log to the server which runs git.First it is better to add the repository on the conf file. And you can add users for this repository.
janaka@versioncontrol~/gitosis-admin $ vi gitosis.conf

Your conf file look like this,
[gitosis]                  

[group gitosis-admin]
writable = gitosis-admin
members = janaka@versioncontrol

Edit it and add new repo, Now it will looks like,
[gitosis]                  

[group gitosis-admin]
writable = gitosis-admin
members = janaka@versioncontrol

[repo my_first_repo]
gitweb = no
description = This could be my first repo
owner = No one else thats me,
daemon = no

Creating a user group


Your conf file look like this,
[gitosis]                  

[group gitosis-admin]
writable = gitosis-admin
members = janaka@versioncontrol

[repo my_first_repo]
gitweb = no
description = This could be my first repo
owner = No one else thats me,
daemon = no

Edit this file and add new group,

[gitosis]                  

[group gitosis-admin]
writable = gitosis-admin
members = janaka@versioncontrol

[group my_users]
writable = my_first_repo
members = janaka@versioncontrol gituser1

[repo my_first_repo]
gitweb = no
description = This could be my first repo
owner = No one else thats me,
daemon = no

gituser1 is the newly added user, his key is named as gituser1.pub on keydir/ directory.
Now you should add this changed gitosis.conf file and update gitosis-admin repository. You can do it by executing following commands.
janaka@versioncontrol~/gitosis-admin$ git add gitosis.conf
janaka@versioncontrol~/gitosis-admin$ git commit -m "Added new repo on conf file"
janaka@versioncontrol~/gitosis-admin$ git push

Then you can create the directory you want and create physical repository.

Creating new repository


Create a directory with naming it on your wish.
janaka@versioncontrol~$ mkdir my_first_repo
Now go into it, Don't forget to add some files before initialize this repo. Otherwise it will not be added as a repository,
janaka@versioncontrol/my_first_repo~$ touch readme
then follow the given commands to initialize the repository,
cd my_first_repo
git init
git remote add origin git@192.168.4.90:my_first_repo.git
git add .
git commit -m "Hai buddies, we added my_first_repo"
git push origin master:refs/heads/master
192.168.4.90-is the server which runs git on it. Now the new repository is created.
This is it, now gituser1 can use the my_first_repo repository. You can check on client machine,
git clone git@192.168.4.90:my_first_repo

On windows client side, If you'r only using command prompt, why don't you go for TortoiseGit to make life easier. TortoiseGit is a graphical tool to access git repository. Follow

"Graphical controls for Git windows clients"

link and study it.

Saturday, November 14, 2009

(Tortoisegit + Gitosis + msysgit) configuring - Adding windows users and creating repo

Cont. from previous post
Now you are ready to add users to the server.
It is not that much hard to add ubuntu users.
You just have to install git-core and by adding keys to the key directory. You can add users. If you are a windows user you have to do some installations. There are few ways to access repositories as windows user.

Clients using who use command prompt


First you should download msysgit from here and run the exe. Assume you downloaded the full version.

Give the path as your wish.
To run git commands at anywhere from your command prompt, define a path variable on environment variables Right Click on My Computer => Properties => Advanced => Environemnt Variables => Edit or New give the variable name as "PATH", and variable value "C:\msysgit\msysgit\bin", the bin folder of the msysgit installed. And add the "C:\msysgit\msysgit\mingw\bin" path also, that means, variable value will looks like, "C:\msysgit\msysgit\bin;C:\msysgit\msysgit\mingw\bin"

Then go to start => run => cmd type git and press enter. You will get result like,

Now you are ready to use git.

Adding user


First of all you have to create a key pair for your self. Get the command prompt and navigate to the msysgit folder to create .ssh directory.
C:\msysgit\msysgit\> mkdir .ssh

You are not allowed to create folders beginning with "." by right click => New => folder. So that it should be created this way.

Now navigate into the .ssh folder. We are going to create key pair.
C:\msysgit\msysgit\.ssh> ssh-keygen 
give id_rsa for the key name.
Following result will be shown.
C:\msysgit\msysgit\.ssh>ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (//.ssh/id_rsa): id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
92:0c:83:16:16:14:04:51:30:42:da:5d:df:fd:fb:15 Janaka@XPWINDOWS7

Now you need to transfer the public key to the server where your gitosis administrator belongs. With previous posts, you might remember my username at version control is janaka. Following command will carry your key to that server's keydir directory.On my command prompt,
C:\msysgit\msysgit\.ssh>scp id_rsa.pub janaka@192.168.4.90:gitosis-admin/keydir/gituser1.pub
(Assume 192.168.4.90 is the server that installed gitosis) using this command or anyhow you can copy your key to keydir/ directory.
Don't change the names of the public and private keys of the(id_rsa,id_rsa.pub) on your local machine. Keep it as it is on .ssh folder.
Now the administrators part,
Admin should allow this key to copy to server and rename it. When renaming, keep remember to give the same name that you are planing to add in .conf file. If new user is gituser1, then public key should be renamed to gituser1.pub on server.
On the server, admin should enter the this user into a user list of gitosis.conf file. You can see how to create new repository and work groups in this link.
On gitosis.conf,
[group myteam]
members = gituser1
writable = my_current_project
member should be the name of the added public key(without .pub) and writable is the repository which are writable by this team. Group name is myteam.
Now administrator should add these key file and changed gitosis.conf file and push it to git user. Actually administrator just cloning the gitosis-admin part from the "git" user and after changes pushing it back. Here are the commands for admin,
janaka@versioncontrol~$ git clone git@192.168.4.90:gitosis-admin
janaka@versioncontrol~$ cd gitosis-admin
janaka@versioncontrol~/gitosis-admin $ git add keydir/gituser1.pub
janaka@versioncontrol~/gitosis-admin $ git add gitosis.conf
janaka@versioncontrol~/gitosis-admin $ git commit -a -m "Added gituser1 as a new user"
janaka@versioncontrol~/gitosis-admin $ git push
Now on the client side (gituser1) can check whether he had successfully added. On command prompt he can try to add a clone.
 
C:\MyFolder> git clone git@192.168.4.90:my_current_project

If it imported my_current_project folder without asking any password for git user it is successfully worked. Issued can be occured,
  • Ask a password for git user- your keys are not matching recreate the key pairs and install keys accurately.

  • Giving no permissions error- You have not been added successfully for that group on gitosis.conf file. Reedit the conf file.

Widows clients using Gitosis

You can find post regarding this from here

Creating new repository and adding permission groups

will be discussed here

Setting up Gitosis(version control system) on Linux server and adding windows clients

Git is a kind of a version control system which allows users to keep their project information safely in repositories. In this case, I will be using Gitosis for this setup, Gitosis is a tool written for Git that helps in the setup of secure access controlled repositories. It will manage multiple repositories under one user account on the host machine. Using SSH keys to identify users, your repositories will be securely accessed and controlled. One large advantage to Gitosis is that your users will not need shell accounts on the machine to access repositories, they will however speak to a singular account what will not allow arbitrary commands. Now let's see how gitosis can install in to your Ubuntu server.

Installing Gitosis


First of all you should have installed Git.
sudo apt-get install git-core

By cloning the gitosis directory from "eagain.net" you will get the gitosis directory on your home page. Follow these lines to do it,
   cd ~/
git clone git://eagain.net/gitosis.git

Now you better move that folder to /usr/local
   sudo mv gitosis/ /usr/local

Now go to Gitosis folder.
   cd /usr/local/gitosis/

If you haven't installed python in your computer get it installed.
   sudo apt-get install python-setuptools

Then, you can run the setup.py file which is in this directory.
   sudo python setup.py install

Now you are successfully installed Gitosis on your Linux server.
Next, you have to create user called "git". "git" is the repositories holding real user. It creates in a way that users to identify through ssh-keys. Anyone can't login through entering password.
 sudo adduser \
--system \
--shell /bin/sh \
--gecos 'git version control' \
--group \
--disabled-password \
--home /home/git \
git

gitosis-admin


Now let's initialize the gitosis and begin with adding an administrator to git repository. As a convention we used to create administrator account on the same server/computer. Because administrator has the super power of creating new repository, users and so on.
To identify administrator, you need to create create ssh key and initialize gitosis with that key. So you will add as the administrator.(janaka is the user, versioncontrol is the name of the linux server)
janaka@versioncontrol~$ ssh-keygen -t rsa

Keep blank for the parse phrase of the key. Found this generated key pair at /home/janaka/.ssh/ as id_rsa(private key) and id_rsa.pub(public key).
Since janaka@versioncontrol wants to the admin and versioncontrol is the git server itself. You can directly initialize with this key.
sudo -H -u git gitosis-init < /home/janaka/.ssh/id_rsa.pub 

If your admin wants to be another user, generate this kind of key pair and copy it some where at versioncontrol(i.e. /tmp/id_rsa.pub). In this case,
sudo -H -u git gitosis-init < /tmp/id_rsa.pub 

If you are success then it will give,
Initialized empty Git repository in ./
Initialized empty Git repository in ./

Congratz, Now you are the gitosis-admin....!!!!
Now you need to make executable the post-update hook. Run following command.
sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

Its ready to take your admin clone..............
git clone git@YOUR_SERVER_HOSTNAME:gitosis-admin.git

In my case YOUR_SERVER_NAME is "localhost". If you (admin) from another host, give the ip of the linux server/machine(versioncontrol).
Now have go to cloned gitosis-admin directory.
janaka@versioncontrol~$ cd gitosis-admin 

Have a look at the list,
You will found "gitosis.conf" file and "keydir" directory.
Have a look at the gitosis.conf file,
vi gitosis.conf

 [gitosis]                  

[group gitosis-admin]
writable = gitosis-admin
members = janaka@versioncontrol

Now lets see how to add users and creating new repositories.
Since this post getting longer, Follow Next post. click here

Adding Windows users to the gitosis and Many more