Expose software type separately from partition parameters.
[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 import json
29 from ConfigParser import RawConfigParser
30
31 cast_dict = {
32 'json': (True, json.dumps),
33 'str': (False, str),
34 }
35
36 class Recipe(object):
37 """
38 Retrieves slap partition parameters, and makes them available to other
39 buildout section in various ways, and in various encodings.
40 Populates the buildout section it is used in with all slap partition
41 parameters.
42
43 Input:
44 url
45 Slap server url.
46 Example:
47 ${slap-connection:server-url}
48 key & cert (optional)
49 Path of files containing key and certificate for secure connection to
50 slap server.
51 Example:
52 ${slap-connection:key-file}
53 ${slap-connection:cert-file}
54 computer
55 Computer identifier.
56 Example:
57 ${slap-connection:computer-id}
58 partition
59 Partition identifier.
60 Example:
61 ${slap-connection:partition-id}
62 unsafe (optional, 0 by default)
63 Enables formats which are unsafe when represented back into a buildout
64 text file. Set to 0 to explicitly disable unsafe formats, any other
65 integer value to enable them.
66
67 Output:
68 slap-software-type.<format>
69 Current partition's software type, serialised in each available format.
70 <format>
71 All partition parameters serialised in that format as values.
72 Example:
73 json = {"foo": "bar"}
74 <format>.key
75 One key per partition parameter, prefixed with serialisation format
76 followed by a dot. Example:
77 json.foo = "bar"
78
79 Supported serialisation formats:
80 json (safe)
81 JavaScript Object Notation
82 str (unsafe)
83 Python string representation.
84 """
85
86 # XXX: used to detect if a configuration key is a valid section key. This
87 # assumes buildout uses ConfigParser - which is currently the case.
88 OPTCRE = RawConfigParser.OPTCRE
89
90 def __init__(self, buildout, name, options):
91 slap = slapos.slap.slap()
92 slap.initializeConnection(
93 options['url'],
94 options.get('key'),
95 options.get('cert'),
96 )
97 parameter_dict = slap.registerComputerPartition(
98 options['computer'],
99 options['partition'],
100 ).getInstanceParameterDict()
101 # XXX: those are not partition parameters, strictly speaking.
102 # Discard them, and make them available as separate section keys.
103 slap_software_type = parameter_dict.pop('slap_software_type')
104 del parameter_dict['ip_list']
105 allow_unsafe = bool(int(options.get('unsafe', '0')))
106 match = self.OPTCRE.match
107 for name, (safe, cast) in cast_dict.iteritems():
108 if not safe and not allow_unsafe:
109 continue
110 options['slap-software-type.' + name] = cast(slap_software_type)
111 options[name] = cast(parameter_dict)
112 for key, value in parameter_dict.iteritems():
113 if match(key) is not None:
114 # It should be OK to skip silently and unconditionally: such
115 # parameter cannot be accessed in a well-formed buildout
116 # config.
117 continue
118 options[name + '.' + key] = cast(value)
119
120 install = update = lambda self: []