Skip to content

Instantly share code, notes, and snippets.

@arjan
Last active December 20, 2015 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arjan/6172711 to your computer and use it in GitHub Desktop.
Save arjan/6172711 to your computer and use it in GitHub Desktop.
Generate varnish if-then-else statement for hosting multiple Zotonic versions
#!/usr/bin/env python
from glob import glob
import re
def backend(path):
return path.split('/')[3].replace('-', '_').replace('.', '_')
def site(path):
return path.split('/')[7]
def hostnames(path):
s = open(path + "/config").read()
h = re.findall('hostname, "(.*?)"', s, re.M)
a = re.findall('hostalias, "(.*?)"', s, re.M)
a2 = re.findall('hostalias, (\[.*?\])', s, re.M)
a2 = sum([eval(x) for x in a2], [])
return h + a + a2
all = {}
for d in glob("/home/zotonic/zotonic-*/zotonic/priv/sites/*"):
s = site(d)
b = backend(d)
if s in ('testsandbox', 'zotonic_status'):
continue
if b not in all:
all[b] = []
all[b] += hostnames(d)
#print all
print "#"
print "# Auto-generated Varnish Zotonic backend switch"
print "#"
first = True
for b in all:
if first:
print "if (",
else:
print "elseif (",
print " || ".join(["req.http.host ~ \"^%s$\"" % h for h in all[b]]),
print ") {"
print " set req.backend = " + b + ";"
print "}"
first = False
print
@arjan
Copy link
Author

arjan commented Aug 7, 2013

Example output:

#
# Auto-generated Varnish Zotonic backend switch
#
if ( req.http.host ~ "^miraclethings.nl$" || req.http.host ~ "^www.miraclethings.nl$" || req.http.host ~ "^beta.miraclethings.nl$" || req.http.host ~ "^www.foo.nl$" || req.http.host ~ "^mftest.bar.com$" || req.http.host ~ "^fdsfsd.zotonic.com$" || req.http.host ~ "^fdfdfd.nl$" || req.http.host ~ "^www.fdsafsd.nl$" || req.http.host ~ "^asdfsadf.nl$" ) {
  set req.backend = zotonic_0_9;
}
elseif ( req.http.host ~ "^disco.miraclethings.nl$" || req.http.host ~ "^192.168.2.11:8000$" || req.http.host ~ "^www.example.com$" || req.http.host ~ "^example.com$" ) {
  set req.backend = zotonic_master;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment