def develop(): "发布应用" #提取应用信息 aid=int(request.form.get("aid",None)) sql="select * from paas_app where id = %d limit 1"%(aid) dao=db.execute(sql) appData=objToDict(dao.first()) dao.close() #把任务交给后台队列 option=request.form.get("option",None) if option == "reboot": gitUrl="" command="cp" elif option == "develop": gitUrl=appData['gitUrl'] if git.checkLocationCode(aid): command="pull" else: command="clone" executeSql="update paas_app set status = 1 where id = %d"%(aid) #为了防止恶意不断请求操作应用,因此一段时间只保存一个操作 sql="delete from paas_gitQueue where aid = %d"%(aid) dao=db.execute(sql) dao.close() sql="insert into paas_gitQueue(aid,command,gitUrl,executeSql,appAccount,appGroup,appPath) values(%d,'%s','%s','%s','%s','%s','%s')"%(aid,command,gitUrl,sqlDeal(executeSql),appData['appAccount'],appData['appGroup'],appData['appPath']) dao=db.execute(sql) dao.close() return "ok"
def optionApp(option): "操作应用" aid = int(request.form.get("aid", None)) #防止操作别人的应用 sql = "select count(*) from paas_app where uid = %d" % (session['user']) dao = db.execute(sql) r = objToDict(dao.first()) dao.close() if r['count(*)'] == 0: return abort(403) if option == "start": #启动应用 client.startApp(aid) return "ok" elif option == "stop": #停止应用 client.stopApp(aid) return "ok" elif option == "reboot": #重启应用 client.developApp(aid, "reboot") return "ok" elif option == "develop": #部署应用 client.developApp(aid, "develop") return "ok"
def buildApp(aid,appHost,language): "生成一个应用" #请求应用服务器生成应用 #根据轮转算法选择服务器 dao=db.execute("select count(*) from paas_app") r=objToDict(dao.first()) dao.close() num=r['count(*)'] % len(config.REMOTE_SERVER_PHP) data={'language':language,'appHost':config.REMOTE_SERVER_PHP[num],'aid':str(aid)} result=urlPostWithToken(config.REMOTE_SERVER_PHP[num],"/servlet/buildApp",data) #在主服务器生成反向代理配置文件 obj=json.loads(result) if obj['result'] == "ok": buildMainServerConfig(aid,appHost,obj['remoteSocket']) #把远程服务器地址写入数据库 sql="update paas_app set remoteServer = '%s' ,remoteSocket = '%s' where id = %d"%(config.REMOTE_SERVER_PHP[num],obj['remoteSocket'],aid) dao=db.execute(sql) dao.close() return True else: return False
def init(): sleepSign=True sql="select * from paas_gitQueue limit 1" dao=db.execute(sql) obj=dao.first() if obj != None: sleepSign=False r=objToDict(obj) optionResult=deal(r) if optionResult: if r['executeSql'] != '': #执行回调sql dao2=db.execute(r['executeSql']) dao2.close() else: #发布失败 dao2=db.execute("update paas_app set status = 5 where id = %d"%(r['aid'])) dao2.close() sql="delete from paas_gitQueue where id = %d"%(r['id']) dao2=db.execute(sql) dao2.close() dao.close() if sleepSign: print 'sleep 1s' time.sleep(1)
def optionApp(option): "操作应用" aid=int(request.form.get("aid",None)) #防止操作别人的应用 sql="select count(*) from paas_app where uid = %d"%(session['user']) dao=db.execute(sql) r=objToDict(dao.first()) dao.close() if r['count(*)']==0: return abort(403) if option == "start": #启动应用 client.startApp(aid) return "ok" elif option == "stop": #停止应用 client.stopApp(aid) return "ok" elif option == "reboot": #重启应用 client.developApp(aid,"reboot") return "ok" elif option == "develop": #部署应用 client.developApp(aid,"develop") return "ok"
def startApp(aid): "启动app" #提取应用数据 sql="select * from paas_app where id = %d limit 1"%(aid) dao=db.execute(sql) appData=objToDict(dao.first()) dao.close() baseObj=json.loads(getConfig("config")) data=getConfig("mainServer") data=data.replace("{{ appHost }}",appData['host']).replace("{{ remoteSocket }}",appData['remoteSocket']).replace("{{ appId }}",str(aid)) #main_作为前缀 fp=open(baseObj['nginx']['confPath']+"/main_"+str(aid)+".conf","w") fp.write(data) fp.close() #修改状态 sql="update paas_app set status = 1 where id =%d"%(aid) dao=db.execute(sql) dao.close() #平滑重启服务器 reloadServer()
def getAppFromToken(apiKey,secretKey): "获取应用信息" sql="select aid from paas_app_token where apiKey = '%s' AND secretKey = '%s'"%(sqlDeal(apiKey),sqlDeal(secretKey)) dao=db.execute(sql) r=objToDict(dao.first()) dao.close() return r
def userMessage(): "修改用户信息" uid=request.args.get("id",None) if request.method == "GET": g.title=u"修改信息" g.add=False dao=db.execute("select * from paas_account where id = %s limit 1"%(sqlDeal(uid))) g.obj=objToDict(dao.fetchone()) dao.close() return render_template("admin/userAdd.html") else: password=request.form.get("password",None) status=request.form.get("status",None) realname=request.form.get("realname",None) college=request.form.get("college",None) specialty=request.form.get("specialty",None) if password == "": args=map(sqlDeal,(status,realname,college,specialty,uid)) sql="update paas_account set status = '%s',realname = '%s',college = '%s',specialty='%s' where id = %s"%tuple(args) else: #加密密码 password=hashlib.md5(password).hexdigest() args=map(sqlDeal,(password,status,realname,college,specialty,uid)) sql="update paas_account set password = '******',status = '%s',realname = '%s',college = '%s',specialty='%s' where id = %s"%tuple(args) #修改用户信息 dao=db.execute(sql) dao.close() return redirect("/admin/userManager")
def startApp(aid): "启动app" #提取应用数据 sql = "select * from paas_app where id = %d limit 1" % (aid) dao = db.execute(sql) appData = objToDict(dao.first()) dao.close() baseObj = json.loads(getConfig("config")) data = getConfig("mainServer") data = data.replace("{{ appHost }}", appData['host']).replace( "{{ remoteSocket }}", appData['remoteSocket']).replace("{{ appId }}", str(aid)) #main_作为前缀 fp = open(baseObj['nginx']['confPath'] + "/main_" + str(aid) + ".conf", "w") fp.write(data) fp.close() #修改状态 sql = "update paas_app set status = 1 where id =%d" % (aid) dao = db.execute(sql) dao.close() #平滑重启服务器 reloadServer()
def buildApp(aid, appHost, language): "生成一个应用" #请求应用服务器生成应用 #根据轮转算法选择服务器 dao = db.execute("select count(*) from paas_app") r = objToDict(dao.first()) dao.close() num = r['count(*)'] % len(config.REMOTE_SERVER_PHP) data = { 'language': language, 'appHost': config.REMOTE_SERVER_PHP[num], 'aid': str(aid) } result = urlPostWithToken(config.REMOTE_SERVER_PHP[num], "/servlet/buildApp", data) #在主服务器生成反向代理配置文件 obj = json.loads(result) if obj['result'] == "ok": buildMainServerConfig(aid, appHost, obj['remoteSocket']) #把远程服务器地址写入数据库 sql = "update paas_app set remoteServer = '%s' ,remoteSocket = '%s' where id = %d" % ( config.REMOTE_SERVER_PHP[num], obj['remoteSocket'], aid) dao = db.execute(sql) dao.close() return True else: return False
def editApp(): "编辑应用" aid=request.args.get("id",None) if request.method == "GET": g.add=False sql="select * from paas_app where id = %s limit 1"%(sqlDeal(aid)) dao=db.execute(sql) g.obj=objToDict(dao.first()) dao.close() sql="select * from paas_account where status != 3" dao=db.execute(sql) g.users=map(objToDict,dao.fetchall()) dao.close() return render_template("admin/addApp.html") else: uid=request.form.get("uid",None) title=request.form.get("title",None) description=request.form.get("description",None) language=request.form.get("language",None) host=request.form.get("host",None) gitUrl=request.form.get("gitUrl",None) #处理git地址,防止注入恶意代码 gitUrl=gitUrl.replace(" ","") args=map(sqlDeal,[uid,title,description,language,host,gitUrl,aid]) sql="update paas_app set uid = '%s',title='%s',description='%s',language='%s',host='%s',gitUrl='%s' where id = %s"%tuple(args) dao=db.execute(sql) dao.close() return redirect("/admin/appManager")
def getAppFromToken(apiKey, secretKey): "获取应用信息" sql = "select aid from paas_app_token where apiKey = '%s' AND secretKey = '%s'" % ( sqlDeal(apiKey), sqlDeal(secretKey)) dao = db.execute(sql) r = objToDict(dao.first()) dao.close() return r
def appMessage(apiKey, secretKey): "获取应用状态" r = getAppFromToken(apiKey, secretKey) sql = "select * from paas_app where id = %d limit 1" % (r['aid']) dao = db.execute(sql) obj = objToDict(dao.first()) dao.close() obj['createTime'] = obj['createTime'].strftime("%Y-%m-%d") return jsonify(obj)
def appMessage(apiKey,secretKey): "获取应用状态" r=getAppFromToken(apiKey,secretKey) sql="select * from paas_app where id = %d limit 1"%(r['aid']) dao=db.execute(sql) obj=objToDict(dao.first()) dao.close() obj['createTime']=obj['createTime'].strftime("%Y-%m-%d") return jsonify(obj)
def getAppMysql(obj): "获取应用的数据库信息" if obj['language'] == "static": return obj sql="select username as dbUsername,password as dbPassword,dbName,host as dbHost,port as dbPort from paas_db where aid = "+str(obj['id']) dao=db.execute(sql) lists=objToDict(dao.first()) dao.close() for key in lists: obj[key]=lists[key] return obj
def getAppMysql(obj): "获取应用的数据库信息" if obj['language'] == "static": return obj sql = "select username as dbUsername,password as dbPassword,dbName,host as dbHost,port as dbPort from paas_db where aid = " + str( obj['id']) dao = db.execute(sql) lists = objToDict(dao.first()) dao.close() for key in lists: obj[key] = lists[key] return obj
def index(): "主页面" g.apiHost=config.API_HOST sql="select paas_app_token.apiKey,paas_app_token.secretKey,paas_app.status,paas_app.id,paas_app.gitUrl,paas_app.host,paas_app.remoteServer,paas_app.title,paas_app.description,paas_app.language,paas_account.username from paas_app_token,paas_app,paas_account where paas_app.status != 4 AND paas_app.uid = paas_account.id AND paas_app_token.aid=paas_app.id AND paas_app.uid = %d order by paas_app.id desc"%(session['user']) dao=db.execute(sql) g.lists=map(objToDict,dao.fetchall()) #显示数据库信息 g.lists=map(client.getAppMysql,g.lists) dao.close() dao=db.execute("select * from paas_account where id = %d limit 1"%(session['user'])) g.user=objToDict(dao.first()) dao.close() return render_template("user/index.html")
def developApp(aid,option): "部署应用,无论是不是第一次部署,主机不处理,逻辑交给应用服务器" #提取应用数据 sql="select * from paas_app where id = %d limit 1"%(aid) dao=db.execute(sql) appData=objToDict(dao.first()) dao.close() #部署过程走异步路线,所以只改变标志位 data={'option':option,'language':appData['language'],'appHost':appData['remoteServer'],'aid':appData['id']} result=urlPostWithToken(appData['remoteServer'],"/servlet/developApp",data) #修改状态 sql="update paas_app set status = 2 where id =%d"%(aid) dao=db.execute(sql) dao.close()
def buildApp(): "创建应用" try: aid=request.form.get("aid",None) appHost=request.form.get("appHost",None) language=request.form.get("language",None) #确定应用端口号 dao=db.execute("select count(*) from paas_app where remoteServer = '%s'"%(appHost)) r=objToDict(dao.first()) dao.close() appPort=10001+int(r['count(*)']) buildServerConfig(aid,appHost,appPort,language) return json.dumps({"result":"ok","remoteSocket":"http://"+appHost+":"+str(appPort)}) except: return json.dumps({"result":"fail"})
def index(): "主页面" g.apiHost = config.API_HOST sql = "select paas_app_token.apiKey,paas_app_token.secretKey,paas_app.status,paas_app.id,paas_app.gitUrl,paas_app.host,paas_app.remoteServer,paas_app.title,paas_app.description,paas_app.language,paas_account.username from paas_app_token,paas_app,paas_account where paas_app.status != 4 AND paas_app.uid = paas_account.id AND paas_app_token.aid=paas_app.id AND paas_app.uid = %d order by paas_app.id desc" % ( session['user']) dao = db.execute(sql) g.lists = map(objToDict, dao.fetchall()) #显示数据库信息 g.lists = map(client.getAppMysql, g.lists) dao.close() dao = db.execute("select * from paas_account where id = %d limit 1" % (session['user'])) g.user = objToDict(dao.first()) dao.close() return render_template("user/index.html")
def editApp(): "编辑应用" aid = request.args.get("id", None) sql = "select * from paas_app where id = %s limit 1" % (sqlDeal(aid)) dao = db.execute(sql) g.obj = objToDict(dao.first()) dao.close() if request.method == "GET": g.add = False sql = "select * from paas_account where status != 3" dao = db.execute(sql) g.users = map(objToDict, dao.fetchall()) dao.close() return render_template("admin/addApp.html") else: uid = request.form.get("uid", None) title = request.form.get("title", None) description = request.form.get("description", None) host = request.form.get("host", None) gitUrl = request.form.get("gitUrl", None) #处理git地址,防止注入恶意代码 gitUrl = gitUrl.replace(" ", "") args = map(sqlDeal, [uid, title, description, host, gitUrl, aid]) sql = "update paas_app set uid = '%s',title='%s',description='%s',host='%s',gitUrl='%s' where id = %s" % tuple( args) dao = db.execute(sql) dao.close() #修改配置文件,防止修改域名后nginx配置文件不一致 obj = g.obj webApp.buildMainServerConfig(obj['id'], obj['host'], obj['remoteSocket']) return redirect("/admin/appManager")
def developApp(aid, option): "部署应用,无论是不是第一次部署,主机不处理,逻辑交给应用服务器" #提取应用数据 sql = "select * from paas_app where id = %d limit 1" % (aid) dao = db.execute(sql) appData = objToDict(dao.first()) dao.close() #部署过程走异步路线,所以只改变标志位 data = { 'option': option, 'language': appData['language'], 'appHost': appData['remoteServer'], 'aid': appData['id'] } result = urlPostWithToken(appData['remoteServer'], "/servlet/developApp", data) #修改状态 sql = "update paas_app set status = 2 where id =%d" % (aid) dao = db.execute(sql) dao.close()
def account(): "用户请登录" if request.method == "GET": return render_template("user/account.html") else: username=request.form.get("username",None) password=hashlib.md5(request.form.get("password",None)).hexdigest() sql="select * from paas_account where username = '******' AND password = '******' limit 1"%(sqlDeal(username),sqlDeal(password)) dao=db.execute(sql) obj=dao.first() if obj != None: obj=objToDict(obj) session['user']=obj['id'] r=True else: r=False dao.close() if r: return redirect("/user/") else: return redirect("/user/account")
def userMessage(): "修改用户信息" uid = request.args.get("id", None) if request.method == "GET": g.title = u"修改信息" g.add = False dao = db.execute("select * from paas_account where id = %s limit 1" % (sqlDeal(uid))) g.obj = objToDict(dao.fetchone()) dao.close() return render_template("admin/userAdd.html") else: password = request.form.get("password", None) status = request.form.get("status", None) realname = request.form.get("realname", None) college = request.form.get("college", None) specialty = request.form.get("specialty", None) if password == "": args = map(sqlDeal, (status, realname, college, specialty, uid)) sql = "update paas_account set status = '%s',realname = '%s',college = '%s',specialty='%s' where id = %s" % tuple( args) else: #加密密码 password = hashlib.md5(password).hexdigest() args = map(sqlDeal, (password, status, realname, college, specialty, uid)) sql = "update paas_account set password = '******',status = '%s',realname = '%s',college = '%s',specialty='%s' where id = %s" % tuple( args) #修改用户信息 dao = db.execute(sql) dao.close() return redirect("/admin/userManager")
def account(): "用户请登录" if request.method == "GET": return render_template("user/account.html") else: username = request.form.get("username", None) password = hashlib.md5(request.form.get("password", None)).hexdigest() sql = "select * from paas_account where username = '******' AND password = '******' limit 1" % ( sqlDeal(username), sqlDeal(password)) dao = db.execute(sql) obj = dao.first() if obj != None: obj = objToDict(obj) session['user'] = obj['id'] r = True else: r = False dao.close() if r: return redirect("/user/") else: return redirect("/user/account")
def buildApp(): "创建应用" try: aid = request.form.get("aid", None) appHost = request.form.get("appHost", None) language = request.form.get("language", None) #确定应用端口号 dao = db.execute( "select count(*) from paas_app where remoteServer = '%s'" % (appHost)) r = objToDict(dao.first()) dao.close() appPort = 10001 + int(r['count(*)']) buildServerConfig(aid, appHost, appPort, language) return json.dumps({ "result": "ok", "remoteSocket": "http://" + appHost + ":" + str(appPort) }) except: return json.dumps({"result": "fail"})
def develop(): "发布应用" #提取应用信息 aid = int(request.form.get("aid", None)) sql = "select * from paas_app where id = %d limit 1" % (aid) dao = db.execute(sql) appData = objToDict(dao.first()) dao.close() #把任务交给后台队列 option = request.form.get("option", None) if option == "reboot": gitUrl = "" command = "cp" elif option == "develop": gitUrl = appData['gitUrl'] if git.checkLocationCode(aid): command = "pull" else: command = "clone" executeSql = "update paas_app set status = 1 where id = %d" % (aid) #为了防止恶意不断请求操作应用,因此一段时间只保存一个操作 sql = "delete from paas_gitQueue where aid = %d" % (aid) dao = db.execute(sql) dao.close() sql = "insert into paas_gitQueue(aid,command,gitUrl,executeSql,appAccount,appGroup,appPath) values(%d,'%s','%s','%s','%s','%s','%s')" % ( aid, command, gitUrl, sqlDeal(executeSql), appData['appAccount'], appData['appGroup'], appData['appPath']) dao = db.execute(sql) dao.close() return "ok"