Browse Source

同步jumpserver功能

pull/27/head
StarsL.cn 2 years ago
parent
commit
12e6d1fb9b
  1. 32
      flask-consul/units/jms/sync_jms.py
  2. 40
      flask-consul/views/jms.py
  3. 3
      flask-consul/views/jobs.py
  4. 4
      vue-consul/src/api/jms.js
  5. 42
      vue-consul/src/views/jms/index.vue

32
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)

40
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')

3
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):

4
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 }
})

42
vue-consul/src/views/jms/index.vue

@ -43,19 +43,30 @@
</el-button>
</div>
</el-dialog>
<el-table v-loading="listLoading" :data="exp_list" :default-sort="{ prop: 'EndTime', order: 'ascending' }" border fit highlight-current-row style="width: 100%;">
<el-table-column type="index" align="center" />
<el-table v-loading="listLoading" :data="ecs_list" border fit highlight-current-row style="width: 100%;">
<el-table-column type="index" align="center" width="30" />
<el-table-column prop="vendor" label="云厂商" sortable align="center" width="90" />
<el-table-column prop="account" label="账号" sortable align="center" width="100" />
<el-table-column prop="Region" label="区域" sortable align="center" width="100" show-overflow-tooltip />
<el-table-column prop="Ptype" label="类型" sortable align="center" width="150" show-overflow-tooltip />
<el-table-column prop="Product" label="产品" sortable align="center" width="200" show-overflow-tooltip />
<el-table-column prop="Name" label="名称" sortable align="center" show-overflow-tooltip />
<el-table-column prop="id" label="实例ID" sortable align="center" show-overflow-tooltip />
<el-table-column prop="EndTime" label="到期日" sortable align="center" width="100" />
<el-table-column label="通知" align="center" width="60" class-name="small-padding fixed-width">
<el-table-column prop="account" label="账号" sortable align="center" width="100" show-overflow-tooltip />
<el-table-column prop="count_linux" label="Linux" sortable align="center" width="90" />
<el-table-column prop="count_win" label="Win" sortable align="center" width="80" />
<el-table-column prop="count_mem" label="总内存" sortable align="center" width="120" />
<el-table-column prop="count_cpu" label="总CPU" sortable align="center" width="110" />
<el-table-column prop="count_ecs" label="资源数" sortable align="center" width="100">
<template slot-scope="{row}">
<el-switch v-model="row.isnotify" active-color="#13ce66" @change="fetchNotify(row.vendor, row.account, row.notify_id, row.isnotify)" />
<span style="font-weight:bold">{{ row.count_ecs }} </span>
<el-tooltip style="diaplay:inline" effect="dark" placement="top">
<div slot="content"> 开机{{ row.count_on }}关机{{ row.count_off }} </div>
<i class="el-icon-info" />
</el-tooltip>
</template>
</el-table-column>
<el-table-column prop="count_sync" label="同步数" sortable align="center" width="90" />
<el-table-column prop="runtime" label="上次同步" sortable align="center" />
<el-table-column prop="interval" label="同步间隔" sortable align="center" />
<el-table-column prop="nextime" label="下次同步" sortable align="center" />
<el-table-column label="同步" align="center" width="60" class-name="small-padding fixed-width">
<template slot-scope="{row}">
<el-switch v-model="row.sync" active-color="#13ce66" @change="fetchNotify(row.vendor, row.account, row.notify_id, row.isnotify)" />
</template>
</el-table-column>
</el-table>
@ -71,7 +82,7 @@
</template>
<script>
import { getExpList, getJmsConfig, postJmsConfig, postExpIsnotify } from '@/api/jms'
import { getJmsList, getJmsConfig, postJmsConfig, postExpIsnotify } from '@/api/jms'
export default {
data() {
return {
@ -79,7 +90,7 @@ export default {
listLoading: false,
dialogFormVisible: false,
query: { vendor: '', account: '' },
exp_list: [],
ecs_list: [],
vendor_list: [],
account_list: [],
amount_list: [],
@ -116,11 +127,10 @@ export default {
},
fetchData() {
this.listLoading = true
getExpList(this.query).then(response => {
getJmsList(this.query).then(response => {
this.vendor_list = response.vendor_list
this.account_list = response.account_list
this.exp_list = response.exp_list
this.amount_list = response.amount_list
this.ecs_list = response.ecs_list
this.listLoading = false
})
},

Loading…
Cancel
Save