Compare commits

..

No commits in common. "c97fa9334bb1b9a7fa27099fc3684a7a6cc611c9" and "e8a13889b3d7c10f1b1aa85277710e68899ba708" have entirely different histories.

4 changed files with 111 additions and 210 deletions

111
clean.py Normal file
View File

@ -0,0 +1,111 @@
'''
Clean the direction in CML Server (tmp2 or home directory)
Suppose that the CLEAN_PATH may seems like this.
And we will only check the directories & files under the user directory with "depth=1"
CLEAN_PATH
|
+---- user1 (user directory)
| |
| +--- file1 (depth=1, will be check)
| |
| +--- Project1 (depth=1, will be check)
| | |
| | +--- subset1-1 (depth=2, won't be check)
| |
| +--- Project2 (depth=1)
| |
| +--- subset2-1 (depth=2)
| |
| +--- subset2-2 (depth=2)
|
+---- user2 (user directory)
|
+--- Project1 (depth=1)
|
+--- subset1-1 (depth=2)
'''
import os
import subprocess
import smtplib
from email.mime.text import MIMEText
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"]
WHITE_LIST = ["mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang"]
DATE = '2024-02-01'
GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw"
GOOGLE_CODE = os.environ['GOOGLE_CODE']
class Mailer():
def __init__(self, google_owner, google_code):
self.google_owner = google_owner
self.google_code = google_code
def send(self, sender, receiver, subject, msg):
msg = MIMEText(msg, 'html')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login(self.google_owner, self.google_code)
status = smtp.send_message(msg)
smtp.quit()
if status == {}:
return True
else:
return False
class Cleaner():
def __init__(self, root, date, mailer, white_list=WHITE_LIST, keep_files=KEEP_FILES):
self.root = root
self.date = date
self.white_list = white_list
self.keep_files = keep_files
self.mailer = mailer
def run(self, test=True, send_mail=False):
for user in sorted(os.listdir(self.root)):
print(user)
if user in self.white_list:
continue
for obj in sorted(os.listdir(f'{self.root}/{user}')):
if obj in self.keep_files:
continue
obj = obj.replace(' ', '\ ')
# stat {file or dir}:
# get Create, modified information
# grep Modify:
# get Modified time
# awk:
# -F ' ': use space to split the data
# print $2: column 2 (get date)
modify_date = subprocess.getoutput(f"stat {self.root}/{user}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" )
needRemove = modify_date < DATE
print(f' {obj}')
print(f' {modify_date} {needRemove}')
mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE)
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)
cleaner.run()

View File

@ -1,82 +0,0 @@
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):
self.user_dir = user_dir
self.date = date
self.white_list = white_list
self.keep_files = keep_files
self.mailer = mailer
def run(self, test=True, send_mail=False):
username = self.user_dir.split('/')[-1]
if username in self.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:
continue
obj = obj.replace(' ', '\ ')
# stat {file or dir}:
# get Create, modified information
# grep Modify:
# get Modified time
# awk:
# -F ' ': use space to split the data
# 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:
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'<li>{date} - {obj}</li>'
msg = MAIL_MESSAGE.format(username, msg)
self.mailer.send(sender, receiver, subject, msg)

View File

@ -1,27 +0,0 @@
import smtplib
from email.mime.text import MIMEText
class Mailer():
def __init__(self, google_owner, google_code):
self.google_owner = google_owner
self.google_code = google_code
def send(self, sender, receiver, subject, msg):
msg = MIMEText(msg, 'html')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login(self.google_owner, self.google_code)
status = smtp.send_message(msg)
smtp.quit()
if status == {}:
return True
else:
return False

101
main.py
View File

@ -1,101 +0,0 @@
'''
Clean the direction in CML Server (tmp2 or home directory)
Suppose that the CLEAN_PATH may seems like this.
And we will only check the directories & files under the user directory with "depth=1"
CLEAN_PATH
|
+---- user1 (user directory)
| |
| +--- file1 (depth=1, will be check)
| |
| +--- Project1 (depth=1, will be check)
| | |
| | +--- subset1-1 (depth=2, won't be check)
| |
| +--- Project2 (depth=1)
| |
| +--- subset2-1 (depth=2)
| |
| +--- subset2-2 (depth=2)
|
+---- user2 (user directory)
|
+--- Project1 (depth=1)
|
+--- subset1-1 (depth=2)
'''
from mailer import Mailer
from cleaner import Cleaner
import os
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/'):
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, send_mail=False):
mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE)
if clean_type == 'home':
student_list = []
for student_type in ['master', 'phd', 'ra', 'extra']:
func = globals()[f'get_{student_type}']
student_list += func()
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)
if __name__ == "__main__":
main('home', test=False, send_mail=True)