diff --git a/Makefile b/Makefile index 4c2ae21..80cb545 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ help: build: cd flask-consul && docker build -t flask-consul:latest . cd vue-consul && docker build -t nginx-consul:latest . - cd vue-consul && docker build -t nginx-consul:tensuns-latest -f Dockerfile.tensuns . + cd vue-consul && docker build -t nginx-consul:tensuns-latest -f Dockerfile.tensuns . echo -e "\n\n自行编译的版本,注意修改docker-compose.yml中的镜像地址为本地仓库后再启动。\nBlackbox-Manager:\nhttp://{ip}:1026\n" push: @@ -16,8 +16,8 @@ push: docker push registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:${ver} docker push registry.cn-shenzhen.aliyuncs.com/starsl/flask-consul:latest docker push registry.cn-shenzhen.aliyuncs.com/starsl/flask-consul:${ver} - docker tag nginx-consul:tensuns-latest registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest - docker push registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest + docker tag nginx-consul:tensuns-latest registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest + docker push registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest update: docker-compose pull && docker-compose up -d diff --git a/flask-consul/units/cloud/sync_ecs.py b/flask-consul/units/cloud/sync_ecs.py index 9ed6486..e891ae0 100644 --- a/flask-consul/units/cloud/sync_ecs.py +++ b/flask-consul/units/cloud/sync_ecs.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 import requests,json +from units import consul_kv from config import consul_token,consul_url,vendors,regions headers = {'X-Consul-Token': consul_token} geturl = f'{consul_url}/agent/services' @@ -34,8 +35,13 @@ def w2consul(vendor,account,region,ecs_dict): print({"code": 50000,"data": f'{dereg.status_code}:{dereg.text}'}, flush=True) else: on = on + 1 - port = 9100 if v['ostype'] == 'linux' else 9182 - ip = v['ip'] if isinstance(v['ip'],list) is False else v['ip'][0] + custom_ecs = consul_kv.get_value(f'ConsulManager/assets/sync_ecs_custom/{iid}') + port = custom_ecs.get('port') + ip = custom_ecs.get('ip') + if port == None: + port = 9100 if v['ostype'] == 'linux' else 9182 + if ip == None: + ip = v['ip'] if isinstance(v['ip'],list) is False else v['ip'][0] instance = f'{ip}:{port}' data = { 'id': iid, @@ -73,4 +79,5 @@ def w2consul(vendor,account,region,ecs_dict): #print({f"{account}:code": 20000,"data": "增加成功!"}, flush=True) else: print({f"{account}:code": 50000,"data": f'{reg.status_code}:{reg.text}'}, flush=True) + #return {"code": 50000,"data": f'{reg.status_code}:{reg.text}'} return off,on diff --git a/flask-consul/units/consul_svc.py b/flask-consul/units/consul_svc.py new file mode 100644 index 0000000..1ef786b --- /dev/null +++ b/flask-consul/units/consul_svc.py @@ -0,0 +1,31 @@ +import requests,json +import sys +sys.path.append("..") +from config import consul_token,consul_url + +headers = {'X-Consul-Token': consul_token} + +def get_sid(iid): + url = f'{consul_url}/agent/service/{iid}' + response = requests.get(url, headers=headers) + if response.status_code == 200: + info = response.json() + return {'code': 20000,'instance':info} + else: + return {'code': 50000, 'data': f'{response.status_code}:{response.text}'} + +def del_sid(iid): + reg = requests.put(f'{consul_url}/agent/service/deregister/{iid}', headers=headers) + if reg.status_code == 200: + return {"code": 20000, "data": f"【{iid}】删除成功!"} + else: + return {"code": 50000, "data": f"{reg.status_code}【{iid}】{reg.text}"} + +def add_sid(instance_dict): + reg = requests.put(f'{consul_url}/agent/service/register', headers=headers, data=json.dumps(instance_dict)) + if reg.status_code == 200: + return {"code": 20000, "data": f"增加成功!"} + else: + print(f"{reg.status_code}:{reg.text}") + return {"code": 50000, "data": f"{reg.status_code}:{reg.text}"} + diff --git a/flask-consul/units/selfnode_manager.py b/flask-consul/units/selfnode_manager.py index 38669dc..577c903 100644 --- a/flask-consul/units/selfnode_manager.py +++ b/flask-consul/units/selfnode_manager.py @@ -67,5 +67,6 @@ def del_service(vendor,account,region,group,name): if reg.status_code == 200: return {"code": 20000, "data": f"【{sid}】删除成功!"} else: + print(f"{reg.status_code}【{sid}】{reg.text}") return {"code": 50000, "data": f"{reg.status_code}【{sid}】{reg.text}"} diff --git a/flask-consul/units/upload.py b/flask-consul/units/upload.py index e79e38a..bfd89ad 100644 --- a/flask-consul/units/upload.py +++ b/flask-consul/units/upload.py @@ -1,7 +1,6 @@ #!/usr/bin/python3 import requests, json -import xlrd -import sys +import xlrd,re,sys sys.path.append("..") from config import consul_token,consul_url @@ -18,6 +17,7 @@ def importconsul(row,imptype): } elif imptype == 'selfnode': vendor,account,region,group,name,instance,os = row + print(row) sid = f"{vendor}/{account}/{region}/{group}@{name}" ip = instance.split(':')[0] port = instance.split(':')[1] @@ -52,7 +52,19 @@ def read_execl(file_contents,imptype): row = table.row_values(rownum) if rownum == 0: continue - imp = importconsul(row,imptype) + nrow = [] + for i in row: + try: + float(i) + if i % 1 == 0: + i = int(i) + nrow.append(str(i)) + except: + j = i.strip() + if i != row[5]: + j = re.sub('[[ \]`~!\\\#$^/&*=|"{}\':;?]','_',j) + nrow.append(j) + imp = importconsul(nrow,imptype) if imp['code'] == 50000: return imp return {"code": 20000, "data": f"导入成功!共导入 {rownum} 条数据。"} diff --git a/flask-consul/views/nodes.py b/flask-consul/views/nodes.py index 93901f2..cc101e3 100644 --- a/flask-consul/views/nodes.py +++ b/flask-consul/views/nodes.py @@ -3,7 +3,7 @@ from flask_restful import reqparse, Resource, Api from flask_apscheduler import APScheduler #import sys #sys.path.append("..") -from units import token_auth,consul_kv,gen_config +from units import token_auth,consul_kv,gen_config,consul_svc blueprint = Blueprint('nodes',__name__) api = Api(blueprint) @@ -11,6 +11,10 @@ api = Api(blueprint) parser = reqparse.RequestParser() parser.add_argument('job_id',type=str) parser.add_argument('services_dict',type=dict) +parser.add_argument('cst_ecs_dict',type=dict) +parser.add_argument('iid',type=str) +parser.add_argument('jobecs_name',type=str) +parser.add_argument('checked',type=str) class Nodes(Resource): decorators = [token_auth.auth.login_required] @@ -40,9 +44,61 @@ class Nodes(Resource): return {'code': 20000,'services_list': sorted(set(services_list))} elif stype == 'rules': return gen_config.get_rules() + elif stype == 'cstecsconf': + args = parser.parse_args() + iid = args['iid'] + cst_ecs_config = consul_kv.get_value(f'ConsulManager/assets/sync_ecs_custom/{iid}') + cst_ecs_config.update({'iid': iid,'ipswitch': False,'portswitch': False}) + if 'ip' in cst_ecs_config and cst_ecs_config['ip'] != '': + cst_ecs_config['ipswitch'] = True + if 'port' in cst_ecs_config and cst_ecs_config['port'] != '': + cst_ecs_config['portswitch'] = True + return {'code': 20000, 'cst_ecs': cst_ecs_config} + elif stype == 'cstecslist': + args = parser.parse_args() + jobecs_name = args['jobecs_name'] + checked = args['checked'] + cst_ecs_dict = consul_kv.get_kv_dict('ConsulManager/assets/sync_ecs_custom/') + cst_ecs_keylist = [k.split('/')[-1] for k,v in cst_ecs_dict.items() if v != {}] + ecs_info = consul_kv.get_ecs_services(jobecs_name) + if checked == 'false': + return ecs_info + else: + cst_ecs_list = [i for i in ecs_info['ecs_list'] if i['iid'] in cst_ecs_keylist] + return {'code': 20000, 'ecs_list': cst_ecs_list} + def post(self, stype): if stype == 'config': args = parser.parse_args() services_dict = args['services_dict'] return gen_config.ecs_config(services_dict['services_list'],services_dict['ostype_list']) + elif stype == 'cstecs': + args = parser.parse_args() + cst_ecs_dict = args['cst_ecs_dict'] + consul_ecs_cst = {} + iid = cst_ecs_dict['iid'] + try: + sid_dict = consul_svc.get_sid(iid)['instance'] + if cst_ecs_dict['portswitch'] and cst_ecs_dict['port'] != '': + consul_ecs_cst['port'] = int(cst_ecs_dict['port']) + sid_dict['Port'] = consul_ecs_cst['port'] + if cst_ecs_dict['ipswitch'] and cst_ecs_dict['ip'] != '': + consul_ecs_cst['ip'] = cst_ecs_dict['ip'] + sid_dict['Address'] = consul_ecs_cst['ip'] + consul_kv.put_kv(f'ConsulManager/assets/sync_ecs_custom/{iid}',consul_ecs_cst) + del sid_dict['TaggedAddresses'] + del sid_dict['Weights'] + del sid_dict['ContentHash'] + del sid_dict['Datacenter'] + sid_dict['name'] = sid_dict.pop('Service') + sid_dict['Meta']['instance'] = f"{sid_dict['Address']}:{sid_dict['Port']}" + sid_dict["check"] = { "tcp": sid_dict['Meta']['instance'],"interval": "60s" } + consul_svc.del_sid(iid) + consul_svc.add_sid(sid_dict) + return {'code': 20000, 'data': '自定义实例信息修改成功!'} + except Exception as e: + print(e,flush=True) + return {'code': 50000, "data": '提交自定义实例信息格式错误!'} + + api.add_resource(Nodes, '/api/nodes/') diff --git a/vue-consul/src/api/node-exporter.js b/vue-consul/src/api/node-exporter.js index 1cdc261..62ac1ed 100644 --- a/vue-consul/src/api/node-exporter.js +++ b/vue-consul/src/api/node-exporter.js @@ -65,3 +65,27 @@ export function getRules() { method: 'get' }) } + +export function postCstEcs(cst_ecs_dict) { + return request({ + url: '/api/nodes/cstecs', + method: 'post', + data: { cst_ecs_dict } + }) +} + +export function getCstEcsConfig(iid) { + return request({ + url: '/api/nodes/cstecsconf', + method: 'get', + params: { iid } + }) +} + +export function getCstEcsList(jobecs_name, checked) { + return request({ + url: '/api/nodes/cstecslist', + method: 'get', + params: { jobecs_name, checked } + }) +} diff --git a/vue-consul/src/views/blackbox/.index.vue.swp b/vue-consul/src/views/blackbox/.index.vue.swp new file mode 100644 index 0000000..51f0830 Binary files /dev/null and b/vue-consul/src/views/blackbox/.index.vue.swp differ diff --git a/vue-consul/src/views/consul/instances.vue b/vue-consul/src/views/consul/instances.vue index 7ca9800..4cab18f 100644 --- a/vue-consul/src/views/consul/instances.vue +++ b/vue-consul/src/views/consul/instances.vue @@ -1,6 +1,7 @@