GIT – How to push code to multiple remotes simultaneously

Well, if you are looking for the way to push multiple remotes at the same time, you might be aware of git well enough. We call remote repositories as remotes in git. Pushing changes to remotes would be the part of usualy development cycle.

Some times you may need to push changes to  multiple remotes like github and bitbucket etc. To do so, you can follow below given instructions.

List your existing remotes

You can list all avaible remotes using following command

$ git remote -v

If you don’t have already any(other) remote configured. You can do by using git remote

$ git remote add  remote_name  remote_url

Example:
git remote add github   http://github.com/path/to/repo

Usually, we push changes by addressing remote name by default origin something like git push origin. You can configure group multiple remotes and give it a name. So you push to all those remotes by referring that name.

You can add multiple remotes by using either git remote or git config commands or even you can edit config file.

As git can group multiple remotes. You can follow any of the following way to configure multiple remotes to push simultaneously(no need  all)

Add remotes using git remote

You can set multiple remote urls to single remote using git remote

If you don’t have remote named all already create it using git remote add then use git remote set-url –add to add  new url to existing remote  

git remote set-url all –push –add <remote url>
git remote set-url all  –push –add <another repo remote url>
git remote set-url  –add  all   <another>

You can cross check added new remotes using git remote -v

(OR)

Group multiple remotes using git config

Command git config is used to configure git parameters. It will edit the .git/config file as per given input

git config –add remote.all.url  http://domain.com/repo.git
git config –add remote.all.url  ssh://user@host/repos/repo.git

Note with out –add option command will replace existing remote url. You can verify updated conf  at .git/config

(OR)

Edit file .git/config  to add remote and multiple remote urls if you are aware of configuration format.

Now you can push multiple remotes simultaneously by referring remote name which is having multiple remote urls assigned.

git push all master

You can always push to multiple remote repos with out grouping them using formal bash syntax

git push server master && git push github master 

How to check system information on linux command line

There are several commands on Linux to fetch system information like number of cpus, partitions, their type and hardware information. In this article we will see few commands which are helpful to fetch information like mentioned above.

Command lscpu

The command lscpu will give the brief summary about cpu and cores. That is total number of cpus and number of cores per cpu are available on machine. The command output would be something like below describing cups and cores.

 

Command lspci

Command lspci will list all PCI devices  available and recognized by kernel on machine

This command is useful to find available devices on machine and corresponding vendor.

Command procinfo

This command display system statistics gathered from /proc. That is you will get insight overview from file system /proc

This is command is useful to find,

Users, boot up time, load, swap, memory and interrupt etc.

 

Command lsdev

The command lsdev display information about installed hardware.  This command also gathers some information from /proc file system and will give  quick overview of which hardware uses what I/O addresses, what IRQ and DMA channels

Command lsblk

This is useful to list the block devices available. To list type the following,

lsblk

 

Command lsusb

This command will list all available USB ports on machine.

 

Command lshw

The command lshw will list the complete hardware information. It gives pretty big output. Try this out on your linux box to get complete hardware information,

lshw

Command df -ah

This command displays information regarding partitions something like used space, available space and file system type. To do so type following command

df -ah

Where option -a and -h  are to list  all volumes and display usage in human readable format respectively.

Command mount

The command mount is used for both to display mounted volumes information and mount the required volume at specified location.

To display mounted volumes and location, just type the following command

mount

As mentioned this can also be useful to mount paritions, to do so,

mount /dev/sda2  /location/to/mount

Where /dev/sda2  is the partition to mount at specified location

File /proc/interrupts

The /proc/interrupts contains the list of all supported interrupts by this CPU.  This is nothing more than a file having information about interrupts.

 

How does bash fork bomb :(){ :|:& };: work

The fork bomb is kind of DOS (denial-of-service) attack on system. It attacks the system by consuming all resources. It makes use of the fork operation, that is why it is called as fork bomb.

Here is the bash version of fork bomb

:(){ :|:& };:

It is nothing more than bash function definition and calling it recursively to consume all system resources. We can also write this fork bomb in different programming languages like python. It creates endless number of processes to consumes all system resources.

WARNING!   These examples may crash your system. Please don’t try it on production machines or in your libraries or on your machine. If you want to test. Try it executing in Virtual Machine 

….

Once the fork bomb is successfully executed on system it may not be possible to bring system back into normal state with out restarting it.

Break down of bash fork bomb :(){ :|:& };:

Bash fork bomb

:(){ :|:& };:

Here is elaborated  version of bash fork bomb,

Where,

 :()  –  is formal bash function definition. That is, we created a function named as : (colon).  We are calling the same function recursively twice in side function definition.

:|:   –  This function is getting called itself and piping output to another function call using |.  This is programming technique recursion nothing but a process of calling function itself.

& –   This thing will put the process execution to background

;   –   Termination of function definition

:   –    Calling the defined function (:). It initiates the fork bomb. With out this thing fork bomb won’t get started.

Here is human readable version of bash fork bomb. Probably it might make sense,

fbomb(){ fbomb | fbomb &}; fbomb

That is,

This fork bomb is some times used by system administrators to test process limits of user. You can save the system from crashing by setting process limits. These process limits can be changed using command ulimit  or these limits can configured  at /etc/security/limits.conf and PAM.  Process limits can be configured per user also.
If you set process limits to any process, all child processes will inherit those limits.

Well configured system would not go down when fork  bomb is initiated.

To query all existing limit try the following command.

ulimit -a

You can try executing fork bomb by reducing these limits. So, once these limits are hit, kernel will impose a restriction over creating new processes, this is how you can protect the system from crashing  because these kind DOS attacks.

You can run the fork bomb by reducing max user process limits

ulimit -u 100

….

As there is restriction over creating endless processes your system won’t go down or no need to restart to resume normal operation.

 

How to Interactively discard selected changes in git

Question: How to interactively discard the selected changes in git ?
Answer:
git is offering us the way to discard selected changes interactively. Here is how you will do it,

To checkout or discard selected changes from all modified files

git checkout -p  

If you want to checkout or discard selected changes from specific file, give that file as an argument

git checkout -p  file_name

As per this command git will break down the file into chunks called hunks. After you execute this command, you will be prompted with following question with several options to choose.

Discard this hunk from worktree [y,n,q,a,d,/,e,?]?

Here is the description of each option,

y – discard this hunk from worktree
n – do not discard this hunk from worktree
q – quit; do not discard this hunk nor any of the remaining ones
a – discard this hunk and all later hunks in the file
d – do not discard this hunk nor any of the later hunks in the file
g – select a hunk to go to
/ – search for a hunk matching the given regex
j – leave this hunk undecided, see next undecided hunk
J – leave this hunk undecided, see next hunk
k – leave this hunk undecided, see previous undecided hunk
K – leave this hunk undecided, see previous hunk
s – split the current hunk into smaller hunks
e – manually edit the current hunk
? – print help

You can also unstage the selected changes from all staged files or from specific file.

To unstage selected changes from all staged files use following command

git reset HEAD -p

To unstage changes from specific file give that file as an argument.

git reset HEAD -p  file_name

Here also you will be prompted to choose any one of the option to unstage hunk.

 

How to create read only attributes and restrict setting attribute values on object in python

There are different way to prevent setting attributes and make attributes read only on object in python. We can use any one of the following way to  make attributes readonly

  1. Property Descriptor
  2. Using descriptor methods __get__ and __set__
  3. Using slots  (only restricts setting arbitary attributes)

Property Descriptor

Python ships with built in function called property. We can use this function to customize the way attributes be accessed and assigned.

First I will explain you about property before I get you idea about how it is useful to make attribute readonly.

Typical signature of the function property is

 property([fget[, fset[, fdel[, doc]]]]

As you can see here this function take four arguments, those are

fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute value. And doc creates a docstring for the attribute.

All these function are for the sake of single attribute. That is fget function will be called when you access/get the attribute. fset function will be called when you are trying to set the attribute.

Simple example

Instantiate Foo and try to play the instance attribute x

I hope, you got what exactly the function property is and how we use it. In many cases we use this property to hide actual attributes and abstract them with another name.

You can use property as decorator also. Something like

Now let’s come to actual thing how we make attribute readonly.

It’s simple you just don’t define setter for the property attribute. Let’s see the following example

Here, as we didn’t define setter for the property attribute. So python won’t allow setting that specific attribute even you can’t delete if you don’t define fdel. Thus, attribute becomes read only. Still you can access b._money  and you can set that attribute there is no restriction over setting this internal attribute.

Descriptor methods __get__ and __set__

These magic methods define descriptor for the object attribute. To get complete understanding and usage about descriptor magic methods, please check other article .

Like fget and fset functions that property function takes, __get__ is used to define behavior when descriptor’s value is retrieved. __set__ method is used to define behavior  when descriptor value is getting set(assigned). Where __delete__ is used to define behavior when descriptor is getting deleted.

To restrict setting attribute and make it readonly. You have to use __set__ magic method of descriptor and raise exception in it.

Let’s see the simple example demonstrating descriptor object and readonly attributes using descriptors

Lets see the result and trying to set speed attribute

As you can see here, we can’t set the attribute speed on instance v of Vehicle. Because we are restricting it in descriptor method __set__ of class Speed

Python __slots__

The basic usage of __slots__ is to save space in objects. Instead of having a dynamic dict that allows adding attributes to objects at anytime, there is a static structure which does not allow additions after creation. This will also gain us some performance due to lack of dynamic  attribute assignment. That is, it saves the overhead of one dict for every object that uses slots.

Think of you are creating lot of (hundreds, thousands) instances from the same class, this could be useful as memory and performance optimization tool.

If you are using __slots__ means you are defining static attributes on class. This is how we save memory and gain performance as there is not dynamic attribute assignment. Thus you can’t set new attributes on object.

You see, in the above example we are not able to set attribute c as it not given in __sots__. Any way it’s about restricting  assignment to new attributes and you can combine either above two methods to make existing attributes readonly.

 

References:

[1] __get__ and __set__ data descriptors don’t work on instance attributes http://stackoverflow.com/questions/23309698/why-is-the-descriptor-not-getting-called-when-defined-as-instance-attribute

[2] http://stackoverflow.com/questions/472000/usage-of-slots

How to check memory usage command line on linux

Linux is having different set of utility command line tools, one of them is free. We use it determine used physical memory, swapped memory and available memory. That is, you can get RAM statistics using this command.

Display memory (RAM) information

To display physical memory information just type free

But it is less readable. You can request command free to display memory in mega bytes or kilo bytes.

free takes several arguments. We can use them to format and display output as we need.

Typical usage of command free

 

To check memory statistics periodically at regular intervals use,

Here command,

free -k -s 1

displays memory in kilo bytes for every 1 second. To monitory memory usage in real time you better use top or any other monitoring tools available on linux rather than this command.

If you want to check memory consumed by specific process, you can use the command pmap

Command pmap

Command pmapsynopsis

 

How to write and configure startup or init script using linux command chkconfig

If you restart you server and you want your whole stack up and running, you need to configure start or init script. It’s just a simple shell script with some extra headers that the command chkconfig can understand. The approach of adding stratup script is same for most of the distribution with little bit variation in syntax or flow. In the fedora or redhat linux distribution we use the command chkconfig to add startup or init script.

First you need to understand the run levels to understand how linux initiate the start up script

Run Levels

There are different run levels that init script runs as configured, those run levels are described below. Each run level is a state of machine, like 0 describes the shutdown and 6 describes the reboot. This configuration slightly varies between linux distributions. There are different stats assigned from 0 to 6. That is total 7 run levels.

After the Linux kernel has booted, the /sbin/init program reads the/etc/inittab file to determine the behavior for each runlevel. Unless the user specifies another value as a kernel boot parameter, the system will attempt to enter (start) the default runlevel.

levelDescription

0 Halt
1 Single User Text Mode
2 Not Used (User Definable)
3 Full Multi User Text Mode
4 Not Used (User Definable)
5 Full Multi User Graphical Mode
6 Reboot

Little bit more descriptive. Modes may vary little bit as distribution to distribution.

ID Name Description
0 Halt Shuts down the system.
1 Single-user mode Mode for administrative tasks.
2 Multi-user mode Does not configure network interfaces and does not export networks services.
3 Multi-user mode with networking Starts the system normally.
4 Not used/user-definable For special purposes.
5 Start the system normally with appropriate display manager (with GUI) Same as runlevel 3 + display manager.
6 Reboot Reboots the system.

As I said, it’s just a shell script with some extra comments that chkconfig program can understand. Those comments look like

Chkconfig Configuration

Checkconfig is the tool where we can configure our startup script in different  runlevels. This command can also be used to check currently active init scripts

Install chkconfig

If the command chkconfig is not available, it can be installed using package manager (yum on centos)

List Configured Startup Services

Out put describes on which level that corresponding script is set to run.

You can query the specific service by passing that service name as argument

 

….

To configure shell script as a init script using chkconfig, we should keep the following comments in  the file. Usually we put them after shabang line. Formally the chkconfig config/comments would look like. These commands would instruct the chkconfig about init script configuration.
init script are usally located in location /etc/init.d

Enable Service

You can enable the service to run in runlevels 2, 3, 4 and 5  by using following command

# chkconfig service_name on

Let’s say if you want enable httpd service to run on startup

# chkconfig httpd on 

If you want to enable service in particular run level pass the option –level to this command followed by run levels from 0 to 6. Something like,

# chkconfig service_name on –level runlevels

For instance, to enable httpd service in runlevels 2, 3 and 4 by specifying runlevels explicitly

# chkconfig httpd on –level 234

Disable Service

To disable a service in all runlevels (2,3,4 and 5)

# chkconfig service_name  off

Let’s say if you want to disable httpd service in all runleves,

# chkconfig httpd off

You can specify the run explicitly with option –level to  disable those specific runlevels

# chkconfig  service_name off –level runlevels

For instance, to disable httpd in runlevels 2  and 3,

# chkconfig httpd off –level 23

REFERENCES

[1] https://www.redhat.com/archives/psyche-list/2002-December/msg01555.html

 

How to enable gzip compression in apache

What is gzip compression? Why do we need to enable it?

Gzip is the file format and method of compression and decompression of files using it’s algorithm to reduce the file size. It is used in web server, where web servers  send data to http client with gzip compression for faster data transfer and low bandwidth consumption.

Enabling gzip compression is the best practice and it is highly recommended, so pages are likely to load faster in browsers.

Enable gzip compression to improve website speed
Enable gzip compression to improve website speed

How to check gzip compression enabled or not

You can check if gzip compression is enabled or not on a particular website using following methods.

You can use the google PageSeep Insights tool not only to check gzip compression but also to analyze performance of a website.

You can also check if gzip compression is  enabled on server or not using command line tool curl on linux. Try out the following,

Replace the URL with what ever the website that you want to check for gzip compression.

Where,

-I   means, only make HEAD request to server to get headers

-H    specify the accepted encoding gzip using header ‘Accept-Encoding’

If you see header Content-Encoding with gzip in response headers, then compression is enabled on server and it’s working

It works in such a way that, client would specify it’s supported compression and encoding using header Accept-Encoding. Server will use compression if it is enabled on server else will send plain text back to client. Server will notify encoding format and compression to client through header Content-Encoding in response headers.

Here is the curl request on google.co.in to verify gzip compression

We can see that, the header Content-Encoding: gzip in response headers. Means gzip compression is enabled.

Enable gzip compression in apache

The gzip compression can be enabled  by directly changing httpd conf file. That is httpd.conf  or you can use .htaccess file to target only specific directory or path or site.

Add the following code to  /etc/httpd/conf/httpd.conf    if apache is installed somewhere add the that specific httpd.conf file

To enable gzip compression via .htaccess, put the following code into .htaccess file which  in the desired site directory.

Restart the apache. That’s it, apache will compress the response which is being sent to client. Enabling gzip compression absolutely would boost your site performance.

You can test if gzip compression is enabled or not using curl as mentioned in this article.

The gzip compression is recommended on all types of text files including following files and extensions

.html
.php
.css
.js
.txt
.json

But, gzip compression is not recommended on graphic files or .zip (in the sense files which are compressed already). Because by compressing these kind of files we hardly save few bytes. Thus increase in loading time because of added unaffected compression task.

Why GZIP compression ?

As explained, gzip compression saves the lot of bandwidth by reducing file size. It saves up to 50% to 80% of bandwidth. So, it reduces download and waiting time of browser for resources. If you enable gzip compression, it won’t affect unsupported browsers, where they can fallback to normal(no compression) data download.

 

Summary

GZIP compression is process of zipping or compressing files on server before they get delivered or transferred via network to browser. Browser will uncompress the data before it uses. As it saves 50% to 80% of bandwidth, if it enabled performance of  website will increase considerably.

User Account Management In MySQL

You need to have a user account to execute queries on data. When you install mysql you will prompted to enter root user details or some times you might have installed mysql with root user and no password. It is recommended to have a password for root user after completion of installation and it is best practice to use different set of mysql users to serve different purposes instead of root user to mitigate security vulnerabilities.

To execute any of the following instructions to create and edit user you have to connect with mysql server using mysql client.

mysql -u root -p

Assuming your mysql server is on localhost else specify server with option -h

Create User

To create user we use the statement CREAT USER.  Find the definitive syntax here

mysql>  CREATE USER ‘Bob’@’localhost’ IDENTIFIED BY ‘password‘;

In mysql every user is associated with host. Here it is localhost specified in the statement above. It meas user Bob can only login from host localhost(same machine)

After your create user, he will be assigned the default permissions. To let him access specific database or table you have to grant him the permissions. To do so, we use the statement GRANT

To grant all permissions on all database,

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘Bob’@’localhost’;

To grant all permissions on specific database,

mysql> GRANT ALL PRIVILEGES ON db_name.* TO ‘Bob’@’localhost’;

Once you are done with creating user. Reload privileges using

mysql> flush privileges;

This command will reload users and their privileges.

Change User Password

User can be altered using the statement ALTER USER. To change the password of an user, use the following statement

mysql> ALTER USER ‘Bob’@’localhost’ IDENTIFIED BY ‘password’;

You can change your own password by following statment,

mysql> ALTER USER USER() IDENTIFIED BY ‘password’;

You can also change the password of an user using following two statements as well,

SET PASSWORD
GRANT

Change password using SET PASSWORD

mysql> SET PASSWORD FOR ‘Bob’@’localhost’ =’password’;

Note: Before MySQL 5.7.6 you have to give hash to the statement SET PASSWORD using password function.

Change password using GRANT

mysql> GRANT ALL PRIVILEGES ON  *.* TO ‘Bob’@’localhost’ IDENTIFIED BY ‘new_password’;

List All Users

Following query will list all available users

mysql> select User, Host from mysql.user;

Change host associated with user account

You can change host of any user by updating the Host column of table mysql.user for that user. With out having permissions to update this table you can’t change the host of any user

mysql> update mysql.user set Host=’host’ where Host=’old_host’;

Delete User

To delete an user, we use the statement DROP USER.

mysql> DROP USER ‘Bob’@’localhost’; 

 

Python: __new__ magic method explained

Python is Object oriented language, every thing is an object in python. Python is having special type of  methods called magic methods named with preceded and trailing double underscores.

When we talk about magic method __new__ we also need to talk about __init__

These methods will be called when you instantiate(The process of creating instance from class is called instantiation). That is when you create instance. The magic method __new__ will be called when instance is being created. Using this method you can customize the instance creation. This is only the method which will be called first then __init__ will be called to initialize instance when you are creating instance.

Method __new__ will take class reference as the first argument followed by arguments which are passed to constructor(Arguments passed to call of class to create instance). Method __new__ is responsible to create instance, so you can use this method to customize object creation. Typically method __new__ will return the created instance object reference. Method __init__ will be called once __new__ method completed execution.

You can create new instance of the class by invoking the superclass’s __new__ method using super. Something like super(currentclass, cls).__new__(cls, [,….])

Usual class declaration and instantiation

A class implementation with __new__ method overridden

OutPut:

Note:

You can create instance inside __new__  method either by using super function or by directly calling __new__ method over object  Where if parent class is object. That is,

instance = super(MyClass, cls).__new__(cls, *args, **kwargs)

or

instance = object.__new__(cls, *args, **kwargs)

Things to remember

If __new__ return instance of  it’s own class, then the __init__ method of newly created instance will be invoked with instance as first (like __init__(self, [, ….]) argument following by arguments passed to __new__ or call of class.  So, __init__ will called implicitly.

If __new__ method return something else other than instance of class,  then instances __init__ method will not be invoked. In this case you have to call __init__ method yourself.

Applications

Usually it’s uncommon to override __new__ method, but some times it is required if you are writing APIs or customizing class or instance creation or abstracting something using classes.

Singleton using __new__

You can implement the singleton design pattern using __new__ method. Where singleton class is a class that can only have one object. That is, instance of class.

Here is how you can restrict creating more than one instance by overriding __new__

It is not limited to singleton. You can also impose limit on total number created instances

 

Customize Instance Object

You can customize the instance created and make some operations over it before initializer __init__  being called.Also you can impose restriction on instance creation based on some constraints

 

Customize Returned Object

Usually when you instantiate class it will return the instance of that class.You can customize this behaviour and you can return some random object you want.

Following  one is simple example to demonstrate that returning random object other than class instance

Output:

Here you can see when we instantiate class it returns  3 instead of instance reference. Because we are returning 3 instead of created instance from __new__ method. We are calling __init__ explicitly.  As I mentioned above, we have to call __init__ explicitly if we are not returning instance object from __new__ method.

The __new__ method is also used in conjunction with meta classes to customize class creation

Conclusion

There are many possibilities on how you can use this feature.  Mostly it is not always required to override __new__ method unless you are doing something regarding instance creation.

Simplicity is better than complexity. Try to make life easier use this method only if it is necessary to use.