Python – Unable to start gunicorn with fabric

Unable to start gunicorn with fabric… here is a solution to the problem.

Unable to start gunicorn with fabric

My code is in /home/ubuntu/api on the remote server. The WSGI object is named app, and it exists in /home/ubuntu/api/api.py. My gunicorn conf has a filename of gunicorn.conf.py and exists in /home/ubuntu/api

My gunicorn.conf.py

import multiprocessing

bind = "127.0.0.1:8000"
workers = multiprocessing.cpu_count() * 2 + 1
backlog = 2048
worker_class = 'gevent'
daemon = True
debug = True
loglevel = 'debug'
accesslog = '/mnt/log/gunicorn_access.log'
errorlog = '/mnt/log/gunicorn_error.log'
max_requests = 1000
graceful_timeout = 20

I’m trying to remotely launch Gunicorn on a server via Fabric. My Fabric code looks like this

@roles('stag_api')
def run_server():
    with cd('/home/ubuntu/api'):
        sudo('gunicorn -c gunicorn.conf.py api:app')

Now the fabric shows no errors, but gunicorn does not start.

So I created __init__.py in /home/ubuntu/api to make it a package. I wrote this in the __init__.py file

from api import app

This makes WSGI app available in the package’s namespace. Then I changed my Fabric code to this

@roles('stag_api')
def run_server():
    sudo('gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app')

Even though the fabric doesn’t show any errors now, gunicorn doesn’t start.

So I created a shell script called server, and its code looks like this

if [ "$1" = "start" ]; then
        echo 'starting'
        gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi

if [ "$1" = "stop" ]; then
        echo 'stopping'
        pkill gunicorn
fi

if [ "$1" = "restart" ]; then
        echo 'restarting'
        pkill gunicorn
        gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi

I put this shell script in /home/ubuntu/api

Now my Fabric code looks like this

@roles('stag_api')
def stag_server(action='restart'):
    if action not in ['start', 'stop', 'restart']:
        print 'not a valid action. valid actions are start, stop and restart'
        return
    sudo('./server %s' % action)

Now, when I try to start the server via fabric, it prints starting so the shell script is executing and reaches the if block, but I still can’t start the server over fabric.

But if I connect to the server via ssh and do sudo ./server start, gunicorn starts.

Can anyone explain what I did wrong?

Solution

Related Problems and Solutions