def get_varnish_dash_modules(): """ Using python-varnish and the Django settings, attempt to create a VarnishDashboardModule for every varnish server. Returns a list of modules, or an empty list. In the __init__ method of your custom dashboard add: self.children.extend(get_varnish_dash_modules()) Note it says extend not append. """ from varnish import VarnishManager from django.conf import settings server_modules = [] for server in getattr(settings,'VARNISH_MANAGEMENT_ADDRS', ()): try: manager = VarnishManager((server,)) stats = manager.run('stats')[0][0] server_modules.append(VarnishDashboardModule(server, stats)) except Exception, e: if settings.DEBUG: raise else: continue
def verify(self): host = self.option.host port = self.option.port thost = host + ":" + str(port) try: conn = VarnishManager((thost, )) vcl_list = conn.run('vcl.list') except Exception, e: self.result.error = "连接发生错误: {error}".format(error=str(e)) return
def varnish_invalidate_url(url, hostname=None): if not hostname: hostname = settings.MAIN_HOSTNAME ban_path = r'obj.http.x-url ~ ^(?i)(%(url)s(/*)(\\?.*)?)$ && obj.http.x-host ~ ^((?i)(.*\\.)?%(host)s(:[0-9]*)?)$' # Varnish needs it quoted, but has a wonky way of encoding URLs :/ url = urllib.unquote(url) url = urllib.quote(url, safe=VARNISH_SAFE) if type(url) != unicode: url = url.decode('utf-8') ban_cmd = (ban_path % {'url': url, 'host': hostname}).encode('utf-8') manager = VarnishManager(settings.VARNISH_MANAGEMENT_SERVERS) manager.run('ban', ban_cmd, secret=settings.VARNISH_SECRET)
class VarnishTests(unittest.TestCase): def setUp(self): self.manager = VarnishManager((ADDR,)) def test_ping(self): result = self.manager.run('ping')[0][0] self.assertEqual(len(result), 2) self.assert_(map(lambda x: isinstance(x, float), (True,True))) def test_purge(self): resp = self.manager.run( 'purge.url', 'http://%s/myrandomurl/.*' % WEB_ADDR)[0][0] self.assertEqual(resp.status, 200) def test_ban(self): regex = '^/banned/*' self.manager.run('ban.url', regex) self.assert_(regex, str(self.manager.run('ban.list'))) def test_multiple(self): result = self.manager.run(( ('ping',None),('ping',None) )) self.assertEqual(result[0][0], result[0][1]) def tearDown(self): self.manager.close()
class VarnishTests(unittest.TestCase): def setUp(self): self.manager = VarnishManager((ADDR,)) def test_ping(self): result = self.manager.run('ping')[0][0] self.assertEqual(len(result), 2) self.assert_(map(lambda x: isinstance(x, float), (True,True))) def test_threading(self): self.manager.run(('ban.url', '^/myrandomurl/$'), threaded=True) self.assert_(self.manager.run('ban.list')[0][0].endswith('^/myrandomurl/$\n')) def test_stats(self): self.assert_(isinstance(self.manager.run('stats')[0][0], dict)) def test_multiple(self): result = self.manager.run(( ('ping',None),('ping',None) )) self.assertEqual(result[0][0], result[0][1]) def tearDown(self): self.manager.close()
class VarnishTests(unittest.TestCase): def setUp(self): self.manager = VarnishManager((ADDR, )) def test_ping(self): result = self.manager.run('ping')[0][0] self.assertEqual(len(result), 2) self.assert_(map(lambda x: isinstance(x, float), (True, True))) def test_threading(self): self.manager.run(('purge.url', '^/myrandomurl/$'), threaded=True) self.assert_( self.manager.run('purge.list')[0][0].endswith('^/myrandomurl/$\n')) def test_stats(self): self.assert_(isinstance(self.manager.run('stats')[0][0], dict)) def test_multiple(self): result = self.manager.run((('ping', None), ('ping', None))) self.assertEqual(result[0][0], result[0][1]) def tearDown(self): self.manager.close()
def setUp(self): self.manager = VarnishManager((ADDR,))
import re from varnish import VarnishManager from sefaria.model import * from sefaria.local_settings import VARNISH_ADM_ADDR, VARNISH_FRNT_PORT, VARNISH_SECRET, FRONT_END_URL from sefaria.system.exceptions import InputError from urlparse import urlparse from httplib import HTTPConnection import urllib import logging logger = logging.getLogger(__name__) with open(VARNISH_SECRET, "r") as sfile: secret = sfile.read().replace('\n', '') manager = VarnishManager([VARNISH_ADM_ADDR]) def invalidate_ref(oref, lang=None, version=None, purge=False): """ Called when 'ref' is changed. We aim to PURGE the main page, so that the results of any save will be immediately visible to the person editing. All other implications are handled with a blanket BAN. todo: Tune this so as not to ban when the version changed is not a displayed version """ if not isinstance(oref, Ref): return if getattr(oref.index_node, "depth", False) and len(oref.sections) >= oref.index_node.depth - 1:
class Command(BaseCommand): def handle(self, *args, **options): if args: pprint(manager.run(*args)) else: print manager.help() ########NEW FILE######## __FILENAME__ = manager from varnish import VarnishManager from django.conf import settings from atexit import register manager = VarnishManager(getattr(settings, 'VARNISH_MANAGEMENT_ADDRS', ())) register(manager.close) ########NEW FILE######## __FILENAME__ = settings ########NEW FILE######## __FILENAME__ = signals from django.db.models.signals import post_save from django.db.models import get_model from django.conf import settings from manager import manager def absolute_url_purge_handler(sender, **kwargs): manager.run('purge.url', r'^%s$' % kwargs['instance'].get_absolute_url())
def setUp(self): self.manager = VarnishManager( (raw_input('Varnish Management Address (ip:port): '), ))
def setUp(self): self.manager = VarnishManager((raw_input('Varnish Management Address (ip:port): '),))
import inspect from tpt import private_settings from varnish import VarnishManager, http_purge_url varnish = private_settings.VARNISH_MANAGEMENT_HOST + ':' +\ str(private_settings.VARNISH_MANAGEMENT_PORT) manager = VarnishManager((varnish,)) def purge_urls(urls): if not type(urls) is list: urls = [urls] for path in urls: url = 'http://{}:{}{}'.format(private_settings.VARNISH_HOST, private_settings.VARNISH_PORT, path) http_purge_url(url) def ban_cache_groups(cache_groups): if not type(cache_groups) is list: cache_groups = [cache_groups] for obj in cache_groups: if type(obj) is str: group = obj elif inspect.isclass(obj): group = obj.class_cache_group else: raise Exception('Unknown type of cache_groups param: ' +