Commit 886b6002 authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼

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

parent c915b5d7
......@@ -96,12 +96,41 @@ def synchroniseRunnerWorkingDirectory(config, backup_path):
)
def getBackupFilesModifiedDuringExportList(export_start_date):
def getBackupFilesModifiedDuringExportList(config, export_start_date):
export_time = time.time() - export_start_date
return subprocess.check_output((
'find', '-cmin', str(export_time / 60.), '-type', 'f', '-path', '*/srv/backup/*'
)).split()
# find all files that were modified during export
modified_files = subprocess.check_output((
'find', 'instance', '-cmin', str(export_time / 60.), '-type', 'f', '-path', '*/srv/backup/*'
))
if not modified_files:
return ()
# filter those modified files through rsync --exclude getExcludePathList.
# Indeed, some modified files may be listed in getExcludePathList and in this
# case, we won't copy them to PBS so it's not really important if they are
# modified.
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_file_list = output.splitlines()
not_important_modified_file_set = set(modified_files.splitlines()).difference(important_modified_file_list)
if not_important_modified_file_set:
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_file_set), sep='\n')
return important_modified_file_list
def runExport():
export_start_date = int(time.time())
......@@ -146,11 +175,11 @@ def runExport():
time.sleep(10)
# Check that export didn't happen during backup of instances
with CwdContextManager(os.path.join(runner_working_path, 'instance')):
modified_file_list = getBackupFilesModifiedDuringExportList(export_start_date)
with CwdContextManager(runner_working_path):
modified_file_list = getBackupFilesModifiedDuringExportList(args, export_start_date)
if len(modified_file_list):
print("ERROR: Files were modified since the backup started, exporter should be re-run."
" Let's sleep %s minutes, to let the backup end. Modified files:\n%s" % (
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.\n%s" % (
args.backup_wait_time, '\n'.join(modified_file_list)))
time.sleep(args.backup_wait_time * 60)
sys.exit(1)
......@@ -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
rendered = /some/prefix/slappart18/test/srv/exporter.exclude
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():
......@@ -76,24 +84,37 @@ class TestRunnerExporter(unittest.TestCase):
"""Create data mirroring tested_instance_cfg"""
os.makedirs('instance/slappart0/etc')
os.makedirs('instance/slappart0/srv/backup')
os.makedirs('instance/slappart0/srv/backup/important_logs')
os.makedirs('instance/slappart1/etc')
os.makedirs('instance/slappart1/srv/backup')
os.makedirs('instance/slappart1/srv/backup/log')
self._createFile('instance/slappart0/.installed.cfg',
tested_instance_cfg.format(cwd=os.getcwd()))
self._createFile('instance/slappart0/srv/backup/data.dat',
'all my fortune lays on this secret !')
self._createFile('instance/slappart0/srv/backup/important_logs/this_is_a.log',
'this log is very important !')
self._createFile('instance/slappart0/srv/backup/data.log',
'this log is not so important !')
self._createFile('instance/slappart0/srv/exporter.exclude',
'srv/backup/**')
'srv/backup/*.log')
self._createFile('instance/slappart0/etc/config.json')
self._createFile('instance/slappart0/etc/.parameters.xml')
self._createFile('instance/slappart0/etc/.project',
'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(
'instance/slappart1/srv/.backup_identity_script',
"#!/bin/sh\nexec xargs -0 md5sum"
......@@ -120,8 +141,10 @@ class TestRunnerExporter(unittest.TestCase):
'.installed*.cfg',
'instance/slappart0/etc/nicolas.txt',
'instance/slappart0/etc/rafael.txt',
'instance/slappart0/srv/backup/**',
'instance/slappart0/srv/backup/*.log',
'instance/slappart0/srv/exporter.exclude',
'instance/slappart1/srv/backup/log/**',
'instance/slappart1/srv/exporter.exclude',
]
)
......@@ -155,7 +178,7 @@ class TestRunnerExporter(unittest.TestCase):
self.assertEqual(check_output_mock.call_count, 1)
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):
......@@ -204,18 +227,21 @@ class TestRunnerExporter(unittest.TestCase):
def test_getBackupFilesModifiedDuringExportList(self):
self._setUpFakeInstanceFolder()
with runner_exporter.CwdContextManager('instance'):
config = Config()
config.rsync_binary = 'rsync'
self.assertEqual(
runner_exporter.getBackupFilesModifiedDuringExportList(time.time() - 5),
['./slappart0/srv/backup/data.dat']
runner_exporter.getBackupFilesModifiedDuringExportList(config, time.time() - 5),
['instance/slappart0/srv/backup/data.dat',
'instance/slappart0/srv/backup/important_logs/this_is_a.log',
'instance/slappart1/srv/backup/data.dat']
)
time.sleep(2)
self.assertEqual(
runner_exporter.getBackupFilesModifiedDuringExportList(time.time() - 1),
[]
self.assertFalse(
runner_exporter.getBackupFilesModifiedDuringExportList(config, time.time() - 1)
)
self._createFile('slappart1/srv/backup/bakckup.data', 'my backup')
self._createFile('instance/slappart1/srv/backup/bakckup.data', 'my backup')
self.assertEqual(
runner_exporter.getBackupFilesModifiedDuringExportList(time.time() - 1),
['./slappart1/srv/backup/bakckup.data']
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