From ef3e8b58e9f14a0f7b3847bb7d486d1158983b34 Mon Sep 17 00:00:00 2001 From: ysh Date: Fri, 18 Jul 2025 14:58:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E8=A3=85=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/k8sManager/multiple_clusters.py | 16 ++++++++-------- app/k8sManager/ssh_utils.py | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/k8sManager/multiple_clusters.py b/app/k8sManager/multiple_clusters.py index a66084a..39378f4 100644 --- a/app/k8sManager/multiple_clusters.py +++ b/app/k8sManager/multiple_clusters.py @@ -224,13 +224,13 @@ async def new_cluster_install(params): if username == "root": # 如果是root用户,直接执行安装脚本 debug(f'开始Root用户安装集群节点,用户名: {username}, 角色: {role},主机: {host},端口: {port}') - ssh_utils.ssh_execute_command(host, port, username, password, + await ssh_utils.ssh_execute_command(host, port, username, password, install_clusterrole_command, real_time_log=True, scp_map=scp_map) else: # 如果是普通用户,需要先将处理好 debug(f'开始普通用户安装集群节点,用户名: {username}, 角色: {role},主机: {host},端口: {port}') - ssh_utils.ssh_execute_command_noroot(host, port, username, password, + await ssh_utils.ssh_execute_command_noroot(host, port, username, password, install_clusterrole_command, real_time_log=True, scp_map=scp_map, sudo_timeout=500) # 设置较长的超时时间,适应K8s安装过程 @@ -245,19 +245,19 @@ async def new_cluster_install(params): # 第三步:返回加入凭证给cpcc保存(pcapi无状态) clusterauth_command = ['kubeadm token create --print-join-command --ttl 0'] if username != "root": - join_idp = ssh_utils.ssh_execute_command_noroot(host, port, username, password, clusterauth_command, + join_idp = await ssh_utils.ssh_execute_command_noroot(host, port, username, password, clusterauth_command, real_time_log=True, sudo_timeout=60) # 获取token命令应该较快完成 else: - join_idp = ssh_utils.ssh_execute_command(host, port, username, password, clusterauth_command, real_time_log=True) + join_idp = await ssh_utils.ssh_execute_command(host, port, username, password, clusterauth_command, real_time_log=True) join_idp = join_idp[0][0].strip() debug(f'集群验证码:{join_idp=}') kubeconfig_context_command = ['cat /root/.kube/config'] if username != "root": - kubeconfig = ssh_utils.ssh_execute_command_noroot(host, port, username, password, + kubeconfig = await ssh_utils.ssh_execute_command_noroot(host, port, username, password, kubeconfig_context_command, real_time_log=True, sudo_timeout=60) # 获取kubeconfig命令应该较快完成 else: - kubeconfig = ssh_utils.ssh_execute_command(host, port, username, password, kubeconfig_context_command, real_time_log=True) + kubeconfig = await ssh_utils.ssh_execute_command(host, port, username, password, kubeconfig_context_command, real_time_log=True) kubeconfig = kubeconfig[0][0].strip() debug(f'集群上下文:{kubeconfig=}') results = join_idp + "###" + kubeconfig @@ -269,10 +269,10 @@ async def new_cluster_install(params): join_command = params.get("join_command") if username != "root": - ssh_utils.ssh_execute_command_noroot(host, port, username, password, [join_command], + await ssh_utils.ssh_execute_command_noroot(host, port, username, password, [join_command], real_time_log=True, sudo_timeout=120) # 工作节点加入可能需要一些时间 else: - ssh_utils.ssh_execute_command(host, port, username, password, [join_command], real_time_log=True) + await ssh_utils.ssh_execute_command(host, port, username, password, [join_command], real_time_log=True) return results diff --git a/app/k8sManager/ssh_utils.py b/app/k8sManager/ssh_utils.py index dccd6ec..479d5af 100644 --- a/app/k8sManager/ssh_utils.py +++ b/app/k8sManager/ssh_utils.py @@ -9,7 +9,7 @@ import paramiko import socket import traceback -def ssh_execute_command(host, port, username, password, commands, real_time_log=False, +async def ssh_execute_command(host, port, username, password, commands, real_time_log=False, remote_exec=True, scp_map=dict()): try: # 创建 SSH 对象 @@ -62,7 +62,7 @@ def ssh_execute_command(host, port, username, password, commands, real_time_log= # ----------------------------------------以下是非Root用户进行Root操作基座------------------------------------------- -def ssh_execute_command_noroot(host, port, username, password, commands, real_time_log=False, +async def ssh_execute_command_noroot(host, port, username, password, commands, real_time_log=False, remote_exec=True, scp_map=dict(), temp_dir="/tmp/ssh_temp", sudo_timeout=500): """ 增强版SSH执行命令函数,支持普通用户向root目录传输文件和执行sudo命令 @@ -137,7 +137,7 @@ def ssh_execute_command_noroot(host, port, username, password, commands, real_ti return [(None, str(e))] -def execute_sudo_command(ssh, command, password, real_time_log, sudo_timeout, username): +async def execute_sudo_command(ssh, command, password, real_time_log, sudo_timeout, username): """ 执行需要sudo权限的命令,处理密码交互和超时 """ -- 2.34.1