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

Handle openstacksdk < 0.10.0: fix AttributeError #50285

Conversation

pilou-
Copy link
Contributor

@pilou- pilou- commented Dec 24, 2018

SUMMARY

Handle openstacksdk < 0.10.0: fix AttributeError

Error was:

The full traceback is:
Traceback (most recent call last):
  File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 113, in <module>
    _ansiballz_main()
  File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 105, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 48, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_os_security_group_payload_keFTIJ/__main__.py", line 163, in <module>
  File "/tmp/ansible_os_security_group_payload_keFTIJ/__main__.py", line 115, in main
  File "/tmp/ansible_os_security_group_payload_keFTIJ/ansible_os_security_group_payload.zip/ansible/module_utils/openstack.py", line 121, in openstack_cloud_from_module
AttributeError: 'module' object has no attribute 'version'
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

lib/ansible/module_utils/openstack.py

ADDITIONAL INFORMATION

@ansibot
Copy link
Contributor

ansibot commented Dec 24, 2018

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. cloud community_review In order to be merged, this PR must follow the community review workflow. needs_triage Needs a first human triage before being processed. openstack support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback. labels Dec 24, 2018
@opendev-zuul
Copy link

opendev-zuul bot commented Dec 24, 2018

Build succeeded (third-party-check pipeline).

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jan 1, 2019
@ansibot
Copy link
Contributor

ansibot commented Apr 7, 2019

cc @gtema
click here for bot help

module.fail_json(
msg="To utilize this module, the installed version of "
"the openstacksdk library MUST be >=0.10.0")

Copy link
Contributor

Choose a reason for hiding this comment

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

Why this code change is required?
Isn't this already checked in the lines above?

Copy link
Contributor

Choose a reason for hiding this comment

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

min_version also defaults to 0.12.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another version could be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When openstacksdk 0.9.19 is used (assuming caller is using a lower version than the default one), without the additional check, the following exception occurs:

Traceback (most recent call last):
  File "~/.ansible/tmp/ansible-tmp-1555747400.49-76880204403181/AnsiballZ_os_security_group.py", line 113, in <module>
    _ansiballz_main()
  File "~/.ansible/tmp/ansible-tmp-1555747400.49-76880204403181/AnsiballZ_os_security_group.py", line 105, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "~/.ansible/tmp/ansible-tmp-1555747400.49-76880204403181/AnsiballZ_os_security_group.py", line 48, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_os_security_group_payload_0EWiaz/__main__.py", line 163, in <module>
  File "/tmp/ansible_os_security_group_payload_0EWiaz/__main__.py", line 115, in main
  File "/tmp/ansible_os_security_group_payload_0EWiaz/ansible_os_security_group_payload.zip/ansible/module_utils/openstack.py", line 162, in openstack_cloud_from_module
AttributeError: 'module' object has no attribute 'exceptions'

The current code isn't compatible with openstacksdk < 0.10.0.
Because this is a module_utils, other module not belonging to the ansible repository might use this method with another value than the default one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marsavela @dominikholler: please, could you take a look at this ?

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 29, 2019
@@ -114,16 +114,22 @@ def openstack_cloud_from_module(module, min_version='0.12.0'):
# Due to the name shadowing we should import other way
import importlib
sdk = importlib.import_module('openstack')
sdk_version = importlib.import_module('openstack.version')
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you think this additional line is helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you read the stacktrace mentioned in the description of this PR and in the first commit ?

sdk.version.__version__ expression raises an AttributeError exception when openstacksdk < 0.10.0 is used.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you read the stacktrace mentioned in the description of this PR and in the first commit ?

Sure, but I did not understand how this is related, but with your additional exlaination, I understand now.
The commit message could be improved by writing something like
"openstack.version.__version__ expression raises an AttributeError exception when openstacksdk < 0.10.0 is used. openstack.version is now imported as a module, which works for all openstacksdk versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR updated:

  • commit message updated
  • rebased on devel

@@ -114,16 +114,22 @@ def openstack_cloud_from_module(module, min_version='0.12.0'):
# Due to the name shadowing we should import other way
import importlib
sdk = importlib.import_module('openstack')
sdk_version = importlib.import_module('openstack.version')
except ImportError:
module.fail_json(msg='openstacksdk is required for this module')

Copy link
Contributor

Choose a reason for hiding this comment

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

The current code isn't compatible with openstacksdk < 0.10.0.
Because this is a module_utils, other module not belonging to the ansible repository might use this method with another value than the default one.

Sounds like min_version should be set to max(StrictVersion('0.10.0'), StrictVersion(min_version)) here.
This would avoid the redundancy below, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some modules rely on this default value and don't work with 0.10.0.

For example using os_security_group:

  1. with devel branch
  2. the following diff applied
    -def openstack_cloud_from_module(module, min_version='0.12.0'):
    +def openstack_cloud_from_module(module, min_version='0.10.0'):
    
  3. openstacksdk==0.10.0 installed (same error occurs with 0.11.0)
  4. this task:
    - os_security_group:
        cloud: mycloud
        state: present
        name: foo
    

this exception occurs:

TASK [os_security_group] ***
An exception occurred during task execution. To see the full traceback, use -vvv.
The error was: AttributeError: 'Connection' object has no attribute 'current_project_id'
fatal: [localhost]: FAILED! => {
    "changed": false, 
    "rc": 1
}

MSG:

MODULE FAILURE
See stdout/stderr for the exact error


MODULE_STDERR:

Traceback (most recent call last):
  File "$HOME/.ansible/tmp/ansible-tmp-1556617879.09-260636442237090/AnsiballZ_os_security_group.py", line 125, in <module>
    _ansiballz_main()
  File "$HOME/.ansible/tmp/ansible-tmp-1556617879.09-260636442237090/AnsiballZ_os_security_group.py", line 117, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "$HOME/.ansible/tmp/ansible-tmp-1556617879.09-260636442237090/AnsiballZ_os_security_group.py", line 54, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_os_security_group_payload_uLUg4r/__main__.py", line 163, in <module>
  File "/tmp/ansible_os_security_group_payload_uLUg4r/__main__.py", line 123, in main
AttributeError: 'Connection' object has no attribute 'current_project_id'

Then if 0.10.0 is used by default, callers must be modified in order to pass 0.12.0 when required. Is that OK with you ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Some modules rely on this default value and don't work with 0.10.0.

No doubts about this.

Then if 0.10.0 is used by default, callers must be modified in order to pass 0.12.0 when required. Is that OK with you ?

Sorry, I did not mean to use 0.10.0 as the default, but process the bigger value, either 0.10.0 or the parmeter value, which is 0.12.0 by default.
This way we would use 0.10.0 if < 0.10.0 is requested or 0.12.0 if the default parmater value is used. If the parameter is set explicitly to > 0.10.0, we would use this.
Is this explained understandable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks!

PR updated.

@pilou- pilou- force-pushed the AttributeError_module_utils_openstack_module_version branch from 6d2f527 to f3e0a72 Compare April 30, 2019 10:11
@opendev-zuul
Copy link

opendev-zuul bot commented Apr 30, 2019

Build succeeded (third-party-check pipeline).

@pilou- pilou- force-pushed the AttributeError_module_utils_openstack_module_version branch from f3e0a72 to 970d508 Compare April 30, 2019 10:46
Copy link
Contributor

@dominikholler dominikholler left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!
Did can you please write in a comment, for which versions of openstack sdk you verified this code?

@@ -114,15 +114,20 @@ def openstack_cloud_from_module(module, min_version='0.12.0'):
# Due to the name shadowing we should import other way
import importlib
sdk = importlib.import_module('openstack')
sdk_version = importlib.import_module('openstack.version')
except ImportError:
module.fail_json(msg='openstacksdk is required for this module')

if min_version:
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can remove the if min_version:? min_version has a useful default, and if min_version=None is passed, it is OK to explode, from my point of view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not OK (see).

@opendev-zuul
Copy link

opendev-zuul bot commented Apr 30, 2019

Build succeeded (third-party-check pipeline).

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Apr 30, 2019
@pilou-
Copy link
Contributor Author

pilou- commented Apr 30, 2019

Did can you please write in a comment, for which versions of openstack sdk you verified this code?

  • 0.9.0
$ pip list |grep openstacksdk
openstacksdk       0.9.0    
TASK [os_security_group] 
fatal: [localhost]: FAILED! => {
    "changed": false
}

MSG:

To utilize this module, the installed version of the openstacksdk library MUST be >=0.12.
  • 0.10.0
$ pip list |grep openstacksdk
openstacksdk       0.10.0    
TASK [os_security_group] 
fatal: [localhost]: FAILED! => {
    "changed": false
}

MSG:

To utilize this module, the installed version of the openstacksdk library MUST be >=0.12.
  • 0.11.0
$ pip list |grep openstacksdk
openstacksdk       0.11.0
TASK [os_security_group] 
fatal: [localhost]: FAILED! => {
    "changed": false
}

MSG:

To utilize this module, the installed version of the openstacksdk library MUST be >=0.12.
  • 0.12.0
$ pip list |grep openstacksdk
openstacksdk       0.12.0    

No error.

  • Manual unit test with openstacksdk 0.9.0 installed (these exceptions doesn't occur when using 0.12.0)
>>> from units.module_utils.urls.test_fetch_url import FakeAnsibleModule
>>> from ansible.module_utils.openstack import openstack_cloud_from_module
>>> try:
...     openstack_cloud_from_module(FakeAnsibleModule(), min_version=None)
... except Exception as e:
...   print(e.kwargs)
{'msg': 'To utilize this module, the installed version of the openstacksdk library MUST be >=0.10.'}
>>> try:
...     openstack_cloud_from_module(FakeAnsibleModule(), min_version='0.9.0')
... except Exception as e:
...   print(e.kwargs)
{'msg': 'To utilize this module, the installed version of the openstacksdk library MUST be >=0.10.'}

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Apr 30, 2019
@pilou-
Copy link
Contributor Author

pilou- commented May 3, 2019

bot_status

@ansibot
Copy link
Contributor

ansibot commented May 3, 2019

Components

lib/ansible/module_utils/openstack.py
support: community
maintainers: Shrews cloudnull dagnello emonty evrardjp gtema juliakreger mnaser odyssey4me rcarrillocruz

Metadata

waiting_on: maintainer
changes_requested_by: null
needs_info: False
needs_revision: False
needs_rebase: False
merge_commits: []
too many files or commits: False
mergeable_state: clean
shippable_status: success
maintainer_shipits (module maintainers): 0
community_shipits (namespace maintainers): 0
ansible_shipits (core team members): 0
shipit_actors (maintainer or core team member): []
shipit_actors_other: []
automerge: automerge shipit test failed

click here for bot help

@emonty
Copy link
Contributor

emonty commented May 9, 2019

We should not attempt to handle openstacksdk < 0.10.0. The Ansible OpenStack modules have never supported <0.10.0 and should not attempt to start doing so now. People who want to use older versions of libraries with Ansible for out-of-tree drivers should be using the shade library, which is what was needed for the Ansible OpenStack modules before sdk 0.10.0. Even still, I'm not sure we even want to attempt to support 0.10.0 when the default min is 0.12.0. Let's just keep the default min as 0.12.

That said - I actually like most of this PR - since it prevents people from having modules that call this with a lower min_version than our actual min and gives them a better error message. Let's update it to use 0.12 instead of 0.12 and then I'm good with it.

msg="To utilize this module, the installed version of"
"the openstacksdk library MUST be >={min_version}".format(
min_version=min_version))
min_version = max(StrictVersion('0.10.0'), StrictVersion(min_version))
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change this to 0.12 to match our min.

min_version=min_version))
min_version = max(StrictVersion('0.10.0'), StrictVersion(min_version))
else:
min_version = StrictVersion('0.10.0')
Copy link
Contributor

Choose a reason for hiding this comment

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

And this.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed community_review In order to be merged, this PR must follow the community review workflow. needs_triage Needs a first human triage before being processed. labels May 9, 2019
pilou- added 3 commits May 9, 2019 17:35
`openstack.version.__version__` expression raises an `AttributeError`
exception when openstacksdk < 0.10.0 is used. `openstack.version` is
now imported as a module, which works for all openstacksdk versions.

Error was:

    The full traceback is:
    Traceback (most recent call last):
      File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 113, in <module>
        _ansiballz_main()
      File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 105, in _ansiballz_main
        invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
      File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 48, in invoke_module
        imp.load_module('__main__', mod, module, MOD_DESC)
      File "/tmp/ansible_os_security_group_payload_keFTIJ/__main__.py", line 163, in <module>
      File "/tmp/ansible_os_security_group_payload_keFTIJ/__main__.py", line 115, in main
      File "/tmp/ansible_os_security_group_payload_keFTIJ/ansible_os_security_group_payload.zip/ansible/module_utils/openstack.py", line 121, in openstack_cloud_from_module
    AttributeError: 'module' object has no attribute 'version'
@pilou- pilou- force-pushed the AttributeError_module_utils_openstack_module_version branch from 970d508 to 6e9b003 Compare May 9, 2019 15:36
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels May 9, 2019
@opendev-zuul
Copy link

opendev-zuul bot commented May 9, 2019

Build succeeded (third-party-check pipeline).

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels May 9, 2019
@pilou-
Copy link
Contributor Author

pilou- commented May 9, 2019

@emonty thanks for the review !

PR rebased and updated following your feedback (openstack documentation fragment has been updated).

ready_for_review

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels May 9, 2019
Copy link
Contributor

@emonty emonty left a comment

Choose a reason for hiding this comment

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

shipit

Copy link
Contributor

@gtema gtema left a comment

Choose a reason for hiding this comment

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

shipit

@ansibot ansibot added shipit This PR is ready to be merged by Core and removed community_review In order to be merged, this PR must follow the community review workflow. labels May 10, 2019
@pilou-
Copy link
Contributor Author

pilou- commented May 13, 2019

bot_status

@ansibot
Copy link
Contributor

ansibot commented May 13, 2019

Components

lib/ansible/module_utils/openstack.py
support: community
maintainers: Shrews cloudnull dagnello emonty evrardjp gtema juliakreger mnaser odyssey4me rcarrillocruz

lib/ansible/plugins/doc_fragments/openstack.py
support: community
maintainers:

Metadata

waiting_on: maintainer
changes_requested_by: null
needs_info: False
needs_revision: False
needs_rebase: False
merge_commits: []
too many files or commits: False
mergeable_state: clean
shippable_status: success
maintainer_shipits (module maintainers): 1
community_shipits (namespace maintainers): 0
ansible_shipits (core team members): 1
shipit_actors (maintainers or core team members): emonty gtema
shipit_actors_other: []
automerge: automerge is_module test failed

click here for bot help

@pilou-
Copy link
Contributor Author

pilou- commented May 14, 2019

@emonty please, could you manually merge this one ? automerge isn't available when module_utils paths are involved.

@emonty emonty merged commit 4f25435 into ansible:devel May 24, 2019
@ansible ansible locked and limited conversation to collaborators Aug 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. cloud openstack shipit This PR is ready to be merged by Core support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants