merge erp5 branch into master
[slapos.git] / slapos / recipe / request.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 logging
28
29 from slapos import slap as slapmodule
30
31 class Recipe(object):
32
33 def __init__(self, buildout, name, options):
34 self.logger = logging.getLogger(name)
35
36 slap = slapmodule.slap()
37
38 self.software_release_url = options['software-url']
39
40 slap.initializeConnection(options['server-url'],
41 options.get('key-file'),
42 options.get('cert-file'),
43 )
44 computer_partition = slap.registerComputerPartition(
45 options['computer-id'], options['partition-id'])
46 self.request = computer_partition.request
47
48 self.isSlave = False
49 if 'slave' in options:
50 self.isSlave = options['slave'].lower() in ['y', 'yes', 'true', '1']
51
52 self.return_parameters = []
53 if 'return' in options:
54 self.return_parameters = [str(parameter).strip()
55 for parameter in options['return'].split()]
56 else:
57 self.logger.debug("No parameter to return to main instance."
58 "Be careful about that...")
59
60 software_type = 'RootInstanceSoftware'
61 if 'software-type' in options:
62 software_type = options['software-type']
63
64 filter_kw = {}
65 if 'sla' in options:
66 for sla_parameter in options['sla'].split():
67 filter_kw[sla_parameter] = options['sla-%s' % sla_parameter]
68
69 partition_parameter_kw = {}
70 if 'config' in options:
71 for config_parameter in options['config'].split():
72 partition_parameter_kw[config_parameter] = \
73 options['config-%s' % config_parameter]
74
75 self.instance = self.request(options['software-url'], software_type,
76 options.get('name', name), partition_parameter_kw=partition_parameter_kw,
77 filter_kw=filter_kw, shared=self.isSlave)
78
79 self.failed = None
80 for param in self.return_parameters:
81 try:
82 options['connection-%s' % param] = str(
83 self.instance.getConnectionParameter(param))
84 except slapmodule.NotFoundError:
85 options['connection-%s' % param] = ''
86 if self.failed is None:
87 self.failed = param
88
89 def install(self):
90 if self.failed is not None:
91 # Check instance status to know if instance has been deployed
92 try:
93 status = self.instance.getState()
94 except slapmodule.NotFoundError:
95 status = "not ready yet, please try again"
96 # XXX-Cedric : currently raise an error. So swallow it...
97 except AttributeError:
98 status = "unknown"
99 raise KeyError("Connection parameter %s not found. "
100 "Status of requested instance is : %s." % (self.failed, status))
101 return []
102
103 update = install