fix: correct test & sendmail's condition

This commit is contained in:
TING-JUN WANG 2024-04-26 21:14:21 +08:00
parent 227051f875
commit c441763d39
3 changed files with 86 additions and 66 deletions

45
constant.py Normal file
View File

@ -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, {}<br>
<br>
CML Server 掛載家目錄(~) NAS 空間即將不足我們將於 4/28() 00:00 開始清除長時間沒有修改紀錄的資料夾<br>
<br>
本次清理我們將移除 2024/02/01 以後沒有修改紀錄的資料夾檔案<br>
以下是您家目錄中可能被移除的內容<br>
<ul>
{}
</ul>
<br>
若是仍要保留的資料請隨意對其做修改以更新修改紀錄的時間<br>
造成不便敬請見諒<br><br>
<br>
* 信件由網管自動化工具寄出若為錯誤寄送請忽略此信件<br>
<br>
Best,<br>
CMLab Unix Manager, Ting-Jun Wang<br>
CMLab, National Taiwan University<br>
Email: unix_manager@cmlab.csie.ntu.edu.tw<br>
<pre>
__ __ _
/ / / /__ (_)_ __
__ ___/ /_/ / _ \/ /\ \ /
/ |/ /\____/_//_/_//_\_\____ ____
/ /|_/ / _ `/ _ \/ _ `/ _ `/ -_) __/
/_/ /_/\_,_/_//_/\_,_/\_, /\__/_/
/___/
</pre>
'''

27
main.py
View File

@ -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)

View File

@ -1,47 +1,22 @@
import os
import subprocess
MAIL_MESSAGE = '''
Hi, {}<br>
<br>
CML Server 掛載家目錄(~) NAS 空間即將不足我們將於 4/28() 00:00 開始清除長時間沒有修改紀錄的資料夾<br>
<br>
本次清理我們將移除 2024/02/01 以後沒有修改紀錄的資料夾檔案<br>
以下是您家目錄中可能被移除的內容<br>
<ul>
{}
</ul>
<br>
若是仍要保留的資料請隨意對其做修改以更新修改紀錄的時間<br>
造成不便敬請見諒<br><br>
<br>
* 信件由網管自動化工具寄出若為錯誤寄送請忽略此信件<br>
<br>
Best,<br>
CMLab Unix Manager, Ting-Jun Wang<br>
CMLab, National Taiwan University<br>
Email: unix_manager@cmlab.csie.ntu.edu.tw<br>
<pre>
__ __ _
/ / / /__ (_)_ __
__ ___/ /_/ / _ \/ /\ \ /
/ |/ /\____/_//_/_//_\_\____ ____
/ /|_/ / _ `/ _ \/ _ `/ _ `/ -_) __/
/_/ /_/\_,_/_//_/\_,_/\_, /\__/_/
/___/
</pre>
'''
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,7 +42,20 @@ class Cleaner():
remove_objs.append((obj, modify_date))
print(f' {modify_date} {obj}')
if send_mail and len(remove_objs) != 0:
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:
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:
@ -77,6 +65,6 @@ class Cleaner():
msg = ''
for obj, date in remove_objs:
msg += f'<li>{date} - {obj}</li>'
msg = MAIL_MESSAGE.format(username, msg)
msg = self.mail_template.format(username, msg)
self.mailer.send(sender, receiver, subject, msg)