publish: add support for slave instances
[slapos.git] / slapos / recipe / publish.py
1 ##############################################################################
2 #
3 # Copyright (c) 2010 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 zc.buildout
28 from slapos.recipe.librecipe import wrap
29 from slapos.recipe.librecipe import GenericSlapRecipe
30
31 CONNECTION_PARAMETER_STRING = 'connection-'
32
33 class Recipe(GenericSlapRecipe):
34 def _install(self):
35 publish_dict = dict()
36 options = self.options.copy()
37 del options['recipe']
38 slave_reference = options.pop('-slave-reference', None)
39 for k, v in options.iteritems():
40 if k[:1] == '-':
41 continue
42 publish_dict[k] = v
43 self._setConnectionDict(publish_dict, slave_reference)
44 return []
45
46 def _setConnectionDict(self, publish_dict, slave_reference=None):
47 return self.setConnectionDict(publish_dict, slave_reference)
48
49 SERIALISED_MAGIC_KEY = '_'
50
51 class Serialised(Recipe):
52 def _setConnectionDict(self, publish_dict, slave_reference=None):
53 return super(Serialised, self)._setConnectionDict(wrap(publish_dict), slave_reference)
54
55
56
57 class PublishSection(GenericSlapRecipe):
58 """
59 Take a list of "request" sections, and publish every connection parameter.
60
61 Input:
62 section-list: String, representing the list of sections to fetch
63 parameters to publish, in order, separated by a space.
64 """
65 def _install(self):
66 publish_dict = dict()
67 for section in self.options['section-list'].strip().split():
68 section = section.strip()
69 options = self.buildout[section].copy()
70 for k, v in options.iteritems():
71 if k.startswith(CONNECTION_PARAMETER_STRING):
72 print k, v
73 publish_dict[k.lstrip(CONNECTION_PARAMETER_STRING)] = v
74 self.setConnectionDict(publish_dict)
75 return []
76