Permalink
Browse files

Add (protected) API to queue varnish purge requests

We want an API for this so they end up in the queue with all the other
requests, and get delivered to all our frontends without needing each node
to know about which frontends exist.
  • Loading branch information...
1 parent 33ed403 commit ef711553b9e86c74c89b26adff6436c02bab67d1 @mhagander mhagander committed Oct 3, 2012
Showing with 20 additions and 1 deletion.
  1. +16 −1 pgweb/core/views.py
  2. +1 −0 pgweb/settings.py
  3. +3 −0 pgweb/urls.py
View
@@ -4,6 +4,7 @@
from django.contrib.auth.decorators import login_required, user_passes_test
from django.db.models import Count
from django.db import connection, transaction
+from django.conf import settings
from datetime import date, datetime
from os import uname
@@ -12,7 +13,7 @@
from pgweb.util.decorators import ssl_required, cache
from pgweb.util.contexts import NavContext
-from pgweb.util.helpers import simple_form, PgXmlHelper
+from pgweb.util.helpers import simple_form, PgXmlHelper, HttpServerError
from pgweb.util.moderation import get_all_pending_moderations
from pgweb.util.misc import get_client_ip, is_behind_cache, varnish_purge
from pgweb.util.sitestruct import get_all_pages_struct
@@ -198,6 +199,20 @@ def admin_purge(request):
'latest_purges': latest,
})
+@ssl_required
+def api_varnish_purge(request):
+ if not request.META['REMOTE_ADDR'] in settings.VARNISH_PURGERS:
+ return HttpServerError("Invalid client address")
+ if request.method != 'POST':
+ raise HttpServerError("Can't use this way")
+ n = int(request.POST['n'])
+ curs = connection.cursor()
+ for i in range(0, n):
+ expr = request.POST['p%s' % i]
+ curs.execute("SELECT varnish_purge_expr(%s)", (expr, ))
+ transaction.commit_unless_managed()
+ return HttpResponse("Purged %s entries\n" % n)
+
# Merge two organisations
@login_required
@user_passes_test(lambda u: u.is_superuser)
View
@@ -130,6 +130,7 @@
# varnish frontend servers in use.
FTP_MASTERS=() # A tuple containing the *IP addresses* of all machines
# trusted to upload ftp structure data
+VARNISH_PURGERS=() # Extra servers that can do varnish purges through our queue
# Load local settings overrides
from settings_local import *
View
@@ -123,6 +123,9 @@
# Sync timestamp, for automirror
(r'^web_sync_timestamp$', 'pgweb.core.views.sync_timestamp'),
+ # API endpoints
+ (r'^api/varnish/purge/$', 'pgweb.core.views.api_varnish_purge'),
+
# Override some URLs in admin, to provide our own pages
(r'^admin/pending/$', 'pgweb.core.views.admin_pending'),
(r'^admin/purge/$', 'pgweb.core.views.admin_purge'),

0 comments on commit ef71155

Please sign in to comment.