Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 67 lines (53 sloc) 1.68 KB
#!/usr/bin/python
# coding: utf-8
#
# Ninja web server that listens on POSTS to 0.0.0.0:5000 and purges Varnish
# cache by telnetting into the Varnish CLI.
#
# The user running this needs access to /etc/varnish/secret aka the Varnish CLI
# shared secret file.
#
import hashlib
import telnetlib
import BaseHTTPServer
SECRET_CACHE = ""
def get_secret():
global SECRET_CACHE
if not SECRET_CACHE:
pw_file_fp = open('/etc/varnish/secret')
secret = pw_file_fp.read()
pw_file_fp.close()
SECRET_CACHE = secret[:-1]
return SECRET_CACHE
def flush_cache():
tn = telnetlib.Telnet("localhost", 6082)
print "Varnish CLI connection open."
# Wait till we get the auth required code
tn.read_until("107 59")
tn.read_until("\n")
# Read out the challenge
challenge = tn.read_until("\n")
challenge = challenge[:-1]
print "Got auth challenge: %s" % challenge
secret = get_secret()
auth_resp = hashlib.sha256(challenge + "\n" + secret + "\n" + challenge + "\n").hexdigest()
print "Writing auth response…"
tn.write("auth %s\n" % auth_resp)
tn.read_until("close CLI session.\n")
tn.write("purge.url .*\n")
tn.read_until("200")
print "Varnish Cache flushed!"
tn.close()
class GithubHookHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(s):
flush_cache()
s.send_response(204)
if __name__ == '__main__':
httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', 5000), GithubHookHandler)
print "Github Receive Hook server starting…"
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print "Github Receive Hook server stopped."