简体   繁体   English

将Django应用迁移到多台服务器

[英]Migrate django app to multiple servers

I'd like to migrate a django app to multiple hosts with a fabfile. 我想使用fabfile将Django应用程序迁移到多个主机。 The problem is all hosts connects to same database (in another server) and migrate command runs for each host. 问题是所有主机都连接到同一数据库(在另一台服务器中),并且为每个主机运行migrate命令。

I may select one host as master and run migrate command only from master but wonder if there is more elegant and proper solution for this. 我可能会选择一台主机作为主机,并且仅从主机上运行迁移命令,但想知道是否有更优雅,更合适的解决方案。

fabfile.py fabfile.py

def migrate():
    virtualenv('python manage.py makemigrations')
    virtualenv('python manage.py migrate')

def prod():
    env.user = 'myuser'
    env.hosts = ['X1', 'X2']

You have about three options. 您大约有三个选择。

There is a @runs_once decorator you can use. 您可以使用@runs_once装饰器。 Documented here . 记录在这里 Where you'd just do something like: 您将在哪里做类似的事情:

@runs_once
def migrate():
    virtualenv('python manage.py makemigrations')
    virtualenv('python manage.py migrate')

def prod():
    env.user = 'myuser'
    env.hosts = ['X1', 'X2']``

Called like: 像这样称呼:

$ fab -R myRole migrate update

You can also just apply specific roles to use on said tasks which is shown here : 你也可以申请使用其上显示说任务的特定角色, 在这里

from fabric.api import run, roles

env.roledefs = {
    'db': ['db1'],
    'web': ['web1', 'web2', 'web3'],
}

@roles('db')
def migrate():
    # Database stuff here.
    pass

@roles('web')
def update():
    # Code updates here.
    pass

Called like: 像这样称呼:

$ fab migrate update

And if you'd like to get more fine grained those same functions can be coupled with the execute() function (as is shown in that section's docs) and make a deploy function that calls those other tasks for you. 而且,如果您想获得更细粒度的信息,可以将那些相同的函数与execute()函数结合使用(如该部分的文档所示),并创建一个可以为您调用其他任务的deploy函数。 Looking like this: 看起来像这样:

def deploy():
    execute(migrate)
    execute(update)

Called like: 像这样称呼:

 $ fab deploy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM