ops-api/scripts/insert_test_data.py

133 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from asset.models import Host
from project.models import Project, Channel, Server, Version
import logging
logger = logging.getLogger('flask.app.script')
projects = [
{
"name": "ts01/cn",
"id": 1,
"desc": "测试",
"domain": "www.baidu.com",
"www_ip": "127.0.0.1",
"ops_ip": "10.2.2.10",
"transfer_ip": "10.2.2.10",
"webhook": "",
"kw": ""
},
{
"name": "ab01/cn",
"id": 2,
"desc": "测试ab01",
"domain": "ab01.www.baidu.com",
"www_ip": "127.0.0.1",
"ops_ip": "10.2.2.11",
"transfer_ip": "10.2.2.13",
"webhook": "",
"kw": ""
},
{
"name": "aa11/tw",
"id": 3,
"desc": "1212",
"domain": "www.baidu.com",
"www_ip": "127.0.0.1",
"ops_ip": "11.22.33.44",
"transfer_ip": "11.22.33.45",
"webhook": "",
"kw": ""
},
{
"name": "qq88/cn",
"id": 4,
"desc": "qq88/cn",
"domain": "qq88.com",
"www_ip": "127.0.0.1",
"ops_ip": "10.2.2.10",
"transfer_ip": "10.2.2.11",
"webhook": "",
"kw": ""
},
{
"name": "ug41/cn",
"id": 5,
"desc": "ug41",
"domain": "ug41.huanyuantech.com",
"www_ip": "127.0.0.1",
"ops_ip": "127.0.0.1",
"transfer_ip": "127.0.0.1",
"webhook": "",
"kw": "ug41-cn"
}
]
# def sync_project():
# for p in projects:
# proj = p
# name = proj.pop("name")
# game, fork = name.split("/")
# proj["name"] = game
# proj["fork"] = fork
# proj.pop("id")
# # 字典中缺少的值会是None字段必须null=True
# obj = models.Project(**proj)
# obj.save()
def sync_server_info():
project = Project.objects(name="ug41", fork="cn").first()
info = [{"ip": "10.2.2.10", "spid": "abo", "num": 1, "server_version": "2021100801", "cfg_version": "8616", "admin_version": "2021062801", "lua_version": "122", "flag": "2", "status": "0", "port": 0}, {"ip": "10.2.2.11", "spid": "dev", "num": 1, "server_version": "2021100801", "cfg_version": "7950", "admin_version": "2021062801", "lua_version": "122", "flag": "0", "status": "1", "port": 0}, {"ip": "10.2.2.11", "spid": "dev", "num": 2, "server_version": "2021100801", "cfg_version": "2014", "admin_version": "2021062801", "lua_version": "122", "flag": "0", "status": "1", "port": 0}, {"ip": "10.2.2.11", "spid": "dev", "num": 3, "server_version": "2021100801", "cfg_version": "7003", "admin_version": "2021062801", "lua_version": "122", "flag": "0", "status": "1", "port": 0}, {"ip": "10.2.2.11", "spid": "dev", "num": 4, "server_version": "2021100801", "cfg_version": "8406", "admin_version": "2021062801", "lua_version": "122", "flag": "0", "status": "1", "port": 0}, {"ip": "10.2.2.11", "spid": "dev", "num": 5, "server_version": "2021100801", "cfg_version": "17399", "admin_version": "2021062801", "lua_version": "122", "flag": "0", "status": "1", "port": 0}, {"ip": "10.2.2.11", "spid": "dev", "num": 6, "server_version": "2021100801", "cfg_version": "8375", "admin_version": "2021062801", "lua_version": "122", "flag": "0", "status": "1", "port": 0}]
for srv in info:
ip = srv.get("ip")
num = srv.get("num")
spid = srv.get("spid")
port = srv.get("port", 0)
# 内嵌的版本字段
version = Version()
version.admin = srv.get("admin_version", "")
version.server = srv.get("server_version", "")
version.config = srv.get("cfg_version", "")
version.lua = srv.get("lua_version", "")
version.bin = srv.get("bin_version", "")
version.sql = srv.get("sql_version", "")
state = srv.get("status", "1")
if srv.get("flag", "0") == "2":
status = "closed"
else:
status = "running" if state == "1" else "error"
try:
host = Host.objects(public_ip=ip).first()
if not host:
host = Host(public_ip=ip)
host.save()
channel = Channel.objects(project=project, spid=spid).first()
if not channel:
channel = Channel(project=project, spid=spid)
channel.save()
# 更新、创建的参数
defaults = dict(host=host, version=version, port=port, status=status)
srv_obj = Server.objects(num=num, channel=channel).first()
if not srv_obj: # 创建
srv_obj = Server(num=num, channel=channel, **defaults)
srv_obj.save()
logger.info(f"创建 {spid}_{num} 区服信息成功")
continue
# 更新对象
srv_obj.update(**defaults)
srv_obj.save()
logger.info(f"更新 {spid}_{num} 区服信息成功")
except:
logger.exception("同步区服出错")
continue
if __name__ == '__main__':
# sync_project()
sync_server_info()