Update wrapper recipe to make it simpler and more dev-friendly.
[slapos.git] / slapos / recipe / wrapper.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
28 import shlex
29
30 from slapos.recipe.librecipe import GenericBaseRecipe
31
32 class Recipe(GenericBaseRecipe):
33 def install(self):
34 command_line = shlex.split(self.options['command-line'])
35 wrapper_path = self.options['wrapper-path']
36 wait_files = self.options.get('wait-for-files')
37 environment = self.options.get('environment')
38
39 if not wait_files and not environment:
40 # Create a simple wrapper as shell script
41 return [self.createWrapper(
42 name=wrapper_path,
43 command=command_line[0],
44 parameters=command_line[1:],
45 )]
46
47 # More complex needs: create a Python script as wrapper
48
49 if wait_files is not None:
50 wait_files = [filename.strip() for filename in wait_files.split()
51 if filename.strip()]
52 if environment is not None:
53 environment = dict((k.strip(), v.strip()) for k, v in [
54 line.split('=')
55 for line in environment.split('\n')
56 ])
57 return [self.createPythonScript(
58 wrapper_path,
59 'slapos.recipe.librecipe.execute.generic_exec',
60 (command_line, wait_files, environment,),
61 )]