Commit 2c352e90 authored by Sebastien Robin's avatar Sebastien Robin

cleanup old logs regularly

parent 4a01afec
......@@ -564,3 +564,24 @@ branch = foo
TaskDistributionTool.createTestResult = original_createTestResult
test_node._prepareSlapOS = original_prepareSlapOS
test_node.runTestSuite = original_runTestSuite
def test_16_cleanupLogDirectory(self):
# Make sure that we are able to cleanup old log folders
test_node = self.getTestNode()
def check(file_list):
log_directory_dir = os.listdir(self.log_directory)
self.assertTrue(set(file_list).issubset(
set(log_directory_dir)),
"%r not contained by %r" % (file_list, log_directory_dir))
check([])
os.mkdir(os.path.join(self.log_directory, 'ab-llzje'))
a_file = open(os.path.join(self.log_directory, 'a_file'), 'w')
a_file.close()
check(set(['ab-llzje', 'a_file']))
# default log file time is 15 days, so nothing is going to be deleted
test_node._cleanupLog()
check(set(['ab-llzje', 'a_file']))
# then we set keep time to 0, folder will be deleted
test_node.max_log_time = 0
test_node._cleanupLog()
check(set(['a_file']))
\ No newline at end of file
......@@ -43,6 +43,8 @@ from Updater import Updater
from erp5.util import taskdistribution
DEFAULT_SLEEP_TIMEOUT = 120 # time in seconds to sleep
MAX_LOG_TIME = 15 # time in days we should keep logs that we can see through
# httd
supervisord_pid_file = None
PROFILE_PATH_KEY = 'profile_path'
......@@ -141,7 +143,7 @@ class NodeTestSuite(SlapOSInstance):
class TestNode(object):
def __init__(self, log, config):
def __init__(self, log, config, max_log_time=MAX_LOG_TIME):
self.testnode_log = log
self.log = log
self.config = config or {}
......@@ -151,6 +153,7 @@ class TestNode(object):
if self.config.get('working_directory', '').endswith("slapos/"):
self.config['working_directory'] = self.config[
'working_directory'][:-(len("slapos/"))] + "testnode"
self.max_log_time = max_log_time
def checkOldTestSuite(self,test_suite_data):
config = self.config
......@@ -378,10 +381,22 @@ branch = %(branch)s
cwd=node_test_suite.test_suite_directory,
log_prefix='runTestSuite', get_output=False)
def _cleanupLog(self):
config = self.config
log_directory = self.config['log_directory']
now = time.time()
for log_folder in os.listdir(log_directory):
folder_path = os.path.join(log_directory, log_folder)
if os.path.isdir(folder_path):
if (now - os.stat(folder_path).st_mtime)/86400 > self.max_log_time:
self.log("deleting log directory %r" % (folder_path,))
shutil.rmtree(folder_path)
def cleanUp(self,test_result):
log = self.log
log('Testnode.cleanUp')
self.process_manager.killPreviousRun()
self._cleanupLog()
def run(self):
log = self.log
......
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