Sunday, August 18, 2019

Show GIT branch with colours in bash prompt.

Update the below code in ~/.bashrc file

force_color_prompt=yes
color_prompt=yes

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\] $(parse_git_branch)\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

Comment-out below line if it's uncommented.

#unset color_prompt force_color_prompt

Reload the ~/.bashrc file using below command:

source ~/.bashrc

Friday, August 9, 2019

SonarQube Docker Composer

Prerequisite:

Postgresql - [postgresql-docker-using-docker-compose]

Create a docker-compose.yml file as follows:

version: "3"

services:
  sonarqube:
    image: sonarqube
    expose:
      - 9000
    ports:
      - "127.0.0.1:9000:9000"
    networks:
      - sonarnet
    environment:
      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=sonar
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

networks:
  sonarnet:

volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_bundled-plugins:
  postgresql:
  postgresql_data:

Use below command to start docker compose:

docker-compose up

Go to below URL in your browser:

http://localhost:9000/

Insert below default username and password if required:

Username:  admin
Password: admin

Thursday, July 25, 2019

PostgreSQL docker using Docker Compose

Create a docker-compose.yml file like below:

version: '3'

services:
  db:
    image: postgres:latest
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: 'postgres'
    volumes:
      - database_data:/var/lib/postgresql/data

volumes:
  database_data:
    driver: local

User below command to start docker compose:

docker-compose up

Install pgAdmin client to connect started PostgreSQL docker:







Wednesday, July 24, 2019

Vault Docker CLI commands


  • Create app role:
curl -X POST -H "X-Vault-Token:${vault-token}" -d '{"type":"approle"}' http://127.0.0.1:8200/v1/sys/auth/approle
  • Add policies:
curl -X POST -H "X-Vault-Token:${vault-token}" -d '{"policies":"dev-policy,test-policy"}' http://127.0.0.1:8200/v1/auth/approle/role/testrole
  • Get role id and secret:
curl -X GET -H "X-Vault-Token:${vault-token}" http://127.0.0.1:8200/v1/auth/approle/role/testrole/role-id | jq .

curl -X POST -H "X-Vault-Token:${vault-token}" http://127.0.0.1:8200/v1/auth/approle/role/testrole/secret-id | jq .
  • Get client token:
curl -X POST -d '{"role_id":"${role-id}","secret_id":"${secret-id}"}' http://127.0.0.1:8200/v1/auth/approle/login | jq .
  • Create kv engine mount:
tee payload.json <<EOF
{
  "type": "kv",
  "options": {
    "version": "1"
  }
}
EOF

curl --header "X-Vault-Token:${vault-token}" --request POST --data @payload.json http://127.0.0.1:8200/v1/sys/mounts/secret

  • Store data:
tee payload.json <<EOF
{
  "value": "localhost"
}
EOF

curl --header "X-Vault-Token:${vault-token}" --request POST --data @payload.json http://127.0.0.1:8200/v1/secret/dev/esb.host

tee payload.json <<EOF
{
  "value": "9443"
}
EOF

curl --header "X-Vault-Token:${vault-token}" --request POST --data @payload.json http://127.0.0.1:8200/v1/secret/dev/esb.port


  • Retrieve data:
curl -H "X-Vault-Token:${vault-token}" http://127.0.0.1:8200/v1/secret/dev/esb.host | jq .data.value | sed 's/"//g'

curl -H "X-Vault-Token:${vault-token}" http://127.0.0.1:8200/v1/secret/dev/esb.port | jq .data.value | sed 's/"//g'


Vault Docker setup in local using Docker Compose

1. Create folder called "vault-docker"
2. Create Following file structure inside that folder

3. Inside volume folder:
4. Inside config folder:
5. Create a docker-composer.yml file and fill it like below:

version: '3'
services:
  vault:
    image: vault
    container_name: vault
    ports:
      - "8200:8200"
    restart: always
    volumes:
      - /home/prabod/UOE/vault-docker/volumes/config:/vault/config
      - /home/prabod/UOE/valut-docker/volumes/logs:/vault/logs
      - /home/prabod/UOE/valut-docker/volumes/file:/vault/file
    cap_add:
      - IPC_LOCK
    entrypoint: vault server -config=/vault/config/vault.json

Note: Please update above highlighted path as your.

6. Create a vault.json file and fill it like below:

{
  "backend": {
    "file": {
      "path": "/vault/file"
    }
  },
  "listener": {
    "tcp":{
      "address": "0.0.0.0:8200", # Listener host and port
      "tls_disable": 1
    }
  },
  "ui": true  # Enable UI
}

After creating all files and folders, Open terminal and navigate the location to vault-docker folder and run below command to up docker composer

docker-compose up

After successfully start docker vault, You should be able to login into the vault UI:

http://localhost:8200/ui/



Then create a vault token for you login.


Saturday, April 6, 2019

Singleton Design Pattern

What is Singleton Design Pattern?

A class must ensure that only one single instance should be created and that single object can be used by all other classes.


Advantages:

Save memory because an object is not created each request. Only one instance is using every time.

Usage:

This pattern is mostly using in multi-threaded and database applications. It is using in logging, caching, thread pools, configuration settings, etc.

How to create a singleton design pattern?

To create a singleton class, we need to have static member of class, private constructor and factory method.
  • Static member: It gets memory only once because of static, it contains the instance of the singleton class.
  • Private constructor: It will prevent to instantiate the singleton class from outside the class.
  • Static factory method: This provides access to the singleton object and returns the instance to the caller.

Method 1:

Create the instance of the class at the time of declaring the static member, so the instance of the class is created at the time of class loading.

Method 2:

Create the instance of the class in a synchronized method or synchronized block, so the instance of the class is created when required