ClusterLabs / fence-agents

Fence agents
104 stars 160 forks source link

fence_aliyun: UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32 #452

Closed bd233 closed 3 years ago

bd233 commented 3 years ago

When the user uses the Python2 environment to run the commands fence_aliyun --region cn-hangzhou -a <ak> -s <sk> --action list-status, they will get such an error:

Traceback (most recent call last): File "/usr/sbin/fence_aliyun", line 173, in main() File "/usr/sbin/fence_aliyun", line 169, in main result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list) File "/usr/share/fence/fencing.py", line 963, in fence_action print(outlet_id + options["--separator"] + alias + options["--separator"] + status) UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: ordinal not in range(128)

First of all, this is because the "InstanceName" of the user's instance contains Chinese characters, resulting in print(outlet_id + options["--separator"] + alias + options["--separator"] + status) error

So we are considering whether to replace InstanceName with HostName to support this scenario: https://github.com/ClusterLabs/fence-agents/blob/ef39c292fda212e2b532b7b93acd8fcdb49dc4b4/agents/aliyun/fence_aliyun.py#L70

If this is not a good solution, then we can only recommend that users use Python3 or modify InstanceName.

oalbrigt commented 3 years ago

Thanks for the info.

Can you try to update outlet_id in the print-line above to: outlet_id.encode(sys.getdefaultencoding())

If not I can add the parameter as suggested and give an error message that suggests changing it when the agent gets the UnicodeEncodeError exception.

bd233 commented 3 years ago

Can you try to update outlet_id in the print-line above to: outlet_id.encode(sys.getdefaultencoding())

I think it should not be updated for outlet_id, it should be alias,alias.encode(sys.getdefaultencoding()), in these two places https://github.com/ClusterLabs/fence-agents/blob/ef39c292fda212e2b532b7b93acd8fcdb49dc4b4/lib/fencing.py.py#L961 https://github.com/ClusterLabs/fence-agents/blob/ef39c292fda212e2b532b7b93acd8fcdb49dc4b4/lib/fencing.py.py#L963 I did not test and verify this

If not I can add the parameter as suggested and give an error message that suggests changing it when the agent gets the UnicodeEncodeError exception.

I am unlikely to suggest that Alibaba Cloud users modify fencing.py manually, so giving an error message may be a better way

oalbrigt commented 3 years ago

Yeah. I was suggesting if you test and verify that alias.encode(sys.getdefaultencoding()) we can patch it in the project.

bd233 commented 3 years ago

Yeah. I was suggesting if you test and verify that alias.encode(sys.getdefaultencoding()) we can patch it in the project.

I tried to fix the problem in python2 in the following way:

try: print(outlet_id + options["--separator"] + alias) except UnicodeEncodeError as e: print((outlet_id + options["--separator"] + alias).encode("utf-8"))

This is feasible

oalbrigt commented 3 years ago

Great. I'll make a patch to do that.

oalbrigt commented 3 years ago

I've made a patch based on your suggestion: https://github.com/ClusterLabs/fence-agents/pull/454

Can you test it and also verify that -o list-status works fine as well?

bd233 commented 3 years ago

I've made a patch based on your suggestion: #454

Can you test it and also verify that -o list-status works fine as well?

Yes, no problem, I will test and give feedback tomorrow

bd233 commented 3 years ago

I've made a patch based on your suggestion: #454

Can you test it and also verify that -o list-status works fine as well?

I tested it under Python3 and 2, and it seems to work very well. python2 image python3 image

bd233 commented 3 years ago

@oalbrigt I have another question: In which version will this fix be released, and how do I update to this version.

oalbrigt commented 3 years ago

Great. Thanks for testing.

It'll be in v4.11.0, which will be available in about a weeks time.

You should file a bug against common distros used (since this is Python 2.x issue distro versions needing it probably wont get the updated version, but they can backport the patch easily).

oalbrigt commented 3 years ago

v4.11.0 has now been released: https://github.com/ClusterLabs/fence-agents/releases/tag/v4.11.0