Follow buildout requirements.
[slapos.git] / slapos / recipe / slapparameters.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 One key per supported serialisation format, with all partition parameters
69 serialised in that format as values and format's name.
70 Also, one key per partition parameter, prefixed with serialisation format
71 followed by a dot. Example:
72 json = {"foo": "bar"}
73 json.foo = "bar"
74
75 Supported serailisation formats:
76 json (safe)
77 JavaScript Object Notation
78 str (unsafe)
79 Python string representation.
80 """
81
82 # XXX: used to detect if a configuration key is a valid section key. This
83 # assumes buildout uses ConfigParser - which is currently the case.
84 OPTCRE = RawConfigParser.OPTCRE
85
86 def __init__(self, buildout, name, options):
87 slap = slapos.slap.slap()
88 slap.initializeConnection(
89 options['url'],
90 options.get('key'),
91 options.get('cert'),
92 )
93 parameter_dict = slap.registerComputerPartition(
94 options['computer'],
95 options['partition'],
96 ).getInstanceParameterDict()
97 allow_unsafe = bool(int(options.get('unsafe', '0')))
98 match = self.OPTCRE.match
99 for name, (safe, cast) in cast_dict.iteritems():
100 if not safe and not allow_unsafe:
101 continue
102 options[name] = cast(parameter_dict)
103 for key, value in parameter_dict.iteritems():
104 if match(key) is not None:
105 # It should be OK to skip silently and unconditionally: such
106 # parameter cannot be accessed in a well-formed buildout
107 # config.
108 continue
109 options[name + '.' + key] = cast(value)
110
111 def install(self):
112 return []
113
114 update = install