Skip to content

Commit

Permalink
2023年10月17日 域名同步consul
Browse files Browse the repository at this point in the history
  • Loading branch information
ss1917 committed Oct 17, 2023
1 parent af2259e commit 81b056c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmdb/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def start_server(self):
:return:
"""
try:

init_logging() # LOG
init_scheduler()
logging.info('[App Init] progressid: %(progid)s' % dict(progid=options.progid))
Expand Down
6 changes: 3 additions & 3 deletions libs/base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

import json
import logging
from loguru import logger
from abc import ABC
from tornado.web import HTTPError
from websdk2.jwt_token import AuthToken, jwt
Expand All @@ -32,7 +32,7 @@ def hide_key(data) -> None:
_body["ssh_key"] = "***************"
data["body"] = json.dumps(_body)
except Exception as error:
logging.error("hide_key error: {}".format(error))
logger.error("hide_key error: {}".format(error))

# def prepare(self):

Expand All @@ -58,7 +58,7 @@ def prepare(self):
if log_dict["body"]: self.hide_key(log_dict)
# 非GET请求的日志都写数据库记录
if self.request.method != "GET":
logging.info(json.dumps(log_dict, indent=4, separators=(',', ':')))
logger.info(json.dumps(log_dict, indent=4, separators=(',', ':')))
# 保存到数据库?
pass

Expand Down
33 changes: 32 additions & 1 deletion libs/consul_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sync_consul():
def index():
ins_log.read_log('info', f'sync to consul start {datetime.datetime.now()}')
c = ConsulOpt()
for asset_type in ['server', 'mysql', 'redis']:
for asset_type in ['server', 'mysql', 'redis', 'domain']:
try:
cmdb_ins = []
for server in consul_sync_factory(asset_type):
Expand All @@ -74,6 +74,9 @@ def consul_sync_factory(asset_type):

elif asset_type == 'redis':
return get_registry_redis_info()

elif asset_type == 'domain':
return get_registry_domain_info()
else:
return None

Expand Down Expand Up @@ -242,6 +245,34 @@ def get_registry_redis_info():
yield register_data


def get_registry_domain_info():
# 暂时没有和服务树关联
redis_conn = cache_conn()
asset_type = 'domain'
__model = asset_mapping[asset_type]
with DBContext('r') as session:
__info = session.query(__model).all()

biz_info_str = redis_conn.get("BIZ_INFO_STR")
biz_info_map = convert(biz_info_str) if biz_info_str else {}
if isinstance(biz_info_map, str):
biz_info_map = json.loads(biz_info_map)

for i in __info:
data = model_to_dict(i)
biz_id = "504"
# if not i[3]:
# logging.error(f"{asset_type} {data} {biz_id} err")
# continue
inner_ip, port = f"{data.get('record_id')}-{data.get('domain_rr')}.{data.get('domain_name')}", 443
# node_meta = dict(biz_id=biz_id, biz_cn_name=biz_info_map.get(biz_id, biz_id), env_name=data['env_name'],
# region_name=data['region_name'], module_name=data['module_name'])
node_meta = dict(biz_id=biz_id, biz_cn_name=biz_info_map.get(biz_id, biz_id), env_name='prod')
server_name = f"{asset_type}-exporter"
register_data = (server_name, f"{server_name}-{biz_id}-{inner_ip}", inner_ip, port, [biz_id], node_meta)
yield register_data


def async_consul_info():
executor = ThreadPoolExecutor(max_workers=1)
executor.submit(sync_consul)
Expand Down
11 changes: 3 additions & 8 deletions libs/logger.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : logger.py
# @Author:
# @Date : 2022/3/28
# @Role :


import os
import sys
import tornado.log
import logging
from tornado.options import options

options.log_file_prefix = os.path.join(os.path.dirname(os.path.dirname(__file__)), f'log/cmdb.log')
options.log_file_prefix = os.path.join(os.path.dirname(os.path.dirname(__file__)), f'/tmp/codo.log')


class LogFormatter(tornado.log.LogFormatter):
def __init__(self):
super(LogFormatter, self).__init__(
fmt=f'LOG_%(levelname)s %(asctime)s %(filename)s:%(funcName)s %(lineno)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
fmt='%(color)s%(asctime)s | %(levelname)s%(end_color)s | %(filename)s:%(funcName)s:%(lineno)s - %(message)s',
datefmt="%Y-%m-%d %H:%M:%S"
)


Expand Down
4 changes: 0 additions & 4 deletions log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : __init__.py.py
# @Author:
# @Date : 2022/3/17
# @Role :
3 changes: 2 additions & 1 deletion models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
AssetVSwitchModels, AssetEIPModels, SecurityGroupModels
from .tree import TreeModels, TreeAssetModels
from .tag import TagModels
from .domain import DomainRecords

asset_type_enum = Enum(
'server',
Expand Down Expand Up @@ -60,7 +61,7 @@

asset_mapping = {'server': AssetServerModels, 'mysql': AssetMySQLModels, 'redis': AssetRedisModels,
'lb': AssetLBModels, 'eip': AssetEIPModels, 'vpc': AssetVPCModels, 'vswitch': AssetVSwitchModels,
'security_group': SecurityGroupModels}
'security_group': SecurityGroupModels, 'domain': DomainRecords}

ORDER_STATUS_MAP = (
("0", "进行中"),
Expand Down

0 comments on commit 81b056c

Please sign in to comment.