Skip to content

Commit

Permalink
managers/ec2: read VCL template from an external file
Browse files Browse the repository at this point in the history
The new VCL template also supports ESI.
  • Loading branch information
Francisco Souza committed Apr 15, 2014
1 parent f7c44c8 commit 8b96faf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 8 additions & 7 deletions feaas/managers/ec2.py
Expand Up @@ -10,11 +10,8 @@
import varnish
from feaas import storage

VCL_TEMPLATE = (r""" "director app dns {{ {{ .backend = {{ .host = \"{0}\"; """
r""".port = \"80\"; }} }} .ttl = 5m; }} sub vcl_recv {{"""
r""" set req.http.X-Host = req.http.host; set req.http.Host = \"{0}\";"""
r""" if(req.url ~ \"/_varnish_healthcheck\") """
r"""{{ error 200 \"WORKING\"; set req.http.Connection = \"close\"; }} }}" """)
VCL_TEMPLATE_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",
"misc", "default.vcl"))
DUMP_VCL_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",
"misc", "dump_vcls.bash"))

Expand Down Expand Up @@ -108,7 +105,7 @@ def get_instance_addr(self, name):
return instance.dns_name, instance.secret

def write_vcl(self, instance_addr, secret, app_addr):
vcl = self.vcl_template().format(app_addr)
vcl = self.vcl_template() % {"app_host": app_addr}
handler = varnish.VarnishHandler("{0}:6082".format(instance_addr),
secret=secret)
handler.vcl_inline("feaas", vcl)
Expand All @@ -123,7 +120,11 @@ def remove_vcl(self, instance_addr, secret):
handler.quit()

def vcl_template(self):
return VCL_TEMPLATE
with open(VCL_TEMPLATE_FILE) as f:
content = f.read()
content = content.replace("\n", "")
content = content.replace('"', r'\"')
return '"%s"' % content

def remove_instance(self, name):
instance = self.storage.retrieve(name=name)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_ec2_manager.py
Expand Up @@ -217,7 +217,9 @@ def test_unbind_instance(self):

def test_vcl_template(self):
manager = ec2.EC2Manager(None)
self.assertEqual(ec2.VCL_TEMPLATE, manager.vcl_template())
with open(ec2.VCL_TEMPLATE_FILE) as f:
content = f.read().replace("\n", "").replace('"', r'\"')
self.assertEqual('"%s"' % content, manager.vcl_template())

@patch("varnish.VarnishHandler")
def test_write_vcl(self, VarnishHandler):
Expand All @@ -226,7 +228,7 @@ def test_write_vcl(self, VarnishHandler):
app_host, instance_ip = "yeah.cloud.tsuru.io", "10.2.1.2"
manager = ec2.EC2Manager(None)
manager.write_vcl(instance_ip, "abc-def", app_host)
vcl = manager.vcl_template().format(app_host)
vcl = manager.vcl_template() % {"app_host": app_host}
VarnishHandler.assert_called_with("{0}:6082".format(instance_ip),
secret="abc-def")
varnish_handler.vcl_inline.assert_called_with("feaas", vcl)
Expand Down

0 comments on commit 8b96faf

Please sign in to comment.