Compare commits
2 Commits
227051f875
...
d66a589dfc
| Author | SHA1 | Date | |
|---|---|---|---|
| d66a589dfc | |||
| c441763d39 |
11
check
Executable file
11
check
Executable file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
from src.cleaner import Cleaner
|
||||
from constant import DATE, WHITE_LIST, KEEP_FILES
|
||||
import os
|
||||
|
||||
student_dir = os.environ['HOME']
|
||||
cleaner = Cleaner(student_dir, DATE, WHITE_LIST, KEEP_FILES)
|
||||
cleaner.run(test=True, notify_stage=False)
|
||||
|
||||
45
constant.py
Normal file
45
constant.py
Normal 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
27
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, DATE, WHITE_LIST, KEEP_FILES, mailer, 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)
|
||||
|
||||
@ -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, white_list, keep_files, mailer=None, 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,23 @@ 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 self.mailer == None:
|
||||
return
|
||||
|
||||
if test:
|
||||
receiver = 'snsd0805@cmlab.csie.ntu.edu.tw'
|
||||
else:
|
||||
@ -77,6 +68,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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user