Posts

Deploy Python Application with Gunicorn and Supervisor on Ubuntu

Suppose we have to deploy 3 python applications on a Ubuntu server. The applications and their python versions are as follows: 1. Analytics application (analytics.example.com): python2 2. Android API (android.example.com): python3.5 3. Auth API (auth.example.com): python3.6 What will be the solution? How can we run 3 different python applications on 3 different python versions on same machine? Here is a simple solution to achieve this goal. I'm assuming you know about python virtual environments and wsgi. If not Google will help you on this. The following steps should work with most of Ubuntu versions. Here are our plans to achieve our goal: 1. We'll create 3 separate virtual environment to run these applications 2. We'll use gunicorn inside these virtual environments to run these applications on 3 different ports e.g. 8001, 8002, 8003 3. We'll use supervisor to start, stop and monitor these 3 applications running on gunicorn Lets assume we've the co...

Get commits between two tags in Git

We often need to get commits between two git tags. For example, we want to prepare a release note. So we want to get the commit messages between the two release tags. Here is the command to do this: git log --pretty=oneline tagA...tagB # three dots Example: git log --pretty=oneline v3.9.0...v3.9.1 Source:  https://stackoverflow.com/questions/5863426/get-commit-list-between-tags-in-git

Installing Python 2.7.13 on CentOS 6.5

This post describes how to install python 2.7 from source without affecting the existing python installation. It'll install the python2.7 in a separate location other than the standard location in CentOS. Install required packages:  yum -y update yum groupinstall -y 'development tools' yum install -y gcc zlib-devel bzip2-devel openssl-devel xz-libs wget Download and extract python source: wget http://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz  xz -d Python-2.7.13.tar.xz  tar -xvf Python-2.7.13.tar Create a directory where to install python e.g. ~/localpython: mkdir /home/nayan/localpython Go to extracted python source directory: cd Python-2.7.13 Run the configure: ./configure --prefix=/home/nayan/localpython --enable-shared Compile and install it:  make make altinstall Check if python installed by running python2.7 Make a link to Python library path: On 32 bit OS ln -s /home/nayan/localpython/lib/libpython2.7.so.1...

Generate model class in peewee

peewee is a very lighweight but rich orm for python. Often we need to generate model classes from an existing database table. It's possible to do this in peewee using an addon pwiz model generator . After installing pwiz, use the following command to generate model classes from an existing database. python -m pwiz -e mysql -u root -H localhost -P pesp_db > models.py Here is a brief description of the options: -e : name of the database engine e.g. mysql, postgresql -u : name of db user -H: name of db host The last parameter pesp_db in this case is the name of the database schema. If you run this command, you'll be prompted to provide your password. After providing the password, the model classes will be generated and written in the models.py

Generate a Excel file from Spring

We often need to generate a spreadsheet file from server and allow users to download the file. Here is how to do that with Spring using the Apache POI library. Dependency: We need to add the following dependencies in our spring application. Here is the Gradle dependencies. compile group: 'org.apache.poi', name: 'poi', version: '3.15' compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15' Once we add the dependencies we'll be able to access the POI library in our application. Generate WorkBook We need to generate a Workbook object in POI. Here is a sample method that returns a Workbook object public Workbook downloadPersonList() throws IOException {     // sample list. this can be the output of a DB query     List<Person> personList = new ArrayList<>();       personList.add(new Person("nayan", 29));     personList.add(new Person("rafiq", 35)) ;     personList.add(ne...

Print a progress in Python

Here is how to do this: import sys sys.stdout.write("\r%d" % i) sys.stdout.flush() Writing '\r' will move the cursor back to the beginning of the line. If we want to print a %, we need to print an additional % like the folllowing: sys.stdout.write("\r%d%%" % i)

Automatic MongoDB backup in windows

It's a very good idea to take backups of our MongoDB periodically. We can use the following script to create a folder with current date time and dump the database in it. The script also runs 7zip and compresses the backup directory. When compression is done, this script will delete the backup directory. This will save a lot of disk space. @echo OFF :: This will create a timestamp like yyyy-mm-dd-hh-mm-ss. set BACKUPNAME=E:\mongo-db-backup set BACKUPNAME=%BACKUPNAME%\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2% @echo BACKUPNAME=%BACKUPNAME% :: Create a new directory md "%BACKUPNAME%" echo Running backup "%BACKUPNAME%" mongodump -h localhost -d stipend_icr -u stipend_icr_user -p pr0g0t1 -o "%BACKUPNAME%" REM ZIP the backup directory echo Running 7zip on backup "%BACKUPNAME%" "C:\Program Files\7-Zip\7z.exe" a -tzip "%BACKUPNAME%.zip" "%BACKUPNAME%" REM Delete the back...