def set_vnc_extraconfig(content, vm, enabled, ip, port, password):
    result = dict(
        changed=False,
        failed=False,
    )
    # set new values
    key_prefix = "remotedisplay.vnc."
    new_values = dict()
    for key in ['enabled', 'ip', 'port', 'password']:
        new_values[key_prefix + key] = ""
    if enabled:
        new_values[key_prefix + "enabled"] = "true"
        new_values[key_prefix + "password"] = str(password).strip()
        new_values[key_prefix + "ip"] = str(ip).strip()
        new_values[key_prefix + "port"] = str(port).strip()

    # get current vnc config
    current_values = get_vnc_extraconfig(vm)
    # check if any value is changed
    reconfig_vm = False
    for key, val in new_values.items():
        key = key.replace(key_prefix, "")
        current_value = current_values.get(key, "")
        # enabled is not case-sensitive
        if key == "enabled":
            current_value = current_value.lower()
            val = val.lower()
        if current_value != val:
            reconfig_vm = True
    if not reconfig_vm:
        return result
    # reconfigure vm
    spec = vim.vm.ConfigSpec()
    spec.extraConfig = []
    for key, val in new_values.items():
        opt = vim.option.OptionValue()
        opt.key = key
        opt.value = val
        spec.extraConfig.append(opt)
    task = vm.ReconfigVM_Task(spec)
    try:
        wait_for_task(task)
    except TaskError as task_err:
        result['failed'] = True
        result['msg'] = to_native(task_err)

    if task.info.state == 'error':
        result['failed'] = True
        result['msg'] = task.info.error.msg
    else:
        result['changed'] = True
        result['instance'] = gather_vm_facts(content, vm)
    return result
 def get_new_vm_info(self, vm):
     # to check if vm has been cloned in the destination vc
     # query for the vm in destination vc
     # get the host and datastore info
     # get the power status of the newly cloned vm
     info = {}
     vm_obj = find_vm_by_name(content=self.destination_content, vm_name=vm)
     if vm_obj is None:
         self.module.fail_json(msg="Newly cloned VM is not found in the destination VCenter")
     else:
         vm_facts = gather_vm_facts(self.destination_content, vm_obj)
         info['vm_name'] = vm
         info['vcenter'] = self.destination_vcenter
         info['host'] = vm_facts['hw_esxi_host']
         info['datastore'] = vm_facts['hw_datastores']
         info['vm_folder'] = vm_facts['hw_folder']
         info['power_on'] = vm_facts['hw_power_status']
     return info
Exemplo n.º 3
0
    def deploy(self):
        facts = {}

        if self.params['inject_ovf_env']:
            self.inject_ovf_env()

        if self.params['power_on']:
            task = self.entity.PowerOn()
            if self.params['wait']:
                wait_for_task(task)
                if self.params['wait_for_ip_address']:
                    _facts = wait_for_vm_ip(self.content, self.entity)
                    if not _facts:
                        self.module.fail_json(
                            msg='Waiting for IP address timed out')
                    facts.update(_facts)

        if not facts:
            facts.update(gather_vm_facts(self.content, self.entity))

        return facts
Exemplo n.º 4
0
 def gather_facts(self, vm):
     return gather_vm_facts(self.content, vm)
Exemplo n.º 5
0
    def get_lease(self):
        datastore, datacenter, resource_pool, network_mappings = (
            self.get_objects()
        )

        params = {"diskProvisioning": self.params["disk_provisioning"]}
        if self.params["name"]:
            params["entityName"] = self.params["name"]
        if network_mappings:
            params["networkMapping"] = network_mappings
        if self.params["deployment_option"]:
            params["deploymentOption"] = self.params["deployment_option"]
        if self.params["properties"]:
            params["propertyMapping"] = []
            for key, value in self.params["properties"].items():
                property_mapping = vim.KeyValue()
                property_mapping.key = key
                property_mapping.value = (
                    str(value) if isinstance(value, bool) else value
                )
                params["propertyMapping"].append(property_mapping)

        if self.params["folder"]:
            folder = self.content.searchIndex.FindByInventoryPath(
                self.params["folder"]
            )
            if not folder:
                self.module.fail_json(
                    msg="Unable to find the specified folder %(folder)s"
                    % self.params
                )
        else:
            folder = datacenter.vmFolder

        spec_params = vim.OvfManager.CreateImportSpecParams(**params)

        ovf_descriptor = self.get_ovf_descriptor()

        self.import_spec = self.content.ovfManager.CreateImportSpec(
            ovf_descriptor, resource_pool, datastore, spec_params
        )

        errors = [
            to_native(e.msg) for e in getattr(self.import_spec, "error", [])
        ]
        if self.params["fail_on_spec_warnings"]:
            errors.extend(
                (
                    to_native(w.msg)
                    for w in getattr(self.import_spec, "warning", [])
                )
            )
        if errors:
            self.module.fail_json(
                msg="Failure validating OVF import spec: %s"
                % ". ".join(errors)
            )

        for warning in getattr(self.import_spec, "warning", []):
            self.module.warn(
                "Problem validating OVF import spec: %s"
                % to_native(warning.msg)
            )

        if not self.params["allow_duplicates"]:
            name = self.import_spec.importSpec.configSpec.name
            match = find_vm_by_name(self.content, name, folder=folder)
            if match:
                self.module.exit_json(
                    instance=gather_vm_facts(self.content, match),
                    changed=False,
                )

        if self.module.check_mode:
            self.module.exit_json(changed=True, instance={"hw_name": name})

        try:
            self.lease = resource_pool.ImportVApp(
                self.import_spec.importSpec, folder
            )
        except vmodl.fault.SystemError as e:
            self.module.fail_json(
                msg="Failed to start import: %s" % to_native(e.msg)
            )

        while self.lease.state != vim.HttpNfcLease.State.ready:
            time.sleep(0.1)

        self.entity = self.lease.info.entity

        return self.lease, self.import_spec