部署django应用程序最简单,最完整的解决方案是什么?

时间:2014-01-24 00:19:48

标签: python django

我想将django应用部署到远程服务器。在rails中,它们有capistrano来处理安装依赖项,gem更新,git更新,shell命令等。

是否为django构建了与capistrano一样完整且易于使用的内容?

注意:我认为您也可以将capistranodjango一起使用,但python django中是否有专门内置的内容?

关闭?:许多人提出的解决方案被提交给2010年的一个问题。除非你们对解决方案有绝对的信心,否则请不要关闭这个问题。软件不断变化,始终有创新。自2010年以来是否有任何新的/增加的解决方案?

1 个答案:

答案 0 :(得分:0)

使用Fabric将django应用程序部署到远程服务器。 Fabric是一个Python库和命令行工具,用于简化SSH在应用程序部署或系统管理任务中的使用。您可以使用与capistrano gem相同的Fabric。看看这个实时代码。

from fabric.api import *


def dev():
    env.user = "example"
    env.hosts = ["example.com", ]
    env.dev = True
    env.prod = False


def prod():
    env.user = "example"
    env.hosts = ["192.68.1.23", ]
    env.dev = False
    env.prod = True


def start_virtualenv():
    local("workon django_test")

# Local developent
def start_dev_server():
    local("python manage.py runserver_plus --settings django_test.settings.dev")

def start_dev_server_z():
    local("python manage.py runserver_plus --settings django_test.settings.dev 0.0.0.0:9000")

def start_dev_shell():
    local("python manage.py shell --settings django_test.settings.dev")

def start_dev_dbshell():
    local("python manage.py dbshell --settings django_test.settings.dev")

def run_dev_command(command_name=""):
    """Run a command with the settings thing already setup"""
    local("python manage.py %s --settings django_test.settings.dev" % command_name)

# Remote serving
def run_prod_command(command_name=""):
    """ Just run this command on remote server """
    with cd("/srv/www/django_test/app/"):
        with prefix("source /home/user/.virtualenvs/agn/bin/activate"):
            run("python manage.py %s --settings django_test.settings.prod" % command_name)

def restart_prod_server():
    """ Start a gunicorn instance using the supervisor daemon from the server """
    run("sudo supervisorctl restart django_test")

# Deploy and shit
def deploy(commit="true"):
    """
    TODO: there is sure a better way to set that prefix thing
    """
    if commit == "true":
        local("git add .")
        local("git commit -a")
        local("git push")

    with cd("/srv/www/agn/app"):
        run("git pull")

    if env.dev:
        account_name = 'exampledev'
    else:
        account_name = 'user'
    prefix_string = 'source /home/%s/.virtualenvs/django_test/bin/activate' % account_name

    with cd("/srv/www/django_test/app/requirements"):
        with prefix(prefix_string):
            run("pip install -r prod.txt")

    with cd("/srv/www/django_test/app"):
        with prefix(prefix_string):
            run("python manage.py migrate --settings django_test.settings.prod")
            run("python manage.py collectstatic --settings django_test.settings.prod --noinput")

    restart_prod_server()