Friday 11 November 2016

RY_01_Ruby_and_Rails_Installation

Ruby on Rails Installation Process

We are using a Ubuntu machine for this installation and practice.

Install Ruby on Rails using "rbenv" on Ubtuntu:

  • Install the required packages and pre-requisites:
    sudo apt-get update -y
    sudo apt-get install -y git gitk
    sudo apt-get install -y gcc build-essential libpq-dev libssl-dev libreadline-dev libsqlite3-dev zliblg-dev
  • Clone the rbenv repository:
    git clone git://github.com/sstephenson/rbenv.git /GitHub/ruby/.rbenv
  • Export the RECIPE PUPPY site with port to bashrc:
    echo 'export RECIPEPUPPY_HOSTPORT=www.recipepuppy.com:80' >> ~/.bashrc
  • Export rbenv/bin:
    echo 'export PATH="$PATH:/GitHub/ruby/.rbenv/bin"' >> ~/.bashrc
    echo 'eval "$(rbenv init .)"' >> ~/.bashrc
  • Now clone the ruby-build repository:
    git clone git://github.com/sstephenson/ruby-build.git /GitHub/ruby/.rbenv/plugins/ruby-build
  • Set path for this plugins directory:
    echo 'export PATH="$PATH:/GitHub/ruby/.rbenv/plugins/ruby-build/bin"' >> ~/.bashrc
  • Now we need to source the bashrc as we have some entries into it.:
    source ~/.bashrc
  • Lets begin the installation now:
    rbenv install -v 2.2.2
  • Lets set the default version of Ruby:
    rbenv global 2.2.2
    ruby -v
  • We need to stop the gems to generate local documentation:
    echo "gem: --no-document" > ~/.gemrc
  • Install the Ruby gem manager:
    gem install bundler
  • Install Rails:
    gem install rails -v 4.2.3
  • Now we need to rehash as it will let us to get command completion which relates to rails.
    rbenv rehash
  • Let us install some other packages which are needed in our practice
    sudo apt-get install -y software-properties-common python-software-properties
  • Now let us install some javascripts through a custom location
    sudo add-apt-repository ppa:chris-lea/node.js
    sudo apt-get install -y node.js
    sudo apt-get install -y bzip2
  • Let us setup path for some ENV
    echo 'export PHANTOM_JS="phantomjs-1.9.8-linux-x86-64"' >> ~/.bashrc
  • Let us download and install the PhantomJS package:
    cd /tmp
    curl -L https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2 | tar -xjvf
    sudo mv $PHANTOM_JS /usr/local/share
    sudo ln -s /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin
    phantomjs -v
  • Install text editor:
    curl http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%202.0.2%20x64.tar.bz2 | tar -xjf
    sudo mv 'Sublime Text 2' /opt/SublimeText2
    echo 'export PATH=$PATH:/opt/SublimeText2' >> ~/.bashrc
    source ~/.bashrc
  • Let us check all the versions now
    git --version
    phantomjs -v
    ruby -v
    rails -v
  • Now let us create a rails application
    Create a project directory and then create a application
    mkdir /u01/ruby/projects &&cd /u01/ruby/projects

    rails new test-install -q
NOTE: Here we are using -r to run this command in quite mode as it will produce lot of output.
  • Now cd into the new rails application we have created and run the server
    cd test-install ; rails server
  • Let us check the application in the browser
    http://localhost:3000
  • Now we have completed configuring ruby on rails.

Thursday 10 November 2016

CI-CD_005_Git_with_Jenkins

Integrating Git with Jenkins

If we are dealing with some specific code based builds where the code doesn't need any modification can be directly used for a build. But in some cases the code need to be changed several times depending on the need. In such case the periodic builds need to have those modifications. We use SCM (Source Code Management) to overcome this situation. And instead of manually copying the code to the build we can integrate the repositories from the SCM tools with our Jenkins builds.

Configuration

  • Global Settings In the Configure System page, the Git Plugin provides the following options:
    • Global Config user.name Value: if provided git config user.name is called before builds. This can be overridden by individual projects.
    • Global Config user.email Value: if provided git config user.email is called before builds. This can be overridden by individual projects.
    • Create new accounts base on author/committer's email: if checked, upon parsing of git change logs, new user accounts are created on demand for the identified committers / authors in the internal Jenkins database. The e-mail address is used as the id of the account.

Project Configuration

At the project level the Git Plugin is configured by selecting the Git option at the Source Code Management section.
The main section is Repositories where several can be configured. The information to provide include:
  • The Repository URL, which is mandatory. The URL uses the same syntax as the git clone command. This can be a URL or a local file path. Note that for super-projects (repositories with submodules), only a local file path or a complete URL is valid.
    • The following are examples of valid git URLs.
      ssh://git@github.com/github/git.git
      git@github.com:github/git.git (short notation for ssh protocol)
      ssh://user@other.host.com/~/repos/R.git (to access the {repos/R.git repository in the user's home directory)
      https://github.com/github/git.git
      git://github.com/github/git.git
    • If the repository is a super-project, the location from which to clone submodules is dependent on whether the repository is bare or non-bare (i.e. has a working directory).
      • If the super-project is bare, the location of the submodules will be taken from .gitmodules.
      • If the super-project is not bare, it is assumed that the repository has each of its submodules cloned and checked out appropriately. Thus, the submodules will be taken directly from a path like ${SUPER_PROJECT_URL}/${SUBMODULE}, rather than relying on information from .gitmodules.
    • For a local URL/path to a super-project, git rev-parse --is-bare-repository is used to detect whether the super-project is bare or not.
    • For a remote URL to a super-project, the ending of the URL determines whether a bare or non-bare repository is assumed:
      • If the remote URL ends with /.git, a non-bare repository is assumed.
      • If the remote URL does NOT end with /.git, a bare repository is assumed.
  • Credentials: Credentials to use to connect to the repository (unless anonymous access is allowed), using the Jenkins Credentials Management functionality. The type of credentials used depends on the underlying protocol. For SSH connections only private key authentication is supported.
The next section is Branches to Build in which several branch specifiers can be provided. For each of these specs, leaving it blank means that all branches will be examined for changes and built. The safest way is to use the refs/heads/ syntax. This way the expected branch is unambiguous. See the online help for more options.
A Repository Browser can also be configured, which adds links in "changes" views within Jenkins to an external system for browsing the details of those changes. The "Auto" selection attempts to infer the repository browser from other jobs, if supported by the SCM and a job with matching SCM details can be found, though it can also be selected manually.
Finally, the Git Plugin is extensible and the plugin itself as well as external plugins can provide Additional Behaviours to tweak the SCM configuration inside each particular project. Please refer to the online help of each of the additional options for further information.

Using Extra Repositories

Since GIT is a Distributed SCM, it is possible in the Advanced section to specify multiple repositories. You may wish to do this to, for example, pull all in-progress work from individual developers machines, and pre-test them before they are committed to a centralised repository - this way developers may get an early warning that a branch in progress may not be stable.
The GIT plugin will make reasonable attempts to try and pull submodule states from distributed repositories, with the proviso that this feature is not currently well supported within GIT itself.

Wednesday 9 November 2016

CI-CD_004_Jenkins_Parameterized_Builds

Parameterized Builds

Sometimes, it is useful/necessary to have your builds take several "parameters." Consider the following use case:
  • You set up a test job on Jenkins, and it accepts a distribution bundle as a parameter and perform tests against it. You want to have developers do local builds and let them submit builds for test execution on Jenkins. In such a case, your parameter is a zip file that contains a distribution.
  • Your test suite takes so much time to run that in normal execution you can't afford to run the entire test cycle. So you want to control the portion of the test to be executed. In such a case, your parameter is perhaps a string token that indicates that test suite to be run.
These parameters are available as environment parameters. For example like we use the variables in a Shell script. Here we will have two parameters to be specified which are
1. Name
2. Default Value
If we are using any Shell script for our build and we have some variables need to be specified as arguments for the script then we need to keep the calling name of the variable as "Name" for the build and if we need to use default values for that we can specify those in the "Default Value" column. Or else we need to provide the value while running a build. Those values will be directly assigned to the variable parameters which will be used in the script.
For having our project to use parameters we need to do the following.
After the Project name and Description we'll have a check box "This build is parameterized". We need to check that box enabled to run parameterized builds. Now we need to provide the name and default value to be used within the project.

Tuesday 8 November 2016

CI-CD_003-Jenkins-HipChat-Notifications

Notify your team about the build with status in HipChat

  • To have this feature you need to install the HipChat Plugin.
    * Go to "Manage Jenkins"
    * Click on "Manage Plugins"
    * Click on "Available tab"
    * In the filter search for "HipChat"
    * Select "HipChat Plugin" and click on "Install without restart"
  • Now restart your jenkins. You can do it depending on the way using jenkins.
    • If you are using jenkins directly then
        * service jenkins restart
    
    • If you are using jenkins through tomcat then
        * Login to your tomcat webui, go to "Manager App" and click on "Reload Jenkins"
            (or)
        * http://<HOSTNAME/IP>:8080/jenkins/restart
    
  • Now you need to login to your HipChat web-ui and go to the room on which you have Admin access.
NOTE : To get admin access you need to create a room. You will be admin of the room that you've created.
    * Login to your hipchat account ( https://<my-hipchat>.hipchat.com/rooms/ )
        -- Ex: https://linux-library.hipchat.com/rooms/
    * Find and click on the room you've created.
    * Click on "Tokens"
    * Under the "Create new token" section provide a lable for your notification message.
    * Select "Send Notification" from "Scopes"
    * Click on Create.

    -- Above the "Create new token" section you'll get the Token with the lable you've give.

    * Copy that token.

    -- Now in the url after "tokens/" you'll notice a number that is your room number.

    * Copy that number.
  • Now you need to configure these details in Jenkins.
    * Go to your jenkins home page click on "Manage Jenkins"
    * Click on "Configure System"
    * Go to "Global HipChat Notifier Settings" and provide the following settings
        * HipChat Server    : api.hipchat.com
        * Use v2 API        : yes
        * API Token     : <Paste your token here>
        * Room          : <Paste your room-id here>
    * Click Save
  • Now in your project add the "Post-build Actions"
    * Click on "Add post-build action"
    * Select "HipChat Notifications" and provide the following details
        * Project Room      : <Paste your room-id here>
    * Under message templates provide the following values to get your build status notification.
        * Job started   :   ${JOB_NAME} #${BUILD_NUMBER}  ---  $STATUS  ---  ${URL}
        * Job completed :   ${JOB_NAME} #${BUILD_NUMBER}  ---  $STATUS - $DURATION  ---  ${URL}
  • Now start the build and verify the build status notification in HipChat

Monday 7 November 2016

CI-CD_002-Jenkins-on-Tomcat

Tomcat installation

  • Verify the Java installation
    # java -version
  • Set the Java home and Path
    # echo "export JAVA_HOME=/usr/java/jdk1.8.0_91/" >> ~/.bashrc
    # echo "export JAVA_HOME=/usr/java/jdk1.8.0_91/" >> /etc/bashrc
    # echo "export PATH=$PATH:$JAVA_HOME/bin/" >> /etc/bashrc
    # echo "export PATH=$PATH:$JAVA_HOME/bin/" >> ~/.bashrc
  • Download tomcat application from the following site in tar.gz format
    https://tomcat.apache.org/download-70.cgi
  • Make a custom application directory and untar the tomcat application
    mkdir /u01
    cp -prv ~/Downloads/apache-tomcat-7.0.69.tar.gz /u01/
    cd /u01
    tar -xzvf apache-tomcat-7.0.69.tar.gz
  • Create a soft link for the unzipped directory as tomcat
    ln -s apache-tomcat-7.0.69 tomcat
  • Copy the jenkins.war file into webapps directory
    cp -prv `locate jenkins.war` /u01/tomcat/webapps/
  • Now clone the "Jenkins" repo from Linux-Library git account "vmsnivas" and copy the tomcat script from the scripts dir to /sbin
    mkdir /git
    cd /git
    git clone https://github.com/vmsnivas/jenkins.git
    cp -prv /git/jenkins/scripts/tomcat /sbin/
  • Now start the tomcat application
    tomcat start
NOTE : Here you'll face some problems as the default ports of Jenkins and Tomcat are the same (8080). So we'll change the Jenkins port to some other in my case i am using 18080. Also will change the hostname, users and roles in tomcat
  • To change the default port of Jenkins edit "/etc/sysconfig/jenkins"
    JENKINS_PORT="18080"
  • Now let us configure the Tomcat
    - Change the hostname from "localhost" to "dev1.linux-library.ll"
        <Engine name="Catalina" defaultHost="dev1.linux-lirary.ll">
        <Host name="dev1.linux-library.ll"  appBase="webapps"
  • Configure a user to manage the UI. Edit "/u01/tomcat/conf/tomcat-users.xml" and add the following above "" tag.
    <role rolename="manager-gui"/>
    <role rolename="tomcat"/>
    <user username="arjun" password="aDminTom" roles="tomcat,manager-gui"/>
  • Now you can be able to use Jenkins through Tomcat. All you need to do is to load the below url
    http://dev1.linux-library.ll:8080/jenkins/