summaryrefslogtreecommitdiff
path: root/triggers.py
blob: 83a7b2154759266077b3556aa974d53b44dc29c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests


class test(object):
    def __init__(self, cfg):
        pass

    def pushtrigger(self, reponame, username):
        print("Firing push trigger for repository '%s', due to push by %s" % (reponame, username))


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, both gitweb and cgit.
        # Also, purge the actual http serving git repo itself.
        for u in [
                r'^/gitweb/?$',
                r'^/gitweb/\?p=%s.git' % reponame,
                r'^/git/%s' % reponame,
                r'^/cgit/?$',
                r'^/cgit/%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:
            resp = requests.get(
                "http://{0}/varnish-purge-url".format(self.host),
                headers={
                    'X-Purge-URL': url,
                }
            )
            if resp.status_code == 200:
                return True
            return False
        except Exception as ex:
            return False