From 1d6f3e66c1ab9546dde75a9333e8b60aa76b0d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E4=BA=8C=E7=8C=9B?= Date: Mon, 28 Oct 2019 13:12:30 +0800 Subject: [PATCH] =?UTF-8?q?U=20-=20=E4=B8=BB=E6=9C=BA=E7=AE=A1=E7=90=86=20?= =?UTF-8?q?docker=E8=BF=9E=E6=8E=A5=E5=9C=B0=E5=9D=80=E5=B7=B2=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E5=BF=85=E5=A1=AB=20#18?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/assets/host.py | 26 ++++++++++++++----------- spug_api/libs/ssh/__init__.py | 1 + spug_web/src/components/assets/Host.vue | 26 ++++++++++++++----------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/spug_api/apps/assets/host.py b/spug_api/apps/assets/host.py index 3fc00f2..14b2586 100644 --- a/spug_api/apps/assets/host.py +++ b/spug_api/apps/assets/host.py @@ -37,7 +37,8 @@ def get(): @blueprint.route('/', methods=['POST']) @require_permission('assets_host_add') def post(): - form, error = JsonParser('name', 'type', 'zone', 'docker_uri', 'ssh_ip', 'ssh_port', + form, error = JsonParser('name', 'type', 'zone', 'ssh_ip', 'ssh_port', + Argument('docker_uri', nullable=True, required=False), Argument('desc', nullable=True, required=False)).parse() if error is None: host = Host(**form) @@ -59,7 +60,8 @@ def delete(host_id): @blueprint.route('/', methods=['PUT']) @require_permission('assets_host_edit') def put(host_id): - form, error = JsonParser('name', 'type', 'zone', 'docker_uri', 'ssh_ip', 'ssh_port', + form, error = JsonParser('name', 'type', 'zone', 'ssh_ip', 'ssh_port', + Argument('docker_uri', nullable=True, required=False), Argument('desc', nullable=True, required=False)).parse() if error is None: host = Host.query.get_or_404(host_id) @@ -75,10 +77,11 @@ def get_valid(host_id): if not Setting.has('ssh_private_key'): utils.generate_and_save_ssh_key() if ssh.ssh_ping(cli.ssh_ip, cli.ssh_port): - try: - sync_host_info(host_id, cli.docker_uri) - except DockerException: - return json_response(message='docker fail') + if cli.docker_uri: + try: + sync_host_info(host_id, cli.docker_uri) + except DockerException: + return json_response(message='docker fail') else: return json_response(message='ssh fail') return json_response() @@ -92,10 +95,11 @@ def post_valid(host_id): cli = Host.query.get_or_404(host_id) ssh.add_public_key(cli.ssh_ip, cli.ssh_port, form.secret) if ssh.ssh_ping(cli.ssh_ip, cli.ssh_port): - try: - sync_host_info(host_id, cli.docker_uri) - except DockerException: - return json_response(message='获取扩展信息失败,请检查docker是否可以正常连接!') + if cli.docker_uri: + try: + sync_host_info(host_id, cli.docker_uri) + except DockerException: + return json_response(message='获取扩展信息失败,请检查docker是否可以正常连接!') else: return json_response(message='验证失败!') return json_response(message=error) @@ -139,7 +143,7 @@ def host_import(): def sync_host_info(host_id, uri): host_info = DockerClient(base_url=uri).docker_info() operate_system = host_info.get('OperatingSystem') - memory = math.ceil(int(host_info.get('MemTotal'))/1024/1024/1024) + memory = math.ceil(int(host_info.get('MemTotal')) / 1024 / 1024 / 1024) cpu = host_info.get('NCPU') # outer_ip = 1 # inner_ip = 2 diff --git a/spug_api/libs/ssh/__init__.py b/spug_api/libs/ssh/__init__.py index 1a9f866..83e675f 100644 --- a/spug_api/libs/ssh/__init__.py +++ b/spug_api/libs/ssh/__init__.py @@ -75,6 +75,7 @@ def ssh_ping(hostname, port): hostname, port=port, username='root', + timeout=5, pkey=RSAKey.from_private_key(StringIO(Setting.ssh_private_key))) except AuthenticationException: return False diff --git a/spug_web/src/components/assets/Host.vue b/spug_web/src/components/assets/Host.vue index c42a691..4d0f898 100644 --- a/spug_web/src/components/assets/Host.vue +++ b/spug_web/src/components/assets/Host.vue @@ -33,7 +33,7 @@ {{ props.row.desc }} - 暂没有配置信息,点击验证自动获取... + 暂没有配置信息,点击验证自动获取,需要配置docker连接地址 @@ -41,8 +41,12 @@ - - + + + +