remove logs older than 33 days
Change-Id: Id43bfb5ec27768d336a20aafa8f41561ca78df96
Related-prod:PROD-28239
diff --git a/save_jenkins_console/save_jenkins_console/jenkins_api.py b/save_jenkins_console/save_jenkins_console/jenkins_api.py
index c20c5a0..baf5d54 100644
--- a/save_jenkins_console/save_jenkins_console/jenkins_api.py
+++ b/save_jenkins_console/save_jenkins_console/jenkins_api.py
@@ -13,6 +13,7 @@
from config import LOGGIGNG_JENKINS_API
from config import LOGS_DIRECTORY
from config import JOBS_FOR_GETTING_LOGS_FROM_OUTPUT
+from manage_files import delete_old_files
logging.basicConfig(format='[%(asctime)s][%(name)s][%(levelname)s] %(message)s',
datefmt='%d-%m-%Y %H:%M:%S',
@@ -165,7 +166,6 @@
time.sleep(700)
-
if __name__ == '__main__':
gathering_data_from_jenkins_all_jenkins_job(JOBS_FOR_GETTING_LOGS_FROM_OUTPUT)
-
+ delete_old_files(config.FILES_OLDER_THAN, config.LOGS_DIRECTORY)
diff --git a/save_jenkins_console/save_jenkins_console/manage_files.py b/save_jenkins_console/save_jenkins_console/manage_files.py
new file mode 100644
index 0000000..db69b46
--- /dev/null
+++ b/save_jenkins_console/save_jenkins_console/manage_files.py
@@ -0,0 +1,33 @@
+"""
+-------------
+Manage files
+-------------
+"""
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+import time
+
+
+def delete_old_files(days, path):
+ logger.info("Removing files older than {} days".format(days))
+ time_secs = time.time() - (days * 86400)
+ for root, dirs, files in os.walk(path, topdown=False):
+ for file in files:
+ full_path = os.path.join(root,file)
+ stat = os.stat(full_path)
+ if stat.st_mtime <= time_secs:
+ logger.info("removing: {}".format(full_path))
+ os.remove(full_path)