我想使用 python/boto3 向 ecs/FARGATE 容器发送命令。 我可以使用 execute_command 成功打开会话。
但是我怎样才能获得命令的输出呢?然后等到完成?
我找到了很多关于 aws cli 和 session-manager-plugin 的信息。因此一种方法是从 python 运行 aws cli。但是还有没有办法从原生 python 获取命令的输出?
您可以重新实现
aws execute-command
内部使用的相同过程:
# making an execute-command call to connect to an
# active session on a container would require
# session-manager-plugin to be installed on the client machine.
# Hence, making this empty session-manager-plugin call
# before calling execute-command to ensure that
# session-manager-plugin is installed
# before execute-command-command is made
check_call(["session-manager-plugin"])
client = self._session.create_client(
...
response = client.execute_command(**parameters)
...
ssm_request_params = build_ssm_request_paramaters(response, client)
# ignore_user_entered_signals ignores these signals
# because if signals which kills the process are not
# captured would kill the foreground process but not the
# background one. Capturing these would prevents process
# from getting killed and these signals are input to plugin
# and handling in there
with ignore_user_entered_signals():
# call executable with necessary input
check_call(["session-manager-plugin",
...
此代码作者的提交消息总结了该方法:
https://github.com/ronalddiero/aws-cli/commit/ba4163d46969a8aba0e4712852aba9ad5a3f3667
ECS EXECUTE-COMMAND API 的 CLI 自定义
- ECS执行命令API使用以下命令返回streamUrl和令牌值 用户可以通过调用连接到正在运行的容器内的会话 具有这些值的会话管理器插件。
- 此 CLI 自定义为用户进行此调用,因此在内部 拨打两个电话。