Merge remote-tracking branch 'origin/master' into kvm
[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 self.name = options['name']
40
41 slap.initializeConnection(options['server-url'],
42 options.get('key-file'),
43 options.get('cert-file'),
44 )
45 computer_partition = slap.registerComputerPartition(
46 options['computer-id'], options['partition-id'])
47 self.request = computer_partition.request
48
49 self.isSlave = False
50 if 'slave' in options:
51 self.isSlave = options['slave'].lower() in ['y', 'yes', 'true', '1']
52
53 self.return_parameters = []
54 if 'return' in options:
55 self.return_parameters = [str(parameter).strip()
56 for parameter in options['return'].split()]
57 else:
58 self.logger.debug("No parameter to return to main instance."
59 "Be careful about that...")
60
61 software_type = 'RootSoftwareInstance'
62 if 'software-type' in options:
63 software_type = options['software-type']
64
65 filter_kw = {}
66 if 'sla' in options:
67 for sla_parameter in options['sla'].split():
68 filter_kw[sla_parameter] = options['sla-%s' % sla_parameter]
69
70 partition_parameter_kw = {}
71 if 'config' in options:
72 for config_parameter in options['config'].split():
73 partition_parameter_kw[config_parameter] = \
74 options['config-%s' % config_parameter]
75
76 self.instance = self.request(options['software-url'], software_type,
77 self.name, partition_parameter_kw=partition_parameter_kw,
78 filter_kw=filter_kw, shared=self.isSlave)
79
80 self.failed = None
81 for param in self.return_parameters:
82 try:
83 options['connection-%s' % param] = str(
84 self.instance.getConnectionParameter(param))
85 except slapmodule.NotFoundError:
86 options['connection-%s' % param] = ''
87 if self.failed is None:
88 self.failed = param
89
90 def install(self):
91 if self.failed is not None:
92 # Check instance status to know if instance has been deployed
93 try:
94 status = self.instance.getState()
95 except slapmodule.NotFoundError:
96 status = 'not ready yet, please try again'
97 except AttributeError:
98 status = 'unknown'
99 error_message = 'Connection parameter %s not found. '\
100 'Requested instance is currently %s. If this error persists, '\
101 'check status of this instance.' % (self.failed, status)
102 self.logger.error(error_message)
103 raise KeyError(error_message)
104 return []
105
106 update = install