Tech Chorus

PostgreSQL Cheatsheet

written by Sudheer Satyanarayana on 2016-07-25

New Server Setup

service postgresql initdb

Create New User

 CREATE USER username login PASSWORD 'password';

Allow password based login.

Edit /var/lib/pgsql/data/postgresql.conf

  host    all     all     127.0.0.1/32    md5

Create database

 CREATE DATABASE databasename WITH OWNER = username;

Using Sass Version Of Bootstrap With Webpack

written by Sudheer Satyanarayana on 2016-07-21

You must have gone through the webpack tutorial to understand this blog post. If you have done that, using Sass version of Bootstrap is a breeze.

In your project directory install these packages using npm.

npm install bootstrap-loader css-loader node-sass resolve-url-loader sass-loader style-loader url-loader file-loader jquery imports-loader bootstrap-sass --save-dev

Create the file webpack.config.js

'use strict';

var path = require('path');

module.exports = {
    entry: ['bootstrap-loader', './index.js'],
    output: {
        path: 'output',
        filename: 'bundle.bootstrap-sass.js'
    },
    devtool: 'inline-source-map',
    module: {
        loaders: [
            {
                test: /bootstrap-sass\/assets\/javascripts\//, 
                loader: 'imports?jQuery=jquery'
            },
            {
                test: /\.woff2?$|\.ttf$|\.eot$|\.svg$/,
                loader: 'url'
            },
            {
                test: /\.scss$/,
                loader: 'style-loader!css-loader!'
            }
        ],
        resolve: {
            extensions: ['', '.js', '.jsx', '.css', '.scss'],
            modulesDirectories: [
              'node_modules'
            ]
        },
    }
};

index.js: your project entry file.

/* Put whatever you want in your entry JS file */

index.html: Minimalist HTML file to check whether Bootstrap styles are loaded.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Minimalistic webpage</title>

    <script src="output/bundle.bootstrap-sass.js"></script>
  </head>

  <body role="document">
     <button type="button" class="btn btn-primary" aria-label="Left Align"><span class="glyphicon glyphicon-align-left" aria-hidden="true"></span>Some text</button>
  </body>
</html>

Generate the build

webpack

Open index.html in your browser and you should see the web page with Bootstrap styles loaded.

Firewalld Cheatsheet

written by Sudheer Satyanarayana on 2016-07-12

List supported zones:

firewall-cmd --get-zones

List active zones:

firewall-cmd --get-active-zones

List zones with enabled features:

firewall-cmd --list-all-zones

Permanently add source IP address to a zone:

firewall-cmd --permanent --zone=<zone_name> --add-source=<ip_address>

Permanently remove source IP address from a zone:

firewall-cmd --permanent --zone=<zone_name> --remove-source=<ip_address>

Reload firewall:

firewall-cmd --reload

Permanently add a service to zone:

firewall-cmd --permanent --zone=<zone_name> --add-service=<service_name>

Permanently add an interface to a zone:

firewall-cmd --permanent --zone=<zone_name> --add-interface=<interface_name>

Permanently remove an interface from a zone:

firewall-cmd --permanent --zone=<zone_name> --remove-interface=<interface_name>

How To Interchange Values And Keys Of A Dictionary In Python?

written by Sudheer Satyanarayana on 2015-12-09

Here's our sample dictionary:

my_dict = {'game': 'cricket', 'planet': 'earth'} The values() method of the dictionary returns a list of values.

>>> my_dict.values()
['earth', 'cricket']

The keys() method of the dictionary returns a list of keys.

>>> my_dict.keys()
['planet', 'game']

Notice the 1-1 correspondence between the items of the returned lists. This correspondence remains unchanged if the dictionary is unchanged.

To interchange the keys and values all you have to do is zip and generate another dictionary. Let's complete the remaining steps.

zip(my_dict.values(), my_dict.keys())
[('earth', 'planet'), ('cricket', 'game')]

Now, we have a list of tuples. Each tuple contains the value and key pairs. The last step is to create a dictionary using this list. Pass the above list to the built-in function dict().

>>> dict(zip(my_dict.values(), my_dict.keys()))
{'earth': 'planet', 'cricket': 'game'}

Here's a one-liner to summarize the concept.

dict(zip(my_dict.values(), my_dict.keys()))

Github For Mercurial User

written by Sudheer Satyanarayana on 2015-03-27

You are a Mercurial user and have little experience with Github. You are required to publish your Mercurial repository on Github. The good news is, you can do it without parting with Mercurial. There are bridges between Git and Mercurial. The one we are going to talk about today, is hg-git.

Let's start demonstrating our solution.

Step 1: Create a mercurial repository somewhere

I do it on Bitbucket. We'll call this our canonical repository. Clone the repository.

hg clone ssh://hg@bitbucket.org/bngsudheer/mycoolrepo

Step 2:Add some contents to the cloned repository

cd mycoolrepo/
echo "Sudheer Satyanarayana" >> contributors.txt
hg add contributors.txt
hg commit -m "Initial commit with contributors"

Step 3: Push the changes to the canonical Mercurial repository

hg push ssh://hg@bitbucket.org/bngsudheer/mycoolrepo

Step 4: Create the repository on Github

There are several ways to setup the mirror. One way to do it is importing the Mercurial repository to Gitbut. But in this article, we'll directly pull and push to Github repository. While creating the Git repository on Github, I chose the option - "Initialize this repository with a README".

Step 5: Enable the hg-git extension

Edit .hg/hgrc file in your repository and add these:

[extensions]
hggit =

Step 6: Pull from the newly created Git repository

hg pull git+ssh://git@github.com:bngsudheer/mycoolrepo.git

Pulling for the first time from the newly created Git repository will create another head in your Mercurial repository. For example, running 'hg heads' shows:

changeset:   1:5b467a77b006
bookmark:    master
tag:         tip
parent:      -1:000000000000
user:        Sudheer Satyanarayana <sudheer.zzz@sudheer.net>
date:        Fri Mar 27 22:14:32 2015 +0530
summary:     Initial commit

changeset:   0:688bd09fe790
user:        sudheer
date:        Fri Mar 27 22:07:26 2015 +0530
summary:     Initial commit with contributors
The changeset 1:5b467a77b006 is bookmarked as 'master'.

Step 7: Merge the heads

hg merge master
hg commit -m "merging master"

Step 8: Push the changeset to the canonical Mercurial repository

hg push

Move the bookmark 'master' to default and push to Githib.

hg bookmark -r default master
hg push git+ssh://git@github.com:bngsudheer/mycoolrepo.git

Now, you can adopt this method to work on the repository and keep pushing to both the repositories - The canonical Mercurial repository and the Github Git repository.

Work on the repository. Make some commits. Push to the canonical repository Move the bookmark 'master' to default and push to Githib Git repository If you do not move the bookmark, pushing to the Git repository makes no sense because hg-git will attempt to push the changeset the bookmark points to. In other words, the bookmark 'master' needs to be updated every time you want to push to the Git repository. You can automate the process of moving the bookmark to your latest line of work by activating the bookmark.

hg up master

That's it. Now your working method is:

Work on the repository. Make some commits. Push to the canonical repository Push to Githib Git repository Once the bookmark is active, Mercurial moves the bookmark to your latest line of work automatically.

Short repository name: For your convenience, you can give a short name to your Github repository URL. In your .hg/hgrc update the paths secion:

[paths]
default = ssh://hg@bitbucket.org/bngsudheer/mycoolrepo
github = git+ssh://git@github.com:bngsudheer/mycoolrepo.git

Notes: Make sure not to add '.git' directory. Github will not accept the changeset if there is a '.git' directory.

Access Dictionary Keys As Object Attributes

written by Sudheer Satyanarayana on 2015-02-24

You access Python dictionary keys using the syntax:

my_dicy[my_key]

For example:

>>> my_dict = {'food': 'idly'}
>>> my_dict['food']
'idly'

Sometimes, you might want to access the dictionary keys using:

my_dict.my_key

syntax. If you do this is what happens:

>>> my_dict.food
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'food'

How can you solve this? Easy.

 pip install attrdict

How do you use the newly installed package?

>>> from attrdict import AttrDict
>>> my_dict = AttrDict({'food': 'idly'})
>>> my_dict.food
'idly'

How to get Fedora to suspend laptop when lid is closed?

written by Sudheer Satyanarayana on 2015-02-24

Edit the file:

/etc/systemd/logind.conf

Look for the 'HandleLidSwitch' entry. Add the entry if it is not there already. If the entry is already there, change to:

HandleLidSwitch=hibernate

Then restart systemd-logind:

systemctl restart systemd-logind.service

This is tested on Fedora 21.

Pani

written by Sudheer Satyanarayana on 2014-10-21

Pani version 0.1 has been released.

Pani is a glue tool that provides a web interface atop hg-ssh. Through the web interface, you can add, modify and remove packages and assign access to users.

The documentation is also available.

How To Change Hostname In CentOS 7

written by Sudheer Satyanarayana on 2014-10-07

CentOS 7 comes with the hostnamectl utility to read and write hostname.

To view the hostname simply run the command:

hostname

[root@c7p1 ~]# hostnamectl
Static hostname: localhost.localdomain
Icon name: computer
Chassis: n/a
Machine ID: cda43e8062fb4d5881491f41e06a0c0d
Boot ID: 7cb835d3dd64468392e3399d771051ac
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

To set a hostname:

hostnamectl set-hostname mynewhostname

Example:

hostnamectl set-hostname c7p1.v.lab.techchorus.net

How To Setup MySQL Replication

written by Sudheer Satyanarayana on 2013-11-05

In this tutorial, I will provide step by step process to setup MySQL replication. We will create one master and one slave. We will use two CentOS 6 servers - one for master and the other for slave. This following steps have been tested on two virtual machines.

Our master server will have IP address 192.168.122.10. Our slave server will have IP address 192.168.122.12.

You might want to run SELinux in permissive mode.

Perform these steps on the master:

Install MySQL server.

yum install mysql-server -y

Configure binary log and assign server ID to master. In /etc/my.cnf append the following to mysqld section:

log-bin=mysql-bin
server-id=1

Each server will have a unique ID. Our master will have ID 1 and slave 2.

Start the MySQL server.

/etc/init.d/mysqld start

Set up the initial MySQL server settings.

mysql_secure_installation

Create a MySQL user account and provide REPLICATION SLAVE access to it.

mysql> CREATE USER 'repl'@'192.168.122.12' IDENTIFIED BY 'secretpassword';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.122.12';

Be sure to replace 'secretpassword' with the actual password. Also, note that 192.168.122.12 is the IP address of the slave server. repl MySQL user will be able to access the master server only from the IP address 192.168.122.12.

Let us create a sample database, table and a row.

mysql>CREATE DATABASE sample;
mysql>USE sample;
mysql>CREATE TABLE mytable (mycol varchar(255));
mysql>INSERT INTO mytable VALUES ('first entry');

At this point, we will prepare the master server for replication.

First, we lock all the tables.

mysql>FLUSH TABLES WITH READ LOCK;

Then, we note down the binary log file and position.

mysql>  SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     1377 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

The log file is mysql-bin.000003 and position is 1377. The values on your server may vary.

Don't exit the MySQL client. In a separate session/terminal run the following commands.

Stop MySQL server.

/etc/init.d/mysqld stop

Copy the files to the slave server.

rsync --recursive /var/lib/mysql root@192.168.122.12:/var/lib --exclude="mysql/mysql" --exclude='mysql-bin.*' --exclude='auto.cnf' -v

If you use other methods to copy the data, be sure to exclude mysql.bin, auto.cnf and mysql directory placed inside /var/lib/mysql.

auto.cnf was added in MySQL 5.6.

Start the MySQL server.

/etc/init.d/mysqld start

Perform these steps on the slave server.

Install MySQL server.

yum install mysql-server -y

Make sure mysql is the owner of the files.

chown -R mysql.mysql /var/lib/mysql/

Edit /etc/my.cnf and append the following line in mysqld section.

server-id=2

Start the MySQL server.

/etc/init.d/mysqld start

Perform the initial settings.

mysql_secure_installation

Inform the slave how to access the master server.

mysql> CHANGE MASTER TO
        MASTER_HOST='192.168.122.10',
        MASTER_USER='repl',
         MASTER_PASSWORD='secretpassword',
         MASTER_LOG_FILE='mysql-bin.000003',
         MASTER_LOG_POS=1377;

Remember 1377 is the position and mysql-bin.000003 is the log file name after we locked the tables on master and copied the files.

Start the slave.

mysql> START SLAVE;

You can check the slave status using:

mysql> SHOW SLAVE STATUS\G;

Here's a sample output:

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.122.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 214
               Relay_Log_File: mysqld-relay-bin.000003
                Relay_Log_Pos: 359
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 214
              Relay_Log_Space: 660
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
1 row in set (0.00 sec)

ERROR: 
No query specified

The key fields are Slave_IO_State, Slave_IO_Running and Slave_SQL_Running.

Let's see if our sample database has been copied.

mysql> USE sample;
mysql> SELECT * FROM mytable;

On the master run a few SQL statements.

mysql> USE sample;
mysq;> INSERT INTO mytable VALUES ('second entry');

On the slave, verify whether replication is working.

mysql> USE sample;
mysql> SELECT * FROM mytable;

Reference: MySQL reference manual