多服务器部署上的 Django Compressor

发布于 2025-01-02 09:38:02 字数 1040 浏览 4 评论 0原文

我很幸运地发现 django_compressor 并在我们的堆栈中实现了它,该堆栈部署到许多服务器(目前6,但随着我们部署更小的虚拟机而增长。)

现在,如果您正在使用 django_compressor 的最佳状态,那么这一切都很好。压缩原始 CSS/JS 代码

但是,现在我想引入某种类型的预编译器。假设这个例子是 LESS (css)。其思考过程相当简单:

  • 将node、npm 和less 软件包安装到服务器上。

  • 向预编译器添加更少的内容!

    <块引用>

    COMPRESS_PRECOMPILERS = (('text/less', 'lessc {infile} {outfile}'), )

现在您进行部署,您的服务器将编译 less 文件。一切都太棒了!

现在让我们再添加 8 台服务器,您必须在每台服务器上安装 Node、npm 和 less 吗?

这就是似乎有些不对劲的地方,我觉得我错过了一些东西。我相信 Django 社区以前也遇到过这个问题。

到目前为止,我的想法是:

  • 使用提交后挂钩在开发人员计算机上编译 CSS。这意味着通过 django_compressor,我们链接到 HTML 中已编译的静态文件,并且我们的存储库包含编译版本和未编译版本。我唯一的缺点是它最终没有使用 django_compressor 一半的好处,并且对开发人员来说可能很乏味?

  • 接受它,让 Node、npm 和 less 成为服务器堆栈的一部分。

更新

我做了一些额外的查看,似乎使用COMPRESS_OFFLINE标志(或只是--force)与管理命令将生成一个离线清单文件,它可以完成我的任务需要(仅在本地测试)。因此,使用预部署挂钩来设置它就是答案。

当然,仍然对其他想法持开放态度:-)

I've been fortunate enough to discover django_compressor and implemented it within our stack, which deploys to many servers (Currently 6, but growing as we deploy smaller virtual machines.)

Now this is all fine and dandy if you're using django_compressor at its finest. Compressing raw CSS/JS code

However, say now I want introduce some type of pre-compiler. Let's say for this example it is LESS (css). The thought process for this is fairly simple:

  • Install node, npm, and the less package onto the server.

  • Add less to your precompilers!

    COMPRESS_PRECOMPILERS = ( ('text/less', 'lessc {infile} {outfile}'), )

Now you deploy, and your server compiles the less file. Everything is fantastic!

Now let's add 8 more servers to that and you have to install node, npm, and less on each server?

This is where something doesn't seem right, and I feel like I'm missing something. I believe the Django community has run into this problem before.

My thoughts thus far have been:

  • Use a post-commit hook to compile the CSS on the developers machine. This means that via django_compressor, we link to the compiled static file in the HTML, and our repository contains both the compiled and non-compiled versions. My only downside to this is it ends up not using half of the benefits of django_compressor and may be tedious for developers?

  • Suck it up and make node, npm, and less part of the server stack.

Update

I did some additional looking around and it seems that using the COMPRESS_OFFLINE flag (or just --force) with the management command will produce an offline manifest file that does what I need (only tested locally). So setting this up with a pre-deploy hook likes to be the answer.

Of course, still open to other ideas :-)

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

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

发布评论

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

评论(2

爱要勇敢去追 2025-01-09 09:38:02

结合有关 COMPRESS_OFFLINE 的注释中的提示,您可以查看 django-staticfiles 的存储内容。例如,您可以在 amazon s3 上托管静态文件,因此将其全部托管在一台静态托管服务器上并从所有服务器上使用它也可能是一个不错的解决方案。您无需对各个服务器上的静态(和压缩)文件执行任何操作。

关于多个服务器的替代解决方案:我制作了一个自定义结构(docs.fabfile.org)脚本,用于在我们的服务器上安装/配置内容。我最近才开始使用coffeescript 和更少的东西,但这两个最终都在我的fabfile 中。这为我解决了安装问题。

(fabfile 的替代品是具有标准依赖项的自定义 debian 包。或者 Chef 或 puppet 或类似的东西。)

Coupled with the tips in the comments about COMPRESS_OFFLINE, you could look at django-staticfiles' storage stuff. You can host the static files on amazon s3, for instance, so hosting it all on one static-hosting server and using that from all your servers could also be a nice solution. You wouldn't need to do anything with the static (and compressed) files on the individual servers.

Alternative solution regarding the multiple servers: I've made a custom fabric (docs.fabfile.org) script that installs/configures stuff on our servers. I've only recently started using coffeescript and less, but those two are definitively ending up in my fabfile. That solves the installation problem for me.

(Alternatives to a fabfile are things like a custom debian package with standard dependencies. Or chef or puppet or something similar.)

表情可笑 2025-01-09 09:38:02

你可以使用木偶来完成任务

you can use puppet for the task

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