Friday, December 19, 2014

Create service in ubuntu to run ruby script

My Project requirement to continuously run a ruby script to do some stuff. I achieved it with nohup. If the some exception comes in my script is stop so decide to run it as a service and if service stops it should start again. 

sudo touch /etc/init/service_name.conf

Add these line to the file

description "test service by sachin choudhary"
author "sachin choudhary "


# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [016]

# Automatically restart process if crashed
respawn

# Essentially lets upstart know the process will detach itself to the background
# This option does not seem to be of great importance, so it does not need to be set.
#expect fork

# Specify working directory , where your script file
chdir /home/sachin/Desktop/service

# Specify the process/command to start, e.g.
exec /home/sachin/.rvm/rubies/ruby-2.1.5/bin/ruby test.rb


And you all done :-)

NOTE:- For Ruby guys if you are using rvm and your script might require some gems, then your required gems must be installed in rvm global set else your script not load them.

Friday, November 14, 2014

Do maximum use of ri

ri is a tool that ships with Ruby. It's a companion to rdoc, allowing you to 'display information about Ruby classes, modules, and methods' from your console.

It give benefits like:
  • More Accurate results :-
    type ri new
    It will return you the list of new methods in the installed.
  • Interactive response :-
    type ri -i
    it gives you the auto complete feature.
  • Gives you more details about the class.
    start ri -i
    type String
    It will show you the detail structure including parent class, modules and who all extend it.
 If you are using rvm you have to generate ri documentation first by
rvm docs generate all

Friday, September 5, 2014

Git working, staged and commit states

There are 3 different stages in git. Detail about each stage as follows :-
  • Working :- This is the stage where all the changed files persists.
    change a file and type git status command it will show you the file as unstaged.
  • Staged :- This is the stages where the files will be surely committed in the next commit. Change some files and type command git status it will show you files in unstaged area. now type git add --all. This command will change the area of the files from working to staged. you can see by typing command git status.
  • Commit :- This is the area where files are in committed state.

Friday, July 25, 2014

Why we have to apply self to call setter method for ruby class object

class Test
  attr_accessor :name

  def set_name
    name = "some name"
  end
end

obj = Test.new
obj.set_name
obj.name  #=> nil

Why? i am calling the setter method which is already defined by attr_accessor from the method. Here the preferences comes into picture. Inside a method local variable is given more preference then to call the method, Ruby interpreter treats name as local variable. If you apply self before the method name you are specifying the interpreter to call the method.

class Test
  attr_accessor :name

  def set_name
    self.name = "some name"
  end
end

obj = Test.new
obj.set_name
obj.name #=> "some name"

Friday, June 13, 2014

Download file from Amazon S3 without ruby-sdk

Today my project requirement is to download big file from Amazone S3 bucket. I am using the ruby gem (aws-sdk). This gem provide the read method for S3 object. The disadvantage of this method is it loads the entire file in memory, and you will run short of memory. My project deals in big files so this solution doesn't works for me.

After some google i found that Amazone also provide command line utility s3cmd for s3. You can setup your IAM user account by

s3cmd --configure

It will setup Amazon S3


For s3cmd options type

s3cmd --help

Make bucket
      s3cmd mb s3://BUCKET
  Remove bucket
      s3cmd rb s3://BUCKET
  List objects or buckets
      s3cmd ls [s3://BUCKET[/PREFIX]]
  List all object in all buckets
      s3cmd la
  Put file into bucket
      s3cmd put FILE [FILE...] s3://BUCKET[/PREFIX]
  Get file from bucket
      s3cmd get s3://BUCKET/OBJECT LOCAL_FILE
  Delete file from bucket
      s3cmd del s3://BUCKET/OBJECT
  Synchronize a directory tree to S3
      s3cmd sync LOCAL_DIR s3://BUCKET[/PREFIX] or s3://BUCKET[/PREFIX] LOCAL_DIR
  Disk usage by buckets
      s3cmd du [s3://BUCKET[/PREFIX]]
  Get various information about Buckets or Files
      s3cmd info s3://BUCKET[/OBJECT]
  Copy object
      s3cmd cp s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
  Move object
      s3cmd mv s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
  Modify Access control list for Bucket or Files
      s3cmd setacl s3://BUCKET[/OBJECT]
  Enable/disable bucket access logging
      s3cmd accesslog s3://BUCKET
  Sign arbitrary string using the secret key
      s3cmd sign STRING-TO-SIGN
  Fix invalid file names in a bucket
      s3cmd fixbucket s3://BUCKET[/PREFIX]
  Create Website from bucket
      s3cmd ws-create s3://BUCKET
  Delete Website
      s3cmd ws-delete s3://BUCKET
  Info about Website
      s3cmd ws-info s3://BUCKET
  List CloudFront distribution points
      s3cmd cflist
  Display CloudFront distribution point parameters
      s3cmd cfinfo [cf://DIST_ID]
  Create CloudFront distribution point
      s3cmd cfcreate s3://BUCKET
  Delete CloudFront distribution point
      s3cmd cfdelete cf://DIST_ID
  Change CloudFront distribution point parameters
      s3cmd cfmodify cf://DIST_ID
  Display CloudFront invalidation request(s) status
      s3cmd cfinvalinfo cf://DIST_ID[/INVAL_ID]

Tuesday, May 6, 2014

Setup wifi driver for Broadcom (14e4:4365) on ubuntu 14.10

Setup wifi for broadcom(14e4:4365).

First check whether you have broadcom(14e4:4365) chip or not by :-

 lspci -vnn | grep 14e4

you should get the output something like 
07:00.0 Network controller [0280]: Broadcom Corporation BCM43142 802.11b/g/n [14e4:4365] (rev 01)

Now type these and you are done

sudo dpkg-reconfigure network-manager

sudo apt-get install bcmwl-kernel-source

sudo apt-get install firmware-b43-installer



Monday, April 7, 2014

Add field to all the tables using migration

def change
    ActiveRecord::Base.descendants.each do|_model|
      add_column _model.table_name.to_sym, :lock_version, :integer, :default => 0 unless [Model name for which don't want to add the field].include? _model.name
    end
  end

Thursday, March 6, 2014

Setup the Memcached server to accept the connection other than localhost

In Multiserver Architecture sometimes we require to store some information in a centralized location for some time, in this situation Memcached comes in picture. In Ubuntu you can setup the Memcached by

sudo apt-get install memcached

Memcached has its own configuration file (/etc/memcached.conf). Here you find a option -l 127.0.0.1 you can the change it to the ip of the system and thats it all the other machine now can make the connection to Memcached machine.

Thursday, February 6, 2014

Open mysql to accept connection other then localhost

My current project have a multi layer architecture in which mysql is running on a machine and all the other machines have to take connection on that.

By default Mysql allow connection to localhost(127.0.0.1) only. So i need to open for others as well. I need to change the configuration file of mysql

we can found that file at /etc/mysql/my.cnf (in ubuntu)

We need to change a property(bind address)

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address        = 127.0.0.1
  bind-address        = 0.0.0.0


Now restart the Mysql service

sudo service mysql restart

It may be possible that after doing all this you might not be able to login from different machine. For make that working you have to reset the root user password by

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;

Thursday, January 23, 2014

Multiple operation on pdf in ubuntu using pdftk package

Pdftk package provides a lot of options  to work on Pdf, like concatenation, bursting, bursting  selected number of pages, Encryption , Decryption

 Examples:-
     
  • Decrypt a PDF
    pdftk secured.pdf input_pw foopass output unsecured.pdf
  • Encrypt a PDF using 128-bit strength (the default), withhold all per-       missions (the default)
    pdftk 1.pdf output 1.128.pdf owner_pw foopass
  • Same as above, except password 'baz' must also be used to open output       PDF
    pdftk 1.pdf output 1.128.pdf owner_pw foo user_pw baz
  • Join in1.pdf and in2.pdf into a new PDF, out1.pdf
    pdftk in1.pdf in2.pdf cat output out1.pdf
    or (using handles):
    pdftk A=in1.pdf B=in2.pdf cat A B output out1.pdf
    or (using wildcards):
    pdftk *.pdf cat output combined.pdf
  • Remove 'page 13' from in1.pdf to create out1.pdf
    pdftk in.pdf cat 1-12 14-end output out1.pdf
    or:
    pdftk A=in1.pdf cat A1-12 A14-end output out1.pdf
  • Join two files, one of which requires the password 'foopass'. The out-       put is not encrypted.
    pdftk A=secured.pdf 2.pdf input_pw A=foopass cat output 3.pdf
  • Uncompress PDF page streams for editing the PDF in a text editor (e.g.,       vim, emacs)
  • Burst a single PDF document into pages and dump its data to doc_data.txt
    pdftk in.pdf burst
  • Burst a single PDF document into encrypted pages. Allow low-quality printing
    pdftk in.pdf burst owner_pw foopass allow DegradedPrinting
  • Rotate the first PDF page to 90 degrees clockwise
    pdftk in.pdf cat 1E 2-end output out.pdf
  • Rotate an entire PDF document to 180 degrees
    pdftk in.pdf cat 1-endS output out.pdf

Thursday, January 16, 2014

Ruby clone, dup vs Rails deep_dup

In Ruby:

dup and clone only duplicates the main object and leaves the inner object same.

for example

a= {'key1' => 1, 'key2' => {'key3' => 3, 'key4' => 4}}
b = a.dup

a.object_id == b.object_id # false

a['key2'].object_id == b['key2'].object_id #true


This means if i do something like

a['key2']['key5'] = 5

it will also add in b
b['key2']['key5']  # 5

In Rails:

for example

a= {'key1' => 1, 'key2' => {'key3' => 3, 'key4' => 4}}
b = a.deep_dup

a.object_id == b.object_id # false

a['key2'].object_id == b['key2'].object_id #false


This means if i do something like

a['key2']['key5'] = 5

it will not added in b
b['key2']['key5']  # nil



Thursday, January 9, 2014

large parallel network file system using Glusterfs on ubuntu

To set up a high-availability storage with two storage servers (Ubuntu 11.10) that use GlusterFS. Each storage server will be a mirror of the other storage server, and files will be replicated automatically across both storage servers. The client system (Ubuntu 11.10 as well) will be able to access the storage as if it was a local filesystem. GlusterFS is a clustered file-system capable of scaling to several peta-bytes. It aggregates various storage bricks over Infiniband RDMA or TCP/IP interconnect into one large parallel network file system. Storage bricks can be made of any commodity hardware such as x86_64 servers with SATA-II RAID and Infiniband HBA.


1 Preliminary Note

  • server1: IP address 192.168.0.100 (server)
  • server2: IP address 192.168.0.101 (server)
  • client1: IP address 192.168.0.102 (client) 

All three systems should be able to resolve the other systems' hostnames. If this cannot be done through DNS, you should edit the /etc/hosts file so that it looks as follows on all three systems:

vi /etc/hosts
 
127.0.0.1       localhost.localdomain  
192.168.0.100   server1.example.com    
192.168.0.101   server2.example.com    
192.168.0.102   client1.example.com    

2 Setting Up The GlusterFS Server

Setup GlusterFs on both machine. GlusterFS is available as a package for Ubuntu, therefore we can install it as follows:

sudo apt-get install glusterfs-server

test by

glusterfsd --version

make the both servers peer with each other

on server2
sudo gluster peer probe server1

on server 1
sudo gluster peer probe server2

Check the status of peer by
sudo gluster peer  

we create the share named testvol with two replicas (please note that the number of replicas is equal to the number of servers in this case because we want to set up mirroring) on server1 and server2 in the /gluster-fs-directory directory (this will be created if it doesn't exist)
 
sudo gluster volume create testvol replica 2 transport tcp server1:/gluster-fs-directory server2.example.com:/gluster-fs-directory

Start the volume 
sudo gluster volume start testvol

You can check the status of the volume with the command
sudo gluster volume info

By default, all clients can connect to the volume. If you want to grant access to client1= 192.168.0.102) only, run:
sudo gluster volume set testvol auth.allow 192.168.0.102

Please note that it is possible to use wildcards for the IP addresses (like 192.168.*) and that you can specify multiple IP addresses separated by comma (e.g. 192.168.0.102,192.168.0.103). 


Now you are done with glusterfs server