Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fast-reboot] Fix unicode issue in ipaddress for python2 #1164

Merged
merged 1 commit into from
Oct 14, 2020

Conversation

bingwang-ms
Copy link
Contributor

- What I did
The ipaddress.ip_interface accepts only unicode string as input. However, an exception will be raised on python 2.

Oct 12 03:32:41.758040 str-dx010-acs-4 ERR fast-reboot-dump: Got an exception '192.168.0.240' does not appear to be an IPv4 or IPv6 interface: 
Traceback: 
Traceback (most recent call last):
#012  File "/usr/local/bin/fast-reboot-dump.py", line 298, in <module>
#012    res = main()
#012  File "/usr/local/bin/fast-reboot-dump.py", line 289, in main
#012    neighbor_entries = generate_neighbor_entries(root_dir + '/arp.json', all_available_macs)
#012  File "/usr/local/bin/fast-reboot-dump.py", line 42, in generate_neighbor_entries
#012    if ipaddress.ip_interface(ip_addr).ip.version != 4:
#012  File "/usr/local/lib/python2.7/dist-packages/ipaddress.py", line 239, in ip_interface
#012  ValueError: '192.168.0.240' does not appear to be an IPv4 or IPv6 interface

As a result, fast-reboot aborted with error code 2.

admin@str-dx010-acs-4:~$ sudo fast-reboot -v
Failed to run fast-reboot-dump.py. Exit code: 2
Mon 12 Oct 2020 03:34:21 AM UTC fast-reboot failure (12) cleanup ...

This PR is to fix the issue.
- How I did it
Use str in builtins to handle the conversion of unicode.

- How to verify it
Verified on both python2 and python3.
For python2, running fast-reboot, and the command complete successfully without exception.
For python3, a demo script is tested to verify the update.
- Previous command output (if the output of a command-line utility has changed)
N/A
- New command output (if the output of a command-line utility has changed)
N/A

The 'ipaddress.ip_interface' accepts only unicode str as input, which is
default in python3. However, an exception will raise when running in
python2. This commit fix the issue.

Signed-off-by: bingwang <bingwang@microsoft.com>
@bingwang-ms
Copy link
Contributor Author

Retest this please

@@ -39,7 +40,7 @@ def generate_neighbor_entries(filename, all_available_macs):
arp_output.append(obj)

ip_addr = key.split(':')[2]
if ipaddress.ip_interface(ip_addr).ip.version != 4:
if ipaddress.ip_interface(str(ip_addr)).ip.version != 4:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this fix issue:5580? One comment is that: can you make this Python3 friendly by checking the Python version?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I have a draft PR where I am working on adding support for Python 3. You can look at this PR to see if it will play nice: #1128

Copy link
Contributor Author

@bingwang-ms bingwang-ms Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this fix issue:5580? One comment is that: can you make this Python3 friendly by checking the Python version?

Yes. The update is friendly to both python 2 and python 3. I have verified on both python version.

admin@str-dx010-acs-4:~$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> from builtins import str
>>> ip_addr = '192.168.0.1'
>>> ipaddress.ip_interface(str(ip_addr)).ip.version
4
>>> ipv6_addr = 'fc00:2::32'
>>> ipaddress.ip_interface(str(ipv6_addr)).ip.version
6
>>> exit()
admin@str-dx010-acs-4:~$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> from builtins import str
>>> ip_addr = '192.168.0.1'
>>> ipaddress.ip_interface(str(ip_addr)).ip.version
4
>>> ipv6_addr = 'fc00:2::32'
>>> ipaddress.ip_interface(str(ipv6_addr)).ip.version
6

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bingwang-ms Can you please have a look at ipv6 splitting function below? it does not look that this would work with longer prefixes. Could be in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind. it is splitting the key and recover the Ipv6 from the key and not the ipaddr. it was an oversight.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this fix issue:5580? One comment is that: can you make this Python3 friendly by checking the Python version?

Sorry for my last unclear reply. The issue sonic-net/sonic-buildimage#5580 is not fixed by this PR. The unicode issue is not found in fast-reboot-dump.py in 201911 branch.

@tahmed-dev
Copy link
Contributor

This is similar to PR:1148

@liat-grozovik
Copy link
Collaborator

this is needed for 201911 as well. Not sure if needed for 20206.
@tahmed-dev can you add please the req to the PR message?

@jleveque
Copy link
Contributor

Does this PR make #1148 obsolete?

@bingwang-ms
Copy link
Contributor Author

bingwang-ms commented Oct 14, 2020

Does this PR make #1148 obsolete?

Yes. Indeed. Sorry for not searching enough. But I think the fix in this PR is more elegant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants