Commit d9929520 authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼

[runner] don't check modified files that wouldn't be rsynced anyway

parent 8b8a01f6
...@@ -96,12 +96,38 @@ def synchroniseRunnerWorkingDirectory(config, backup_path): ...@@ -96,12 +96,38 @@ def synchroniseRunnerWorkingDirectory(config, backup_path):
) )
def getBackupFilesModifiedDuringExportList(export_start_date): def getBackupFilesModifiedDuringExportList(config, export_start_date):
export_time = time.time() - export_start_date export_time = time.time() - export_start_date
return subprocess.check_output(( # find all files that were modified during export
'find', '-cmin', str(export_time / 60.), '-type', 'f', '-path', '*/srv/backup/*' modified_files = subprocess.check_output((
)).split() 'find', 'instance', '-cmin', str(export_time / 60.), '-type', 'f', '-path', '*/srv/backup/*'
))
if not modified_files:
return ()
# filter those files through rsync --exclude to see if they would have been transfered anyway
rsync_arg_list = [
config.rsync_binary,
'-n',
'--out-format=%n',
'--files-from=-',
'--relative',
'--no-implied-dirs'
]
rsync_arg_list += map("--exclude={}".format, getExcludePathList(os.getcwd()))
rsync_arg_list += '.', 'unexisting_dir_or_file_just_to_have_the_output'
process = subprocess.Popen(rsync_arg_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = process.communicate(modified_files)[0]
retcode = process.poll()
if retcode:
raise CalledProcessError(retcode, rsync_arg_list[0], output=output)
important_modified_files = output.splitlines()
not_important_modified_files = set(modified_files.splitlines()).difference(important_modified_files)
if not_important_modified_files:
print("WARNING: The following files in srv/backup were modified since the exporter started (srv/backup should contain almost static files):", *sorted(not_important_modified_files), sep='\n')
return important_modified_files
def runExport(): def runExport():
export_start_date = int(time.time()) export_start_date = int(time.time())
...@@ -146,11 +172,11 @@ def runExport(): ...@@ -146,11 +172,11 @@ def runExport():
time.sleep(10) time.sleep(10)
# Check that export didn't happen during backup of instances # Check that export didn't happen during backup of instances
with CwdContextManager(os.path.join(runner_working_path, 'instance')): with CwdContextManager(runner_working_path):
modified_file_list = getBackupFilesModifiedDuringExportList(export_start_date) modified_file_list = getBackupFilesModifiedDuringExportList(args, export_start_date)
if len(modified_file_list): if len(modified_file_list):
print("ERROR: Files were modified since the backup started, exporter should be re-run." print("ERROR: The following files in srv/backup were modified since the exporter started. Since they must be backup, exporter should be re-run."
" Let's sleep %s minutes, to let the backup end. Modified files:\n%s" % ( " Let's sleep %s minutes, to let the backup end.\n%s" % (
args.backup_wait_time, '\n'.join(modified_file_list))) args.backup_wait_time, '\n'.join(modified_file_list)))
time.sleep(args.backup_wait_time * 60) time.sleep(args.backup_wait_time * 60)
sys.exit(1) sys.exit(1)
...@@ -40,7 +40,15 @@ __buildout_signature__ = MarkupSafe-1.0-py2.7-linux-x86_64.egg Jinja2-2.10-py2.7 ...@@ -40,7 +40,15 @@ __buildout_signature__ = MarkupSafe-1.0-py2.7-linux-x86_64.egg Jinja2-2.10-py2.7
recipe = slapos.recipe.template:jinja2 recipe = slapos.recipe.template:jinja2
rendered = /some/prefix/slappart18/test/srv/exporter.exclude rendered = /some/prefix/slappart18/test/srv/exporter.exclude
template = inline: template = inline:
srv/backup/**""" srv/backup/*.log
[exclude1]
__buildout_installed__ = {cwd}/instance/slappart1/srv/exporter.exclude
__buildout_signature__ = MarkupSafe-1.0-py2.7-linux-x86_64.egg Jinja2-2.10-py2.7.egg zc.buildout-2.12.2-py2.7.egg slapos.recipe.template-4.3-py2.7.egg setuptools-40.4.3-py2.7.egg
recipe = slapos.recipe.template:jinja2
rendered = /some/prefix/slappart18/test/srv/exporter.exclude
template = inline:
srv/backup/log/**"""
class Config(): class Config():
...@@ -79,21 +87,32 @@ class TestRunnerExporter(unittest.TestCase): ...@@ -79,21 +87,32 @@ class TestRunnerExporter(unittest.TestCase):
os.makedirs('instance/slappart0/srv/backup') os.makedirs('instance/slappart0/srv/backup')
os.makedirs('instance/slappart1/etc') os.makedirs('instance/slappart1/etc')
os.makedirs('instance/slappart1/srv/backup') os.makedirs('instance/slappart1/srv/backup/log')
self._createFile('instance/slappart0/.installed.cfg', self._createFile('instance/slappart0/.installed.cfg',
tested_instance_cfg.format(cwd=os.getcwd())) tested_instance_cfg.format(cwd=os.getcwd()))
self._createFile('instance/slappart0/srv/backup/data.dat', self._createFile('instance/slappart0/srv/backup/data.dat',
'all my fortune lays on this secret !') 'all my fortune lays on this secret !')
self._createFile('instance/slappart0/srv/backup/data.log',
'this log is not so important !')
self._createFile('instance/slappart0/srv/exporter.exclude', self._createFile('instance/slappart0/srv/exporter.exclude',
'srv/backup/**') 'srv/backup/*.log')
self._createFile('instance/slappart0/etc/config.json') self._createFile('instance/slappart0/etc/config.json')
self._createFile('instance/slappart0/etc/.parameters.xml') self._createFile('instance/slappart0/etc/.parameters.xml')
self._createFile('instance/slappart0/etc/.project', self._createFile('instance/slappart0/etc/.project',
'workspace/slapos-dev/software/erp5') 'workspace/slapos-dev/software/erp5')
self._createFile('instance/slappart1/srv/backup/data.dat',
'This is important data also !')
self._createFile('instance/slappart1/srv/backup/log/log1',
'First log')
self._createFile('instance/slappart1/srv/backup/log/log2',
'Second log')
self._createFile('instance/slappart1/srv/exporter.exclude',
'srv/backup/log/**')
self._createExecutableFile( self._createExecutableFile(
'instance/slappart1/srv/.backup_identity_script', 'instance/slappart1/srv/.backup_identity_script',
"#!/bin/sh\nexec xargs -0 md5sum" "#!/bin/sh\nexec xargs -0 md5sum"
...@@ -120,8 +139,10 @@ class TestRunnerExporter(unittest.TestCase): ...@@ -120,8 +139,10 @@ class TestRunnerExporter(unittest.TestCase):
'.installed*.cfg', '.installed*.cfg',
'instance/slappart0/etc/nicolas.txt', 'instance/slappart0/etc/nicolas.txt',
'instance/slappart0/etc/rafael.txt', 'instance/slappart0/etc/rafael.txt',
'instance/slappart0/srv/backup/**', 'instance/slappart0/srv/backup/*.log',
'instance/slappart0/srv/exporter.exclude', 'instance/slappart0/srv/exporter.exclude',
'instance/slappart1/srv/backup/log/**',
'instance/slappart1/srv/exporter.exclude',
] ]
) )
...@@ -155,7 +176,7 @@ class TestRunnerExporter(unittest.TestCase): ...@@ -155,7 +176,7 @@ class TestRunnerExporter(unittest.TestCase):
self.assertEqual(check_output_mock.call_count, 1) self.assertEqual(check_output_mock.call_count, 1)
check_output_mock.assert_any_call( check_output_mock.assert_any_call(
['rsync', '-rlptgov', '--stats', '--safe-links', '--ignore-missing-args', '--delete', '--delete-excluded', '--exclude=*.pid', '--exclude=*.sock', '--exclude=*.socket', '--exclude=.installed*.cfg', '--exclude=instance/slappart0/etc/nicolas.txt', '--exclude=instance/slappart0/etc/rafael.txt', '--exclude=instance/slappart0/srv/backup/**', '--exclude=instance/slappart0/srv/exporter.exclude', 'instance', 'project', 'proxy.db', 'public', 'backup/runner/runner'] ['rsync', '-rlptgov', '--stats', '--safe-links', '--ignore-missing-args', '--delete', '--delete-excluded', '--exclude=*.pid', '--exclude=*.sock', '--exclude=*.socket', '--exclude=.installed*.cfg', '--exclude=instance/slappart0/etc/nicolas.txt', '--exclude=instance/slappart0/etc/rafael.txt', '--exclude=instance/slappart0/srv/backup/*.log', '--exclude=instance/slappart0/srv/exporter.exclude', '--exclude=instance/slappart1/srv/backup/log/**', '--exclude=instance/slappart1/srv/exporter.exclude', 'instance', 'project', 'proxy.db', 'public', 'backup/runner/runner']
) )
def test_getSlappartSignatureMethodDict(self): def test_getSlappartSignatureMethodDict(self):
...@@ -204,18 +225,20 @@ class TestRunnerExporter(unittest.TestCase): ...@@ -204,18 +225,20 @@ class TestRunnerExporter(unittest.TestCase):
def test_getBackupFilesModifiedDuringExportList(self): def test_getBackupFilesModifiedDuringExportList(self):
self._setUpFakeInstanceFolder() self._setUpFakeInstanceFolder()
with runner_exporter.CwdContextManager('instance'): config = Config()
self.assertEqual( config.rsync_binary = 'rsync'
runner_exporter.getBackupFilesModifiedDuringExportList(time.time() - 5),
['./slappart0/srv/backup/data.dat'] self.assertEqual(
) runner_exporter.getBackupFilesModifiedDuringExportList(config, time.time() - 5),
time.sleep(2) ['instance/slappart0/srv/backup/data.dat',
self.assertEqual( 'instance/slappart1/srv/backup/data.dat']
runner_exporter.getBackupFilesModifiedDuringExportList(time.time() - 1), )
[] time.sleep(2)
) self.assertFalse(
self._createFile('slappart1/srv/backup/bakckup.data', 'my backup') runner_exporter.getBackupFilesModifiedDuringExportList(config, time.time() - 1)
self.assertEqual( )
runner_exporter.getBackupFilesModifiedDuringExportList(time.time() - 1), self._createFile('instance/slappart1/srv/backup/bakckup.data', 'my backup')
['./slappart1/srv/backup/bakckup.data'] self.assertEqual(
) runner_exporter.getBackupFilesModifiedDuringExportList(config, time.time() - 1),
['instance/slappart1/srv/backup/bakckup.data']
)
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