Pass zeo connection.
[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 json_data = json.loads(self.parameter_dict['json'])
46 site_id = str(json_data['site-id'])
47 # prepare zeo
48 output = ''
49 part_list = []
50 zope_dict = {}
51 zope_connection_dict = {}
52 snippet_zeo = open(self.options['snippet-zeo']).read()
53 for zeo_id, zeo_configuration in json_data['zeo'].iteritems():
54 current_zeo_port += 1
55 output += snippet_zeo % dict(
56 zeo_id=zeo_id,
57 zeo_port=current_zeo_port,
58 storage_list=' '.join([str(q['zodb']) for q in zeo_configuration])
59 ) + '\n'
60 part_list.extend([
61 "zeo-instance-%s" % zeo_id,
62 "logrotate-entry-zeo-%s" % zeo_id
63 ])
64 for zeo_slave in zeo_configuration:
65 zope_connection_dict[zeo_slave['zodb']] = {
66 'zope-cache-size': zeo_slave['zope-cache-size'],
67 'zeo-cache-size': zeo_slave['zeo-cache-size'],
68 'mount-point': zeo_slave['mount-point'] % {'site-id': site_id},
69 'storage': zeo_slave['zodb'],
70 'name': zeo_slave['zodb'],
71 'server': '${zeo-instance-%(zeo-id)s:ip}:${zeo-instance-%(zeo-id)s:port}' % {'zeo-id': zeo_id}
72 }
73
74 zeo_connection_list = []
75 a = zeo_connection_list.append
76 for k, v in zope_connection_dict.iteritems():
77 a(' zeo-cache-size=%(zeo-cache-size)s zope-cache-size=%(zope-cache-size)s server=%(server)s mount-point=%(mount-point)s ' % v)
78 zeo_connection_string = '\n'.join(zeo_connection_list)
79 zope_dict.update(
80 timezone=json_data['timezone'],
81 zeo_connection_string=zeo_connection_string
82 )
83 # always one distribution node
84 current_zope_port += 1
85 snippet_zope = open(self.options['snippet-zope']).read()
86 zope_id = 'zope-distribution'
87 part_list.append(zope_id)
88 output += snippet_zope % dict(zope_thread_amount=1, zope_id=zope_id, zope_port=current_zope_port,
89 **zope_dict)
90 # always one admin node
91 current_zope_port += 1
92 zope_id = 'zope-admin'
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 # handle activity key
97 for q in range(1, json_data['activity']['zopecount'] + 1):
98 current_zope_port += 1
99 part_name = 'zope-activity-%s' % q
100 part_list.append(part_name)
101 output += snippet_zope % dict(zope_thread_amount=1, zope_id=part_name, zope_port=current_zope_port,
102 **zope_dict)
103 # handle backend key
104 for backend_type, backend_configuration in json_data['backend'].iteritems():
105 for q in range(1, backend_configuration['zopecount'] + 1):
106 current_zope_port += 1
107 part_name = 'zope-%s-%s' % (backend_type, q)
108 part_list.append(part_name)
109 output += snippet_zope % dict(zope_thread_amount=backend_configuration['thread-amount'], zope_id=part_name, zope_port=current_zope_port, **zope_dict)
110 prepend = open(self.options['snippet-master']).read() % dict(
111 part_list=' \n'.join([' '+q for q in part_list]))
112 output = prepend + output
113 with open(self.options['output'], 'w') as f:
114 f.write(output)
115
116 def _install(self):
117 if not os.path.exists(self.dirname):
118 os.mkdir(self.dirname)
119 if not "json" in self.parameter_dict:
120 # no json transimtted, nothing to do
121 with open(self.options['output'], 'w') as f:
122 f.write("[buildout]\nparts =\n")
123 else:
124 self._generateRealTemplate()
125 return [self.dirname]