mirror of
https://github.com/aaPanel/aaPanel.git
synced 2026-09-30 13:24:50 +02:00
update to 6.8.37
This commit is contained in:
+90
-11
@@ -6,7 +6,7 @@
|
||||
# +-------------------------------------------------------------------
|
||||
# | Author: hwliang <hwl@bt.cn>
|
||||
# +-------------------------------------------------------------------
|
||||
import public,db,os,time,re
|
||||
import public,db,os,time,re, json
|
||||
from BTPanel import session,cache
|
||||
class crontab:
|
||||
field = 'id,name,type,where1,where_hour,where_minute,echo,addtime,status,save,backupTo,sName,sBody,sType,urladdress'
|
||||
@@ -57,10 +57,63 @@ class crontab:
|
||||
|
||||
log_file = '/www/server/cron/{}.log'.format(tmp['echo'])
|
||||
if os.path.exists(log_file):
|
||||
tmp['addtime'] = public.format_date(times=int(os.path.getmtime(log_file)))
|
||||
tmp['addtime'] = self.get_last_exec_time(log_file)
|
||||
data.append(tmp)
|
||||
return data
|
||||
|
||||
def get_backup_list(self, args):
|
||||
'''
|
||||
@name 获取指定备份任务的备份文件列表
|
||||
@author hwliang
|
||||
@param args<dict> 参数{
|
||||
cron_id<int> 任务ID 必填
|
||||
p<int> 页码 默认1
|
||||
rows<int> 每页显示条数 默认10
|
||||
callback<string> jsonp回调函数 默认为空
|
||||
}
|
||||
@return <dict>{
|
||||
page<str> 分页HTML
|
||||
data<list> 数据列表
|
||||
}
|
||||
'''
|
||||
|
||||
p = args.get('p/d', 1)
|
||||
rows = args.get('rows/d', 10)
|
||||
tojs = args.get('tojs/s', '')
|
||||
callback = args.get('callback/s', '') if tojs else tojs
|
||||
|
||||
cron_id = args.get('cron_id/d')
|
||||
count = public.M('backup').where('cron_id=?', (cron_id,)).count()
|
||||
data = public.get_page(count, p, rows, callback)
|
||||
data['data'] = public.M('backup').where('cron_id=?', (cron_id,)).limit(data['row'], data['shift']).select()
|
||||
return data
|
||||
|
||||
def get_last_exec_time(self,log_file):
|
||||
'''
|
||||
@name 获取上次执行时间
|
||||
@author hwliang
|
||||
@param log_file<string> 日志文件路径
|
||||
@return format_date
|
||||
'''
|
||||
exec_date = ''
|
||||
try:
|
||||
log_body = public.GetNumLines(log_file,20)
|
||||
if log_body:
|
||||
log_arr = log_body.split('\n')
|
||||
date_list = []
|
||||
for i in log_arr:
|
||||
if i.find('★') != -1 and i.find('[') != -1 and i.find(']') != -1:
|
||||
date_list.append(i)
|
||||
if date_list:
|
||||
exec_date = date_list[-1].split(']')[0].split('[')[1]
|
||||
except:
|
||||
pass
|
||||
|
||||
finally:
|
||||
if not exec_date:
|
||||
exec_date = public.format_date(times=int(os.path.getmtime(log_file)))
|
||||
return exec_date
|
||||
|
||||
|
||||
#清理日志
|
||||
def __clean_log(self):
|
||||
@@ -96,6 +149,11 @@ class crontab:
|
||||
#检查环境
|
||||
def checkBackup(self):
|
||||
if cache.get('check_backup'): return None
|
||||
|
||||
# 检查备份表是否正确
|
||||
if not public.M('sqlite_master').where('type=? AND name=? AND sql LIKE ?', ('table', 'backup','%cron_id%')).count():
|
||||
public.M('backup').execute("ALTER TABLE 'backup' ADD 'cron_id' INTEGER DEFAULT 0",())
|
||||
|
||||
#检查备份脚本是否存在
|
||||
filePath=public.GetConfigValue('setup_path')+'/panel/script/backup'
|
||||
if not os.path.exists(filePath):
|
||||
@@ -105,7 +163,6 @@ class crontab:
|
||||
if not os.path.exists(filePath):
|
||||
public.downloadFile(public.GetConfigValue('home') + '/linux/logsBackup.py',filePath)
|
||||
#检查计划任务服务状态
|
||||
|
||||
import system
|
||||
sm = system.system()
|
||||
if os.path.exists('/etc/init.d/crond'):
|
||||
@@ -128,7 +185,8 @@ class crontab:
|
||||
self.remove_for_crond(cronInfo['echo'])
|
||||
else:
|
||||
cronInfo['status'] = 1
|
||||
self.sync_to_crond(cronInfo)
|
||||
if not self.sync_to_crond(cronInfo):
|
||||
return public.return_msg_gettext(False,'Unable to write to file, please check if system hardening is enabled!')
|
||||
|
||||
public.M('crontab').where('id=?',(id,)).setField('status',status)
|
||||
public.WriteLog('TYPE_CRON',"MODIFY_CRON_STATUS",(cronInfo['name'],str(status_msg[status])))
|
||||
@@ -163,10 +221,13 @@ class crontab:
|
||||
get['minute'],get['save'],get['backupTo'],get['sBody'],
|
||||
get['urladdress'],get['save_local'],get["notice"],
|
||||
get["notice_channel"])
|
||||
public.M('crontab').where('id=?',(id,)).save(columns,values)
|
||||
self.remove_for_crond(cronInfo['echo'])
|
||||
self.sync_to_crond(cronInfo)
|
||||
public.WriteLog('TYPE_CRON',"MODIFY_CRON",(cronInfo['name']))
|
||||
if cronInfo['status'] == 0: return public.return_msg_gettext(False, 'The current task is Disable status, please open the task before modifying!')
|
||||
if not self.sync_to_crond(cronInfo):
|
||||
return public.return_msg_gettext(False,'Unable to write to file, please check if system hardening is enabled!')
|
||||
public.M('crontab').where('id=?',(id,)).save(columns,values)
|
||||
|
||||
public.WriteLog('TYPE_CRON',"MODIFY_CRON",(cronInfo['name'],))
|
||||
return public.return_msg_gettext(True,'Setup successfully!')
|
||||
|
||||
|
||||
@@ -178,8 +239,7 @@ class crontab:
|
||||
|
||||
#同步到crond
|
||||
def sync_to_crond(self,cronInfo):
|
||||
if 'status' in cronInfo:
|
||||
if cronInfo['status'] == 0: return False
|
||||
if not 'status' in cronInfo: return False
|
||||
if 'where_hour' in cronInfo:
|
||||
cronInfo['hour'] = cronInfo['where_hour']
|
||||
cronInfo['minute'] = cronInfo['where_minute']
|
||||
@@ -188,11 +248,13 @@ class crontab:
|
||||
cronPath=public.GetConfigValue('setup_path')+'/cron'
|
||||
cronName=self.GetShell(cronInfo)
|
||||
if type(cronName) == dict: return cronName
|
||||
#if cronInfo['status'] == 0: return False
|
||||
cuonConfig += ' ' + cronPath+'/'+cronName+' >> '+ cronPath+'/'+cronName+'.log 2>&1'
|
||||
wRes = self.WriteShell(cuonConfig)
|
||||
if type(wRes) != bool: return False
|
||||
self.CrondReload()
|
||||
|
||||
return True
|
||||
|
||||
#添加计划任务
|
||||
def AddCrontab(self,get):
|
||||
if len(get['name'])<1:
|
||||
@@ -219,6 +281,10 @@ class crontab:
|
||||
1,get['save'],get['backupTo'],get['sType'],get['sName'],get['sBody'],
|
||||
get['urladdress'], get["save_local"], get['notice'], get['notice_channel'])
|
||||
addData=public.M('crontab').add(columns,values)
|
||||
public.add_security_logs('TYPE_CRON','Add Cron tasks ['+get['name']+'] success'+str(values))
|
||||
if type(addData) == str:
|
||||
return public.return_msg_gettext(False, addData)
|
||||
public.WriteLog('TYPE_CRON', 'Add Cron tasks [' + get['name'] + '] success')
|
||||
if addData>0:
|
||||
result = public.return_msg_gettext(True,'Setup successfully!')
|
||||
result['id'] = addData
|
||||
@@ -287,7 +353,13 @@ class crontab:
|
||||
#取数据列表
|
||||
def GetDataList(self,get):
|
||||
data = {}
|
||||
data['data'] = public.M(get['type']).field('name,ps').select()
|
||||
if get['type'] == 'databases':
|
||||
data['data'] = public.M(get['type']).where("type=?","MySQL").field('name,ps').select()
|
||||
else:
|
||||
data['data'] = public.M(get['type']).field('name,ps').select()
|
||||
for i in data['data']:
|
||||
if 'ps' in i:
|
||||
i['ps'] = public.xsssec(i['ps'])
|
||||
data['orderOpt'] = []
|
||||
import json
|
||||
tmp = public.readFile('data/libList.conf')
|
||||
@@ -328,6 +400,7 @@ class crontab:
|
||||
try:
|
||||
id = get['id']
|
||||
find = public.M('crontab').where("id=?",(id,)).field('name,echo').find()
|
||||
if not find: return public.return_msg_gettext(False, 'The specified task does not exist!')
|
||||
if not self.remove_for_crond(find['echo']): return public.return_msg_gettext(False,'Unable to write to file, please check if system hardening is enabled!')
|
||||
cronPath = public.GetConfigValue('setup_path') + '/cron'
|
||||
sfile = cronPath + '/' + find['echo']
|
||||
@@ -336,6 +409,7 @@ class crontab:
|
||||
if os.path.exists(sfile): os.remove(sfile)
|
||||
|
||||
public.M('crontab').where("id=?",(id,)).delete()
|
||||
public.add_security_logs("Delete cron", "Delete cron:" + find['name'])
|
||||
public.WriteLog('TYPE_CRON', 'CRONTAB_DEL',(find['name'],))
|
||||
return public.return_msg_gettext(True, 'Successfully deleted')
|
||||
except:
|
||||
@@ -344,7 +418,10 @@ class crontab:
|
||||
#从crond删除
|
||||
def remove_for_crond(self,echo):
|
||||
file = self.get_cron_file()
|
||||
if not os.path.exists(file):
|
||||
return False
|
||||
conf=public.readFile(file)
|
||||
if not conf: return False
|
||||
if conf.find(str(echo)) == -1: return True
|
||||
rep = ".+" + str(echo) + ".+\n"
|
||||
conf = re.sub(rep, "", conf)
|
||||
@@ -479,6 +556,8 @@ echo "--------------------------------------------------------------------------
|
||||
if not os.path.exists(u_path):
|
||||
os.makedirs(u_path,472)
|
||||
public.ExecShell("chown root:crontab {}".format(u_path))
|
||||
if not os.path.exists(cron_path):
|
||||
public.writeFile(cron_path,"")
|
||||
return cron_path
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user