mirror of https://github.com/openspug/spug
improve host import
parent
b84a156454
commit
e0e791b01c
|
@ -101,7 +101,7 @@ def post_import(request):
|
||||||
password = request.POST.get('password')
|
password = request.POST.get('password')
|
||||||
file = request.FILES['file']
|
file = request.FILES['file']
|
||||||
ws = load_workbook(file, read_only=True)['Sheet1']
|
ws = load_workbook(file, read_only=True)['Sheet1']
|
||||||
summary = {'invalid': [], 'skip': [], 'fail': [], 'network': [], 'repeat': [], 'success': [], 'error': []}
|
summary = {'invalid': [], 'skip': [], 'repeat': [], 'success': []}
|
||||||
for i, row in enumerate(ws.rows):
|
for i, row in enumerate(ws.rows):
|
||||||
if i == 0: # 第1行是表头 略过
|
if i == 0: # 第1行是表头 略过
|
||||||
continue
|
continue
|
||||||
|
@ -120,23 +120,15 @@ def post_import(request):
|
||||||
if Host.objects.filter(hostname=data.hostname, port=data.port, username=data.username).exists():
|
if Host.objects.filter(hostname=data.hostname, port=data.port, username=data.username).exists():
|
||||||
summary['skip'].append(i)
|
summary['skip'].append(i)
|
||||||
continue
|
continue
|
||||||
try:
|
|
||||||
if valid_ssh(data.hostname, data.port, data.username, data.pop('password') or password, None,
|
|
||||||
False) is False:
|
|
||||||
summary['fail'].append(i)
|
|
||||||
continue
|
|
||||||
except AuthenticationException:
|
|
||||||
summary['fail'].append(i)
|
|
||||||
continue
|
|
||||||
except socket.error:
|
|
||||||
summary['network'].append(i)
|
|
||||||
continue
|
|
||||||
except Exception:
|
|
||||||
summary['error'].append(i)
|
|
||||||
continue
|
|
||||||
if Host.objects.filter(name=data.name).exists():
|
if Host.objects.filter(name=data.name).exists():
|
||||||
summary['repeat'].append(i)
|
summary['repeat'].append(i)
|
||||||
continue
|
continue
|
||||||
|
try:
|
||||||
|
password = data.pop('password') or password
|
||||||
|
if valid_ssh(data.hostname, data.port, data.username, password, None, False) is False:
|
||||||
|
data.is_verified = True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
host = Host.objects.create(created_by=request.user, **data)
|
host = Host.objects.create(created_by=request.user, **data)
|
||||||
if request.user.role:
|
if request.user.role:
|
||||||
request.user.role.add_host_perm(host.id)
|
request.user.role.add_host_perm(host.id)
|
||||||
|
|
|
@ -26,12 +26,6 @@ export default observer(function () {
|
||||||
title: '导入结果',
|
title: '导入结果',
|
||||||
content: <Form labelCol={{span: 7}} wrapperCol={{span: 14}}>
|
content: <Form labelCol={{span: 7}} wrapperCol={{span: 14}}>
|
||||||
<Form.Item style={{margin: 0}} label="导入成功">{res.success.length}</Form.Item>
|
<Form.Item style={{margin: 0}} label="导入成功">{res.success.length}</Form.Item>
|
||||||
{res['fail'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="验证失败">
|
|
||||||
<Tooltip title={`相关行:${res['fail'].join(', ')}`}>{res['fail'].length}</Tooltip>
|
|
||||||
</Form.Item>}
|
|
||||||
{res['network'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="网络错误">
|
|
||||||
<Tooltip title={`相关行:${res['network'].join(', ')}`}>{res['network'].length}</Tooltip>
|
|
||||||
</Form.Item>}
|
|
||||||
{res['skip'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="重复数据">
|
{res['skip'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="重复数据">
|
||||||
<Tooltip title={`相关行:${res['skip'].join(', ')}`}>{res['skip'].length}</Tooltip>
|
<Tooltip title={`相关行:${res['skip'].join(', ')}`}>{res['skip'].length}</Tooltip>
|
||||||
</Form.Item>}
|
</Form.Item>}
|
||||||
|
@ -41,9 +35,6 @@ export default observer(function () {
|
||||||
{res['repeat'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="重复主机名">
|
{res['repeat'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="重复主机名">
|
||||||
<Tooltip title={`相关行:${res['repeat'].join(', ')}`}>{res['repeat'].length}</Tooltip>
|
<Tooltip title={`相关行:${res['repeat'].join(', ')}`}>{res['repeat'].length}</Tooltip>
|
||||||
</Form.Item>}
|
</Form.Item>}
|
||||||
{res['error'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="其他错误">
|
|
||||||
<Tooltip title={`请通过新建主机查看具体错误信息,相关行:${res['error'].join(', ')}`}>{res['error'].length}</Tooltip>
|
|
||||||
</Form.Item>}
|
|
||||||
</Form>
|
</Form>
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue