def exec_in_container(self, pod_id, container_name, command):
k8s_pod = self._get_by_id(pod_id)
ssh_access = getattr(k8s_pod, 'direct_access', None)
if not ssh_access:
raise ContainerCommandExecutionError(
"Couldn't access the contianer")
if container_name not in ssh_access['links']:
raise NotFound('Container not found')
username, host = ssh_access['links'][container_name].split('@', 1)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=username, password=ssh_access['auth'],
timeout=10, look_for_keys=False, allow_agent=False)
except Exception:
raise ContainerCommandExecutionError(
'Failed to connect to the container')
try:
_, o, _ = ssh.exec_command(command, timeout=20)
exit_status = o.channel.recv_exit_status()
result = o.read().strip('\n')
except Exception:
raise ContainerCommandExecutionError()
return {'exitStatus': exit_status, 'result': result}
评论列表
文章目录