Add varnish purger trigger
authorMagnus Hagander <magnus@hagander.net>
Thu, 21 Feb 2013 15:57:29 +0000 (16:57 +0100)
committerMagnus Hagander <magnus@hagander.net>
Thu, 21 Feb 2013 15:57:29 +0000 (16:57 +0100)
triggers.py

index 97a347edf73a72ea0a24d6879a6538870bbaa55b..c4cf33476690477482444e3174686a43d0374c8b 100644 (file)
@@ -4,3 +4,42 @@ class test(object):
 
        def pushtrigger(self, reponame, username):
                print "Firing push trigger for repository '%s', due to push by %s" % (reponame, username)
+
+
+import httplib
+
+class varnishpurger(object):
+       """
+       Push trigger that purges repositories from varnish. The idea being that
+       the repositories follow a standard gitweb url structure, and we purge
+       all pages related to that repository.
+
+       Requires the config variable "host" set in the section "varnishpurge".
+       This can be an IP address (typically 127.0.0.1) or IP address + port
+       (typically 127.0.0.1:81). The trigger will always post to the URL
+       "/varnish-purge-url", and will include the URL to purge in the form of
+       a regular expression in the custom header X-Purge-URL.
+       """
+       def __init__(self, cfg):
+               self.host = cfg.get('varnishpurge', 'host')
+
+       def pushtrigger(self, reponame, username):
+               # Make a callback to a local varnish server to purge a repository
+               # from it. Assumes gitweb style URLs.
+               for u in ['^/gitweb$', '^/gitweb/?p=%s.git' % reponame]:
+                       if not self._internal_purge(u):
+                               print "Varnish purge failed, website may become slightly out of date"
+                               return
+
+
+       def _internal_purge(self, url):
+               try:
+                       conn = httplib.HTTPConnection(self.host)
+                       conn.request("GET", "/varnish-purge-url", '', {'X-Purge-URL': url})
+                       resp = conn.getresponse()
+                       conn.close()
+                       if resp.status == 200:
+                               return True
+                       return False
+               except Exception, ex:
+                       return False