def get_stats(): secret = getattr(settings, 'VARNISH_SECRET', '') if secret: stats = [x[0] for x in manager.run('stats', secret=secret)] else: stats = [x[0] for x in manager.run('stats')] return zip(getattr(settings, 'VARNISH_MANAGEMENT_ADDRS', ()), stats)
def api_resource_purge_handler (sender, **kwargs): """ Purges object urls in the API. Requires a get_resource_url on the model that returns the url of the api resource object base url. If using tastypie that would look like something like this on a resource named person: @models.permalink def get_resource_url(self): return ('api_dispatch_detail', (), { 'resource_name': 'person', 'api_name': 'v1', 'pk': self.id}) The method will purge all urls *starting* with the url """ instance = kwargs['instance'] if hasattr(instance, 'get_resource_url'): resource_url = instance.get_resource_url() try: manager.run('ban.url', r'^%s' % resource_url) except: logger.warn('No varnish instance running. Could not purge %s ' % resource_url) purge_old_paths(resource_url)
def dispatch(self, request, *args, **kwargs): if not request.user.is_superuser: return HttpResponseRedirect('/admin/') if 'command' in request.REQUEST: runkwargs = dict(request.REQUEST.items()) manager.run(*str(runkwargs.pop('command')).split(), **kwargs) return HttpResponseRedirect(request.path) return super(ManagementView, self).dispatch(request, *args, **kwargs)
def management(request): if not request.user.is_superuser: return HttpResponseRedirect('/admin/') if 'command' in request.REQUEST: kwargs = dict(request.REQUEST.items()) manager.run(*str(kwargs.pop('command')).split(), **kwargs) return HttpResponseRedirect(request.path) stats = subprocess.Popen('/usr/bin/varnishstat -j', shell=True, stdout=subprocess.PIPE)\ .stdout.read() stats = json.loads(stats) return render(request, 'varnish/report.html', {'stats': stats.iteritems()})
def management(request): if not request.user.is_superuser: return HttpResponseRedirect('/admin/') if 'command' in request.REQUEST: kwargs = dict(request.REQUEST.items()) manager.run(*str(kwargs.pop('command')).split(), **kwargs) return HttpResponseRedirect(request.path) stat_cmd = getattr(settings, 'VARNISH_STATCMD', '/usr/bin/varnishstat') stats = subprocess.Popen('%s -j' % stat_cmd, shell=True, stdout=subprocess.PIPE)\ .stdout.read() stats = json.loads(stats) return render(request, 'varnish/report.html', {'stats': stats.iteritems()})
def management(request): if not request.user.is_superuser: return HttpResponseRedirect('/admin/') if 'command' in request.REQUEST: kwargs = dict(request.REQUEST.items()) manager.run(*str(kwargs.pop('command')).split(), **kwargs) return HttpResponseRedirect(request.path) try: stats = get_stats() errors = {} except (Exception, ), e: stats = None errors = { "stats": "Error accessing the stats : %s" % str(e) }
def api_resource_purge_handler(sender, **kwargs): """ Purges object urls in the API. Requires a get_resource_url on the model that returns the url of the api resource object base url. If using tastypie that would look like something like this on a resource named person: @models.permalink def get_resource_url(self): return ('api_dispatch_detail', (), { 'resource_name': 'person', 'api_name': 'v1', 'pk': self.id}) The method will purge all urls *starting* with the url """ instance = kwargs['instance'] resource_urls = [] if hasattr(instance, 'get_resource_url'): resource_urls.extend([instance.get_resource_url(), ]) if hasattr(instance, 'get_related_resource_urls'): resource_urls.extend(instance.get_related_resource_urls()) for resource_url in resource_urls: try: logger.info('Banning API %s' % resource_url) resp = manager.run('ban.url', r'^%s' % resource_url) logger.info(resp) except Exception as ex: logger.warn('%s. Could not purge %s ' % (ex.message, resource_url)) purge_old_paths(resource_url)
def absolute_url_purge_handler(sender, **kwargs): """ Purges the absolute url of the model instance NB: It adds $ to the end of the purge, so no urls with parameters etc are purged, only the url given by get_absolute_url itself """ instance = kwargs['instance'] if hasattr(instance, 'get_absolute_url'): abs_url = instance.get_absolute_url() try: manager.run('purge.url', r'^%s$' % abs_url) except: logger.warn('No varnish instance running. Could not purge %s ' % abs_url) purge_old_paths(abs_url)
def management(request): if not request.user.is_superuser: return HttpResponseRedirect('/admin/') if 'command' in request.REQUEST: kwargs = dict(request.REQUEST.items()) manager.run(*str(kwargs.pop('command')).split(), **kwargs) return HttpResponseRedirect(request.path) try: stats = get_stats() errors = {} except: stats = None errors = {"stats":"Impossible to access the stats for server : %s" \ %getattr(settings, 'VARNISH_MANAGEMENT_ADDRS', ())} extra_context = {'stats':stats, 'errors':errors} return render(request, 'varnish/report.html', extra_context)
def purge_old_paths(abs_url): """ If Django redirects app is installed, search for new paths based on given absolute url and purge all the corresponding old paths to ensure the user gets a redirect to the new path and not an old cached version of the page if i.e. the slug has changed. """ if "django.contrib.redirects" in settings.INSTALLED_APPS: from django.contrib.redirects.models import Redirect oldpaths = Redirect.objects.filter(new_path=abs_url) for p in oldpaths: try: manager.run('purge.url', r'^%s$' % str(p.old_path)) except: logger.warn('No varnish instance running. Could not purge %s' % str(p.old_path))
def get_stats(self): stats = [x[0] for x in manager.run('stats')] return zip(getattr(settings, 'VARNISH_MANAGEMENT_ADDRS', ()), stats)
def absolute_url_purge_handler(sender, **kwargs): manager.run('purge.url', r'^%s$' % kwargs['instance'].get_absolute_url())
def get_stats(): stats = [x[0] for x in manager.run('stats')] return zip(MANAGEMENT_ADDRS, stats)
def get_stats(): stats = [x[0] for x in manager.run('stats')] return zip(getattr(settings, 'VARNISH_MANAGEMENT_ADDRS', ()), stats)
def absolute_url_purge_handler(sender, **kwargs): manager.run('purge.url', r'^%s$' % kwargs['instance'].get_absolute_url(), secret=getattr(settings, 'VARNISH_SECRET', None))
def global_purge_handler(sender, **kwargs): manager.run('purge.url', r'.*', secret=getattr(settings, 'VARNISH_SECRET', None))
def absolute_url_purge_handler(sender, **kwargs): gau = getattr(kwargs['instance'], 'get_absolute_url', False) if gau: manager.run('ban.url', r'^%s.*' % gau())
""" 高并发部署需要文件wsgi配置 """ from manager import manager if __name__ == "__main__": manager.run()