support both IPv4 and IPv6 address.
[slapos.git] / slapos / recipe / generic_varnish / __init__.py
1 ##############################################################################
2 #
3 # Copyright (c) 2012 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 ##############################################################################
27 import os
28 import re
29 from slapos.recipe.librecipe import GenericSlapRecipe
30
31 class Recipe(GenericSlapRecipe):
32 """
33 Instantiate varnish daemon
34
35 TODO:
36 - use varnish3.x and replace .vcl for it
37 """
38 def _install(self):
39 ip = self.options['ip']
40 backend_url = self.parameter_dict['tidstorage-url']
41 backend_server, backend_port = self._getBackendServer(backend_url)
42 varnishd_manager_port = int(self.options['manager-port'])
43 varnishd_server_port = int(self.options['server-port'])
44 path_list = []
45 config = dict(
46 varnishd_binary=self.options['varnishd-binary'],
47 varnish_ip=ip,
48 varnishlog_binary=self.options['varnishlog-binary'],
49 varnishd_manager_port=varnishd_manager_port,
50 varnishd_server_port=varnishd_server_port,
51 varnishd_pid_file=self.options['pid-file'],
52 varnish_instance_name=self.options['varnish-instance-name'],
53 varnish_data=self.options['varnish-data'],
54 shell_path=self.options['shell-path'],
55 vcl_file=self.options['vcl-file'],
56 backend_port=backend_port,
57 backend_server=backend_server,
58 )
59
60 path_list.append(self.createExecutable(self.options['varnishd-wrapper'],
61 self.substituteTemplate(self.getTemplateFilename('varnishd.in'),
62 config)))
63 path_list.append(self.createExecutable(self.options['varnishlog-wrapper'],
64 self.substituteTemplate(self.getTemplateFilename('varnishlog.in'),
65 config)))
66 path_list.append(self.createFile(self.options['vcl-file'],
67 self.substituteTemplate(self.getTemplateFilename('default.vcl.in'),
68 config)))
69 return path_list
70
71 def _getBackendServer(self, url):
72 r = re.compile('\/\/(\[.+\]|[\d.]+):(\d*)')
73 result = r.search(url)
74 ip = result.groups()[0]
75 port = result.groups()[1]
76 return (ip, port)