From 12e6d1fb9b9e5ec688a695a2a628eade5d807ed0 Mon Sep 17 00:00:00 2001 From: "StarsL.cn" Date: Mon, 18 Jul 2022 03:46:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5jumpserver=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flask-consul/units/jms/sync_jms.py | 32 ++++++++++++++--------- flask-consul/views/jms.py | 40 +++++++++++++++++----------- flask-consul/views/jobs.py | 3 +++ vue-consul/src/api/jms.js | 4 +-- vue-consul/src/views/jms/index.vue | 42 ++++++++++++++++++------------ 5 files changed, 76 insertions(+), 45 deletions(-) diff --git a/flask-consul/units/jms/sync_jms.py b/flask-consul/units/jms/sync_jms.py index 9dd8c69..02b785b 100755 --- a/flask-consul/units/jms/sync_jms.py +++ b/flask-consul/units/jms/sync_jms.py @@ -1,17 +1,15 @@ import datetime,requests,json from units import consul_kv,consul_manager,myaes -jms = consul_kv.get_value('ConsulManager/jms/jms_info') -jms_url = jms.get('url') -token = myaes.decrypt(jms.get('token')) -headers = {'Content-Type': 'application/json','Authorization': f"Token {token}"} - #创建node -def create_node(node_id,cloud,account): +def create_node(jms_url,headers,now,node_id,cloud,account): node_url = f"{jms_url}/api/v1/assets/nodes/{node_id}/children/" jms_node_list = requests.request("GET", node_url, headers=headers).json() if type(jms_node_list) == dict: - print(' 【JMS】',jms_node_list.get('detail'),flush=True) + detail = jms_node_list.get('detail','ERROR') + print(' 【JMS】',detail,flush=True) + data = {'count': '失败','update':now,'status':50000,'msg':f'同步资源失败!{detail}'} + consul_kv.put_kv(f'ConsulManager/record/jms/{cloud}/{account}', data) cloud_group_dict = consul_kv.get_value(f'ConsulManager/assets/{cloud}/group/{account}') for k,v in cloud_group_dict.items(): if v not in [i['value'] for i in jms_node_list]: @@ -21,7 +19,7 @@ def create_node(node_id,cloud,account): new_node_dict = {i['value']:i['id'] for i in reget_node_list} return new_node_dict -def update_jms_ecs(new_node_dict,node_id,cloud,account,ecs_info,custom_ecs_info): +def update_jms_ecs(jms_url,headers,new_node_dict,node_id,cloud,account,ecs_info,custom_ecs_info): #比较云主机与JMS中对应node的主机列表,删除jms中多余的主机 ecs_url = f"{jms_url}/api/v1/assets/assets/" reget_ecs_list = requests.request("GET", f'{ecs_url}?node={node_id}', headers=headers).json() @@ -71,7 +69,7 @@ def update_jms_ecs(new_node_dict,node_id,cloud,account,ecs_info,custom_ecs_info) return 'ok' #从JMS中删除没有主机的组 -def del_node(node_id,cloud,account): +def del_node(jms_url,headers,now,node_id,cloud,account): node_tree_url = f"{jms_url}/api/v1/assets/nodes/children/tree/?id={node_id}" jms_node_list = requests.request("GET", node_tree_url, headers=headers).json() for i in jms_node_list: @@ -79,9 +77,14 @@ def del_node(node_id,cloud,account): del_node_url = f"{jms_url}/api/v1/assets/nodes/{i['meta']['node']['id']}/" response = requests.request("DELETE", del_node_url, headers=headers) print(' 【JMS】删除空组===>',i['name'],response.status_code,flush=True) + ecs_count_url = f"{jms_url}/api/v1/assets/assets/?node={node_id}&limit=1&offset=1" + ecs_count = requests.request("GET", ecs_count_url, headers=headers).json()['count'] + data = {'count':ecs_count,'update':now,'status':20000,'msg':f'同步资源成功!总数:{ecs_count}'} + consul_kv.put_kv(f'ConsulManager/record/jms/{cloud}/{account}', data) return 'ok' def run(cloud,account): + now = datetime.datetime.now().strftime('%m%d/%H:%M') print('【JOB】===>',cloud,account,'JMS同步开始',flush=True) node_id = consul_kv.get_value(f'ConsulManager/jms/{cloud}/{account}/node_id')['node_id'] temp_ecs_info = consul_kv.get_value(f'ConsulManager/jms/{cloud}/{account}/ecs_info') @@ -89,7 +92,12 @@ def run(cloud,account): temp_custom_ecs_info = consul_kv.get_value(f'ConsulManager/jms/{cloud}/{account}/custom_ecs_info') custom_ecs_info = consul_kv.get_value(f'ConsulManager/jms/custom_ecs_info') if temp_custom_ecs_info == {} else temp_custom_ecs_info - new_node_dict = create_node(node_id,cloud,account) - update_jms_ecs(new_node_dict,node_id,cloud,account,ecs_info,custom_ecs_info) - del_node(node_id,cloud,account) + jms = consul_kv.get_value('ConsulManager/jms/jms_info') + jms_url = jms.get('url') + token = myaes.decrypt(jms.get('token')) + headers = {'Content-Type': 'application/json','Authorization': f"Token {token}"} + + new_node_dict = create_node(jms_url,headers,now,node_id,cloud,account) + update_jms_ecs(jms_url,headers,new_node_dict,node_id,cloud,account,ecs_info,custom_ecs_info) + del_node(jms_url,headers,now,node_id,cloud,account) print('【JOB】===>',cloud,account,'JMS同步完成',flush=True) diff --git a/flask-consul/views/jms.py b/flask-consul/views/jms.py index 8841760..6bed764 100644 --- a/flask-consul/views/jms.py +++ b/flask-consul/views/jms.py @@ -4,7 +4,7 @@ from flask_apscheduler import APScheduler from units import token_auth,consul_kv,myaes from config import vendors import json -from .jobs import deljob,addjob,runjob +from .jobs import deljob,addjob,runjob,getjob blueprint = Blueprint('jms',__name__) api = Api(blueprint) @@ -19,7 +19,7 @@ class Jms(Resource): if stype == 'list': switch = consul_kv.get_value(f'ConsulManager/jms/jms_info') if switch == {}: - return({'code': 20000,'exp_list':[],'vendor_list':[],'account_list':[]}) + return({'code': 20000,'ecs_list':[],'vendor_list':[],'account_list':[]}) args = parser.parse_args() query_dict = json.loads(args['query_dict']) if query_dict['vendor'] != '': @@ -28,7 +28,7 @@ class Jms(Resource): cloud_job_list = consul_kv.get_keys_list('ConsulManager/jobs') cloud_list = [i for i in cloud_job_list if i.endswith('/group')] - exp_list = [] + ecs_list = [] for i in cloud_list: vendor,account = i.split('/')[2:4] cloud_info_dict = {'vendor':vendor,'account':account} @@ -41,26 +41,36 @@ class Jms(Resource): count_ecs = len(services_meta) count_off,count_on,count_cpu,count_mem,count_win,count_linux = 0,0,0,0,0,0 for i in services_meta: - if i['os'] == linux: + if i['os'] == 'linux': count_linux = count_linux + 1 - elif i['os'] == windows: + elif i['os'] == 'windows': count_win = count_win + 1 - if i.get('stat') == off: + if i.get('stat') == 'off': count_off = count_off + 1 else: count_on = count_on + 1 cpu = int(i['cpu'].replace('核','')) count_cpu = count_cpu + cpu - mem = int(i['cpu'].replace('GB','')) + mem = int(i['mem'].replace('GB','')) count_mem = count_mem + mem - - exp_list.append({'vendor':vendors[vendor],'account':account,'id':k,'Region':v['Region'], - 'Product':v['Product'],'Name':v.get('Name','Null'),'EndTime':v['EndTime'], - 'Ptype':v['Ptype'].replace('hws.resource.type.',''), - 'notify_id': v['notify_id'],'isnotify':isnotify}) - vendor_list = sorted(list(set([i['vendor'] for i in exp_list]))) - account_list = sorted(list(set([i['account'] for i in exp_list]))) - return {'code': 20000,'exp_list':exp_list,'vendor_list':vendor_list,'account_list':account_list,'amount_list':amount_list} + + jms_job = consul_kv.get_value(f"ConsulManager/jms/jobs/{vendor}/{account}") + if jms_job == {}: + count_sync,interval,runtime,nextime,sync = '无','无','无','无',False + else: + interval = f"{jms_job['minutes']}分钟" + jms_job = consul_kv.get_value(f'ConsulManager/record/jms/{vendor}/{account}') + runtime = jms_job.get('update') + count_sync = jms_job.get('count') + nextime = getjob(f'{vendor}/{account}/jms').next_run_time.strftime("%m%d/%H:%M") + sync = True + ecs_list.append({'vendor':vendors[vendor],'account':account,'count_linux':count_linux, + 'count_win':count_win,'count_mem':f'{count_mem}GB','count_cpu':f'{count_cpu}核', + 'count_ecs':count_ecs,'count_on':count_on,'count_off':count_off,'sync':sync, + 'count_sync':count_sync,'interval':interval,'runtime':runtime,'nextime':nextime}) + vendor_list = sorted(list(set([i['vendor'] for i in ecs_list]))) + account_list = sorted(list(set([i['account'] for i in ecs_list]))) + return {'code': 20000,'ecs_list':ecs_list,'vendor_list':vendor_list,'account_list':account_list} if stype == 'config': ecs_info = consul_kv.get_value('ConsulManager/jms/ecs_info') jms_info = consul_kv.get_value('ConsulManager/jms/jms_info') diff --git a/flask-consul/views/jobs.py b/flask-consul/views/jobs.py index 2115c0a..30295aa 100644 --- a/flask-consul/views/jobs.py +++ b/flask-consul/views/jobs.py @@ -26,6 +26,9 @@ def addjob(job_id,job_func,job_args,job_interval): def runjob(jobid): Scheduler.run_job(jobid) +def getjob(jobid): + return Scheduler.get_job(jobid) + class Jobs(Resource): decorators = [token_auth.auth.login_required] def get(self): diff --git a/vue-consul/src/api/jms.js b/vue-consul/src/api/jms.js index b58f65c..9dfe5fa 100644 --- a/vue-consul/src/api/jms.js +++ b/vue-consul/src/api/jms.js @@ -1,8 +1,8 @@ import request from '@/utils/request-ops' -export function getExpList(query_dict) { +export function getJmsList(query_dict) { return request({ - url: '/api/exp/list', + url: '/api/jms/list', method: 'get', params: { query_dict } }) diff --git a/vue-consul/src/views/jms/index.vue b/vue-consul/src/views/jms/index.vue index efda7ca..c502d2a 100644 --- a/vue-consul/src/views/jms/index.vue +++ b/vue-consul/src/views/jms/index.vue @@ -43,19 +43,30 @@ - - + + - - - - - - - - + + + + + + + + + + + + + @@ -71,7 +82,7 @@