kvm frontend listen to ipv6 and ipv4
[slapos.git] / slapos / recipe / kvm_frontend / __init__.py
1 ##############################################################################
2 #
3 # Copyright (c) 2011 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 from slapos.recipe.librecipe import GenericBaseRecipe, GenericSlapRecipe
28 import json
29 import traceback
30 import zc.buildout
31
32 class Recipe(GenericSlapRecipe):
33 """
34 kvm frontend instance configuration.
35 """
36
37 def _getRewriteRuleContent(self, slave_instance_list):
38 """Generate rewrite rules list from slaves list"""
39 rewrite_rule_list = []
40 for slave_instance in slave_instance_list:
41 self.logger.info("Processing slave instance %s..." %
42 slave_instance['slave_reference'])
43 # Check for mandatory fields
44 if slave_instance.get('host', None) is None:
45 self.logger.warn('No "host" parameter is defined for %s slave'\
46 'instance. Ignoring it.' % slave_instance['slave_reference'])
47 continue
48 if slave_instance.get('port', None) is None:
49 self.logger.warn('No "host" parameter is defined for %s slave'\
50 'instance. Ignoring it.' % slave_instance['slave_reference'])
51 continue
52
53 current_slave_dict = dict()
54
55 # Get host, and if IPv6 address, remove "[" and "]"
56 current_slave_dict['host'] = slave_instance['host'].\
57 replace('[', '').replace(']', '')
58 current_slave_dict['port'] = slave_instance['port']
59
60 # Check if target is https or http
61 current_slave_dict['https'] = slave_instance.get('https', 'true')
62 if current_slave_dict['https'] in GenericBaseRecipe.FALSE_VALUES:
63 current_slave_dict['https'] = 'false'
64 # Set reference and resource url
65 # Reference is raw reference from SlapOS Master, resource is
66 # URL-compatible name
67 reference = slave_instance.get('slave_reference')
68 current_slave_dict['reference'] = reference
69 current_slave_dict['resource'] = reference.replace('-', '')
70 rewrite_rule_list.append(current_slave_dict)
71 return rewrite_rule_list
72
73 def _getProxyTableContent(self, rewrite_rule_list):
74 """Generate proxy table file content from rewrite rules list"""
75 proxy_table = dict()
76 for rewrite_rule in rewrite_rule_list:
77 proxy_table[rewrite_rule['resource']] = {
78 'port': rewrite_rule['port'],
79 'host': rewrite_rule['host'],
80 'https': rewrite_rule['https'],
81 }
82
83 proxy_table_content = json.dumps(proxy_table)
84 return proxy_table_content
85
86 def _install(self):
87 # Check for mandatory field
88 if self.options.get('domain', None) is None:
89 raise zc.buildout.UserError('No domain name specified. Please define '
90 'the "domain" instance parameter.')
91 # Generate rewrite rules
92 rewrite_rule_list = self._getRewriteRuleContent(
93 json.loads(self.options['slave-instance-list']))
94 # Create Map
95 map_content = self._getProxyTableContent(rewrite_rule_list)
96 map_file = self.createFile(self.options['map-path'], map_content)
97
98 # Create configuration
99 conf = open(self.getTemplateFilename('kvm-proxy.js'), 'r')
100 conf_file = self.createFile(self.options['conf-path'], conf.read())
101 conf.close()
102
103 # Do we create http dummy server used to redirect to https?
104 if self.options['http-redirection'] in GenericBaseRecipe.TRUE_VALUES:
105 http_redirect_server = '1'
106 else:
107 http_redirect_server = ''
108
109 config = dict(
110 ipv6=self.options['ipv6'],
111 ipv4=self.options['ipv4'],
112 port=self.options['port'],
113 key=self.options['ssl-key-path'],
114 certificate=self.options['ssl-cert-path'],
115 name=self.options['domain'],
116 shell_path=self.options['shell-path'],
117 node_path=self.options['node-binary'],
118 node_env=self.options['node-env'],
119 conf_path=conf_file,
120 map_path=map_file,
121 plain_http=http_redirect_server,
122 )
123
124 runner_path = self.createExecutable(
125 self.options['wrapper-path'],
126 self.substituteTemplate(self.getTemplateFilename('nodejs_run.in'),
127 config))
128
129 # Send connection parameters of slave instances
130 site_url = "https://%s:%s/" % (self.options['domain'], self.options['port'])
131 for slave in rewrite_rule_list:
132 try:
133 self.setConnectionDict(
134 dict(url="%s%s" % (site_url, slave['resource']),
135 domainname=self.options['domain'],
136 port=str(self.options['port']),
137 resource=slave['resource']),
138 slave['reference'])
139 except:
140 self.logger.fatal("Error while sending slave %s informations: %s",
141 slave['reference'], traceback.format_exc())
142
143 return [map_file, conf_file, runner_path]