Remove unused import
[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 if 'name' not in options:
49 options['name'] = name
50
51 self.return_parameters = []
52 if 'return' in options:
53 self.return_parameters = [str(parameter).strip()
54 for parameter in options['return'].split()]
55 else:
56 self.logger.debug("No parameter to return to main instance."
57 "Be careful about that...")
58
59 software_type = 'RootInstanceSoftware'
60 if 'software-type' in options:
61 software_type = options['software-type']
62
63 filter_kw = {}
64 if 'sla' in options:
65 for sla_parameter in options['sla'].split():
66 filter_kw[sla_parameter] = options['sla-%s' % sla_parameter]
67
68 partition_parameter_kw = {}
69 if 'config' in options:
70 for config_parameter in options['config'].split():
71 partition_parameter_kw[config_parameter] = \
72 options['config-%s' % config_parameter]
73
74 instance = self.request(options['software-url'], software_type,
75 options['name'], partition_parameter_kw=partition_parameter_kw,
76 filter_kw=filter_kw)
77
78 self.failed = None
79 for param in self.return_parameters:
80 try:
81 options['connection-%s' % param] = str(instance.getConnectionParameter(param))
82 except slapmodule.NotFoundError:
83 options['connection-%s' % param] = ''
84 if self.failed is None:
85 self.failed = param
86
87 def install(self):
88 if self.failed is not None:
89 raise KeyError("Connection parameter %r not found." % self.failed)
90 return []
91
92 update = install