def GetDir(self,get): if not hasattr(get,'path'): #return public.returnMsg(False,'错误的参数!') get.path = '/www/wwwroot' if sys.version_info[0] == 2: get.path = get.path.encode('utf-8'); if get.path == '': get.path = '/www'; if not os.path.exists(get.path): return public.ReturnMsg(False,'指定目录不存在!') import pwd dirnames = [] filenames = [] search = None if hasattr(get,'search'): search = get.search.strip().lower(); if hasattr(get,'all'): return self.SearchFiles(get) #包含分页类 import page #实例化分页类 page = page.Page(); info = {} info['count'] = self.GetFilesCount(get.path,search); info['row'] = 100 info['p'] = 1 if hasattr(get,'p'): try: info['p'] = int(get['p']) except: info['p'] = 1 info['uri'] = {} info['return_js'] = '' if hasattr(get,'tojs'): info['return_js'] = get.tojs if hasattr(get,'showRow'): info['row'] = int(get.showRow); #获取分页数据 data = {} data['PAGE'] = page.GetPage(info,'1,2,3,4,5,6,7,8') i = 0; n = 0; if not hasattr(get,'reverse'): for filename in os.listdir(get.path): filename = self.xssencode(filename) if search: if filename.lower().find(search) == -1: continue; i += 1; if n >= page.ROW: break; if i < page.SHIFT: continue; try: if sys.version_info[0] == 2: filename = filename.encode('utf-8'); filePath = get.path+'/'+filename link = ''; if os.path.islink(filePath): filePath = os.readlink(filePath); link = ' -> ' + filePath; if not os.path.exists(filePath): filePath = get.path + '/' + filePath; if not os.path.exists(filePath): continue; stat = os.stat(filePath) accept = str(oct(stat.st_mode)[-3:]); mtime = str(int(stat.st_mtime)) user = '' try: user = pwd.getpwuid(stat.st_uid).pw_name except: user = str(stat.st_uid) size = str(stat.st_size) if os.path.isdir(filePath): dirnames.append(filename+';'+size+';'+mtime+';'+accept+';'+user+';'+link); else: filenames.append(filename+';'+size+';'+mtime+';'+accept+';'+user+';'+link); n += 1; except: continue; data['DIR'] = sorted(dirnames); data['FILES'] = sorted(filenames); else: reverse = bool(get.reverse) if get.reverse == 'False': reverse = False for file_info in self.__list_dir(get.path,get.sort,reverse): filename = os.path.join(get.path,file_info['name']) if not os.path.exists(filename): continue if search: if file_info['name'].lower().find(search) == -1: continue; i += 1; if n >= page.ROW: break; if i < page.SHIFT: continue; r_file = file_info['name'] + ';' + str(file_info['size']) + ';' + str(file_info['mtime']) + ';' + str(file_info['accept']) + ';' + file_info['user']+ ';' + file_info['link'] if os.path.isdir(filename): dirnames.append(r_file) else: filenames.append(r_file) n += 1; data['DIR'] = dirnames data['FILES'] = filenames data['PATH'] = str(get.path) if hasattr(get,'disk'): import system data['DISK'] = system.system().GetDiskInfo(); return data
def create_site(self, get): pdata = loads(get['data']) #表单验证 checkRootPath = self.check_root_path(pdata['rootPath']) if checkRootPath: return checkRootPath domainFormat = self.domain_format(pdata['domains']) if domainFormat: return public.ReturnMsg(False, 'SITE_DOMAIN_FORMAT_ERR', (domainFormat, )) domainExists = self.domain_exists(pdata['domains']) if domainExists: return public.ReturnMsg(False, 'SITE_DOMAIN_EXIST', (domainExists, )) return self._generate_nginx_conf(pdata['siteName']) ''' server { listen 80; server_name huang.bt.cn bt001.bt.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/huang.bt.cn; #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 error_page 404 /404.html; error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-70.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/huang.bt.cn.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user\.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README\.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known { allow all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log off; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log off; } access_log /www/wwwlogs/huang.bt.cn.log; error_log /www/wwwlogs/huang.bt.cn.error.log; };''' return pdata
def SetSlave(self, get): if not self.GetPort(get): return public.returnMsg(False, '请确定数据库是否已经开启') if not self.CheckBinLog(): return public.returnMsg(False, '请先开启Mysql二进制日志') sconf = public.readFile(self.__mfile) # 备份需要同步的数据库 masterinfo = self.BackUpMasterDbs(get) try: bkstatus = masterinfo["msg"] except: bkstatus = False if bkstatus: return bkstatus __dbpass = public.M('config').where('id=?', (1, )).getField('mysql_root') slave_version = masterinfo["slave_version"] create_replicate_sql = "" # Mysql5.5版本 if "5.5" in slave_version: create_replicate_sql += "CHANGE MASTER TO MASTER_HOST='%s',MASTER_PORT=%s,MASTER_USER='******',MASTER_PASSWORD='******',MASTER_LOG_FILE='%s',MASTER_LOG_POS=%s" % ( masterinfo["master_ip"], masterinfo["master_port"], masterinfo["slave_user"], masterinfo["slave_pass"], masterinfo["logfile"], masterinfo["logpos"]) # Mysql5.6+版本 addconf = """ log-slave-updates=true enforce-gtid-consistency=true gtid-mode=on""" if "5.5" not in slave_version and not re.match("10", slave_version): if not re.search("gtid-mode=on", sconf): sconf = re.sub("\[mysqld\]", "[mysqld]" + addconf, sconf) create_replicate_sql += "CHANGE MASTER TO MASTER_HOST='%s',MASTER_PORT=%s,MASTER_USER='******',MASTER_PASSWORD='******',MASTER_AUTO_POSITION = 1" % ( masterinfo["master_ip"], masterinfo["master_port"], masterinfo["slave_user"], masterinfo["slave_pass"]) # 构造要同步的数据库配置 replicate_dbs = "" if masterinfo["replicate_dbs"][0] != "alldatabases": for d in masterinfo["replicate_dbs"]: replicate_dbs += "\nreplicate-wild-do-table = %s.%s" % (d, "%") else: sconf = re.sub("replicate-wild-do-table\s*=\s*[\w\%\.\_\-]+", "", sconf) print(replicate_dbs) try: serverid = masterinfo["slave_id"] except: serverid = [int(masterinfo["master_id"]) + 1] localip = public.ExecShell("ip a")[0] netip = public.readFile("%s/data/iplist.txt") index = 0 try: for i in masterinfo["slave_ip"]: if i in localip or i in netip: index += masterinfo["slave_ip"].index("i") break if not index: return public.returnMsg(False, '主库没有设置该主机为从服务器,请先设置主服务器后再配置从库') except: pass serverid = serverid[index] if not re.search("replicate-wild-do-table", sconf): sconf = re.sub("server-id\s*=\s*\d+", "server-id = %s%s" % (serverid, replicate_dbs), sconf) public.writeFile(self.__mfile, sconf) # 导入主库数据库 try: pid_old = public.ExecShell( "ps aux|grep 'mysql.sock'|awk 'NR==1 {print $2}'")[0].split( "\n")[0] self.WriteLog("旧PID %s" % pid_old) pid_new = pid_old public.writeFile("/tmp/mysqlpid", "") for i in range(10): if i == 1: os.system("/etc/init.d/mysqld restart &") time.sleep(10) pid_new = public.ExecShell( "ps aux|grep 'mysql.sock'|awk 'NR==1 {print $2}'" )[0].split("\n")[0] self.WriteLog("新PID %s" % pid_new) if pid_old == pid_new: time.sleep(10) pid_new = public.ExecShell( "ps aux|grep 'mysql.sock'|awk 'NR==1 {print $2}'" )[0].split("\n")[0] else: public.writeFile("/tmp/mysqlpid", "ok") break pm.panelMysql().execute("stop slave") pm.panelMysql().execute("reset master") pm.panelMysql().execute("reset slave all") self.WriteLog("开始导入数据库") speed = public.getSpeed() public.writeSpeed("导入数据库", 1, speed["total"]) error = public.ExecShell( "nohup /usr/bin/mysql -uroot -p%s < %s &" % (__dbpass, masterinfo["backsqlpath"])) self.WriteLog(str(error)) except Exception as e: self.WriteLog("导入数据库失败 %s" % e) return public.ReturnMsg(False, "导入失败") threading.Thread(target=self.SetSpeed()).start() self.WriteLog("导入数据库完成") self.WriteLog("删除备份的数据库文件") os.system("rm -f %s" % masterinfo["backsqlpath"]) os.system("rm -f /tmp/mysqlpid") self.WriteLog("重启mysql") pid_old = public.ExecShell( "ps aux|grep 'mysql.sock'|awk 'NR==1 {print $2}'")[0].split( "\n")[0] self.WriteLog("旧PID %s" % pid_old) pid_new = "" public.writeFile("/tmp/mysqlpid", "") restart = 0 for i in range(10): if i == 1: os.system("/etc/init.d/mysqld restart &") time.sleep(10) pid_new = public.ExecShell( "ps aux|grep 'mysql.sock'|awk 'NR==1 {print $2}'" )[0].split("\n")[0] self.WriteLog("新PID %s" % pid_new) if pid_old == pid_new: time.sleep(10) pid_new = public.ExecShell( "ps aux|grep 'mysql.sock'|awk 'NR==1 {print $2}'" )[0].split("\n")[0] else: public.writeFile("/tmp/mysqlpid", "ok") restart += 1 break if restart == 0: return public.ReturnMsg(False, "导入数据后重启失败") public.writeSpeed("重启数据库", int(1), int(2)) threading.Thread(target=self.SetSpeed()).start() self.WriteLog("mysql重启完成") # 写入同步的数据库到面板数据库 for i in masterinfo["replicate_dbs_info"]: if not i: continue localdb = public.M('databases').where('name=?', (i[2], )).select() if not localdb: public.M('databases').add(("name,username,password,accept,ps"), (i[2], i[3], i[4], i[5], i[6])) # 完整复制将主root密码写入到从的面板 if masterinfo["replicate_dbs"][0] == "alldatabases": self.WriteLog("因为是完整同步,修改从库面板密码为主库") public.M('config').where('id=?', (1, )).setField('mysql_root', masterinfo["slave_ips"]) result = str(pm.panelMysql().query("select version()")[0]) self.WriteLog(result) if result == "1045": public.M('config').where('id=?', (1, )).setField( 'mysql_root', __dbpass) # Mairadb10.*版本 if re.match("10", slave_version): set_slave_pos_sql = "SET GLOBAL gtid_slave_pos='%s'" % masterinfo[ "gtid"] # 需要在数据重启后配置 pm.panelMysql().query(set_slave_pos_sql) create_replicate_sql += "CHANGE MASTER TO MASTER_HOST='%s',MASTER_PORT=%s,MASTER_USER='******',MASTER_PASSWORD='******',master_use_gtid=slave_pos" % ( masterinfo["master_ip"], masterinfo["master_port"], masterinfo["slave_user"], masterinfo["slave_pass"]) self.WriteLog("停止从服务") pm.panelMysql().query("stop slave") self.WriteLog("修改从服务器的主服务器信息") pm.panelMysql().query(create_replicate_sql) self.WriteLog("启动从服务") pm.panelMysql().query("start slave") time.sleep(2) self.WriteLog("获取从状态") slavestatus = pm.panelMysql().query("show slave status")[0] self.WriteLog(str(slavestatus)) self.WriteLog("创建Slave监控用户") create_status_user = "******" % ( "user" + masterinfo["slave_user"], masterinfo["master_ip"], "pass" + masterinfo["slave_pass"]) grant_status_user = "******" % ( "user" + masterinfo["slave_user"], masterinfo["master_ip"]) pm.panelMysql().execute(create_status_user) pm.panelMysql().execute(grant_status_user) n = 0 try: for i in slavestatus: if i == "Yes": n += 1 except: return public.returnMsg(False, '获取主从状态失败') if n == 2: print("设置成功") self.WriteLog("删除btmysql用户") self.__ExceSql('delete from mysql.user where user="******"', masterinfo=masterinfo) if masterinfo["replicate_dbs"][0] != "alldatabases": self.WriteLog("删除从btmysql用户") pm.panelMysql().execute( "delete from mysql.user where user='******'") self.WriteLog("设置成功") os.system("rm -f %s" % self.totalspeedfile) return public.returnMsg(True, '设置成功') else: self.WriteLog("设置失败") os.system("rm -f %s" % self.totalspeedfile) return public.returnMsg(True, '设置失败')