Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Tuesday, December 30, 2014

Setting up Push-to-Deploy with git

Requirements:

  • I have a computer at home/office.
  • We have a VPS on web.
  • On VPS we have a location for our website i.e. /var/www/example.com
  • I need a version control system.
  • I don't want to develop and upload changes manually.
  • I want to push a specific branch i.e. www to vps for deployment.
How can I achieve my goal? Find below the answer:

1. Lets create an example directory first.


Lets create a temp directory and then create sub-directories under it.


mahesh@mknsoft.com ~
[06:43:00]⌘  mkdir test && cd test

mahesh@mknsoft.com ~/test
[06:43:14]⌘  mkdir vps dev www


dev = My development computer
vps = Virtual Private Server on Internet
www = i,e, /var/www/example.com/

2. Setup a --bare git repo in vps.



mahesh@mknsoft.com ~/test
[06:43:33]⌘  cd vps

mahesh@mknsoft.com ~/test/vps
[06:45:51]⌘  git init --bare
Initialized empty Git repository in /Users/mahesh/test/vps/


3. Setup git repo in dev, add remote and do initial commit.



mahesh@mknsoft.com (BARE:master) ~/test/vps
[06:45:59]⌘  cd ../dev

mahesh@mknsoft.com ~/test/dev
[06:49:16]⌘  git init
Initialized empty Git repository in /Users/mahesh/test/dev/.git/

mahesh@mknsoft.com (master #) ~/test/dev
[06:49:31]⌘  git remote add origin ../vps

mahesh@mknsoft.com (master #) ~/test/dev
[06:50:15]⌘  echo "Test line 1" >> file.txt

mahesh@mknsoft.com (master #) ~/test/dev
[06:50:40]⌘  git add .

mahesh@mknsoft.com (master #) ~/test/dev
[06:50:50]⌘  git commit -m "Initial commit."
[master (root-commit) 1683234] Initial commit.
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

mahesh@mknsoft.com (master) ~/test/dev
[06:51:07]⌘  git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 231 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../vps
 * [new branch]      master -> master


4. How to publish?


At this point we are still unable to publish anything inside www directory. Now what can I do? Humm..., this is where hooks come to picture. Lets create a hook (post-receive) inside vps to track new pushes.


mahesh@mknsoft.com (master) ~/test/dev
[06:51:21]⌘  cd ../vps/hooks

mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:02:04]⌘  touch post-receive

mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:02:28]⌘  chmod +x post-receive


Lets get inside post-receive and write some code as per requirement.


mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:02:49]⌘  vim post-receive




  1 #!/bin/bash
  2 
  3 read oldrev newrev refname
  4 echo "Old revision  : $oldrev"
  5 echo "New revision  : $newrev"
  6 echo "Reference name: $refname"
  7 
  8 BRANCH=${refname#refs/heads/}
  9 
 10 echo "Pushed branch : $BRANCH"
 11 
 12 if [ $BRANCH = www ]; then
 13         export GIT_WORK_TREE=/Users/mahesh/test/www
 14         git checkout -f www
 15         echo "Published."
 16 else
 17         echo "Push on branch www to publish"
 18 fi


So fare so good. Lets test it out right away.


mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:10:50]⌘  cd ../../dev

mahesh@mknsoft.com (master) ~/test/dev
[07:14:13]⌘  git branch www && git checkout www
Switched to branch 'www'

mahesh@mknsoft.com (www) ~/test/dev
[07:14:44]⌘  git push origin www
Total 0 (delta 0), reused 0 (delta 0)
remote: Old revision  : 0000000000000000000000000000000000000000
remote: New revision  : 168323487f78ac6e03022ed4b456e1c0e95af21b
remote: Reference name: refs/heads/www
remote: Pushed branch : www
remote: Switched to branch 'www'
remote: Published.
To ../vps
 * [new branch]      www -> www

mahesh@mknsoft.com (www) ~/test/dev
[07:15:28]⌘  ls ../www
file.txt

Note: If you push in master branch, new updates will not be published as condition specified in post-receive source code.

Thursday, December 25, 2014

Bash script - utility bin

Bash utility script files for all.
Can be useful for linux based OS users. Below is the location.

https://github.com/t2mahesh/bin

Fork, contribute or comment is welcome. Enjoy :)

Wednesday, October 8, 2014

Start a Simple Web Server from Any Director

If you haven't installed Python yet, do it right away. This thing requires Python and it's available as a module inside Python.

Navigate to the directory using your terminal window where you want the web server to start and issue the following command:

$ python -m SimpleHTTPServer 8080

It will start the web server right away.

Open web browser and goto:
http://localhost:8080

Enjoy :)


Sunday, July 21, 2013

lsb_release error, Ubuntu 12.04

It's a crash because of TeamViewer installation, it has been reported to TeamViewer developer team. Meanwhile a solution is proposed by gerlosgm which works perfectly fine for me.


Thanks for the solution :)


Thursday, February 14, 2013

Linux terminal prompt (PS1)

I wanted to change a little bit on appearance of my terminal prompt. Don't need username and host to see but the path should be clean and clear. Also if there is a big path I find it hard to make commands. So need the path upside and the command just below that. Again some times I go root user, so lets make root user as red and normal user as green. Get my favourite PS1:

For normal user add the following line at the end of /home/<user>/.bashrc
# My custom prompt for normal user
PS1="\[$(tput setaf 2)\]\n[\w]\n# \[$(tput sgr0)\]"

Example output:
[~/path/to/dir]

For root user add the following line at the end of /root/.bashrc
# My custom prompt for root user
PS1="\[$(tput setaf 1)\]\n[\w]\n# \[$(tput sgr0)\]"

Example output:
[/etc/path/to/dir]

How is that :) ?


Tuesday, February 12, 2013

“Vi” Editor

vi filename
vi editor is a default editor of all Unix systems. It has several modes. Make sure that your terminal has correct settings, vt100 emulation works good if you are logged in using pc.
  • i for insert mode.
    • I inserts text at the curson
    • A appends text at the end of the line.
    • a appends text after cursor.
    • O open a new line of text above the curson.
    • o open a new line of text below the curson.
  • : for command mode.
    • <escape> to invoke command mode from insert mode.
    • :!sh to run unix commands.
    • x to delete a single character.
    • dd to delete an entire line
    • ndd to delete n number of lines.
    • d$ to delete from cursor to end of line.
    • yy to copy a line to buffer.
    • P to paste text from buffer.
    • nyy copy n number of lines to buffer.
    • :%s/stringA/stringb /g to replace stringA with stringB in whole file.
    • G to go to last line in file.
    • 1G to go to the first line in file.
    • w to move forward to next word.
    • b to move backwards to next word.
    • $ to move to the end of line.
    • J join a line with the one below it.
  • /string to search string in file.
  • n to search for next occurence of string.
Once you are done typing then to be in command mode where you can write/search/ you need to hit  :w filename to write, and in case you are done writing and want to exit :w! will write and exit.

Vim 7: Turning auto completion on

Omni completion isn't automatically turned on for Vim 7, and it's super useful if you code in HTML, CSS, Javascript, XML, Python etc.

In your vimrc you can add following to turn completion on:

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete
autocmd FileType c set omnifunc=ccomplete#Complete

You complete things with CTRL-X O.


VIM Quick Command Tutorial #2

DESCRIPTION: Marks are useful when you are working in various places of a file or working in various files. You may need to jump to that place quite frequently. At this situation marks just do great.
1. List all marks
 :marks - Shows a list of marks

2. Marked file places, and global file places
 m - start marking
  a-z - for places within file
  A-Z - for places within global

3. Go to specified mark
 g' - start mark selection
  a-z - for places within file
  A-Z - for places within global
 i.e. g'C will go to C marked file place
      g'c will go to c marked place within the file

4. Deleting marks
 :delmarks a b C V - deletes the provided marks i.e. a b C and V
DESCRIPTION: Now about some buffers. These are files loaded in your current session of VIM.
5. List all buffers
 :buffers - to see all buffers list in current session

6. Working with buffer(s)
 :b 1 - b stands for buffer and 1 stands for the buffer number.
  This will load the buffer number 1 to your VIM window
  Alternatively you can give the name of the file and
  press <tab> to crawl all the relative names.

7. Deleting buffer(s)
 :bdelete! N1 N2 ...
 :bdelete! {bufname}
  N1 N2 stands for buffer number to delete
  alternatively you can give the buffer name and press
   <tab> to select the exact file you want to unload from
   buffer list

VIM Quick Command Tutorial #1

There are 3 basic modes in VIM.
a) <Esc> - Normal
b) i - Insert
c) v - Visual
1. Opening Vim
 vim            - open
 vim [filename] - open a file

2. Quit, and save
 :q! - quit without saving
 :q - quit
 :w - write
 :wq or :x - write and quit

3. Cursor moving
  k       up
 h l  left right
  j      down

 ^ - content beginning of the line
 $ - end of line
 w - move by a word
 b - move backward by a ward
 gg - go to the head of the file
 <shift>+g - go to the end of the file
 :number - to line number of 

 { - previous paragraph
 } - next paragraph

4. Editing feature
 i - insert before the cursor
 a - append after the cursor
 o - new line below
 O - new line above

5. Copy(Yank), paste, and delete
 yy - copy the whole line
 yw - copy the word
 y^ - copy to the beginning of line from cursor position
 y$ - copy to the end of the line from cursor position

 p - paste
 np - where n represents number of times to paste

 dd - delete the current line
 dw - delete the current word
 d^ - delete to the beginning of the line from cursor position
 d$ - delete to the end of the line from cursor position

6. Visual mode
 v - select within line
 V - select lines

7. Formating
 < - shift back  > - shift forward
 == auto indent

8. Repeating
 . - repeats the previous command

Format JSON string in VIM

I need to format a JSON string in a better human readable way. How do I do it? It's easy, use the following Vim command:

:%!python -m json.tool

That all :).


Monday, February 11, 2013

Disable Ubuntu One, MySql, Apache services on boot – Ubuntu 12.04

Ubuntu One is great, but when I am on battery power it just drains a lot. So what I need is, disable Ubuntu One service at startup. I can start it any time I like. But the problem is Ubuntu One is a hidden service. So let's unlock all the startup hidden services.

Open your terminal and fire up the following command:
$ sudo sed -i "s/NoDisplay=true/NoDisplay=false/g" /etc/xdg/autostart/*.desktop

Now open your "Startup Applications" GUI and you will be able to see a lot of things including Ubuntu One. Uncheck to make it disabled at startup. Enjoy :)

Use the follwing command to start or stop or to get more help later on for Ubuntu One:
$ u1sdtool --start
$ u1sdtool --quit
$ u1sdtool --help

Two other services I need on my system, but don’t want them to start unless I require them. They are running MySql and Apache.

To stop mysql from starting on boot:

sudo vim /etc/init/mysql.conf
# Comment out the following line
start on runlevel [2345]

To stop apache2 from starting on boot:
sudo update-rc.d -f apache2 remove

To enable apache2 on startup
sudo update-rc.d -f apache2 defaults


Monday, November 5, 2012

Check Linux partitions and free space using terminal

To check list of partitions in partition table:

# fdisk -lu
Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63      208844      104391   83  Linux
/dev/sda2          208845    10474379     5132767+  8e  Linux LVM


To check free space and usage details:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      3.8G  884M  2.7G  25% /
/dev/sda1              99M   13M   82M  13% /boot
tmpfs                 252M     0  252M   0% /dev/shm


Sunday, November 4, 2012

How to remove old kernels from Ubuntu 12.04?

You might have upgraded from a previous Ubuntu release or may be your kernal is new and old ones still residing in your system. Some times we need to free space. Do the following to remove old kernels:

Open terminal and check your current kernel in use:
$ uname -r
NOTE: You must not remove this one.

To list all installed kernels on your system:
$ dpkg –list | grep linux-image

Identify all kernels that are lower than your current kernel and you want to remove. Now issue the following command to remove:
$ sudo apt-get purge linux-image-x.x.x-xx-generic-pae
NOTE: Replace all 'x' acording to your kernel version which you want to remove.

Finally, run grub2:
$ sudo update-grub2

Now reboot your system and you are all done.

Sunday, May 6, 2012

How To Change Mouse Cursor In Ubuntu 10.04/11.04/12.04 When Compiz Is Enabled

This bug is not fixed from Ubuntu 9.10 then fixed in 10.10 and now is back in 11.04. Found it their in latest 12.04 LTS.

The problem is when you change cursor theme it doesn't change if you have compiz turned on, it changes but it is only visible in some applications i.e. firefox.

If compiz is off then cursor theming works fine.

This instructions will fix this bug:
  1. Download cursor pack.
  2. Extract it as root to /usr/share/icons
  3. Edit as root the index.theme file located in usr/share/icons/default
  4. Change the line Inherits= to the name of your new icon theme
[Icon Theme]
Inherites=DMZ-Black

Logout and login back in, cursor will be changed.


Wednesday, January 4, 2012

Transparent individual window

To make individual window transparent on Ubuntu you will need to install transset or transset-df from repository. Note that you have to be running a compositing manager i.e. Compiz for this to work.

To make a window transparent:
$ transset-df -c
Now click on the desired window
This will make your window 75% transparent.
To reset back:
$ transset-df -c -t
Now click on the desired window
You can additionally get more options in the command by invoking -h for help.

Thursday, November 3, 2011

Create Domain(s), Subdomain(s) as Virtual Host on Ubuntu

Step 1:
Open using gedit editor or as u like...
sudo gedit /etc/hosts


Step 2:
Add lines like below:

127.0.0.1         localhost
127.0.0.1         mydomain.dev www.mydomain.dev

Now save the file.


Step 3:
Open...

sudo gedit /etc/apache2/httpd.conf

Add the following peace of code:

NameVirtualHost *:80
<VirtualHost mydomain.dev:80>
     ServerName www.mydomain.dev
     ServerAlias mydomain.dev *.mydomain.dev
     DocumentRoot /home/my_user_dir/www
</VirtualHost>

Save the file and restart apache.
Test using your favorite web browser with your vertual domain link i.e. http://mydomain.dev
To restart apache use the following command sudo /etc/init.d/apache2 {start | stop | restart | status}
If you want the sites to be available and enabled to the world, look the following directories:

/etc/apache2/sites-available

This contains configuration files for sites which are available but not necessarily enabled.

/etc/apache2/sites-enabled

This directory contains site files which are enabled.

Example: www.mydomain.com (file inside /etc/apache2/sites-enabled/)
<VirtualHost *:80>
     ServerAdmin webmaster@mydomain.com
     ServerName  www.mydomain.com
     ServerAlias mydomain.com

     DocumentRoot /home/my_user_dir/www/www.mydomain.com
     <Directory />
          Options FollowSymLinks
          AllowOverride None
     </Directory>
     <Directory /home/my_user_dir/www/www.mydomain.com/>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          allow from all
     </Directory>

     ScriptAlias /cgi-bin/ /home/my_user_dir/www/www.mydomain.com/cgi-bin/
     <Directory "/usr/lib/cgi-bin">
          AllowOverride None
          Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
          Order allow,deny
          Allow from all
     </Directory>

     ErrorLog /home/my_user_dir/www/www.mydomain.com/error.log

     # Possible values include: debug, info, notice, warn, error, crit,
     # alert, emerg.
     LogLevel warn

     CustomLog /home/my_user_dir/www/www.mydomain.com/access.log combined

     Alias /doc/ "/usr/share/doc/"
     <Directory "/usr/share/doc/">
          Options Indexes MultiViews FollowSymLinks
          AllowOverride None
          Order deny,allow
          Deny from all
          Allow from 127.0.0.0/255.0.0.0 ::1/128
     </Directory>

</VirtualHost>

You need to save the file and restart apache2, for the changes to be reflected.

Tuesday, October 18, 2011

Get TextMate Features And Styles In Gedit With GMate

GMate is a collection of plugins, themes/styles and other improvements to get TextMate like features in Gedit.

To install GMate in Ubuntu, use the following commands:

sudo apt-add-repository ppa:ubuntu-on-rails/ppa
sudo apt-get update
sudo apt-get install gedit-gmate

This package will not replace Gedit! It will only add some themes and plugins you can enable/disable from the Gedit preferences.

For more info and other Linux distributions installation instructions, see https://github.com/gmate/gmate#readme


Saturday, August 14, 2010

UNIX Commands - Introduction

The UNIX operating system is made up of three parts; the kernel, the shell and the programs.

The kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.

As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

The shell
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).

The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.

The tcsh shell has certain features to help the user inputting commands.

Filename Completion - By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.
The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.
Files and processes
Everything in UNIX is either a file or a process.
A process is an executing program identified by a unique PID (process identifier).
A file is a collection of data. They are created by users using text editors, running compilers etc.

Examples of files:
  • a document (report, essay etc.)
  • the text of a program written in some high-level programming language
  • instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file);
  • a directory, containing information about its contents, which may be a mixture of other directories (subdirectories) and ordinary files.
The Directory Structure
All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root. Check here.

Starting
To start click on the Terminal icon on your drop-down menu.