Rely on buildout serialisation.
[slapos.git] / slapos / recipe / slapconfiguration.py
1 ##############################################################################
2 #
3 # Copyright (c) 2012 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 slapos.slap
28 from ConfigParser import RawConfigParser
29
30 class Recipe(object):
31 """
32 Retrieves slap partition parameters, and makes them available to other
33 buildout section in various ways, and in various encodings.
34 Populates the buildout section it is used in with all slap partition
35 parameters.
36
37 Input:
38 url
39 Slap server url.
40 Example:
41 ${slap-connection:server-url}
42 key & cert (optional)
43 Path of files containing key and certificate for secure connection to
44 slap server.
45 Example:
46 ${slap-connection:key-file}
47 ${slap-connection:cert-file}
48 computer
49 Computer identifier.
50 Example:
51 ${slap-connection:computer-id}
52 partition
53 Partition identifier.
54 Example:
55 ${slap-connection:partition-id}
56
57 Output:
58 slap-software-type
59 Current partition's software type.
60 configuration
61 Dict of all parameters.
62 configuration.<key>
63 One key per partition parameter.
64 Partition parameter whose name cannot be represented unambiguously in
65 buildout syntax are ignored. They cannot be accessed from buildout syntax
66 anyway, and are available through "configuration" output key.
67 """
68
69 # XXX: used to detect if a configuration key is a valid section key. This
70 # assumes buildout uses ConfigParser - which is currently the case.
71 OPTCRE_match = RawConfigParser.OPTCRE.match
72
73 def __init__(self, buildout, name, options):
74 slap = slapos.slap.slap()
75 slap.initializeConnection(
76 options['url'],
77 options.get('key'),
78 options.get('cert'),
79 )
80 parameter_dict = slap.registerComputerPartition(
81 options['computer'],
82 options['partition'],
83 ).getInstanceParameterDict()
84 # XXX: those are not partition parameters, strictly speaking.
85 # Discard them, and make them available as separate section keys.
86 options['slap-software-type'] = parameter_dict.pop(
87 'slap_software_type')
88 del parameter_dict['ip_list']
89 options['configuration'] = parameter_dict
90 match = self.OPTCRE_match
91 for key, value in parameter_dict.iteritems():
92 if match(key) is not None:
93 continue
94 options['configuration.' + key] = value
95
96 install = update = lambda self: []