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

Slight refactor on vmware_guest to fix path searching #26511

Merged
merged 14 commits into from
Jul 12, 2017
Merged

Slight refactor on vmware_guest to fix path searching #26511

merged 14 commits into from
Jul 12, 2017

Conversation

jctanner
Copy link
Contributor

@jctanner jctanner commented Jul 7, 2017

SUMMARY

Fixes #25011

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

vmware_guest

ANSIBLE VERSION
devel

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bugfix_pull_request c:module_utils/ cloud needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. vmware VMware community labels Jul 7, 2017
@ansibot
Copy link
Contributor

ansibot commented Jul 7, 2017

The test ansible-test sanity --test pep8 failed with the following errors:

lib/ansible/module_utils/vmware.py:238:9: E265 block comment should start with '# '
lib/ansible/module_utils/vmware.py:240:9: E265 block comment should start with '# '
lib/ansible/modules/cloud/vmware/vmware_guest.py:484:13: E303 too many blank lines (2)
lib/ansible/modules/cloud/vmware/vmware_guest.py:491:33: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1107:13: E265 block comment should start with '# '
lib/ansible/modules/cloud/vmware/vmware_guest.py:1110:9: E265 block comment should start with '# '
lib/ansible/modules/cloud/vmware/vmware_guest.py:1115:17: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1124:17: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1134:17: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1135:17: E702 multiple statements on one line (semicolon)

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jul 7, 2017
@ansibot
Copy link
Contributor

ansibot commented Jul 7, 2017

The test ansible-test sanity --test pep8 failed with the following errors:

lib/ansible/modules/cloud/vmware/vmware_guest.py:1103:13: E265 block comment should start with '# '
lib/ansible/modules/cloud/vmware/vmware_guest.py:1106:9: E265 block comment should start with '# '
lib/ansible/modules/cloud/vmware/vmware_guest.py:1111:17: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1120:17: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1130:17: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:1131:17: E702 multiple statements on one line (semicolon)

click here for bot help

@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jul 7, 2017
@jctanner
Copy link
Contributor Author

jctanner commented Jul 7, 2017

@nerzhul @dav1x @dericcrago @Akasurde @dagwieers

Can you guys test this please?

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jul 7, 2017
result = eval(expression)
except:
pass
return result
Copy link
Contributor

Choose a reason for hiding this comment

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

Something like this instead:

def _get_vm_prop(vm, attributes):
    result = vm
    for attribute in attributes:
        try:
            result = getattr(result, attribute)
        except AttributeError:
            return None
    return result

_get_vm_props(vm, ('guest', 'toolsRunningStatus'))

Copy link
Member

Choose a reason for hiding this comment

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

I think, using pass instead of returning None in AttributeError as return None fails to retrieve snapshots. Snapshot data works perfect with pass.

Copy link
Contributor

Choose a reason for hiding this comment

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

pass doesn't advance result to the next layer of the object, though. Imagine a data structure like this:

class Alice:
    foo = 1

class Bob:
     bar = Alice()
     foo = 2

obj = Bob()
del obj.bar
result = _get_vm_props(obj, ('bar', 'foo')
# with return None:
result == None
# with pass
result == 2

For snapshot, what are you calling? This seems right and it seems like it would do the right thing:

snapshot = _get_vm_prop(vm, ['snapshot'])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Akasurde I've tested this patch on cloning from snapshots, so I think it does work to some degree. Can you extrapolate on which situations you don't think it will work?

@jctanner
Copy link
Contributor Author

jctanner commented Jul 7, 2017

@cigamit are you able to test this?

self.module.fail_json(msg="Folder %(folder)s needs to be an absolute path, starting with '/'." % self.params)
searchpath = '%(datacenter)s%(folder)s' % self.params
# searchpaths do not need to be absolute
searchpath = self.params['folder']
Copy link
Member

Choose a reason for hiding this comment

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

Datacenter is required for FindByInventoryPath. Without datacenter API, fails to find the VM.

Copy link
Member

Choose a reason for hiding this comment

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

Can we move getvm method to vmware.py so that vmware_guest_facts or other modules could use it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have tested, and found that FindByInventoryPath does need the full path to find a folder in both my scenarios where VMs are nested like so.
/TestDCFolder/TestDC/TestCluster/TestFolder/
and
/TestDC/TestCluster/TestFolder/

In both cases, I needed to change searchpath to match the full path to the DC, then /vm/ and the actual folder. So in the above instances it was.
/TestDCFolder/TestDC/vm/TestFolder/
and
/TestDC/vm/TestFolder/

module.params['folder'] = module.params['folder'].rstrip('/')

pyv = PyVmomiHelper(module)

# Check if the VM exists before continuing
vm = pyv.getvm(name=module.params['name'], folder=module.params['folder'], uuid=module.params['uuid'])
if not vm:
module.fail_json(msg="nope")
Copy link
Member

Choose a reason for hiding this comment

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

Could you please change this message to something descriptive about the error?

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jul 7, 2017
@Akasurde
Copy link
Member

Akasurde commented Jul 7, 2017

I have made changes and tested with vmware_guest_facts module.
With @abadger comment and searchpath = '%(datacenter)s%(folder)s' % self.params changes -

ok: [localhost] => {
    "changed": false, 
    "failed": false, 
    "instance": {
        "annotation": "", 
        "current_snapshot": null, 
        "customvalues": {}, 
        "guest_tools_status": "guestToolsNotRunning", 
        "guest_tools_version": "0", 
        "hw_eth0": {
            "addresstype": "generated", 
            "ipaddresses": null, 
            "label": "Network adapter 1", 
            "macaddress": "00:0c:29:93:a7:bc", 
            "macaddress_dash": "00-0c-29-93-a7-bc", 
            "summary": "VM Network"
        }, 
        "hw_guest_full_name": null, 
        "hw_guest_id": null, 
        "hw_interfaces": [
            "eth0"
        ], 
        "hw_memtotal_mb": 256, 
        "hw_name": "SampleVM", 
        "hw_power_status": "poweredOff", 
        "hw_processor_count": 1, 
        "hw_product_uuid": "564dbd8c-30e8-2b9f-50c9-fbc47993a7bc", 
        "ipv4": null, 
        "ipv6": null, 
        "module_hw": true, 
        "snapshots": []
    }, 
    "invocation": {
        "module_args": {
            "datacenter": "ha-datacenter", 
            "folder": "/vm", 
            "hostname": "192.168.122.68", 
            "name": "SampleVM", 
            "name_match": "first", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "username": "root", 
            "uuid": null, 
            "validate_certs": false
        }
    }
}

Without datacenter value in searchpath it fails to find VM.

self.module.fail_json(msg="Folder %(folder)s needs to be an absolute path, starting with '/'." % self.params)
searchpath = '%(datacenter)s%(folder)s' % self.params
# searchpaths do not need to be absolute
searchpath = self.params['folder']
Copy link
Member

Choose a reason for hiding this comment

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

Can we move getvm method to vmware.py so that vmware_guest_facts or other modules could use it?

Copy link
Contributor

@cigamit cigamit left a comment

Choose a reason for hiding this comment

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

This leads me to believe that I should set my folder like so
folder: /Test/TestDC/vm/TestF
but doing that causes the clone task (vm_obj.Clone) to fail with
"A specified parameter was not correct: spec.location.folder"

It works in the scenario that the Datacenter is not nested under a folder, but Nesting it under a folder causes the error above.

@jctanner
Copy link
Contributor Author

jctanner commented Jul 8, 2017

@cigamit thanks for testing. I'll try to get a a real vcenter setup with this configuration and revise the patch accordingly.


# split the searchpath so we can iterate through it
paths = [x.replace('/', '') for x in searchpath.split('/')]
paths = [x.strip() for x in paths if x.strip()]
Copy link
Contributor

Choose a reason for hiding this comment

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

  • x.replace('/', '') shouldn't be necessary. searchpath.split('/') should have removed all instances of "/" from the output.
  • Is it valid to strip the path elements? Could you have a valid path like this: /folder1/ datacenter1 /vm?

@ansibot
Copy link
Contributor

ansibot commented Jul 10, 2017

The test ansible-test sanity --test pep8 failed with the following errors:

lib/ansible/modules/cloud/vmware/vmware_guest.py:464:22: E231 missing whitespace after ','
lib/ansible/modules/cloud/vmware/vmware_guest.py:538:22: E231 missing whitespace after ','
lib/ansible/modules/cloud/vmware/vmware_guest.py:556:25: E702 multiple statements on one line (semicolon)
lib/ansible/modules/cloud/vmware/vmware_guest.py:561:1: E302 expected 2 blank lines, found 1
lib/ansible/modules/cloud/vmware/vmware_guest.py:1343:113: W291 trailing whitespace
lib/ansible/modules/cloud/vmware/vmware_guest.py:1388:161: E501 line too long (186 > 160 characters)

The test ansible-test sanity --test validate-modules failed with the following error:

lib/ansible/modules/cloud/vmware/vmware_guest.py:518:0: E403 Type comparison using type() found. Use isinstance() instead

click here for bot help

@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Jul 10, 2017
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jul 10, 2017
@ansibot
Copy link
Contributor

ansibot commented Jul 10, 2017

The test ansible-test sanity --test validate-modules failed with the following error:

lib/ansible/modules/cloud/vmware/vmware_guest.py:518:0: E403 Type comparison using type() found. Use isinstance() instead

click here for bot help

@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Jul 10, 2017
@alikins alikins removed the needs_triage Needs a first human triage before being processed. label Jul 10, 2017
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jul 11, 2017
@jctanner jctanner merged commit 10fc441 into ansible:devel Jul 12, 2017
@cigamit
Copy link
Contributor

cigamit commented Jul 12, 2017

There still seems to be some regression involving Resource Pools, I'm attempting to debug the code currently. I can clone into my DC that is nested, but not my normal DC. I keep getting the error
A specified parameter was not correct: spec.location.host

  • Cloning without resource or snapshot set fails
  • Cloning with snapshot fails
  • Cloning with resource set to "Resources" fails
  • Cloning into a known Resource Pool succeeds

All these pass when cloning into my Nested DC though. Cross DC clones from Unnested to Nested DC passes also. Its almost like it is still pulling something from the wrong DC.

Also, when debugging the above, I found that if I don't specify a cluster, and instead use esxi_hostname but don't specify a Resource Pool, I get
TypeError was returned, please ensure to give correct inputs. For \"pool\" expected type vim.ResourcePool, but got vim.Datastore
If I specify a RP in the cluster, I get an error about it not being able to find said Pool.

@cigamit
Copy link
Contributor

cigamit commented Jul 12, 2017

If I don't specify a location for the Disk (since I'm not making changes to it) it seems to "Auto-Select" instead of placing it in the same location as the VM I am cloning. I think this is one of the changes that was fixed where it was just inheriting the wrong properties from the template. While this could be acceptable, I can not specify a specific Datastore via disk without also specifying a size (the size seems to want to be required).

@cigamit
Copy link
Contributor

cigamit commented Jul 12, 2017

This fix now works for me in all scenarios I have tested (Nested DCs, Unnested DCs, Cross DC Cloning, With/Without Resource Pools, Cloning from Snapshot, and Linked Clones)

jctanner added a commit that referenced this pull request Jul 12, 2017
)

* vmware_guest: fixes for cache objects and datacenter association
* find_all_objs was only looking for datastores
* Clear the result if it's datacenter is not correct.
* Re-enable pyvmomi installation

Addresses #25011
Addresses #26511
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bugfix_pull_request labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. c:module_utils/ cloud needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:core This issue/PR relates to code supported by the Ansible Engineering Team. vmware VMware community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vmware_guest: VM Deployment fails when Datacenter is nested under a folder
6 participants