Commit d2e8a850 authored by Tomáš Peterka's avatar Tomáš Peterka

cookbook.wrapper: Add options remove_pidfile and cleanup_command

parent 3d31bf86
...@@ -130,8 +130,8 @@ class GenericBaseRecipe(object): ...@@ -130,8 +130,8 @@ class GenericBaseRecipe(object):
return script return script
def createWrapper(self, name, command, parameters, comments=[], def createWrapper(self, name, command, parameters, comments=[],
parameters_extra=False, environment=None, parameters_extra=False, environment=None, cleanup_command=None,
pidfile=None pidfile=None, remove_pidfile=False
): ):
""" """
Creates a shell script for process replacement. Creates a shell script for process replacement.
...@@ -139,6 +139,9 @@ class GenericBaseRecipe(object): ...@@ -139,6 +139,9 @@ class GenericBaseRecipe(object):
Takes care of #! line limitation when the wrapped command is a script. Takes care of #! line limitation when the wrapped command is a script.
if pidfile parameter is specified, then it will make the wrapper a singleton, if pidfile parameter is specified, then it will make the wrapper a singleton,
accepting to run only if no other instance is running. accepting to run only if no other instance is running.
:param remove_pidfile: remove pidfile when the script ends
:param cleanup_command: arbitrary cleanup command being run when the script ends
""" """
lines = [ '#!/bin/sh' ] lines = [ '#!/bin/sh' ]
...@@ -163,6 +166,22 @@ class GenericBaseRecipe(object): ...@@ -163,6 +166,22 @@ class GenericBaseRecipe(object):
fi fi
echo $$ > $pidfile""" % shlex.quote(pidfile))) echo $$ > $pidfile""" % shlex.quote(pidfile)))
endlines = []
if cleanup_command:
endlines.append(shlex.quote(cleanup_command))
if pidfile and remove_pidfile:
endlines.append("rm -f $pidfile")
if endlines:
endlines.insert(0, "function cleanUp {")
endlines.extend(("exit 0",
"}",
"trap cleanUp INT TERM KILL",
""))
lines.extend(endlines)
lines.append(dedent(''' lines.append(dedent('''
# If the wrapped command uses a shebang, execute the referenced # If the wrapped command uses a shebang, execute the referenced
# executable passing the script path as first argument. # executable passing the script path as first argument.
...@@ -185,6 +204,7 @@ class GenericBaseRecipe(object): ...@@ -185,6 +204,7 @@ class GenericBaseRecipe(object):
lines.append('\t' + param) lines.append('\t' + param)
lines.append('') lines.append('')
return self.createFile(name, '\n'.join(lines), 0700) return self.createFile(name, '\n'.join(lines), 0700)
def createDirectory(self, parent, name, mode=0700): def createDirectory(self, parent, name, mode=0700):
......
...@@ -38,6 +38,8 @@ class Recipe(GenericBaseRecipe): ...@@ -38,6 +38,8 @@ class Recipe(GenericBaseRecipe):
environment = self.options.get('environment') environment = self.options.get('environment')
parameters_extra = self.options.get('parameters-extra') parameters_extra = self.options.get('parameters-extra')
pidfile = self.options.get('pidfile') pidfile = self.options.get('pidfile')
remove_pidfile = self.options.get('remove_pidfile', false)
cleanup_command = self.options.get('cleanup_command')
if not wait_files and not environment: if not wait_files and not environment:
# Create a simple wrapper as shell script # Create a simple wrapper as shell script
...@@ -47,6 +49,8 @@ class Recipe(GenericBaseRecipe): ...@@ -47,6 +49,8 @@ class Recipe(GenericBaseRecipe):
parameters=command_line[1:], parameters=command_line[1:],
parameters_extra=parameters_extra, parameters_extra=parameters_extra,
pidfile=pidfile, pidfile=pidfile,
remove_pidfile=remove_pidfile,
cleanup_command=cleanup_command
)] )]
# More complex needs: create a Python script as wrapper # More complex needs: create a Python script as wrapper
...@@ -76,5 +80,7 @@ class Recipe(GenericBaseRecipe): ...@@ -76,5 +80,7 @@ class Recipe(GenericBaseRecipe):
parameters=[], parameters=[],
parameters_extra=parameters_extra, parameters_extra=parameters_extra,
pidfile=pidfile, pidfile=pidfile,
remove_pidfile=remove_pidfile,
cleanup_command=cleanup_command
)] )]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment