Merge branch 'master' into erp5-component
[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 slapos.recipe.librecipe import unwrap
29 from ConfigParser import RawConfigParser
30 from netaddr import valid_ipv4, valid_ipv6
31
32 class Recipe(object):
33 """
34 Retrieves slap partition parameters, and makes them available to other
35 buildout section in various ways, and in various encodings.
36 Populates the buildout section it is used in with all slap partition
37 parameters.
38 Also provides access to partition properties: all IPv4, IPv6 and tap
39 interfaces it is allowed to use.
40
41 Input:
42 url
43 Slap server url.
44 Example:
45 ${slap-connection:server-url}
46 key & cert (optional)
47 Path of files containing key and certificate for secure connection to
48 slap server.
49 Example:
50 ${slap-connection:key-file}
51 ${slap-connection:cert-file}
52 computer
53 Computer identifier.
54 Example:
55 ${slap-connection:computer-id}
56 partition
57 Partition identifier.
58 Example:
59 ${slap-connection:partition-id}
60
61 Output:
62 slap-software-type
63 Current partition's software type.
64 ipv4
65 Set of IPv4 addresses.
66 ipv6
67 Set of IPv6 addresses.
68 ipv4_random
69 One of the IPv4 addresses.
70 ipv6_random
71 One of the IPv6 addresses.
72 tap
73 Set of TAP interfaces.
74 configuration
75 Dict of all parameters.
76 configuration.<key>
77 One key per partition parameter.
78 Partition parameter whose name cannot be represented unambiguously in
79 buildout syntax are ignored. They cannot be accessed from buildout syntax
80 anyway, and are available through "configuration" output key.
81 """
82
83 # XXX: used to detect if a configuration key is a valid section key. This
84 # assumes buildout uses ConfigParser - which is currently the case.
85 OPTCRE_match = RawConfigParser.OPTCRE.match
86
87 def __init__(self, buildout, name, options):
88 slap = slapos.slap.slap()
89 slap.initializeConnection(
90 options['url'],
91 options.get('key'),
92 options.get('cert'),
93 )
94 parameter_dict = slap.registerComputerPartition(
95 options['computer'],
96 options['partition'],
97 ).getInstanceParameterDict()
98 # XXX: those are not partition parameters, strictly speaking.
99 # Make them available as individual section keys.
100 for his_key in (
101 'slap_software_type',
102 'slap_computer_partition_id',
103 'slap_computer_id',
104 'slap_software_release_url',
105 'slave_instance_list',
106 'timestamp',
107 ):
108 try:
109 value = parameter_dict.pop(his_key)
110 except KeyError:
111 pass
112 else:
113 options[his_key.replace('_', '-')] = value
114 ipv4_set = set()
115 v4_add = ipv4_set.add
116 ipv6_set = set()
117 v6_add = ipv6_set.add
118 tap_set = set()
119 tap_add = tap_set.add
120 for tap, ip in parameter_dict.pop('ip_list'):
121 tap_add(tap)
122 if valid_ipv4(ip):
123 v4_add(ip)
124 elif valid_ipv6(ip):
125 v6_add(ip)
126 # XXX: emit warning on unknown address type ?
127 options['ipv4'] = ipv4_set
128 options['ipv6'] = ipv6_set
129
130 # also export single ip values for those recipes that don't support sets.
131 if ipv4_set:
132 options['ipv4_random'] = list(ipv4_set)[0]
133 if ipv6_set:
134 options['ipv6_random'] = list(ipv6_set)[0]
135
136 options['tap'] = tap_set
137 parameter_dict = self._expandParameterDict(options, parameter_dict)
138 match = self.OPTCRE_match
139 for key, value in parameter_dict.iteritems():
140 if match(key) is not None:
141 continue
142 options['configuration.' + key] = value
143
144 def _expandParameterDict(self, options, parameter_dict):
145 options['configuration'] = parameter_dict
146 return parameter_dict
147
148 install = update = lambda self: []
149
150 class Serialised(Recipe):
151 def _expandParameterDict(self, options, parameter_dict):
152 options['configuration'] = parameter_dict = unwrap(parameter_dict)
153 if isinstance(parameter_dict, dict):
154 return parameter_dict
155 else:
156 return {}