diff --git a/constant.py b/constant.py
index 31c9267..959ca97 100644
--- a/constant.py
+++ b/constant.py
@@ -5,18 +5,18 @@ KEEP_FILES = [".bashrc", ".bash_logout", ".bash_profile", "public-html", ".ssh",
WHITE_LIST = ["mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang", "unixmanager5xx"]
# filter date
-DATE = '2024-02-01'
+DATE = '2024-04-01'
# owner, you have to change the GOOGLE_CODE if change this variable
GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw"
# notify_template
-MAIL_MESSAGE = '''
+MAIL_MSG = '''
Hi, {}
因 CML Server 掛載家目錄(~)的 NAS 空間即將不足,我們將於 4/28(日) 00:00 開始清除長時間沒有修改紀錄的資料夾
-本次清理,我們將移除 2024/02/01 以後沒有修改紀錄的資料夾、檔案
+本次清理,我們將移除 '''+ DATE +''' 以後沒有修改紀錄的資料夾、檔案
以下是您家目錄中可能被移除的內容:
{}
@@ -43,3 +43,24 @@ Email: unix_manager@cmlab.csie.ntu.edu.tw
'''
+# leave msg for the user
+MOVE_MSG = """
+Hi,
+
+we have move out your files:
+{}
+
+If you have any problem, please contact us!
+
+Best,
+CMLab Unix Manager, Ting-Jun Wang
+CMLab, National Taiwan University
+Email: unix_manager@cmlab.csie.ntu.edu.tw
+ __ __ _
+ / / / /__ (_)_ __
+ __ ___/ /_/ / _ \/ /\ \ /
+ / |/ /\____/_//_/_//_\_\____ ____
+ / /|_/ / _ `/ _ \/ _ `/ _ `/ -_) __/
+/_/ /_/\_,_/_//_/\_,_/\_, /\__/_/
+ /___/
+"""
diff --git a/main.py b/main.py
index b122e1a..d22cab4 100644
--- a/main.py
+++ b/main.py
@@ -32,41 +32,12 @@ CLEAN_PATH
from src.mailer import Mailer
from src.cleaner import Cleaner
+from src.users import *
import os
-from constant import KEEP_FILES, WHITE_LIST, DATE, GOOGLE_OWNER, MAIL_MESSAGE
+from constant import GOOGLE_OWNER
GOOGLE_CODE = os.environ['GOOGLE_CODE']
-def get_master():
- ans = []
- for year in os.listdir(f'/home/master/'):
- if os.path.isdir(f'/home/master/{year}'):
- for user in os.listdir(f'/home/master/{year}'):
- ans.append(f'/home/master/{year}/{user}')
- return ans
-
-def get_phd():
- ans = []
- for dir_name in os.listdir(f'/home/phd/'):
- if dir_name.isnumeric():
- year = dir_name
- for user in os.listdir(f'/home/phd/{year}'):
- ans.append(f'/home/phd/{year}/{user}')
- else:
- ans.append(f'/home/phd/{dir_name}')
- return ans
-
-def get_ra():
- ans = []
- for user in os.listdir(f'/home/ra/'):
- ans.append(f'/home/ra/{user}')
- return ans
-
-def get_extra():
- ans = []
- for user in os.listdir(f'/home/extra/'):
- ans.append(f'/home/extra/{user}')
- return ans
def main(clean_type, test=True, notify_stage=True):
mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE)
@@ -81,8 +52,8 @@ def main(clean_type, test=True, notify_stage=True):
# start clean
for index, student_dir in enumerate(student_list):
print(f'{index+1}/{len(student_list)} {student_dir}')
- cleaner = Cleaner(student_dir, DATE, WHITE_LIST, KEEP_FILES, mailer, MAIL_MESSAGE)
+ cleaner = Cleaner(student_dir, mailer)
cleaner.run(test=test, notify_stage=notify_stage)
if __name__ == "__main__":
- main('home', test=True, notify_stage=False)
+ main('home', test=True, notify_stage=True)
diff --git a/src/cleaner.py b/src/cleaner.py
index acd28a7..487178e 100644
--- a/src/cleaner.py
+++ b/src/cleaner.py
@@ -1,14 +1,11 @@
import os
import subprocess
+from constant import WHITE_LIST, KEEP_FILES, DATE, MAIL_MSG, MOVE_MSG
class Cleaner():
- def __init__(self, user_dir, date, white_list, keep_files, mailer=None, mail_template=""):
+ def __init__(self, user_dir, mailer=None):
self.user_dir = user_dir
- self.date = date
- self.white_list = white_list
- self.keep_files = keep_files
self.mailer = mailer
- self.mail_template = mail_template
def run(self, test=True, notify_stage=True):
'''
@@ -18,13 +15,13 @@ class Cleaner():
notify_stage=0, test=0: 會刪除,不會寄信(刪除階段)
'''
username = self.user_dir.split('/')[-1]
- if username in self.white_list:
+ if username in WHITE_LIST:
print(f' WHITE LIST: {username}')
return
remove_objs = []
for obj in sorted(os.listdir(self.user_dir)):
- if obj in self.keep_files:
+ if obj in KEEP_FILES:
continue
obj = obj.replace(' ', '\ ')
@@ -38,22 +35,36 @@ class Cleaner():
# print $2: column 2 (get date)
modify_date = subprocess.getoutput(f"stat {self.user_dir}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" )
- if modify_date < self.date:
+ if modify_date < DATE:
remove_objs.append((obj, modify_date))
print(f' {modify_date} {obj}')
-
+
if notify_stage and len(remove_objs) != 0:
self.notify(username, test, remove_objs)
elif not notify_stage and len(remove_objs) != 0:
# delete files
remove_obj_names = [ name for name, date in remove_objs ]
- command = 'rm -rf {}'.format(" ".join(remove_obj_names))
if test:
+ command = 'rm -rf {}'.format(" ".join(remove_obj_names))
print(f' we will run:\n {command}')
else:
- print('remove')
- # subprocess.getoutput(command)
+ if not os.path.isdir(f'/volume1/cmlabhome/home_backup/{username}'):
+ os.mkdir(f'/volume1/cmlabhome/home_backup/{username}')
+
+ msg = ""
+ for name, date in remove_objs:
+ if name[0] != '.' and 'conda' not in name:
+ command = f'mv {self.user_dir}/{name} /volume1/cmlabhome/home_backup/{username}/{name}'
+ else:
+ command = f'rm -rf {self.user_dir}/{name}'
+ print(command)
+ subprocess.getoutput(command)
+ msg += f" - {name}\n"
+ msg = MOVE_MSG.format(msg)
+
+ with open(f'{self.user_dir}/PLEASE_READ_ME', 'w') as fp:
+ fp.write(msg)
def notify(self, username, test, remove_objs):
if self.mailer == None:
@@ -68,6 +79,6 @@ class Cleaner():
msg = ''
for obj, date in remove_objs:
msg += f'- {date} - {obj}
'
- msg = self.mail_template.format(username, msg)
+ msg = MAIL_MSG.format(username, msg)
self.mailer.send(sender, receiver, subject, msg)