有没有办法使用 Fabric 将 django 站点置于维护模式?

发布于 2024-11-14 15:47:55 字数 501 浏览 4 评论 0原文

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

[更新]

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

I'm currently using MaintenanceModeMiddleware to put my site into maintenance mode, but it requires you make the change in the settings.py file on the remote server. I would like to use fabric to remotely put the site into maintenance mode. Is there a way to achieve this? Or is there a better method for doing this? Thanks.

[UPDATE]

Thanks for the feedback everyone in the end this is what I did and it works great for me, http://garthhumphreys.com/2011/06/11/painless-django-maintenance-mode-with-fabric/ - I do like the idea of uncomment lines but with my setup if I were to do that on the production server it would be overwritten once I pushed the new version out, so in the end putting the site into maintenance mode from the server level and not the django level works a lot better and is truly easier and flexible, for me at least :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

棒棒糖 2024-11-21 15:47:55

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

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

Fabric does have commands to help you comment or uncomment lines in a given file in fabric.contrib.files. See the docs here: http://docs.fabfile.org/en/1.0.1/api/contrib/files.html

Personally I prefer to handle this at the front-end proxy rather than in a Django middleware. I would take a look at this question Show a custom 503 page if upstream is down which configures Nginx to use a custom page when the upstream is down.

深海夜未眠 2024-11-21 15:47:55

我的解决方案:

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

这是 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]

然后是结构脚本的相关部分:

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 在维护模式期间处理请求;如果只提供一个静态文件就好了。

My solution:

  1. Create a maintenance mode template and link to it via a urlconf, so when visiting /under-maintenance/ a maintenance page is shown.
  2. Then, configure apache to test for the presence of a 'maintenance-mode-on' file, and if it is present do a 302 redirect to the url of the maintenance mode page.
  3. Configure apache to redirect from the maintenance mode URL to the home page, if the 'maintenance-mode-off' file is present.
  4. Fabric script to facilitate switching the files between maintenance-mode-on and maintenance-mode-off.

Here's the relevant section of the Apache config file:

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]

Then the relevant parts of the fabric script:

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!')

This works well, though it does depend on Django handling requests during maintenance mode; it would be nice to just serve a static file.

给我一枪 2024-11-21 15:47:55

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

There is a fork of django-maintenancemode that allows to turn the maintenance mode on/off by setting a value in database. This way you can, for example, create a simple management command to toggle the maintenance mode and call it via fabric. I think it's more flexible than using mod_rewrite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文