Remove unused comments
[slapos.git] / slapos / recipe / generic_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
28 from json import loads as unjson
29
30 class Recipe(GenericBaseRecipe):
31 """
32 kvm frontend instance configuration.
33 """
34
35 def _getRewriteRuleContent(self, slave_instance_list):
36 """Generate rewrite rules list from slaves list"""
37 rewrite_rule_list = []
38 for slave_instance in slave_instance_list:
39 current_slave_dict = dict()
40 # Get host, and if IPv6 address, remove "[" and "]"
41 current_slave_dict['host'] = current_slave_dict['host'].\
42 replace('[', '').replace(']', '')
43 current_slave_dict['port'] = slave_instance['port']
44 if current_slave_dict['host'] is None \
45 or current_slave_dict['port'] is None:
46 # XXX-Cedric: should raise warning because slave seems badly configured
47 continue
48 # Check if target is https or http
49 current_slave_dict['https'] = slave_instance.get('https', 'true')
50 if current_slave_dict['https'] in FALSE_VALUE_LIST:
51 current_slave_dict['https'] = 'false'
52 # Set reference and resource url
53 # Reference is raw reference from SlapOS Master, resource is
54 # URL-compatible name
55 reference = slave_instance.get('slave_reference')
56 current_slave_dict['reference'] = reference
57 current_slave_dict['resource'] = reference.replace('-', '')
58 rewrite_rule_list.append(current_slave_dict)
59 return rewrite_rule_list
60
61 def _getProxyTableContent(self, rewrite_rule_list):
62 """Generate proxy table file content from rewrite rules list"""
63 proxy_table_content = '{'
64 for rewrite_rule in rewrite_rule_list:
65 rewrite_part = self.substituteTemplate(
66 self.getTemplateFilename('proxytable-resource-snippet.json.in'),
67 rewrite_rule)
68 proxy_table_content += "%s," % rewrite_part
69
70 proxy_table_content += '}\n'
71 return proxy_table_content
72
73 def install(self):
74 # Generate rewrite rules
75 rewrite_rule_list = self._getRewriteRuleContent(
76 unjson(self.options['slave-instance-list']))
77 # Create Map
78 map_content = self._getProxyTableContent(rewrite_rule_list)
79 map_file = self.createFile(self.options['map-path'], map_content)
80
81 # Create configuration
82 conf = open(self.getTemplateFilename('kvm-proxy.js'), 'r')
83 conf_file = self.createFile(self.options['conf-path'], conf.read())
84 conf.close()
85
86 config = dict(
87 ip=self.options['ip'],
88 port=self.options['port'],
89 key=self.options['ssl-key-path'],
90 certificate=self.options['ssl-cert-path'],
91 name=self.options['domain'],
92 shell_path=self.options['shell-path'],
93 node_path=self.options['node-binary'],
94 node_env=self.options['node-env'],
95 conf_path=conf_file,
96 map_path=map_file,
97 plain_http='',
98 )
99
100 runner_path = self.createExecutable(
101 self.options['wrapper-path'],
102 self.substituteTemplate(self.getTemplateFilename('nodejs_run.in'),
103 config))
104
105 return [map_file, conf_file, runner_path]