Commit 9976017e authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼

wrapper recipe: add hash-existing-files option

hash-existing-files list all the files used for hash that are not
handled by buildout. For those files, the hash is calculated as soon as
the __init__ function so that if there is a change in those files,
buildout will remove the existing wrapper (it will uninstall the
section) and replace it with the new wrapper.
parent 1a89ce84
...@@ -25,27 +25,54 @@ ...@@ -25,27 +25,54 @@
# #
############################################################################## ##############################################################################
import shlex import os, shlex
from six.moves import filter
from slapos.recipe.librecipe import GenericBaseRecipe, generateHashFromFiles from slapos.recipe.librecipe import GenericBaseRecipe, generateHashFromFiles
from zc.buildout import UserError
class Recipe(GenericBaseRecipe): class Recipe(GenericBaseRecipe):
"""Recipe to create a script from given command and options. """Recipe to create a script from given command and options.
:param str command-line: shell command which launches the intended process :param str command-line: shell command which launches the intended process
:param str wrapper-path: absolute path to file's destination :param str wrapper-path: absolute path to file's destination
:param lines wait-for-files: list of files to wait for :param lines wait-for-files: list of files to wait for
:param lines hash-files: list of files to be checked by hash :param lines hash-files: list of buildout-generated files to be checked by hash
:param lines hash-existing-files: list of existing files to be checked by hash
:param str pidfile: path to pidfile ensure exclusivity for the process :param str pidfile: path to pidfile ensure exclusivity for the process
:param str private-dev-shm: size of private /dev/shm, using user namespaces :param str private-dev-shm: size of private /dev/shm, using user namespaces
:param bool reserve-cpu: command will ask for an exclusive CPU core :param bool reserve-cpu: command will ask for an exclusive CPU core
""" """
_existing = ()
def __init__(self, buildout, name, options):
self.buildout = buildout
self.options = options
hash_files = options.get('hash-files')
if hash_files:
self.hash_files = hash_files.split()
self._existing = list(filter(os.path.exists, self.hash_files))
else:
self.hash_files = []
hash_files = options.get('hash-existing-files')
if hash_files:
hash_files = hash_files.split()
options['__hash_files__'] = generateHashFromFiles(hash_files)
self.hash_files += hash_files
def getWrapperPath(self):
wrapper_path = self.options['wrapper-path']
if self.hash_files:
wrapper_path += '-' + generateHashFromFiles(self.hash_files)
return wrapper_path
def install(self): def install(self):
if self._existing:
raise UserError(
"hash-files must only list files that are generated by buildout:"
"\n " + "\n ".join(self._existing))
args = shlex.split(self.options['command-line']) args = shlex.split(self.options['command-line'])
wrapper_path = self.options['wrapper-path']
wait_files = self.options.get('wait-for-files') wait_files = self.options.get('wait-for-files')
hash_files = self.options.get('hash-files')
pidfile = self.options.get('pidfile') pidfile = self.options.get('pidfile')
private_dev_shm = self.options.get('private-dev-shm') private_dev_shm = self.options.get('private-dev-shm')
...@@ -65,10 +92,10 @@ class Recipe(GenericBaseRecipe): ...@@ -65,10 +92,10 @@ class Recipe(GenericBaseRecipe):
kw['private_dev_shm'] = private_dev_shm kw['private_dev_shm'] = private_dev_shm
if self.isTrueValue(self.options.get('reserve-cpu')): if self.isTrueValue(self.options.get('reserve-cpu')):
kw['reserve_cpu'] = True kw['reserve_cpu'] = True
if hash_files: return self.createWrapper(self.getWrapperPath(),
hash_file_list = hash_files.split() args, environment, **kw)
hash = generateHashFromFiles(hash_file_list)
wrapper_path = "%s-%s" % (wrapper_path, hash)
return self.createWrapper(wrapper_path, args, environment, **kw)
def update(self):
wrapper_path = self.getWrapperPath()
if not os.path.isfile(wrapper_path):
raise UserError("unstable wrapper path (%r)" % wrapper_path)
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