[2.9] VMware: vmware_export_ovf module fix Python3 compatibility issue (#67514)

(cherry picked from commit 023a9b3166)

Co-authored-by: Diane Wang <41371902+Tomorrow9@users.noreply.github.com>
pull/67986/head
Abhijeet Kasurde 5 years ago committed by GitHub
parent e64b120bff
commit f8aaa7dcd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Fix Python3 compatibility for vmware_export_ovf module.

@ -112,7 +112,7 @@ from time import sleep
from threading import Thread from threading import Thread
from ansible.module_utils.urls import open_url from ansible.module_utils.urls import open_url
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text, to_bytes
from ansible.module_utils.vmware import vmware_argument_spec, PyVmomi from ansible.module_utils.vmware import vmware_argument_spec, PyVmomi
try: try:
from pyVmomi import vim from pyVmomi import vim
@ -179,6 +179,7 @@ class VMwareExportVmOvf(PyVmomi):
total_bytes_to_write): total_bytes_to_write):
mf_content = 'SHA256(' + os.path.basename(temp_target_disk) + ')= ' mf_content = 'SHA256(' + os.path.basename(temp_target_disk) + ')= '
sha256_hash = hashlib.sha256() sha256_hash = hashlib.sha256()
response = None
with open(self.mf_file, 'a') as mf_handle: with open(self.mf_file, 'a') as mf_handle:
with open(temp_target_disk, 'wb') as handle: with open(temp_target_disk, 'wb') as handle:
@ -300,9 +301,9 @@ class VMwareExportVmOvf(PyVmomi):
ovf_descriptor_path = os.path.join(self.ovf_dir, ovf_descriptor_name + '.ovf') ovf_descriptor_path = os.path.join(self.ovf_dir, ovf_descriptor_name + '.ovf')
sha256_hash = hashlib.sha256() sha256_hash = hashlib.sha256()
with open(self.mf_file, 'a') as mf_handle: with open(self.mf_file, 'a') as mf_handle:
with open(ovf_descriptor_path, 'wb') as handle: with open(ovf_descriptor_path, 'w') as handle:
handle.write(vm_descriptor) handle.write(vm_descriptor)
sha256_hash.update(vm_descriptor) sha256_hash.update(to_bytes(vm_descriptor))
mf_handle.write('SHA256(' + os.path.basename(ovf_descriptor_path) + ')= ' + sha256_hash.hexdigest() + '\n') mf_handle.write('SHA256(' + os.path.basename(ovf_descriptor_path) + ')= ' + sha256_hash.hexdigest() + '\n')
http_nfc_lease.HttpNfcLeaseProgress(100) http_nfc_lease.HttpNfcLeaseProgress(100)
# self.facts = http_nfc_lease.HttpNfcLeaseGetManifest() # self.facts = http_nfc_lease.HttpNfcLeaseGetManifest()
@ -313,7 +314,7 @@ class VMwareExportVmOvf(PyVmomi):
kwargs = { kwargs = {
'changed': False, 'changed': False,
'failed': True, 'failed': True,
'msg': to_text(err), 'msg': "get exception: %s" % to_text(err),
} }
http_nfc_lease.HttpNfcLeaseAbort() http_nfc_lease.HttpNfcLeaseAbort()
lease_updater.stop() lease_updater.stop()
@ -348,9 +349,9 @@ def main():
if vm_power_state != 'poweredoff': if vm_power_state != 'poweredoff':
module.fail_json(msg='VM state should be poweredoff to export') module.fail_json(msg='VM state should be poweredoff to export')
results = pyv.export_to_ovf_files(vm_obj=vm) results = pyv.export_to_ovf_files(vm_obj=vm)
module.exit_json(**results)
else: else:
module.fail_json(msg='The specified virtual machine not found') module.fail_json(msg='The specified virtual machine not found')
module.exit_json(**results)
if __name__ == '__main__': if __name__ == '__main__':

@ -0,0 +1,3 @@
cloud/vcenter
shippable/vcenter/group1
needs/target/prepare_vmware_tests

@ -0,0 +1,69 @@
# Test code for the vmware_export_ovf module
# Copyright: (c) 2019, Diane Wang (Tomorrow9) <dianew@vmware.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- when: vcsim is not defined
block:
- name: create temporary build directory
tempfile:
state: directory
suffix: build
register: temp_dir
- debug: var=temp_dir
- import_role:
name: prepare_vmware_tests
vars:
setup_attach_host: true
setup_datastore: true
setup_virtualmachines: true
- name: set state to poweroff on the tested VM
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines_in_cluster[0].name }}"
state: poweredoff
- name: export VM to ovf template
vmware_export_ovf:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines_in_cluster[0].name }}"
datacenter: "{{ dc1 }}"
export_dir: "{{ temp_dir.path }}"
register: ovf_template
- debug: var=ovf_template
- name: assert the ovf template exported
assert:
that:
- "ovf_template.changed == true"
- "ovf_template.instance.device_files | length >= 1"
- name: export VM to ovf template with timeout set
vmware_export_ovf:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines_in_cluster[0].name }}"
datacenter: "{{ dc1 }}"
export_dir: "{{ temp_dir.path }}"
download_timeout: 30
register: ovf_template
- debug: var=ovf_template
- name: assert the ovf template exported
assert:
that:
- "ovf_template.changed == true"
- "ovf_template.instance.device_files | length >= 1"
rescue:
- name: Clean up the temporary dir
file:
path: "{{ temp_dir.path }}"
state: absent
when: temp_dir.path is defined
Loading…
Cancel
Save