Compare commits

...

2 Commits

Author SHA1 Message Date
ysh
970c68d201 Merge pull request '安装异步' (#13) from dev1 into main
Reviewed-on: #13
2025-07-18 14:59:24 +08:00
ysh
ef3e8b58e9 安装异步 2025-07-18 14:58:53 +08:00
2 changed files with 11 additions and 11 deletions

View File

@ -224,13 +224,13 @@ async def new_cluster_install(params):
if username == "root": if username == "root":
# 如果是root用户,直接执行安装脚本 # 如果是root用户,直接执行安装脚本
debug(f'开始Root用户安装集群节点,用户名: {username}, 角色: {role},主机: {host},端口: {port}') 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, install_clusterrole_command, real_time_log=True,
scp_map=scp_map) scp_map=scp_map)
else: else:
# 如果是普通用户,需要先将处理好 # 如果是普通用户,需要先将处理好
debug(f'开始普通用户安装集群节点,用户名: {username}, 角色: {role},主机: {host},端口: {port}') 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, install_clusterrole_command, real_time_log=True,
scp_map=scp_map, scp_map=scp_map,
sudo_timeout=500) # 设置较长的超时时间适应K8s安装过程 sudo_timeout=500) # 设置较长的超时时间适应K8s安装过程
@ -245,19 +245,19 @@ async def new_cluster_install(params):
# 第三步返回加入凭证给cpcc保存pcapi无状态 # 第三步返回加入凭证给cpcc保存pcapi无状态
clusterauth_command = ['kubeadm token create --print-join-command --ttl 0'] clusterauth_command = ['kubeadm token create --print-join-command --ttl 0']
if username != "root": 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命令应该较快完成 real_time_log=True, sudo_timeout=60) # 获取token命令应该较快完成
else: 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() join_idp = join_idp[0][0].strip()
debug(f'集群验证码:{join_idp=}') debug(f'集群验证码:{join_idp=}')
kubeconfig_context_command = ['cat /root/.kube/config'] kubeconfig_context_command = ['cat /root/.kube/config']
if username != "root": 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, kubeconfig_context_command, real_time_log=True,
sudo_timeout=60) # 获取kubeconfig命令应该较快完成 sudo_timeout=60) # 获取kubeconfig命令应该较快完成
else: 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() kubeconfig = kubeconfig[0][0].strip()
debug(f'集群上下文:{kubeconfig=}') debug(f'集群上下文:{kubeconfig=}')
results = join_idp + "###" + kubeconfig results = join_idp + "###" + kubeconfig
@ -269,10 +269,10 @@ async def new_cluster_install(params):
join_command = params.get("join_command") join_command = params.get("join_command")
if username != "root": 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) # 工作节点加入可能需要一些时间 real_time_log=True, sudo_timeout=120) # 工作节点加入可能需要一些时间
else: 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 return results

View File

@ -9,7 +9,7 @@ import paramiko
import socket import socket
import traceback 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()): remote_exec=True, scp_map=dict()):
try: try:
# 创建 SSH 对象 # 创建 SSH 对象
@ -62,7 +62,7 @@ def ssh_execute_command(host, port, username, password, commands, real_time_log=
# ----------------------------------------以下是非Root用户进行Root操作基座------------------------------------------- # ----------------------------------------以下是非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): remote_exec=True, scp_map=dict(), temp_dir="/tmp/ssh_temp", sudo_timeout=500):
""" """
增强版SSH执行命令函数支持普通用户向root目录传输文件和执行sudo命令 增强版SSH执行命令函数支持普通用户向root目录传输文件和执行sudo命令
@ -137,7 +137,7 @@ def ssh_execute_command_noroot(host, port, username, password, commands, real_ti
return [(None, str(e))] 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权限的命令处理密码交互和超时 执行需要sudo权限的命令处理密码交互和超时
""" """