From c441763d3992dcf655e68abe3540fbe602c189d3 Mon Sep 17 00:00:00 2001 From: TING-JUN WANG Date: Fri, 26 Apr 2024 21:14:21 +0800 Subject: [PATCH] fix: correct test & sendmail's condition --- constant.py | 45 ++++++++++++++++++++++++++++ main.py | 27 +++++------------ src/cleaner.py | 80 +++++++++++++++++++++----------------------------- 3 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 constant.py diff --git a/constant.py b/constant.py new file mode 100644 index 0000000..31c9267 --- /dev/null +++ b/constant.py @@ -0,0 +1,45 @@ +# won't delete these files +KEEP_FILES = [".bashrc", ".bash_logout", ".bash_profile", "public-html", ".ssh", ".profile", "examples.desktop", ".bash_history", ".config", ".zshrc", ".vimrc", ".gnupg", ".oh-my-zsh", ".vim", ".viminfo", ".vscode-server", ".gitconfig", ".local", ".zsh_history", ".zsh_profile"] + +# won't scan this directory +WHITE_LIST = ["mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang", "unixmanager5xx"] + +# filter date +DATE = '2024-02-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 = ''' +Hi, {}
+
+因 CML Server 掛載家目錄(~)的 NAS 空間即將不足,我們將於 4/28(日) 00:00 開始清除長時間沒有修改紀錄的資料夾
+
+本次清理,我們將移除 2024/02/01 以後沒有修改紀錄的資料夾、檔案
+以下是您家目錄中可能被移除的內容:
+ +
+若是仍要保留的資料請隨意對其做修改,以更新修改紀錄的時間。
+造成不便,敬請見諒!

+ +
+* 信件由網管自動化工具寄出,若為錯誤寄送請忽略此信件。
+
+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 3d1a1d6..af29a14 100644 --- a/main.py +++ b/main.py @@ -33,18 +33,10 @@ CLEAN_PATH from src.mailer import Mailer from src.cleaner import Cleaner import os +from constant import KEEP_FILES, WHITE_LIST, DATE, GOOGLE_OWNER, MAIL_MESSAGE - -CLEAN_PATH = "/home/master/12" -KEEP_FILES = [".bashrc", ".bash_logout", ".bash_profile", "public-html", ".ssh", ".profile", "examples.desktop", ".bash_history", ".config", ".zshrc", ".vimrc", ".gnupg", ".oh-my-zsh", ".vim", ".viminfo", ".vscode-server", ".gitconfig", ".local", ".zsh_history", ".zsh_profile"] -WHITE_LIST = ["mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang", "unixmanager5xx"] - -DATE = '2024-02-01' - -GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw" GOOGLE_CODE = os.environ['GOOGLE_CODE'] - def get_master(): ans = [] for year in os.listdir(f'/home/master/'): @@ -76,26 +68,21 @@ def get_extra(): ans.append(f'/home/extra/{user}') return ans - -def main(clean_type, test=True, send_mail=False): +def main(clean_type, test=True, notify_stage=True): mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE) if clean_type == 'home': student_list = [] + # find all student's path for student_type in ['master', 'phd', 'ra', 'extra']: func = globals()[f'get_{student_type}'] student_list += func() + # start clean for index, student_dir in enumerate(student_list): print(f'{index+1}/{len(student_list)} {student_dir}') - cleaner = Cleaner(student_dir, "2024-02-01", mailer, WHITE_LIST, KEEP_FILES) - cleaner.run(test=test, send_mail=send_mail) - - # mailer.send('unix_manager@cmlab.csie.ntu.edu.tw', 'snsd0805@cmlab.csie.ntu.edu.tw', '[測試郵件] test', 'Test mail') - - # cleaner = Cleaner('/home/master/12', "2024/02/01", mailer, WHITE_LIST, KEEP_FILES) - # cleaner.run(test=test, send_mail=send_mail) - + cleaner = Cleaner(student_dir, "2024-02-01", mailer, WHITE_LIST, KEEP_FILES, MAIL_MESSAGE) + cleaner.run(test=test, notify_stage=notify_stage) if __name__ == "__main__": - main('home', test=True, send_mail=False) + main('home', test=True, notify_stage=False) diff --git a/src/cleaner.py b/src/cleaner.py index 0a7f9d4..839d167 100644 --- a/src/cleaner.py +++ b/src/cleaner.py @@ -1,47 +1,22 @@ import os import subprocess -MAIL_MESSAGE = ''' -Hi, {}
-
-因 CML Server 掛載家目錄(~)的 NAS 空間即將不足,我們將於 4/28(日) 00:00 開始清除長時間沒有修改紀錄的資料夾
-
-本次清理,我們將移除 2024/02/01 以後沒有修改紀錄的資料夾、檔案
-以下是您家目錄中可能被移除的內容:
- -
-若是仍要保留的資料請隨意對其做修改,以更新修改紀錄的時間。
-造成不便,敬請見諒!

- -
-* 信件由網管自動化工具寄出,若為錯誤寄送請忽略此信件。
-
-Best,
-CMLab Unix Manager, Ting-Jun Wang
-CMLab, National Taiwan University
-Email: unix_manager@cmlab.csie.ntu.edu.tw
-
-            __  __     _
-           / / / /__  (_)_ __
-   __  ___/ /_/ / _ \/ /\ \ /
-  /  |/  /\____/_//_/_//_\_\____ ____
- / /|_/ / _ `/ _ \/ _ `/ _ `/ -_) __/
-/_/  /_/\_,_/_//_/\_,_/\_, /\__/_/
-                      /___/
-
-''' - class Cleaner(): - def __init__(self, user_dir, date, mailer, white_list, keep_files): + def __init__(self, user_dir, date, mailer, white_list, keep_files, mail_template): 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, send_mail=False): + def run(self, test=True, notify_stage=True): + ''' + notify_stage=1, test=1: 不會刪,不會寄信(只寄給開發人員) + notify_stage=1, test=0: 不會刪,會寄信 (提醒階段) + notify_stage=0, test=1: 不會刪,不會寄信(確認) + notify_stage=0, test=0: 會刪除,不會寄信(刪除階段) + ''' username = self.user_dir.split('/')[-1] if username in self.white_list: print(f' WHITE LIST: {username}') @@ -67,16 +42,29 @@ class Cleaner(): remove_objs.append((obj, modify_date)) print(f' {modify_date} {obj}') - if send_mail and len(remove_objs) != 0: - if test: - receiver = 'snsd0805@cmlab.csie.ntu.edu.tw' - else: - receiver = f'{username}@cmlab.csie.ntu.edu.tw' - sender = 'unix_manager@cmlab.csie.ntu.edu.tw' - subject = f'[網管通知] CML Server 清理預計被移除的內容({username})' - msg = '' - for obj, date in remove_objs: - msg += f'
  • {date} - {obj}
  • ' - msg = MAIL_MESSAGE.format(username, msg) + if notify_stage and len(remove_objs) != 0: + self.notify(username, test, remove_objs) - self.mailer.send(sender, receiver, subject, msg) + 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: + print(f' we will run:\n {command}') + else: + print('remove') + # subprocess.getoutput(command) + + def notify(self, username, test, remove_objs): + if test: + receiver = 'snsd0805@cmlab.csie.ntu.edu.tw' + else: + receiver = f'{username}@cmlab.csie.ntu.edu.tw' + sender = 'unix_manager@cmlab.csie.ntu.edu.tw' + subject = f'[網管通知] CML Server 清理預計被移除的內容({username})' + msg = '' + for obj, date in remove_objs: + msg += f'
  • {date} - {obj}
  • ' + msg = self.mail_template.format(username, msg) + + self.mailer.send(sender, receiver, subject, msg)