Infolinks

Wednesday, August 26, 2015

Online resize LVM partitions – shrink home / extend root

Online resize LVM partitions – shrink home / extend root

By default, most Linux Installers create separate /home partition, occupying most of the drive space.
When server is used mostly for system services, you may transfer free space from /home partition to /root.
Before adjustment we have 98% filled root partition and 141GB free space on /home, which we would like to use:
[root@oracle ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_oracle-lv_root
                       50G   1G   50G  98% /
tmpfs                 630M     0  630M   0% /dev/shm
/dev/cciss/c0d0p1     477M  106M  346M  24% /boot
/dev/mapper/vg_oracle-lv_home
                      149G   60M  141G   1% /home
1. First step is to unmount /home partition
umount /home
System may refuse to unmount /home if you have users logged on to the box or services running from /home. After logging off / stopping services command should succeed.
2. Shrink old /home partition to 20GB, (system will force you to check filesystem for errors by running e2fsck)
e2fsck -f /dev/mapper/vg_oracle-lv_home
resize2fs /dev/mapper/vg_oracle-lv_home 20G
3. Reduce the LVM to 20G
lvreduce -L 20G /dev/mapper/vg_oracle-lv_home
4. Extend /root LVM to new size, utilizing 100% of free space on disk
lvextend -l +100%FREE /dev/mapper/vg_oracle-lv_root
5. Grow /root (ext3/4) partition to new LVM size
resize2fs /dev/mapper/vg_oracle-lv_root
6. Mount /home
mount /home
Result
[root@oracle ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_oracle-lv_root
                      178G   50G  128G  28% /
tmpfs                 630M     0  630M   0% /dev/shm
/dev/cciss/c0d0p1     477M  106M  346M  24% /boot
/dev/mapper/vg_oracle-lv_home
                       20G   45M   19G   1% /home

After these simple steps we have 72% of free disk space of root partition.
TL;DR
Resizing /home partition (/dev/mapper/vg_oracle-lv_home) to 20GB and transfering remaining space to /root(/dev/mapper/vg_oracle-lv_root):
umount /home
e2fsck -f /dev/mapper/vg_oracle-lv_home
resize2fs /dev/mapper/vg_oracle-lv_home 20G
lvreduce -L 20G /dev/mapper/vg_oracle-lv_home
lvextend -l +100%FREE /dev/mapper/vg_oracle-lv_root
resize2fs /dev/mapper/vg_oracle-lv_root
mount /home

Tuesday, February 18, 2014

Create and Deploy a basic site using node.js and express framework

Here i am going to explain you a basic tutorial to create a basic site using Node.js and Express framework. This tutorial is aimed at beginners exploring Node.js .  We are going to use express, an excellent web framework for node created by TJ Holowaychuk who seems to be pumping out Node.js libraries like he was ten men.
Here is the site we are going to create. You might also want to grab the source code.

How to setup the System

First we need to setup our development environment. 
If you are on Linux there are plenty of articles on Google.
For Windows users there are also resources on Google but it is a bit more tricky.

Prerequisites

If everything has installed ok you should now have Node.js and npm running on your machine. At the terminal type node -v and npm -v and you should see something like:
node -v
v0.8.21
npm -v
1.2.12

Create an Express site

Still with me? We've covered a lot already! Now let's create an Express site.
First let's install express
npm install -g express
The -g flag means that you are installing express globally on your system.
Now we can create an express application.
express -c stylus express_example
The -c states that we want to use stylus for css. You should see the following output:
create : express_example
create : express_example/package.json
create : express_example/app.js
create : express_example/public
create : express_example/public/javascripts
create : express_example/public/images
create : express_example/public/stylesheets
create : express_example/public/stylesheets/style.styl
create : express_example/routes
create : express_example/routes/index.js
create : express_example/routes/user.js
create : express_example/views
create : express_example/views/layout.jade
create : express_example/views/index.jade

install dependencies:
 $ cd express_example && npm install

run the app:
 $ node app
As per the instructions you'll need to install dependencies so do this
cd express_example && npm install
This will install packages and you will see a lot of output. When this is complete you can boot your application.

Boot the app

That's all the setup you need. Now you can boot the app:
node app.js
You should see Express server listening on port 3000 and if you open http://127.0.0.1:3000 you'll see the default Express page.

Using Git

Git is a version control system that is used heavily in the Node.js ecosystem, particulary with Github. If you aren't familiar with Git Scott Chacon is your go-to man. He's written extensively and eloquently on Git for beginners and experts. Checkout Gitcasts for if you are a beginner and ProGit for more advanced stuff. We are going to use git to version our site and publish it so let's set up our repo now. If your Express server is still running hit CTRL + C to stop it.
git init
git add .
git commit -m 'initial commit'

Developing Node.js sites

Normally when you develop a Node.js site you'll need ot restart your application each time you make a change. Thankfully our home-grown British JavaScript genius Remy Sharp has solved this problem with nodemon. Nodemon will reload your application each time it changes so you don't need to restart it. If you have used Shotgun for Ruby with Sinatra it is similar to that. To install run
npm install -g nodemon
Then you can start your app with
nodemon app.js
Using nodemon means you don't have to restart your app each time you make a change. For more infomation on nodemon see the README

HTML in Express

Express is agnostic as to which templating language you use. Templating languages can be a hot topic of debate but for this article I'm going to use jade. If you've used haml it is similar to that. In the example we use jade to setup a layout template.
!!! 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    link(rel='stylesheet', href='/stylesheets/chunkfive-fontface.css')
  body
    header
      nav
        ul
          li 
            a(href="/") Home
          li 
            a(href="/about") About
          li 
            a(href="/contact") Contact
    section#wrapper!= body
        footer 
          section.css-table
            section.four-column
              section.cell
                p Mauris porttitor <br />felis eu leo aliquet<br /> ac rutrum odio aliquet
              section.cell
                p Mauris porttitor <br />felis eu leo aliquet<br /> ac rutrum odio aliquet
              section.cell
                p Mauris porttitor <br />felis eu leo aliquet<br /> ac rutrum odio aliquet
              section.cell
                p Mauris porttitor <br />felis eu leo aliquet<br /> ac rutrum odio aliquet
This is a common template we can reuse. The line section#wrapper!= body pulls in content from the page it is used on. Express also supports variables that you pass through to the template. In this case we pass the title variable. If you are coming from Sinatra this will all be familiar to you. If you are not I recommend consulting the Express documentation.

CSS in Express

Again Express is agnostic to what you use to generate your CSS - you can use vanilla CSS but for this example I'm using Stylus. This is very similar to Sassand supports variables, mixins, functions and more. I really like it! Here's an example from our stylesheet
body
  font 62.5%/1.5  Helvetica, Arial, "Lucida Grande", "Lucida Sans", Tahoma, Verdana, sans-serif
  text-align center
  background #000

#wrapper
  width 920px
  text-align left 
  margin-left auto 
  margin-right auto
  background #fff
  padding 20px
  border-bottom-radius(15px)
You'll see that stylus is very terse - you don't need brackets or commas.

Routing in Express

Routing is similar to Sinatra, allowing you to set up RESTful routes.
In this example we setup three routes in app.js
app.get('/', function(req, res){
  res.render('index', {
    title: 'Home'
  });
});

app.get('/about', function(req, res){
  res.render('about', {
    title: 'About'
  });
});

app.get('/contact', function(req, res){
  res.render('contact', {
    title: 'Contact'
  });
});
See the Express documentation for more.

Publishing your site

We've now developed a basic Node.js site using express and we want to host it somewhere. Publishing the site to Heroku is free and you can be up and running in no time. You can sign up for an account at Heroku for free and then install the toolbelt.
To make your example node site work with Heroku you must create a file called Procfile in the root of the project. Add the following.
web: node app.js
To make sure Heroku uses the correct versions of node add this to the package.json file
"engines": {
  "node": "0.8.x",
  "npm": "1.2.x"
}
Then you can use the command line tools to create a site on Heroku and publish it.
heroku apps:create 
git push heroku master
Easy!
Some other Node.js hosting providers include nodejitsu, Joyent, Cloud Foundry and Duostack.

Conclusion

This article has showed how to create a very basic site using Node.js and Express. It has introduced a number of things from the Node.js ecosystem and showed you how to deploy your app to Nodester.
The strengths of Node.js as a technology are not so much in building static websites like this. I encourage you to explore some of the Node.js libraries to see what it can do. Particularly for real-time applications Node.js is extremely exciting and I think we'll see some great apps built on Node.js. Try starting with socket.io for a taste of what to expect.

Saturday, February 16, 2013

Secure Push to AJAX Push Engine(APE)

APE (Ajax Push Engine) is an extraordinary Client/Server Framework for creating AJAX PUSH (Comet) applications. In the latest revisions on github there is even the use of HTML5´s Websockets implemented, which makes the standard way of implementing Comet by using a "hanging iframe" obsolete  (for browsers with websocket support). Also the APE server can be scripted by using JavaScript which makes it very easy to extend it´s features (at least for developers with JS knowledge). The default framework used is mootools, but any other or plain JS could do the same.

One of APE´s nice features is to invoke messages to the client by a script or application on the server side or even on a different server. This is very usefull when you want to broadcast certain server events or messages from other applications to the Clients connected to APE.

They way how to do this is is register a certain server command, which then excutes the appropriate logic. The command then can be called by the invoking script through HTTP (Depending on your needs a socket connection would also be  possible). In APE´s demo section an example for such a feature can be found in the "Controller" Demo using a simple PHP script. (If you need an OOP style Client Library to connect to APE through PHP you may want to check my APE_PHP on github) 

However in the Controller demo every message sent by the external script is broadcasted to every user connected to the certain channel. While this is usefull for a lot of cases, there might be the need to send a message to only one specific user on the channel. There is a simple solution for this problem:

One could use a different channel per user and send the message through it.  But this approach has (at least) two drawbacks. One is scalability, the other is security. Since the way this is done could be easily found out on the server side a "bad user" could connect to the other user´s channel and receive all of his private messages. This post has been inspired by a discussion on APE´s mailing list on how to implement such feature in a secure way.

In this scenario we assume that there is some kind of server side authentication process (It wouldn't make much sense otherwise anyway) . So when the user is authenticated a secret key is generated or the session key or something else is used. This key will be send to the client with the page where the APE connection will be established. 
On the server side we create a hook for the "connect" command and register the key as a private property on the user object.


  1. Ape.registerHookCmd("connect", function(params, cmd) {
  2.         // this makes the key a private property on the user object
  3. cmd.ser.key =  params.key
  4.           return 1;
  5. });



Now we create our custom APE command to be called from the script:


  1. Ape.registerCmd("securepush", false, function(params, infos) {
  2.       if (params.password == "foobar") {     
  3.                 if ($defined(params.channel) && $defined(params.key)){
  4.                         var channel = Ape.getChannelByName(params.channel);
  5.                         if (!$defined(channel)) return ["401", "UNKNOWN_CHANNEL"];
  6.                         var sent = false;
  7. channel.userslist.each(function(user){
  8.                            if(user.key == params.key){
  9.                               user.pipe.sendRaw(params.raw, params.data);  
  10. sent = true;
  11.                            }  
  12.                         });            
  13.                  return sent ? {"name":"pushed","data":{"value":"ok"}} : ["400", "USER_NOT_FOUND"];
  14.                 }
  15.                 return ["400", "BAD_KEY"];
  16.         } else {
  17.                 return ["400", "BAD_PASSWORD"];
  18.         }
  19. });
The Client then connects to APE using the special key:
  1. var client = new APE.Client();
  2. client.load();
  3. client.addEvent('load', function() {
  4.     client.core.start({"name": 'UserName', "key":"secretKey"});
  5.     client.addEvent('ready', function() {
  6.         client.core.join('SecureChannel');
  7.     }
  8. });
Now we can send a message (with the secret key)through our script to the user and only he will receive our message (Example in PHP):
  1. $APEserver = 'http://ape.example.com:6969/?';
  2. $APEPassword = 'foobar';
  3. $cmd = array(array(
  4.   'cmd' => 'securepush',
  5.   'params' =>  array(
  6.           'password'  => $APEPassword,
  7.           'raw'       => 'postmsg',
  8.           'channel'   => 'SecureChannel',
  9.           'key'   => 'secretKey',
  10.           'data'      => array(  
  11.               'message' => 'You have a new message!'
  12.           )
  13.    )
  14. ));
  15. if ($res->data->value == 'ok') {
  16.         echo '<br />Message sent! result: ';
  17. } else {
  18.         echo 'Error sending message, server response is : ';
  19. }
  20. echo '<pre>'.print_r($res, 1).'</pre>';


This is the most simple solution on how to do secure pushs to APE. However it still has (atleast) one drawback, that it uses "channel.userslist.each(function(user){[...]});" which doesn´t scale with a lot of users. I hope I will have the time to post a solution using a proper registry and a way to reconnect with usernames (the default would response with "NICK_USED" if a client would refresh the page).

Sunday, July 15, 2012

Auto Chat Bot with Node.js and XMPP

Want something that respond your chats automatically, without your presence. If you want a system like that here i come for solution for that using Node.js and XMPP Protocol (Used in most chat system like google, facebook). So here we go step by step to implement a system like i have describe above.

First of all you have a system installed node.js, After it for use xmpp with node.js you need a node.js module node-xmpp. To install node-xmpp you just need run a command using npm

npm install node-xmpp

The node-xmpp module is entirely written in Node.js. The GitHub page of the project is located here. Refer other details about node-xmpp from GitHub page. Now we are going to create a chat bot.
First of all you need to include node-xmpp module in your code.

Example of a simple Auto Chat bot system


var xmpp = require('node-xmpp');

//Chat Rules Start from Here
var arr_rules_id = new Array("1","2","3","4");
var arr_rules_text = new Array("Hi","How are you","Where are You","Are you Coming");
var arr_rules_resp_text = new Array("Hi There i am auto chat bot","I am fine","I am online","No never");
//Chat Rules Ends Here

//XMPP Account Details Starts from Here
var client_id = "your_chat_account@server.com"; //Use any XMPP Supported Account like Gmail
var client_pwd = "your_chat_account_password";
var client_host = "XMPP HOST of Your Account";  //talk.google.com for gmail
var client_port = "XMPP port for Host";   //5222 for gmail
var status_msg = "I am Online";   //Set it as you want
//XMPP Account Details End Here

//Now Creating XMPP Client
var cl = new xmpp.Client({ jid: client_id,
password: client_pwd,
host: client_host,
port: client_port
});
//Client Created

//Now Set the Presence of Bot
cl.on('online', function() {
    cl.send(new xmpp.Element('presence', { }).
c('show').t('chat').up().
c('status').t(status_msg)
);
console.log("Connected");
});

//Receiving of Stanza's Starts(Messages are also recieved)
cl.on('rawStanza',
      function(stanza) {
console.log(stanza);
 if (stanza.is('presence') && (stanza.attrs.type == 'subscribe')) {
            cl.send(new xmpp.Element('presence', { to: stanza.attrs.from, type: 'subscribed' }));
        }
if (stanza.is('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {

// Swap addresses...
var frm = stanza.attrs.from;
var body = stanza.getChild('body');
console.log(stanza);
console.log("\n"+body);
if (!body) {
    return;
  }

var message_recv = body.getText();   //Get Incoming Message
var index = checkarr(arr_rules_text, message_recv);   //Check for incoming message's response text index in array

function checkarr(arr,mes)
{
for(var i=0; i<arr.length; i++) {
        if (arr[i].toLowerCase() == mes.toLowerCase())
        {
        return i;
        }
    }
    return 'notvalid';
}

if(index!='notvalid')     //If message keyword matches then reply back 
{
    var response = arr_rules_resp_text[index];
    cl.send(new xmpp.Element('message', { to: frm, type: 'chat' }).
        c('body').t(response)
    );
}
else
{
    cl.send(new xmpp.Element('message', { to: frm, type: 'chat' }).
        c('body').t("Not Response Found Against Keyword")
    );
}
}
});
cl.on('error', function(err) {
    console.error(err);
});

cl.on('conn-close', function() {
console.log('connection ends');
});

So here we have done all stuff to do chat automatic response bot now run this script and your account goes online. Below i have attach sample screenshot of autochat.


Saturday, July 14, 2012

Custom Infowindow / InfoBox to Google Maps API V3

Here i am going to tell you that how to create custom infowindow/InfoBox with Google Maps Api V3 .
Here some steps to achieve this.. To use google maps first of all you need to insert google maps API in your code.
Necessary file can be downloaded from here
<script src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=true" type="text/javascript"></script>

Now include infobox.js in your code
<script src="infobox.js" type="text/javascript"></script>

Now here is a simple map example containing custom infobox
<script type="text/javascript">
var map;
function initialize() {
        var myOptions = {
          zoom: 12,
          center: new google.maps.LatLng(-34.397, 150.644),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            myOptions);
  var marker = new google.maps.Marker({
  position: map.getCenter(),
  map: map,
  title: 'Click to zoom'
  });

      var boxText = document.createElement("div");
      boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: #999; border-radius:5px; padding: 5px; color:#FFF;";

      boxText.innerHTML = "Hello This is a custom styled InfoWindow<br />You Can Design it as you want";
   var myOptions1 = {
                 content: boxText,
                 disableAutoPan: false,
                 maxWidth: 0,
                 pixelOffset: new google.maps.Size(-140, 0),
                 zIndex: null,
                 boxStyle: { 
                 background: "url('tipbox.gif') no-repeat",opacity: 0.85,width: "380px"
                 },
                 closeBoxMargin: "10px 2px 2px 2px",
                 closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
                 infoBoxClearance: new google.maps.Size(1, 1),
                 isHidden: false,
                 pane: "floatPane",
                 enableEventPropagation: false
        };
        var ib = new InfoBox(myOptions1);
        ib.open(map, marker);
        google.maps.event.addListener(marker, "click", function() { 
        ib.open(map, marker); 
 });
      }


</script>


Add above scripts in your page's head tag.
Now initialize the map
<body onload="initialize();">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>


That's all now you can view your map with custom InfoWindow.

How to use Node.js with mysql


New to Node.js and want to know that how to use most famous open source database 'mysql' with it. Here i have a big surprise for you - it is surprisingly very easy to get MySQL working with Node.js. My this post will show you that how you can do it
So for use mysql with node.js first of all you need a node.js module mysql. To install mysql module just use following command using npm
$ npm install mysql


The mysql module is entirely written in Node.js, and implements an async MySQL client. The GitHub page of the project is located here , refer to it for other details about the API. Once you have installed the module, you can include it in your Node.js files and configure it this way.

var mycon = require('mysql'); 


var HOST = 'Your_MYSQL_HOST'; 
var PORT = Your_MYSQL_PORT; //Default 3306
var MYSQL_USER = 'Your_MYSQL_USER'; 
var MYSQL_PASS = 'Your_MYSQL_PASSWORD'; 
var DATABASE = 'Your_MYSQL_DATABASE';


var mysql = mycon.createClient({ 
            host: HOST, 
            port: PORT, 
            user: MYSQL_USER, 
            password: MYSQL_PASS, 
            });


mysql.query('use ' + DATABASE);


Now you can execute MySQL commands on the database.
Here is an example of mysql select query


mysql.query('select id,emp_name,emp_dep from TABLE_NAME where emp_salery > 300000',
function(err, result, fields) {
    if (err) throw err;
    else {
        console.log('Employees with salary greater than 300000 \n');
        for (var i in result) {
            var empdata = result[i];
            var empid = empdata.id;
            var emp_name = empdata.emp_name;
            var emp_dep = empdata.emp_dep;
            console.log("Emp ID : "+empid+"  Emp Name : "+emp_name+"  Emp Dep : "+emp_dep);
        }
    }
});


Now Here is an example of mysql insert query

mysql.query('insert into TABLE_NAME (emp_name, emp_dep, emp_salery) values ('XYZ', 'ABC', 500000)',
function selectCb(err, results, fields) {
    if (err) throw err;
    else 
    {
    console.log("Mysql Query Successfully Executed");
    }
});

Same process is used for mysql update query, Now To get the last insert ID, you use the insertid property of the insert result object.

client.query('INSERT INTO TABLE_NAME(column1, column2) values('a','b')', function(err, ress) {
    console.log(ress.insertId);
});


So above describe steps are used to use mysql with node.js. There are also many modules available for use mysql with node.js, but this is the best module which fulfill your requirements.

Tuesday, April 3, 2012

Hide php extension -- URL Rewriting on Apache using htaccess

URL Rewrite using .htaccess on Linux Apache and Wamp Server
URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site. It can also be the source of near-unending misery and suffering. Definitely worth playing carefully with it - lots of testing is recommended. With great power comes great responsibility, and all that.

URL rewriting on Linux Apache and wamp
For rewriting url on Centos Please do these steps.
  1. First go to '/etc/httpd/conf/httpd.conf' and for wamp left click on wamp icon and go to Apache then httpd.conf and uncomment - #LoadModule rewrite_module modules/mod_rewrite.so and #AddModule mod_rewrite.c, so do so please remove trailing #.

  2. Now Edit directory section in this file for directory /var/www/html and change 'AllowOverride none' to 'AllowOverride All'.

    Eg.
    Options Indexes FollowSymLinks
    AllowOverride none
    Order allow,deny
    Allow from all
    ;
    TO

    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    ;

  3. Now make a file in server root directory '/var/www/html' name .htaccess and write these line in this

    Options +FollowSymlinks

    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_FILENAME}\.php -f

    RewriteRule ^(.*)$ $1.php


  4. Now this will remove php extension in your file so now if you want to open index.php then write only http://url/index only instead of http://url/index.php