Django Compressor on a multi-server deployment

1.1k Views Asked by At

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 :-)

2

There are 2 best solutions below

0
On

you can use puppet for the task

0
On

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.)