lovesh
lovesh

Reputation: 5411

unable to start gunicorn with fabric

My code lies in /home/ubuntu/api in the remote server. The WSGI object is named app and its present in /home/ubuntu/api/api.py. My gunicorn conf file is called gunicorn.conf.py and is present 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 am trying to start gunicorn on a server remotely through 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 fabric does not show any error but the 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 the 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 now fabric does not show any error but the gunicorn does not 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 place 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 through fabric it print starting so the shell script is executing and the if block is reached but still i am not able to start the server through fabric.

But if i ssh to the server and do sudo ./server start, gunicorn starts.

Can someone explain what am i doing wrong?

Upvotes: 3

Views: 1011

Answers (2)

juan Isaza
juan Isaza

Reputation: 3987

Try setting pty False globally with:

from fabric.api import env

env.always_use_pty = False

Or setting pty False for that command with:

run('run unicorn command etc...' pty=False)

See Init script section in:

http://www.fabfile.org/faq.html#init-scripts-don-t-work

Upvotes: 1

Related Questions