Sunday, February 9, 2014

JBoss AS 7 / WildFly in Domain Mode

One of the best features which JBoss AS 7 / Wildfly came up was the domain mode, which is been used when you are looking for a single point of controller over all the servers, which can be running on the same/different boxes.

Most people think that domain mode has to be used when they want clustering which is not true, the main reason domain mode was created to control all the servers which can be in cluster/non-cluster from one point which was not available in the earlier versions of JBoss.

So in this article we would be showing you how can we start JBoss servers in a domain mode in two different boxes which are not in cluster. Once you are able to do it with box-2 you can add other host controller on on different boxes as shown in the below image.

Things to Remember

We need to keep in mind the following 4 things for successfully start JBoss AS 7 in domain mode.
Step 1: Add a name to your host controller in the “host” tag:
           <host name="host1" xmlns="urn:jboss:domain:1.2"> 

Step 2: Make sure you replace the “<local/>” tag from the “domain-controller” element in the “host.xml” file, when you are creating a host controller:
    <domain-controller>
              <remote host="${jboss.domain.master.address:<IP_ADDRESS-DOMAIN_CONTROLLER>}" port="${jboss.domain.master.port:<NATIVE_PORT-  

                                     DOMAIN_CONTROLLER>}" security-realm="ManagementRealm"/>
     </domain-controller>


Step 3: Create a “Management User” in the Domain controller, using the same host name (i.e. “host1″)

Step 4: Now convert that password (i.e password given for the host name in step-3 “host1″) into Base64 encoded password which has to be given in the host controller.
<server-identities>
    <!-- Replace this with either a base64 password of your own, or use a vault with a vault expression -->
    <secret value="aG9zdDFwYXNzd29yZA=="/>
</server-identities>

Box-1 (Domain Controller – 10.252.168.173)

bhmed-dt-1q.zone7.downingtown.pa.ula.comcast.net [10.252.168.173] - bhmed-dt-1q.cable.comcast.com
On Box-1 we would setup the domain controller, which would be using the “domain.xml” file and the “host.xml” file which defines that on this box we have the domain controller.

Step 1: From “/jboss-as-7.1.1.Final/bin” start the domain controller using the below command
./domain.sh -b 10.252.168.173  -bmanagement 10.252.168.173

Step 2: Now you would have to create users for “Management User”, using the same host names which you are planning to control (i.e. suppose its “host1″)
./add-user.sh
What type of user do you wish to add?
 a) Management User (mgmt-users.properties)
 b) Application User (application-users.properties)
(a): a
Enter the details of the new user to add.
Realm (ManagementRealm) :
Username : host1
Password : host1password
Re-enter Password : host1password
About to add user 'host1' for realm 'ManagementRealm'
Is this correct yes/no? yes
Added user 'host1' to file '/user/jboss-as-7.1.1.Final/standalone/configuration/mgmt-users.properties'
Added user 'host1' to file '/user/jboss-as-7.1.1.Final/domain/configuration/mgmt-users.properties'
 

Note: You should NOT use any special characters when you are giving a host name, because during creating a “Managment User” you would get the following error
* Error *
JBAS015239: Only alpha/numeric usernames accepted.
That’s it from domain controller side

Base64 Encrypt Password Generator

There are lot of websites available which would help you generate base64 value from clear text, however you can also use one of our code which we have created, which would do the same for you. We are using commons-codec.jar for this and below Base64EncryptPassword.java and Base64Password.sh files
import org.apache.commons.codec.binary.Base64;
public class Base64EncryptPassword {
     public static void main(String ar[]) throws Exception {
        byte[] encodedByte = org.apache.commons.codec.binary.Base64.encodeBase64(ar[0].getBytes());
        String encodedBase64String = new String(encodedByte);
        System.out.println("\n=========================================================");
        System.out.println("Your  Clear Text Password is :     "+ar[0]);
        System.out.println("Base64 Encrypted Password is :     "+encodedBase64String);
        System.out.println("=========================================================\n");
       }
  }


Base64Password.sh
#!/bin/sh
export JAVA_HOME=/user/jdk1.6.0_21   #<= change this path
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=commons-codec.jar:.
 

java Base64EncryptPassword $1

Note: Make sure that you keep all the files commons-codec.jar , Base64EncryptPassword.class and Base64Password.sh in the same folder when you run below command.
/Base64EncryptPassword]$ ./Base64Password.sh host1password
=========================================================
Your  Clear Text Password is :     host1password
Base64 Encrypted Password is :     aG9zdDFwYXNzd29yZA==
=========================================================

Box-2 (Host Controller – 10.252.168.174)

bhmed-dt-2q.zone7.downingtown.pa.ula.comcast.net [10.252.168.174] - bhmed-dt-2q.cable.comcast.com
On Box-2 we would setup the host controller, which would NOT be using its “domain.xml” file, but will use the “host.xml” which has the domain controllers details running on Box-1, this way domain controller would be able to controller its hosts.
Step 1: First we would need to give a name for the host controller in the “host” tag
<host name="host1" xmlns="urn:jboss:domain:1.2">

Step 2: As we need to communicate with the Domain controller we would have to make sure we replace the “<local/>” tag from the “domain-controller” element in the “host.xml” file, when you are creating a host controller.
<domain-controller>
    <remote host="${jboss.domain.master.address:
10.252.168.173}" port="${jboss.domain.master.port:9999}" security-realm="ManagementRealm"/>
</domain-controller>


Note: You should make sure that you have added and give the correct value for “security-realm” or else you would get the following error during starting the host controller.
[Host Controller] 23:09:43,584 ERROR [org.jboss.remoting.remote.connection] (Remoting "host1:MANAGEMENT" read-1) JBREM000200: Remote connection failed: javax.security.sasl.SaslException: Authentication failed: all available authentication mechanisms failed
[Host Controller] 23:09:43,593 ERROR [org.jboss.as.host.controller] (Controller Boot Thread) JBAS010901: Could not connect to master. Aborting. Error was: java.lang.IllegalStateException: JBAS010942: Unable to connect due to authentication failure.


Step 3: Now the password which was given during creating the “Management User” in domain controller (i.e password given for the host name in step-2 “host1password“) has to be converted into Base64 encoded password which has to be given in the host controller. By doing this domain controller would be able to know that an authorized host is trying to connect.
<server-identities>
    <!-- Replace this with either a base64 password of your own, or use a vault with a vault expression -->
    <secret value="aG9zdDFwYXNzd29yZA=="/>
</server-identities>


Step 4: All the required configuration has been done, now its time to start your host controller using the below command
./domain.sh -b 10.252.168.174 -bmanagement 10.252.168.174 -Djboss.domain.master.address=10.252.168.173 -Djboss.domain.master.port=9999

Once everything comes up you can see the below logging inside the domain controller console, which means that our configuration works properly as well you can also login into admin console which would be running on “http://10.252.168.173:9090/console” and you would see “host1″ servers in it.

[Host Controller] 22:19:17,250 INFO  [org.jboss.as.domain] (domain-mgmt-handler-thread - 1) JBAS010918: Registered remote slave host "host1", JBoss AS




Sunday, January 26, 2014

GLANCE @ GIT


Create a new repository

Create a new directory, open it and perform a "$ git init" to create a new git repository.
eg:
$ mkdir example.git
$ cd example.git
$ git init [--bare]

Checkout a repository

Create a working copy of a local repository by running the command
"$ git clone /path/to/repository
when using a remote server, your command will be
"$ git clone username@host:/path/to/repository".

Workflow

Your local repository consists of three "trees" maintained by git. the first one is your Working Directory which holds the actual files. the second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you've made.






Add & Commit 

You can propose changes (add it to the Index) using
"$ git add <filename>"
"$ git add *"
This is the first step in the basic git workflow. To actually commit these changes use
"$ git commit -a -m "Commit message"
Committing your changes "$ git commit"
Committing files from working directory skipping staging area " $ git commit -a"
Viewing commit history: "$ git log"

Now the file is committed to the HEAD, but not in your remote repository yet.

Pushing Changes

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute:
"$ git push origin master"
Change master to whatever branch you want to push your changes to.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
"$ git remote add origin <server>"
Now you are able to push your changes to the selected remote server

Forking

Forking and updating a repo:

  1. Fork this repository: This is a GitHub operation, in which you are making a copy of Joe's repository (including the files, commit history, issues, and more). This repository now lives in your GitHub account. Nothing has yet happened to your local computer.
  2. Clone your repo: This is a Git operation, in which you are using Git to tell GitHub "please send me a copy of my repo" The repo is now stored on your local computer.
  3. Update some files: You can now make updates to the files in whatever program or environment you like.
  4. Commit your changes: This is a Git operation, in which you are using Git to tell GitHub "here are my changes" Pushing does not happen automatically, so until you do this step, GitHub does not know about your commits.
  5. Push your changes to your github repo: This is a Git operation, in which you are using Git to tell GitHub "here are my changes" Pushing does not happen automatically, so until you do this step, GitHub does not know about your commits.
  6. Send a pull request to Joe: If you think that Joe might like to incorporate your changes, you send him a pull request. This is a GitHub operation, in which you are communicating your changes to Joe, and "requesting" that he "pull" form your repo. It is up to him whether he pulls from you or not.

Syncing a fork

Let's say that Joe and other contributors have made some more updates to the game, and you've thought of some more updates you'd like to make. Before you do anything else, it's best to "sync your fork" so that you are working on the latest copy of the files. Here's what you do:
  1. Fetch changes from Joe's repo: This is a Git operation, in which you are using Git to tell GitHub that you would like to retrieve the latest files from Joe's repo.
  2. Merge those changes into your repo: This is a Git operation, in which you are updating the repo on your local computer with those changes (which have been temporarily stored in a "branch"). Note: Steps 1 and 2 are often combined into a single Git operation called a "pull".
  3. Push the updates to your GitHub repo:(optional): Remember that your local computer does not automatically update your GitHub repo. Therefore, the only way to get your GitHub repo up-to-date is by pushing up the latest changes. You can either do this right away, or you can wait until you have made some updates of your own and committed them locally.
Take note of the contrast between the workflow for forking and the workflow for syncing: When you initially fork a repo, the flow of information is from Joe's repo to your repo, and then down to your local computer. But after that initial process, the flow of information is from Joe's repo to your local computer, and then up to your repo.

Branching

Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
create a new branch named "feature_x" and switch to it using
"$ git checkout -b feature_x"
switch back to master
"$ git checkout master"
and delete the branch again
"$ git branch -d feature_x"
a branch is not available to others unless you push the branch to your remote repository
"$ git push origin <branch>"
Checking Status of the files: "$ git status "

Update & Merge




To update your local repository to the newest commit, execute
[fetch+merge]"$ git pull"
[fetch + rebase] "$git pull --rebase"
in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
"$ git merge <branch>"
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After
changing, you need to mark them as merged with
"$ git add <filename>"
before merging changes, you can also preview them by using
"$ git diff <source_branch> <target_branch>"

Tagging

it's recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag named 1.0.0 by executing
"$ git tag 1.0.0 1b2e1d63ff"
the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag. You can get the commit id with
"$ git log"
you can also use fewer characters of the commit id, it just has to be unique.

Replace local changes

In case you did something wrong (which for sure never happens ;) you can replace local changes using the command
"$ git checkout -- <filename>"
this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this
"$ git fetch origin"
"$ git reset --hard origin/master"

Useful Hints

gitk: built-in git GUI
Use colorful git output: "$ git config color.ui true"
Show log on just one line per commit: "$ git config format.pretty oneline"
Use interactive adding: "git add -i"

Exceptions:

#1: fatal: This operation must be run in a work tree [duplicate]
Explanation: You repository is bare, i.e. it does not have a working tree attached to it. You can clone it locally to create a working tree for it, or you could use one of several other options to tell Git where the working tree is, e.g. the --work-tree option for single commands, the GIT_WORK_TREE environment variable, or the core.worktree configuration option.
"$ git config core.bare false"

Quick Reference:

Cloning a remote repo (that you created or forked on GitHub)

  • git clone < your-repo-URL >: copies your remote repo to your local machine (in a subdirectory with the repo's name), and automatically creates an "origin" handle
  • git remote add upstream < forked-repo-URL >: adds an "upstream" handle for the repo you forked
  • git remote -v: shows the handles for your remotes
  • git remote show < handlename >: inspect a remote in detail

Tracking, committing, and pushing your changes

  • git add < name >: if untracked, start tracking a file or directory; if tracked and modified, stage it for committing
  • git reset HEAD < name >: unstage a changed file
  • git commit -m "message": commits everything that has been staged with a message
    • -a -m "message": automatically stages any modified files, then commits
    • --amend -m "new message": fixes the message from the last commit
  • git push origin master: pushes your commits to the master branch of the origin

Syncing your local repo with the upstream repo

  • git fetch upstream [master]: fetch the upstream and store its master branch in "upstream/master"
  • git merge upstream/master: merge that branch into the working branch

Viewing the status of your files

  • git status: check which files have been modified and/or staged since the last commit
  • git diff: shows the diff for files that are modified but not staged
    • --staged: shows the diff for files that are staged but not committed

Viewing the commit history

  • git log: shows the detailed commit history
    • -1: only shows the last 1 commit
    • -p: shows the line diff for each commit
    • -p --word-diff: shows the word diff for each commit
    • --stat: shows stats instead of diff details
    • --name-status: shows a simpler version of stat
    • --oneline: just shows commit comments
  • gitk: open a visual commit browser

Managing branches

  • git branch: shows a list of local branches
    • < branchname >: create a new branch with that name
    • -d < branchname >: delete a branch
    • -v: show the last commit on each local branch
    • -a: show local and remote branches
    • -va: show the last commit on each local and remote branch
    • --merged: list which branches are already merged into the working branch (safe to delete)
    • --no-merged: list which branches are not merged into the working branch
  • git checkout < branchname >: switch the HEAD pointer to a different branch
    • -b < branchname >: create a new branch and switch to it

Removing, deleting, and reverting files

  • git rm < name >: deletes that file from the disk, then stages its deletion
    • --cached < name >: stops tracking a file, then stages its deletion (but does not delete it from the disk)
  • git mv < oldname > < newname >: renames the file on disk, then stages the deletion of the old name and addition of the new name
  • git checkout -- < name >: revert a modified file on disk back to the last committed version

Other basic commands

  • git init: initialize Git in an existing directory
  • git config --list: shows your Git configuration
  • touch .gitignore: create an empty .gitignore file

GIT Daily workflow



    Whole Idea is to keep your repository latest and pull others changes and resolve conflict if any:

    Note:
    1. $ git branch (is local branch on your machine)
    2. $ git fetch origin (Remote Branch to your reposioty)

    1. $ git fetch upstream (Remote Branch to company/project repository)


    1. Takes latest from Remote (upstream):
      1. $ git fetch upstream master
      2. $ git merge upstream/master
    2. Resolve conflicts:
      1. $ git add .
      2. $ git commit -a -m "your_message"
      3. $ git status
    3. Bring these changes to your remote repository (from your local).
      1. $ git push origin master # git push origin BRANCH_NAME

    Q. How to clone and remote repository?
    Check:           
    $ git branch -r # to list all remote branches only.

    $ git remote -v # check list of all remote branches


    Origin: $ git clone https://git.company.com/andixit/mkp.git # this is automatically set as origin.
    Upstream: $ git remote add upstream https://git.company.com/equinix/mkp.git # this will add upstream branch.

    Q. List all branch Local or remote?
    $ git branch -a # to check all branches local or remote.

    Q. How to remove / delete a repository?
    A. there could be three kind of delete / remove repository path in GIT:

    /apps/opt/projects/marketplace/services/report-data-service>git push origin -d MKP-5.3

    1. Remote:
    $ git push origin --delete <branch_name>
    $ git branch -d <branch_name>
    > git push origin --delete <branch>  # Git version 1.7.0 or newer
    > git push origin :<branch>          # Git versions older than 1.7.0

    2. Local:
    $ git branch --delete <branch>
    $ git branch -d <branch> # Shorter version
    $ git branch -D <branch> # Force delete un-merged branches

    3. Deleting a local remote-tracking branch:
    $ git branch --delete --remotes <remote>/<branch>
    $ git branch -dr <remote>/<branch> # Shorter

    $ git fetch <remote> --prune # Delete multiple obsolete tracking branches
    $ git fetch <remote> -p      # Shorter


    Q. How to checkout a specific revision?

    $ git clone https://git.company.com/anupdixit/mkp.git
    $ git checkout usermanagment

    # checkout a particular revision

    $ git checkout <sha1>
    $ git checkout 17a5d87d31aab43e9e6b8eb51bf1516b13ec78c6
    Q. How to switch to already existing branch?
    $ git checkout anup-dev # switched to anup-dev # use this command if branch already created.

    Q. How to create a new feature specific branch?
    $ git checkout –b anup-dev # execute this command to create a new feature specifc branch from current checkout branch.
    or use following 2 commnad equivalent to above command:
    1. $ git branch anup-dev
    2. $ git checkout anup-dev


    Q. Copy code from one local branch to another local branch?

    Go to the target branch (git checkout target) and merge the code-having branch (git merge code-having). then, if needed, push the updated target branch to its remote.
    eg. feature specific code is checked in to anup-dev, now i want to move it to user’s remote master.
    $ git checkout master # first go to target branch
    $ git merge anup-dev # merge the changes from branch that have code.


    Q. Copy code from one Remote branch to another Remote branch?

    A. $ git push origin anup-dev:master # push changes to master from anup-dev in origin

    Q. How to tag your release?
    1. Checkout A Tag:

      $ git checkout -b <NEW_BRANCH_NAME> <TAG_NAME>
      $ git checkout -b MKP_R3.10_UAT_RC5 MKP_R3.10_UAT_RC4
    2. Tag A Release:
      $ git tag -a <TAG_NAME> -m <TAG_MESSAGE>
      $ git push origin <TAG_NAME>

      Eg. $ git push origin MKP_R4.0_RC5_23052012
      $ git push origin --tags
    eg:
      $ git tag -a MKP_R3.10_UAT_RC5 -m "RC5"
      $ git push origin MKP_R3.10_UAT_RC5

    Q. How to undo a git commit?

    There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):

    1. Undo from local commit: Lets say I committed locally, but now want to remove that commit.

    $ git log

       commit 101: bad commit    # latest commit, this would be called 'HEAD'
       commit 100: good commit   # second to last commit, this is the one we want

    To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:
    $ git reset --soft HEAD^     # use --soft if you want to keep your changes
    $ git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made
    Now git log will show that our last commit has been removed.

    2. Undo a public commit: If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).
    $ git revert HEAD
    Your changes will now be reverted and ready for you to commit:
    $ git commit -m 'restoring the file I removed by accident
    $ git log
       commit 102: restoring the file I removed by accident
       commit 101: removing a file we dont need
       commit 100: adding a file that we need
    3. Undo last 5 commits: $ git reset --hard 5a7404742c85
    HEAD is now at 5a74047 Added one more page to catalogue
    $ git push origin master --force
    Total 0 (delta 0), reused 0 (delta 0)
    remote: bb/acl: neoneye is allowed. accepted payload.
    To git@github.org:thecompany/mkp.git
    + 09a6480...5a74047 master -> master (forced update)
    Q. Git Reset:
    $ git reset --hard # removes staged and working directory changes
    ## !! be very careful with these !! you may end up deleting what you don't want to read comments and manual. $ git clean -f -d # remove untracked $ git clean -f -x -d # CAUTION: as above but removes ignored files like config. $ git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo
    (without :/, the operation affects only the current directory)

    Q. How to find difference between different branch?
    A. $ git diff MKP-5.3 master services\report-data-service

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - -
    $ git log -5 —oneline # check last 5 changes to repository

    https://github.com
    Username:
    First you need to tell git your name, so that it can properly label the commits you make.
    $ git config --global user.name “your name here”
    $ Sets the default name for git to use when you commit.

    Email: Git saves your email into the commits you make. We use the email address to associate your commits with your github account.
    $ git config --global user.email “your_email@example.com”
    $ sets the default email for git to use when you commit.

    $ git clone <url_from_github>
    # give password when required

    # no need to run init because it already been initialized.
    $ git remote -v # check git remote location
    $ git remote add origin <url_from_github>

    $ git status # to check status of new and modified file
    $ git add . # add everything in current directory.

    $ git commit -m “Committing file”
    $ git status # to confirm nothing else to commit.
    $ git log # to check everything is committed

    $ git push origin master # push master to origin (remote) - give password when required.

    Sync Fork with upstream require 2 operation:
    1. Fetch:  from remote to local
    2. Merge: merge changes and differences in local with others changes.
    3. Push: once satisfied push changes to remote repo. 

    $ git remote -v
    $ git remote add upstream <github url>
    $git remote -v
    $ git fetch upstream
    - - if required we can switch upstream and view changes ---
    $ git merge upstream/master
    $ git push origin master # give password if required

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

    Source Tree- GIT Client:

    With recent update of Source Tree v 1.9.8 we start seeing problem where it fail to clone with message "warning: templates not found /usr/local/git/share/git-core/templates" on mac osX.

    Following is the solution for this problem:
    1. Got to your home directory. $ cd ~
    2. open .gitconfig in vi (or your comfortable editor).
    3. Add this at beginning of file:
      1. [init]
        templatedir = /Applications/SourceTree.app/Contents/Resources/git_local/share/git-core/templates
    This will resolve the issue, if required restart your Source Tree or machine.

    GIT Hub /or GIT Lab Setting up SSH Keys

    SSH key allows you to establish a secure connection between your computer and GitLab. Before generating an SSH key, check if your system already has one by running cat ~/.ssh/id_rsa.pub If your see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.

    To generate a new SSH key just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename you can press enter to use the default. It is a best practice to use a password for an SSH key but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can't be altered or retrieved.

    $ ssh-keygen -t rsa -C "email@gmail.com"

    Use the code below to show your public key.
    cat ~/.ssh/id_rsa.pub
    ------------------------
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDncSVBPs0hg5YRZ0NYJHueGpouluJPeKDpHO7gRsFSwUfkjGL5fKg4BCgxJZjdrHQ8G0e9jmt0oXYJpilde5olbrnx5NSEbP1hv18Mq8jTYEd91VH2HsrYJ2TNTgQ79JXA6m1v2UYTd15UwPkhzJEcpP29Cuh/MSX1URBA/aQ3wEfIc6LF45Kyx7St/kiBfPQwJfmcyRsIDv6ly6TQp8GT2nNzWVsFe6JigHN6XKf2YzFXK8oBGJZhv5vBNXYEr86rg7t34E8xYI/yBSGg1k2V+nymEfwhSRbmRyB+zdcvCVKVTSsgg3ySXj89EgDGULc7mfucNnkq0lThLVL1ObyJ email@gmail.com
    ------------------------

    Copy-paste the key to the 'My SSH Keys' section under the 'SSH' tab in your user profile. Please copy the complete key starting with ssh- and ending with your username and host.

    References:

    GIT SCM, About, GitRef, Interactive Git Reference, Git 30 mins Crash Course

    Sunday, January 12, 2014

    How To Manually Install Oracle Java on Linux

    Introduction

    Java is a programming technology originally developed by Sun Microsystems and later acquired by Oracle. Oracle Java is a proprietary implementation for Java that is free to download and use for commercial use, but not to redistribute, therefore it is not included in a officially maintained repository.
    There are many reasons why you would want to install Oracle Java over OpenJDK. In this tutorial, we will not discuss the differences between the above mentioned implementations.

    Assumptions

    This tutorial assumes that you have an account with DigitalOcean, as well as a Droplet running Debian 7 or Ubuntu 12.04 or above. You will need root privileges (via sudo) to complete the tutorial.
    You will need to know whether you are running a 32 bit or a 64 bit OS:
    $ uname -m
    • x86_64: 64 bit kernel
    • i686: 32 bit kernel

    Downloading Oracle Java JDK

    Using your web browser, go to the Oracle Java SE (Standard Edition) website and decide which version you want to install:
    • JDK: Java Development Kit. Includes a complete JRE plus tools for developing, debugging, and monitoring Java applications.
    • Server JRE: Java Runtime Environment. For deploying Java applications on servers. Includes tools for JVM monitoring and tools commonly required for server applications.
    In this tutorial we will be installing the JDK Java SE Development Kit 8 x64 bits. Accept the license and copy the download link into your clipboard. Remember to choose the right tar.gz (64 or 32 bits). Use wget to download the archive into your server:
        $ wget --header "Cookie: oraclelicense=accept-securebackup-cookie" \
           http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz \
            --no-check-certificate 
     
    Oracle does not allow downloads without accepting their license, therefore we needed to modify the header of our request. Alternatively, you can just download the compressed file using your browser and manually upload it using a SFTP/FTP client.
    Always get the latest version from Oracle's website and modify the commands from this tutorial accordingly to your downloaded file.

    Installing Oracle JDK

    In this section, you will need sudo privileges:
        sudo su
    The /opt directory is reserved for all the software and add-on packages that are not part of the default installation. Create a directory for your JDK installation:
        mkdir /opt/jdk
    and extract java into the /opt/jdk directory:
        tar -zxf jdk-8u5-linux-x64.tar.gz -C /opt/jdk
    Verify that the file has been extracted into the /opt/jdk directory.
        ls /opt/jdk

    Setting Oracle JDK as the default JVM

    In our case, the java executable is located under /opt/jdk/jdk1.8.0_05/bin/java . To set it as the default JVM in your machine run:
        update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_05/bin/java 100
    and
        update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_05/bin/javac 100

    Verify your installation

    Verify that java has been successfully configured by running:
        update-alternatives --display java
    and
        update-alternatives --display javac
    The output should look like this:
        java - auto mode
    link currently points to /opt/jdk/jdk1.8.0_05/bin/java
    /opt/jdk/jdk1.8.0_05/bin/java - priority 100
    Current 'best' version is '/opt/jdk/jdk1.8.0_05/bin/java'.

    javac - auto mode
    link currently points to /opt/jdk/jdk1.8.0_05/bin/javac
    /opt/jdk/jdk1.8.0_05/bin/javac - priority 100
    Current 'best' version is '/opt/jdk/jdk1.8.0_05/bin/javac'.
    Another easy way to check your installation is:
        java -version
    The output should look like this:
        java version "1.8.0_05"
    Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

    (Optional) Updating Java

    To update Java, simply download an updated version from Oracle's website and extract it under the /opt/jdk directory, then set it up as the default JVM with a higher priority number (in this case 110):
        update-alternatives --install /usr/bin/java java /opt/jdk/jdk.new.version/bin/java 110
    update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk.new.version/bin/javac 110
    You can keep the old version or delete it:
        update-alternatives --remove java /opt/jdk/jdk.old.version/bin/java
    update-alternatives --remove javac /opt/jdk/jdk.old.version/bin/javac

    rm -rf /opt/jdk/jdk.old.version

    Wednesday, November 20, 2013

    Installing Python 3 on CentOS/Redhat 5.x / 6.x From Source

    The latest release of the python scripting language is Python 3.4.0 However due to backwards incompatibilities with Python 2, it has not been adopted for CentOS / Redhat Linux 5. The primary reason for this, is because release of ‘yum’ (package management) used in EL5 requires Python 2. Because of this, Python cannot be upgraded in place to version 3 without breaking the package manager.

    Therefore to use Python 3, it will need to be installed outside of /usr.

    Installing

    Below is the list of command (with inline comments) on what is required to compile and install Python 3 from source. The installation will be done into the prefix /opt/python3. This will ensure the installation does not conflict with system software installed into /usr.
    - - - - - - - - - - - - #1 will install following- - - - - - - - - - - - - - - - - - 
    Installed:
      bzip2-devel.i386 0:1.0.3-6.el5_5                bzip2-devel.x86_64 0:1.0.3-6.el5_5                expat-devel.i386 0:1.95.8-11.el5_8
      expat-devel.x86_64 0:1.95.8-11.el5_8            gdbm-devel.i386 0:1.8.0-28.el5                    gdbm-devel.x86_64 0:1.8.0-28.el5
      openssl-devel.i386 0:0.9.8e-22.el5_8.4          openssl-devel.x86_64 0:0.9.8e-22.el5_8.4          readline-devel.i386 0:5.1-3.el5
      readline-devel.x86_64 0:5.1-3.el5               sqlite-devel.i386 0:3.3.6-6                       sqlite-devel.x86_64 0:3.3.6-6

    Dependency Installed:
      gdbm.i386 0:1.8.0-28.el5                         keyutils-libs-devel.x86_64 0:1.2-1.el5         krb5-devel.x86_64 0:1.6.1-70.el5
      libselinux-devel.x86_64 0:1.33.4-5.7.el5         libsepol-devel.x86_64 0:1.15.2-3.el5           libtermcap-devel.x86_64 0:2.0.8-46.1
      sqlite.i386 0:3.3.6-6

    Dependency Updated:
      expat.i386 0:1.95.8-11.el5_8                     expat.x86_64 0:1.95.8-11.el5_8             gdbm.x86_64 0:1.8.0-28.el5
      krb5-libs.i386 0:1.6.1-70.el5                    krb5-libs.x86_64 0:1.6.1-70.el5            krb5-workstation.x86_64 0:1.6.1-70.el5
      libselinux.i386 0:1.33.4-5.7.el5                 libselinux.x86_64 0:1.33.4-5.7.el5         libselinux-python.x86_64 0:1.33.4-5.7.el5
      libselinux-utils.x86_64 0:1.33.4-5.7.el5         libsepol.i386 0:1.15.2-3.el5               libsepol.x86_64 0:1.15.2-3.el5
      openssl.i686 0:0.9.8e-22.el5_8.4                 openssl.x86_64 0:0.9.8e-22.el5_8.4         sqlite.x86_64 0:3.3.6-6

    Complete!
    - - - - - - - - - - - - #1 will install above & if these are already installed we will see following- - - - - - - - - - - - - - 
    [root@bhmed-dt-2q ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
    Loaded plugins: logchanges, security
    Setting up Install Process
    Package openssl-devel-0.9.8e-22.el5_8.4.x86_64 already installed and latest version
    Package openssl-devel-0.9.8e-22.el5_8.4.i386 already installed and latest version
    Package bzip2-devel-1.0.3-6.el5_5.x86_64 already installed and latest version
    Package bzip2-devel-1.0.3-6.el5_5.i386 already installed and latest version
    Package expat-devel-1.95.8-11.el5_8.x86_64 already installed and latest version
    Package expat-devel-1.95.8-11.el5_8.i386 already installed and latest version
    Package gdbm-devel-1.8.0-28.el5.x86_64 already installed and latest version
    Package gdbm-devel-1.8.0-28.el5.i386 already installed and latest version
    Package readline-devel-5.1-3.el5.x86_64 already installed and latest version
    Package readline-devel-5.1-3.el5.i386 already installed and latest version
    Package sqlite-devel-3.3.6-6.x86_64 already installed and latest version
    Package sqlite-devel-3.3.6-6.i386 already installed and latest version
    Nothing to do
    [root@bhmed-dt-2q ~]#
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    1. # Install required build dependencies
      1. $ yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
    2. # Fetch and extract source. Please refer to http://www.python.org/download/releases to ensure the latest source is used.
      1. $ wget 
        1. https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz --no-check-certificate
        2. https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tar.xz --no-check-certificate
      2. $ tar -xjf Python-3.2.tar.bz2
      3. $ cd Python-3.2
    3. # Configure the build with a prefix (install dir) of /opt/python3, compile, and install.
      1. $ ./configure --prefix=/opt/python3
      2. $ make
      3. $ sudo make install
    4. #Python 3 will now be installed to /opt/python3.
      1. $ /opt/python3/bin/python3 -V
        1. Output: Python 3.2
    5. Ensure your python 3 scripts and applications query the correct interpreter.
      1. #!/opt/python3/bin/python3 Generally Work here is completed python3 can be accessed using this path.
    6. Now make sure you set your new PYTHON as in your PATH. try
      1. $which python: If this return a different version of python may be your system default then you might want to set your new python3.4 path or in the python3.4 directory try ./python that should open your new 3.4 python.
        1. echo $PATH
        2. export PATH=/YOUR/PYTHON/3.4_HOMEDIR/PATH:$PATH
        3. echo $PATH : (this is temporary, you can add to config files to make this export permanent) notice that i am adding it to the front so it gets picked before our system default.
    7. Now lets install some packages:
      1. # Create your virtual env: so this installation will not clash with system default python and/or you are not using the wrong python pip
        1. $ pyvenv-3.4 py3.4env
      2. # Activate it:
        1. $ source py3.4env/bin/activate
      3. # This should be pointing to the right pip in your new python 3.4 Home
        1. (py3.4env) $ which pip
      4. # This checks if your env is clean ..this would not return any  output
        1. (py3.4env) $ pip freeze
        2. (py3.4env) $ pip install numpy
        3. (py3.4env) $ pip install pytz

    Friday, October 18, 2013

    CENTOS v 6.4, GIT v 1.7.1 and JENKINS v 1.535 CONTINOUS INTEGRATION

    Having tuned our Agile process to release iteratively and often I decided it was time to spend some time looking at how we could introduce continuous integration into our PHP and Javascript BDD workflows. Given that Travis isn’t suitable for most of our work (your code must be open source), I chose Jenkins as our CI server and was able to get up and running fairly quickly. I  found a few resources that covered integrating Git with Jenkins but I ended up doing a bit of digging myself so thought I’d quickly share the steps I followed.

    Install Jenkins and Git

    I provisioned a fresh CENTOS v 6.4 box, which meant I was able to follow the official Jenkins docs without any problems. If you’re using anything other than Ubuntu/Debian or Redhat you may need to look elsewhere. The installation instructions outline simple Apache or Nginx vhost configurations that can be used to serve the Jenkins administration console. You’ll need to install git on the same box so that Jenkins can eventually pull down, or commit to, your git repositories. To install Git I simply ran $ sudo apt-get install git (debian) and $ yum install git (Centos).

    Install the Git Plugin

    Once you can access your Jenkins console,  goto `Manage Jenkins -> Manage Plugins` from the home screen.
    Open the ‘Available’ tab and find the plugin entitled Git Plugin.There is a filter box but it didn’t work particularly well for me, I ended up using Find in Chrome.

    Create ssh key pair

    Part of the installation process will create the user `jenkins` which will envoke all Jenkins processes, including all git commands. Therefore you’ll need to provision a keypair for this user then add the public key to your git repo. There’s a bit of trick to doing the former which I’ll cover now:

    1. Login to your box and switch to the Jenkins user. The installation process doesn’t create a password so you’ll need to have root/sudo permissions to do this. Run the command sudo su - jenkins. The ‘-’ specifies a login shell, and will switch you to jenkins’ home directory (for me this was /var/lib/jenkins’).
    2. Create a .ssh directory in the jenkins home directory.
    3. Create the public private key pair. There are many tutorials which cover using the ssh-keygen command to do this. The most important thing is not to set a password, otherwise the jenkins user will not be able to connect to the git repo in an automated way.
    4. Add the public key to your Git repo. We use bitbucket so this was fairly straightforward for me and I imagine anyone reading this will have performed similar actions for all their devs keys in the past.
    5. Set a git user and email address. This is also mentioned in the git plugin documentation. Run:cd /srv/jenkins/jobs/project/workspacegit config user.email "some@email.com"git config user.name "jenkins".
    Connect to the Git repo. This is a one time step which will dismiss that ‘Are you sure you want to connect’ ssh message, again jenkins won’t be able to deal with this. Just run ssh git@your_git_server_url info.

    Create a new Job in Jenkins

    The official docs provide a good level of detail on how to configure a basic Jenkins job, so I’d recommend following them here. The git-plugin docs also provide some useful info, so there’s not too much more for me to say other than list the steps I followed:

    1. Select Git in Source Code Management.
    2. Enter your git repository URL. This performs a asynchronous check and will give you an error if it can’t connect. Double check you followed the steps in #3 if you get an error.
    3. Select the branch to build. This is branch jenkins will pull from when a build is started so enter whatever is suitable. We are building from develop so I entered this here.

    Run a test Build

    Once you’ve saved the job it’s worth kicking off a build to check everything’s working OK. You can do this in the main dashboard. Have a look at the latest build, you should see the commit id that was pulled down. If you dig around in the docs you’ll find that the plugin will have fetched your branch (in my case develop) and pulled from the remote repo. The repo is checked out into the job’s `workspace` directory and during the build the Jenkins user is cd’d into this directory. By adding your makefiles and build scripts into your repository it’s then a straightforward case of configuring the Jenkins job to execute these upon build. Again, this is covered in good detail in the docs.

    Post Receive Hook

    Finally you’ll need to setup your Git repository to initialise a build each time you push to your repository. Github and bitbucket both have hooks ready to be used, but if you’re self hosted a good place to start is at the plugin documentation which specifies the HTTP endpoint which can be used to trigger the job.