【问题标题】:Is there anyway to put a django site into maintenance mode using fabric?无论如何使用结构将 django 站点置于维护模式?
【发布时间】:2011-09-12 15:47:15
【问题描述】:

我目前正在使用 MaintenanceModeMiddleware 将我的站点置于维护模式,但这需要您在远程服务器上的 settings.py 文件中进行更改。我想使用结构远程将站点置于维护模式。有没有办法做到这一点?或者有没有更好的方法来做到这一点?谢谢。

[更新]

最后感谢大家的反馈,这就是我所做的,它对我很有用,http://garthhumphreys.com/2011/06/11/painless-django-maintenance-mode-with-fabric/ - 我确实喜欢取消注释行的想法,但如果我要在生产服务器上这样做,我的设置一旦我推出新版本就会被覆盖,因此最终将站点从服务器级别而不是 django 级别置于维护模式会更好,并且至少对我来说确实更容易和灵活:)

【问题讨论】:

  • 这个question 可能是相关的。我对fabric 了解不多,我并不是说听起来粗鲁,但是,使用SSH 和更改一条设置线不是很容易吗?那是一个远程方法
  • 我发现 Garth Humphreys 的方法不是很好,因为浏览器缓存。它会导致以下情况: - 在维护模式期间看到非维护模式页面 - 在不处于维护模式时看到维护模式页面 我会将调整后的解决方案放在单独的答案中。

标签: python django fabric maintenance-mode


【解决方案1】:

我的解决方案:

  1. 创建维护模式模板并通过 urlconf 链接到该模板,因此在访问 /under-maintenance/ 时会显示维护页面。
  2. 然后,配置 apache 以测试是否存在“maintenance-mode-on”文件,如果存在,则执行 302 重定向到维护模式页面的 url。
  3. 如果存在“maintenance-mode-off”文件,配置 apache 以将维护模式 URL 重定向到主页。
  4. 结构脚本,便于在维护模式开启和维护模式关闭之间切换文件。

这是 Apache 配置文件的相关部分:

RewriteEngine On
# If this file (toggle file) exists then put the site into maintenance mode
RewriteCond /path/to/toggle/file/maintenance-mode-on -f
RewriteCond %{REQUEST_URI} !^/static.* 
RewriteCond %{REQUEST_URI} !^/admin.* 
RewriteCond %{REQUEST_URI} !^/under-maintenance/ 
# redirect to the maintenance mode page
RewriteRule ^(.*) /under-maintenance/ [R,L]

#If not under maintenance mode, redirect away from the maintenance page
RewriteCond /path/to/toggle/file/maintenance-mode-off -f
RewriteCond %{REQUEST_URI} ^/under-maintenance/
RewriteRule ^(.*) / [R,L]

然后是fabric脚本的相关部分:

env.var_dir = '/path/to/toggle/file/'

def is_in_mm():
    "Returns whether the site is in maintenance mode"
    return files.exists(os.path.join(env.var_dir, 'maintenance-mode-on'))

@task
def mm_on():
    """Turns on maintenance mode"""
    if not is_in_mm():
        with cd(env.var_dir):
            run('mv maintenance-mode-off maintenance-mode-on')
            utils.fastprint('Turned on maintenance mode.')
    else:
        utils.error('The site is already in maintenance mode!')


@task
def mm_off():
    """Turns off maintenance mode"""
    if is_in_mm():
        with cd(env.var_dir):
            run('mv maintenance-mode-on maintenance-mode-off')
            utils.fastprint('Turned off maintenance mode.')
    else:
        utils.error('The site is not in maintenance mode!')

这很好用,尽管它确实依赖于 Django 在维护模式下处理请求;最好只提供一个静态文件。

【讨论】:

    【解决方案2】:

    有一个fork of django-maintenancemode 允许通过在数据库中设置一个值来打开/关闭维护模式。例如,通过这种方式,您可以创建一个简单的管理命令来切换维护模式并通过结构调用它。我认为它比使用 mod_rewrite 更灵活。

    【讨论】:

    • 那一定意味着通过 django 提供的每个页面都需要对数据库进行额外的访问?
    • 如果网站因数据库工作而处于维护模式怎么办?
    【解决方案3】:

    Fabric 确实有一些命令可以帮助您注释或取消注释fabric.contrib.files 中给定文件中的行。在此处查看文档:http://docs.fabfile.org/en/1.0.1/api/contrib/files.html

    我个人更喜欢在前端代理而不是在 Django 中间件中处理这个问题。我会看看这个问题Show a custom 503 page if upstream is down,它将 Nginx 配置为在上游关闭时使用自定义页面。

    【讨论】:

    • 但是没有 503 的管理员/员工用户仍然可以进入吗?中间件看起来很酷,尤其是对于我们这些倾向于在生产服务器上开发的人。 Nginx 503 在 Django 项目实际关闭时会很好。
    • @j_syk 没有理由不能同时使用两者。如果 Django 服务器关闭,则中间件将无法工作,Nginx 将启动。
    • 感谢大家的反馈,最后这就是我所做的,它对我很有用,garthhumphreys.com/2011/06/11/… - 我确实喜欢取消注释行的想法,但如果我要这样做的话,我的设置在生产服务器上,一旦我推出新版本,它将被覆盖,因此最终将站点从服务器级别而不是 django 级别置于维护模式会更好,并且确实更容易和灵活,至少对我来说:)
    • 对不起@MarkLavin 我没有说谢谢你指出了fabric中的cmets命令,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2019-03-27
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多