def startTaskApi(self): sid = request.form.get('id', '') echo = public.M('crontab').where('id=?', (sid, )).getField('echo') execstr = public.getServerDir() + '/cron/' + echo os.system('chmod +x ' + execstr) os.system('nohup ' + execstr + ' >> ' + execstr + '.log 2>&1 &') return public.returnJson(True, '任务已执行!')
def getDataListApi(self): stype = request.form.get('type', '').encode('utf-8') if stype == 'databases': db_list = {} db_list['orderOpt'] = [] path = public.getServerDir() + '/mysql' if not os.path.exists(path + '/mysql.db'): db_list['data'] = [] else: db_list['data'] = public.M('databases').dbPos( path, 'mysql').field('name,ps').select() return public.getJson(db_list) data = {} data['data'] = public.M(stype).field('name,ps').select() data['orderOpt'] = [] # try: # tmp = public.readFile('data/libList.conf') # libs = json.loads(tmp) # import imp # for lib in libs: # imp.find_module(lib['module']) # tmp = {} # tmp['name'] = lib['name'] # tmp['value'] = lib['opt'] # data['orderOpt'].append(tmp) # except Exception as e: # print e return public.getJson(data)
def logsApi(self): sid = request.form.get('id', '') echo = public.M('crontab').where("id=?", (sid, )).field('echo').find() logFile = public.getServerDir() + '/cron/' + echo['echo'] + '.log' if not os.path.exists(logFile): return public.returnJson(False, '当前日志为空!') log = public.getNumLines(logFile, 2000) return public.returnJson(True, log)
def closeLogsApi(self): logPath = public.getLogsDir() os.system('rm -f ' + logPath + '/*') os.system('kill -USR1 `cat ' + public.getServerDir() + 'openresty/nginx/logs/nginx.pid`') public.writeLog('文件管理', '网站日志已被清空!') tmp = self.getDirSize(logPath) return public.returnJson(True, tmp[0].split()[0])
def delLogsApi(self): sid = request.form.get('id', '') try: echo = public.M('crontab').where("id=?", (sid, )).getField('echo') logFile = public.getServerDir() + '/cron/' + echo + '.log' os.remove(logFile) return public.returnJson(True, '任务日志已清空!') except: return public.returnJson(False, '任务日志清空失败!')
def syncToCrond(self, cronInfo): if 'status' in cronInfo: if cronInfo['status'] == 0: return False if 'where_hour' in cronInfo: cronInfo['hour'] = cronInfo['where_hour'] cronInfo['minute'] = cronInfo['where_minute'] cronInfo['week'] = cronInfo['where1'] cuonConfig, cronInfo, name = self.getCrondCycle(cronInfo) cronPath = public.getServerDir() + '/cron' cronName = self.getShell(cronInfo) if type(cronName) == dict: return cronName cuonConfig += ' ' + cronPath + '/' + cronName + \ ' >> ' + cronPath + '/' + cronName + '.log 2>&1' wRes = self.writeShell(cuonConfig) if type(wRes) != bool: return False self.crondReload()
def delApi(self): sid = request.form.get('id', '') try: find = public.M('crontab').where( "id=?", (sid, )).field('name,echo').find() if not self.removeForCrond(find['echo']): return public.returnJson(False, '无法写入文件,请检查是否开启了系统加固功能!') cronPath = public.getServerDir() + '/cron' sfile = cronPath + '/' + find['echo'] if os.path.exists(sfile): os.remove(sfile) sfile = cronPath + '/' + find['echo'] + '.log' if os.path.exists(sfile): os.remove(sfile) public.M('crontab').where("id=?", (sid, )).delete() public.writeLog('计划任务', public.getInfo('删除计划任务[{1}]成功!', (find['name'], ))) return public.returnJson(True, '删除成功') except Exception as e: return public.returnJson(False, '删除失败:' + str(e))
def getShell(self, param): # try: stype = param['stype'] if stype == 'toFile': shell = param.sFile else: head = "#!/bin/bash\nPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin\nexport PATH\n" log = '.log' wheres = { 'path': head + "python " + public.getServerDir() + "/mdserver-web/scripts/backup.py path " + param['sname'] + " " + str(param['save']), 'site': head + "python " + public.getServerDir() + "/mdserver-web/scripts/backup.py site " + param['sname'] + " " + str(param['save']), 'database': head + "python " + public.getServerDir() + "/mdserver-web/scripts/backup.py database " + param['sname'] + " " + str(param['save']), 'logs': head + "python " + public.getServerDir() + "/mdserver-web/scripts/logs_backup.py " + param['sname'] + log + " " + str(param['save']), 'rememory': head + "/bin/bash " + public.getServerDir() + '/mdserver-web/scripts/rememory.sh' } if param['backup_to'] != 'localhost': cfile = public.getServerDir( ) + "/mdserver-web/plugin/" + param['backup_to'] + "/" + param[ 'backup_to'] + "_main.py" if not os.path.exists(cfile): cfile = public.getServerDir() + "/mdserver-web/script/backup_" + \ param['backup_to'] + ".py" wheres = { 'path': head + "python " + cfile + " path " + param['sname'] + " " + str(param['save']), 'site': head + "python " + cfile + " site " + param['sname'] + " " + str(param['save']), 'database': head + "python " + cfile + " database " + param['sname'] + " " + str(param['save']), 'logs': head + "python " + public.getServerDir() + "/mdserver-web/scripts/logs_backup.py " + param['sname'] + log + " " + str(param['save']), 'rememory': head + "/bin/bash " + public.getServerDir() + '/mdserver-web/scripts/rememory.sh' } try: shell = wheres[stype] except: if stype == 'toUrl': shell = head + "curl -sS --connect-timeout 10 -m 60 '" + \ param['urladdress'] + "'" else: shell = head + param['sbody'].replace("\r\n", "\n") shell += ''' echo "----------------------------------------------------------------------------" endDate=`date +"%Y-%m-%d %H:%M:%S"` echo "★[$endDate] Successful" echo "----------------------------------------------------------------------------" ''' cronPath = public.getServerDir() + '/cron' if not os.path.exists(cronPath): public.execShell('mkdir -p ' + cronPath) if not 'echo' in param: cronName = public.md5(public.md5(str(time.time()) + '_mw')) else: cronName = param['echo'] file = cronPath + '/' + cronName public.writeFile(file, self.checkScript(shell)) public.execShell('chmod 750 ' + file) return cronName
def addApi(self): iname = request.form.get('name', '') field_type = request.form.get('type', '') week = request.form.get('week', '') where1 = request.form.get('where1', '') hour = request.form.get('hour', '') minute = request.form.get('minute', '') save = request.form.get('save', '') backup_to = request.form.get('backupTo', '') stype = request.form.get('sType', '') sname = request.form.get('sName', '') sbody = request.form.get('sBody', '') urladdress = request.form.get('urladdress', '') if len(iname) < 1: return public.returnJson(False, '任务名称不能为空!') params = { 'name': iname, 'type': field_type, 'week': week, 'where1': where1, 'hour': hour, 'minute': minute, 'save': save, 'backup_to': backup_to, 'stype': stype, 'sname': sname, 'sbody': sbody, 'urladdress': urladdress, } # print params cuonConfig, get, name = self.getCrondCycle(params) cronPath = public.getServerDir() + '/cron' cronName = self.getShell(params) # print cuonConfig, _params, name # print cronPath, cronName # print stype if type(cronName) == dict: return cronName cuonConfig += ' ' + cronPath + '/' + cronName + \ ' >> ' + cronPath + '/' + cronName + '.log 2>&1' wRes = self.writeShell(cuonConfig) if type(wRes) != bool: return wRes self.crondReload() addData = public.M('crontab').add( 'name,type,where1,where_hour,where_minute,echo,addtime,status,save,backup_to,stype,sname,sbody,urladdress', (iname, field_type, where1, hour, minute, cronName, time.strftime('%Y-%m-%d %X', time.localtime()), 1, save, backup_to, stype, sname, sbody, urladdress)) if addData > 0: return public.returnJson(True, '添加成功') return public.returnJson(False, '添加失败')
def getServerDir(): return public.getServerDir() + '/' + getPluginName()
def contentReplace(content): service_path = public.getServerDir() content = content.replace('{$SERVER_PATH}', service_path) return content