Fill template.
[slapos.git] / slapos / recipe / generate_erp5_tidstorage.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 GenericSlapRecipe
28 import os
29 import json
30
31 ZOPE_PORT_BASE = 12000
32 ZEO_PORT_BASE = 15000
33 HAPROXY_PORT_BASE = 11000
34 APACHE_PORT_BASE = 10000
35
36 class Recipe(GenericSlapRecipe):
37 def _options(self, options):
38 self.dirname = os.path.join(self.buildout['buildout']['parts-directory'],
39 self.name)
40 options['output'] = os.path.join(self.dirname, self.name + '.cfg')
41
42 def _generateRealTemplate(self):
43 current_zeo_port = ZEO_PORT_BASE
44 current_zope_port = ZOPE_PORT_BASE
45 current_apache_port = APACHE_PORT_BASE
46 current_haproxy_port = HAPROXY_PORT_BASE
47 num = 1
48 for l in self.parameter_dict['json'].splitlines():
49 print '%3s %s' % (num, l)
50 num +=1
51 json_data = json.loads(self.parameter_dict['json'])
52 site_id = str(json_data['site-id'])
53 # prepare zeo
54 output = ''
55 part_list = []
56 zope_dict = {}
57 zope_connection_dict = {}
58 snippet_zeo = open(self.options['snippet-zeo']).read()
59 for zeo_id, zeo_configuration in json_data['zeo'].iteritems():
60 current_zeo_port += 1
61 output += snippet_zeo % dict(
62 zeo_id=zeo_id,
63 zeo_port=current_zeo_port,
64 storage_list=' '.join([str(q['zodb']) for q in zeo_configuration])
65 ) + '\n'
66 part_list.extend([
67 "zeo-instance-%s" % zeo_id,
68 "logrotate-entry-zeo-%s" % zeo_id
69 ])
70 for zeo_slave in zeo_configuration:
71 zope_connection_dict[zeo_slave['zodb']] = {
72 'zope-cache-size': zeo_slave['zope-cache-size'],
73 'zeo-cache-size': zeo_slave['zeo-cache-size'],
74 'mount-point': zeo_slave['mount-point'] % {'site-id': site_id},
75 'storage': zeo_slave['zodb'],
76 'name': zeo_slave['zodb'],
77 'server': '${zeo-instance-%(zeo-id)s:ip}:${zeo-instance-%(zeo-id)s:port}' % {'zeo-id': zeo_id}
78 }
79
80 zeo_connection_list = []
81 a = zeo_connection_list.append
82 for k, v in zope_connection_dict.iteritems():
83 a(' zeo-cache-size=%(zeo-cache-size)s zope-cache-size=%(zope-cache-size)s server=%(server)s mount-point=%(mount-point)s ' % v)
84 zeo_connection_string = '\n'.join(zeo_connection_list)
85 zope_dict.update(
86 timezone=json_data['timezone'],
87 zeo_connection_string=zeo_connection_string
88 )
89 # always one distribution node
90 current_zope_port += 1
91 snippet_zope = open(self.options['snippet-zope']).read()
92 zope_id = 'zope-distribution'
93 part_list.append(zope_id)
94 output += snippet_zope % dict(zope_thread_amount=1, zope_id=zope_id, zope_port=current_zope_port,
95 **zope_dict)
96 # always one admin node
97 current_zope_port += 1
98 zope_id = 'zope-admin'
99 part_list.append(zope_id)
100 output += snippet_zope % dict(zope_thread_amount=1, zope_id=zope_id, zope_port=current_zope_port,
101 **zope_dict)
102 # handle activity key
103 for q in range(1, json_data['activity']['zopecount'] + 1):
104 current_zope_port += 1
105 part_name = 'zope-activity-%s' % q
106 part_list.append(part_name)
107 output += snippet_zope % dict(zope_thread_amount=1, zope_id=part_name, zope_port=current_zope_port,
108 **zope_dict)
109 # handle backend key
110 snippet_backend = open(self.options['snippet-backend']).read()
111 for backend_name, backend_configuration in json_data['backend'].iteritems():
112 for q in range(1, backend_configuration['zopecount'] + 1):
113 current_zope_port += 1
114 part_name = 'zope-%s-%s' % (backend_name, q)
115 part_list.append(part_name)
116 output += snippet_zope % dict(zope_thread_amount=backend_configuration['thread-amount'], zope_id=part_name, zope_port=current_zope_port, **zope_dict)
117 # now generate backend access
118 current_apache_port += 1
119 current_haproxy_port += 1
120 part_list.append('apache-%(backend_name)s ca-apache-%(backend_name)s logrotate-entry-apache-%(backend_name)s haproxy-%(backend_name)s' % dict(backend_name=backend_name))
121 backend_dict = dict(
122 backend_name=backend_name,
123 apache_port=current_apache_port,
124 haproxy_port=current_haproxy_port,
125 access_control_string=backend_configuration['access-control-string'],
126 maxconn=backend_configuration['maxconn'],
127 server_check_path='/%s/getId' % site_id
128 )
129 output += snippet_backend % backend_dict
130 prepend = open(self.options['snippet-master']).read() % dict(
131 part_list=' \n'.join([' '+q for q in part_list]))
132 output = prepend + output
133 with open(self.options['output'], 'w') as f:
134 f.write(output)
135
136 def _install(self):
137 if not os.path.exists(self.dirname):
138 os.mkdir(self.dirname)
139 if not "json" in self.parameter_dict:
140 # no json transimtted, nothing to do
141 with open(self.options['output'], 'w') as f:
142 f.write("[buildout]\nparts =\n")
143 else:
144 self._generateRealTemplate()
145 return [self.dirname]