diff --git a/spug_api/apps/host/add.py b/spug_api/apps/host/add.py index 1bbe521..601d5bb 100644 --- a/spug_api/apps/host/add.py +++ b/spug_api/apps/host/add.py @@ -31,7 +31,10 @@ def cloud_import(request): Argument('ak', help='请输入AccessKey ID'), Argument('ac', help='请输入AccessKey Secret'), Argument('region_id', help='请选择区域'), - Argument('group_id', type=int, help='请选择分组') + Argument('group_id', type=int, help='请选择分组'), + Argument('username', help='请输入默认SSH用户名'), + Argument('port', type=int, help='请输入默认SSH端口号'), + Argument('host_type', filter=lambda x: x in ('public', 'private'), help='请选择连接地址'), ).parse(request.body) if error is None: group = Group.objects.filter(pk=form.group_id).first() @@ -46,12 +49,23 @@ def cloud_import(request): for item in instances: instance_id = item['instance_id'] host_name = item.pop('instance_name') - item['public_ip_address'] = json.dumps(item['public_ip_address'] or []) - item['private_ip_address'] = json.dumps(item['private_ip_address'] or []) + public_ips = item['public_ip_address'] or [] + private_ips = item['private_ip_address'] or [] + item['public_ip_address'] = json.dumps(public_ips) + item['private_ip_address'] = json.dumps(private_ips) if HostExtend.objects.filter(instance_id=instance_id).exists(): HostExtend.objects.filter(instance_id=instance_id).update(**item) else: - host = Host.objects.create(name=host_name, created_by=request.user) + if form.host_type == 'public': + hostname = public_ips[0] if public_ips else None + else: + hostname = private_ips[0] if private_ips else None + host = Host.objects.create( + name=host_name, + hostname=hostname, + port=form.port, + username=form.username, + created_by=request.user) HostExtend.objects.create(host=host, **item) host_add_ids.append(host.id) if host_add_ids: diff --git a/spug_api/apps/host/utils.py b/spug_api/apps/host/utils.py index 9e068b1..bc10389 100644 --- a/spug_api/apps/host/utils.py +++ b/spug_api/apps/host/utils.py @@ -218,14 +218,16 @@ def batch_sync_host(token, hosts, password, ): with futures.ThreadPoolExecutor(max_workers=max_workers) as executor: for host in hosts: t = executor.submit(_sync_host_extend, host, private_key, public_key, password) - t.h_id = host.id + t.host = host threads.append(t) for t in futures.as_completed(threads): exception = t.exception() if exception: - rds.rpush(token, json.dumps({'key': t.h_id, 'status': 'fail', 'message': f'{exception}'})) + rds.rpush(token, json.dumps({'key': t.host.id, 'status': 'fail', 'message': f'{exception}'})) else: - rds.rpush(token, json.dumps({'key': t.h_id, 'status': 'ok'})) + rds.rpush(token, json.dumps({'key': t.host.id, 'status': 'ok'})) + t.host.is_verified = True + t.host.save() rds.expire(token, 60) diff --git a/spug_web/src/pages/host/CloudImport.js b/spug_web/src/pages/host/CloudImport.js index f0eb7e0..46fc8df 100644 --- a/spug_web/src/pages/host/CloudImport.js +++ b/spug_web/src/pages/host/CloudImport.js @@ -5,7 +5,7 @@ */ import React, { useState } from 'react'; import { observer } from 'mobx-react'; -import { Modal, Form, Input, Select, Button, Steps, Cascader, message } from 'antd'; +import { Modal, Form, Input, Select, Button, Steps, Cascader, Radio, message } from 'antd'; import http from 'libs/http'; import store from './store'; import styles from './index.module.less'; @@ -18,10 +18,22 @@ export default observer(function () { const [regionId, setRegionId] = useState(); const [groupId, setGroupId] = useState([]); const [regions, setRegions] = useState([]); + const [username, setUsername] = useState('root'); + const [port, setPort] = useState('22'); + const [host_type, setHostType] = useState('private'); function handleSubmit() { setLoading(true); - const formData = {ak, ac, type: store.cloudImport, region_id: regionId, group_id: groupId[groupId.length - 1]}; + const formData = { + ak, + ac, + type: store.cloudImport, + region_id: regionId, + group_id: groupId[groupId.length - 1], + username, + port, + host_type + }; http.post('/api/host/import/cloud/', formData, {timeout: 120000}) .then(res => { message.success(`已同步/导入 ${res} 台主机`); @@ -58,14 +70,14 @@ export default observer(function () { -