Category Archives: Python

Fix yum error TypeError: can’t multiply sequence by non-int of type ‘float’

I ran into this bug which prevented me to update yum packages, and after a quick search, this helped to fix it:

# vim /usr/lib/python2.7/site-packages/urlgrabber/grabber.py

Go to line 1539, with vim you can use :1539

Replace the line with this:

if cur > (float(max_size)*1.10):

 

How to deploy django with nginx + gunicorn

Django is a great web framework and we have several ways to deploy it in production.

You can use apache too for example, but for django my favorite is nginx + gunicorn.

Nginx is a well-known player and it will be used as front-end. Gunicorn is not as well-known, but it has proved to be quite stable (instagram and pinterest use it).

Installation:

yum install nginx
pip install gunicorn

Previously you have to have installed python pip and epel repository.

Next step is to run gunicorn, which is quite easy. As we are going to run it with a django project, it is possible to use gunicorn_django 

cd /path/to/your/django/project
gunicorn_django --workers=4 -b 127.0.0.1:8888 -D

And that is it. We have told gunicorn to run with 4 workers (processes), to listen on localhost:8888 and to run in background with -D.

Last step is to configure a server for nginx:

upstream app_server_djangoapp {
    server localhost:8888 fail_timeout=0;
}

server {
        listen 80;
        server_name  server.com;

        access_log  /path/log/access.log;
        error_log  /path/log/error.log info;

    keepalive_timeout 5;

    # path for static folder
    location /static {
        root /path/to/static/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://app_server_djangoapp;
            break;
        }
    }
}

Restart nginx:

service nginx restart

Our django site is deployed, as easy as that.

Python script to backup and restore rpm packages

I’ve written a simple Python script to backup and restore installed rpm packages.

On Ubuntu, I used APTonCD for this matter, but in this case, for Fedora I decided to use my own script.

The usage is quite simple, only two parameters:

Backup:

python rpm-backup.py --backup

Restore:

python rpm-backup.py --restore

 

This is source code, although you can download it from my github (link to the gh-page of the entire repository)

#!/usr/bin/python

''' Backup script for installed packages.
Only RPM.
Tested under Fedora
Error list:
Code 1: Missing modules
Code 2: Not root
Code 3: Tried to restore without backup file'''

print "     ____  ____  __  ___"
print "    / __ \/ __ \/  |/  /"
print "   / /_/ / /_/ / /|_/ / "
print "  / _, _/ ____/ /  / /  "
print " /_/ |_/_/   /_/  /_/   "


print "        ____  ___   ________ ____  ______ "
print "       / __ )/   | / ____/ //_/ / / / __ \ "
print "      / __  / /| |/ /   / ,< / / / / /_/ /"
print "     / /_/ / ___ / /___/ /| / /_/ / ____/ "
print "    /_____/_/  |_\____/_/ |_\____/_/      "

#Import modules
try:
    import os
    import argparse
except ImportError:
    print "Could not find required modules. Exiting..."
    exit(1)

# Create parser
parser = argparse.ArgumentParser(description='Backup your installed packages.', epilog="Written by Adrian Espinosa (aesptux).")


# if user types --backup it will be stored as true in variable backup
#if user types --restore it will be stored as true in variable backup
parser.add_argument('--backup', action='store_true', dest='backup', help='Backup your packages.')
parser.add_argument('--restore',  action='store_true', dest='restore', help='Restore your packages.')


args = parser.parse_args()
#print args
#print(args.option(args.integers))

#variables
userlogged = os.environ['LOGNAME']
directory = '/%s' % (userlogged)
filename = 'packages-installed.bak'
path = directory + '/' + filename
rpm_list = []
print " "
print "Hello %s!" % (userlogged)
print "If you are having troubles using the script, use the argument '--help'"


def dobackup():

    ''' Create backup '''
    print "Starting copy..."
    command = 'rpm -qa > ' + path
    #os.system('touch '+path)
    os.system(command)
    os.system('echo Total packages: ; cat ' + path + ' | wc -l ')
    print "Done."


def dorestore():

    ''' Restore packages'''
    print "Starting restore..."
    global rpm_list
    # try, except, to see if the backup file exists
    try:
        for line in open(path, 'r'):
            rpm_list.append(line)
    except IOError:
        print "The backup file does not exist. Please, create a backup first. "
        exit(3)
    # take the list and join it
    rpm_list = ''.join(rpm_list)
    # remove \n
    rpm_list = rpm_list.replace('\n', ' ')
    os.system('yum install ' + rpm_list)


#This script must be run as root
if userlogged != 'root':
    print "You have to run the script as root"
    exit(2)


# if backup is true, dobackup. Else if restore is true, dorestore
if args.backup == True:
    dobackup()
elif args.restore == True:
    dorestore()

 

Then, you can set up a cron job like this:

0 22 * * * /usr/bin/python /root/rpm-backup.py --backup