I'm running some commands over an AWS ec2 instance using fabric. The output (in some parts) contains characters such as:
[K 81% |██████████████████████████ | 142.9MB 161.0MB/s eta 0:00:01
[K 81% |██████████████████████████ | 142.9MB 165.6MB/s eta 0:00:01
[K 81% |██████████████████████████ | 142.9MB 167.0MB/s eta 0:00:01
[K 81% |██████████████████████████ | 142.9MB 167.2MB/s eta 0:00:01
[K 81% |██████████████████████████ | 143.0MB 166.0MB/s eta 0:00:01
[K 81% |██████████████████████████ | 143.0MB 162.4MB/s eta 0:00:01
These are the progress bars that would be printed properly on a terminal shell should the command be run locally.
How do we capture this correctly in stdout of a fabric connection run() command? I've tried setting the expected environment variables and other fabric settings for encoding (if that's the issue here).
This is how I set the fabric connection
def get_ec2_fabric_connection(instance_id, instance_pem_file, region):
"""
establish connection with EC2 instance if necessary
:param instance_id: ec2_instance id
:param instance_pem_file: instance key name
:param region: Region where ec2 instance is launched
:return: Fabric connection object
"""
user = get_instance_user(instance_id, region=region)
conn = Connection(
user=user,
host=get_public_ip(instance_id, region),
inline_ssh_env=True,
connect_kwargs={"key_filename": [instance_pem_file]},
)
return conn
This is how I run a command over fabric:
for command in commands_list:
LOGGER.info(f"*** Executing command on ec2 instance: {command}")
ret_obj = ec2_connection.run(
command,
echo=True,
warn=True,
pty=True,
shell="/bin/bash",
env={
"LC_CTYPE": "en_US.utf8",
"JAVA_HOME": "/usr/lib/jvm/java-11-openjdk-amd64",
"PYTHONIOENCODING": "utf8",
},
encoding="utf8"
)
Thank you.
Edit:
My resolution was the following, based on this link: I was using 'pty=True' for no good reason. Removing its usage meant that progress bars were no longer printed by most processes. The output is now proper.