Skip to content

Commit

Permalink
added delete old logs script for nfs server
Browse files Browse the repository at this point in the history
  • Loading branch information
OriHoch committed Nov 14, 2023
1 parent 09a9388 commit a20343e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions modules/hasadna/delete-old-logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import sys
from datetime import datetime
import shutil


def delete_old_files(root_path, cutoff_date):
for root, dirs, files in os.walk(root_path, topdown=False):
for name in files:
file_path = os.path.join(root, name)
try:
mod_date = datetime.fromtimestamp(os.path.getmtime(file_path))
except FileNotFoundError:
mod_date = None
if mod_date is not None and mod_date < cutoff_date:
# print(f'remove: {file_path}')
os.remove(file_path)

# Check if the directory is empty after file deletion
if not os.listdir(root):
# print(f'rmtree: {root}')
shutil.rmtree(root)


if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script.py [path] [date in YYYY-MM-DD format]")
sys.exit(1)

path = sys.argv[1]
date_str = sys.argv[2]

try:
cutoff_date = datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
print("Invalid date format. Please use YYYY-MM-DD.")
sys.exit(1)

delete_old_files(path, cutoff_date)
7 changes: 7 additions & 0 deletions modules/hasadna/hasadna_nfs1.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ EOF
]
}
}

# Added delete-old-logs.py from this directory to home directory on hasadna-nfs1 server
# this script needs to run periodically to delete old airflow logs
# currently I run it manually when disk space is low:
# python3 delete-old-logs.py /srv/default2/openbus/airflow-home/logs 2023-09-12
# python3 delete-old-logs.py /srv/default2/oknesset/airflow-home/logs 2023-09-12
# python3 delete-old-logs.py /srv/default2/datacity/ckan-dgp-logs/airflow-logs 2023-09-12

0 comments on commit a20343e

Please sign in to comment.