HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/esxi.cpython-310.pyc
o

�N�g��@s�dZddlZddlZddlZddlmZddlZddlm	Z	m
Z
ddlmZm
Z
mZmZmZmZddlmZzddlZdZWneyIdZYnwe�e�Zz!dd	lmZd
ejvrmejdkrmejdkrme�d
�e��dZWney{dZYnwdd�Z dd�Z!e!d+dd��Z"e!dd��Z#e!				d,dd��Z$e!d-dd��Z%e!d.dd��Z&e!					d/dd��Z'e!				d0d d!��Z(ee�ee�e!d.d"d#����Z)ee�ee�e!	$		d1d%d&����Z*d'd(�Z+d)d*�Z,dS)2a
Manage VMware ESXi Hosts.

.. Warning::
    This module will be deprecated in a future release of Salt. VMware strongly
    recommends using the
    `VMware Salt extensions <https://docs.saltproject.io/salt/extensions/salt-ext-modules-vmware/en/latest/all.html>`_
    instead of the ESXi module. Because the Salt extensions are newer and
    actively supported by VMware, they are more compatible with current versions
    of ESXi and they work well with the latest features in the VMware product
    line.


Dependencies
============

- pyVmomi Python Module
- ESXCLI


pyVmomi
-------

PyVmomi can be installed via pip:

.. code-block:: bash

    pip install pyVmomi

.. note::

    Version 6.0 of pyVmomi has some problems with SSL error handling on certain
    versions of Python. If using version 6.0 of pyVmomi, Python 2.6,
    Python 2.7.9, or newer must be present. This is due to an upstream dependency
    in pyVmomi 6.0 that is not supported in Python versions 2.7 to 2.7.8. If the
    version of Python is not in the supported range, you will need to install an
    earlier version of pyVmomi. See `Issue #29537`_ for more information.

.. _Issue #29537: https://github.com/saltstack/salt/issues/29537

Based on the note above, to install an earlier version of pyVmomi than the
version currently listed in PyPi, run the following:

.. code-block:: bash

    pip install pyVmomi==5.5.0.2014.1.1

The 5.5.0.2014.1.1 is a known stable version that this original ESXi State
Module was developed against.

ESXCLI
------

Currently, about a third of the functions used in the vSphere Execution Module require
the ESXCLI package be installed on the machine running the Proxy Minion process.

The ESXCLI package is also referred to as the VMware vSphere CLI, or vCLI. VMware
provides vCLI package installation instructions for `vSphere 5.5`_ and
`vSphere 6.0`_.

.. _vSphere 5.5: http://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.vcli.getstart.doc/cli_install.4.2.html
.. _vSphere 6.0: http://pubs.vmware.com/vsphere-60/index.jsp#com.vmware.vcli.getstart.doc/cli_install.4.2.html

Once all of the required dependencies are in place and the vCLI package is
installed, you can check to see if you can connect to your ESXi host or vCenter
server by running the following command:

.. code-block:: bash

    esxcli -s <host-location> -u <username> -p <password> system syslog config get

If the connection was successful, ESXCLI was successfully installed on your system.
You should see output related to the ESXi host's syslog configuration.

.. note::

    Be aware that some functionality in this state module may depend on the
    type of license attached to the ESXi host.

    For example, certain services are only available to manipulate service state
    or policies with a VMware vSphere Enterprise or Enterprise Plus license, while
    others are available with a Standard license. The ``ntpd`` service is restricted
    to an Enterprise Plus license, while ``ssh`` is available via the Standard
    license.

    Please see the `vSphere Comparison`_ page for more information.

.. _vSphere Comparison: https://www.vmware.com/products/vsphere/compare

About
-----

This state module was written to be used in conjunction with Salt's
:mod:`ESXi Proxy Minion <salt.proxy.esxi>`. For a tutorial on how to use Salt's
ESXi Proxy Minion, please refer to the
:ref:`ESXi Proxy Minion Tutorial <tutorial-esxi-proxy>` for
configuration examples, dependency installation instructions, how to run remote
execution functions against ESXi hosts via a Salt Proxy Minion, and a larger state
example.
�N��wraps)�DiskGroupsDiskScsiAddressSchema�HostCacheSchema)�ArgumentValueError�CommandExecutionError�InvalidConfigError�VMwareApiError�VMwareObjectRetrievalError�VMwareSaltError)�dependsTF)�VmomiSupportz	vim25/6.0)��)rr�	zFpyVmomi not loaded: Incompatible versions of Python. See Issue #29537.cCsdtvrdSdS)N�esxi.cmdT)Fzesxi module could not be loaded)�__salt__�rr�D/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/esxi.py�__virtual__�srcst���fdd��}|S)z>
    Decorator wrapper to warn about azurearm deprecation
    cs6tjjjddtd��|itjjjdi|����}|S)NZArgonz�The 'esxi' functionality in Salt has been deprecated and its functionality will be removed in version 3008 in favor of the saltext.vmware Salt Extension. (https://github.com/saltstack/salt-ext-modules-vmware))�categoryr)�salt�utilsZversionsZ
warn_until�
FutureWarning�argsZclean_kwargs)r�kwargs�ret��functionrr�wrapped�s�z%_deprecation_message.<locals>.wrappedr)rrrrr�_deprecation_message�sr �vmk0�dcCsH|didd�}d}d}tdd}t|d��|�}	|	�d	�}
|
r*d
|
��|d<|S|	�d�}	|	�d
�}||kryd
||d�i}tdsqt|d|d��|�}
|
�d	�}
|
r^d
|
��|d<|S|sqd|d<||d<|d�|�|S|d�|�n|s�d|d<||d<|Sd}|	�d�}||kr�|d�d||d�i�d}|	�d�}||kr�|d�d||d�i�d}|	�d�}|t|�kr�|d�d|t|�d�i�d}td�s|du�rt|d|||d��|�}
|
�d�du�r|
�d�}|s�|
�d�}d
|��|d<|Sd|d<|dik�rd|d<|Std�r"d |d<d!|d<|S)"a�
    Ensures a host's core dump configuration.

    name
        Name of the state.

    enabled
        Sets whether or not ESXi core dump collection should be enabled.
        This is a boolean value set to ``True`` or ``False`` to enable
        or disable core dumps.

        Note that ESXi requires that the core dump must be enabled before
        any other parameters may be set. This also affects the ``changes``
        results in the state return dictionary. If ``enabled`` is ``False``,
        we can't obtain any previous settings to compare other state variables,
        resulting in many ``old`` references returning ``None``.

        Once ``enabled`` is ``True`` the ``changes`` dictionary comparisons
        will be more accurate. This is due to the way the system coredemp
        network configuration command returns data.

    dump_ip
        The IP address of host that will accept the dump.

    host_vnic
        Host VNic port through which to communicate. Defaults to ``vmk0``.

    dump_port
        TCP port to use for the dump. Defaults to ``6500``.

    Example:

    .. code-block:: yaml

        configure-host-coredump:
          esxi.coredump_configured:
            - enabled: True
            - dump_ip: 'my-coredump-ip.example.com'

    F���name�result�changes�commentrzXESXi requires that the core dump must be enabled before any other parameters may be set.�proxy�hostZget_coredump_network_config�Error�Error: r(zCoredump Config�enabled��old�new�testZcoredump_network_enable)r-Tr&r'�ip�dump_ip�	host_vnic�port�	dump_portZset_coredump_network_config)r3r4r6�success�stderr�stdoutz8Core Dump configuration is already in the desired state.Nz$Core dump configuration will change.)�
__pillar__r�get�__opts__�update�str)r%r-r3r4r6r�esxi_cmdZenabled_msgr*Zcurrent_config�errorZcurrent_enabledZenabled_changes�responser'Z
current_ipZcurrent_vnicZcurrent_port�msgrrr�coredump_configured�s�*�


��



���


rCc
Cs�|dddd�dd�}d}tdrd	|d
<d|d<|Szt|d
|d�W|StyD}zd|d
<d|��|d<|WYd	}~Sd	}~ww)a�
    Ensures the given password is set on the ESXi host. Passwords cannot be obtained from
    host, so if a password is set in this state, the ``vsphere.update_host_password``
    function will always run (except when using test=True functionality) and the state's
    changes dictionary will always be populated.

    The username for which the password will change is the same username that is used to
    authenticate against the ESXi host via the Proxy Minion. For example, if the pillar
    definition for the proxy username is defined as ``root``, then the username that the
    password will be updated for via this state is ``root``.

    name
        Name of the state.

    password
        The new password to change on the host.

    Example:

    .. code-block:: yaml

        configure-host-password:
          esxi.password_present:
            - password: 'new-bad-password'
    T�unknownz********r.zHost password was updated.r$rr1Nr&zHost password will change.r(Zupdate_host_password)Znew_passwordFr,)r<rr)r%�passwordrr?�errrrr�password_present9s&����rGcCs$|didd�}d}tdd}d}	t|d��|�}
t|d	|	d
��|�}|�d�}|r5d|��|d
<|S|�|	�}|rot|�t|
�krotdsct|d|d��|�}
|
�d�}|rcd|��|d
<|S|d�d|
|d�i�||kr�tds�|dur�t|d|	d
��|�}
|
�d�}|r�d|��|d
<|Snt|d|	d
��|�}
|
�d�}|r�d|��|d
<|S|d�d||d�i�|�rt|d|	d
��|�}|�d�}|r�d|��|d
<|S|�|	�}||k�rtd�st|d|	|d��|�}
|
�d�}|�rd|��|d
<|S|d�d||d�i�|�rBtd�s6t|d��|�}
|
�d�}|�r6d|��|d
<|S|d�dddd�i�|�rrtd�sft|d|	d
��|�}
|
�d�}|�rfd|��|d
<|S|d�ddd d�i�d|d!<|dik�r�d"|d
<|Std�r�d#|d!<d$|d
<|S)%ar
    Ensures a host's NTP server configuration such as setting NTP servers, ensuring the
    NTP daemon is running or stopped, or restarting the NTP daemon for the ESXi host.

    name
        Name of the state.

    service_running
        Ensures the running state of the ntp daemon for the host. Boolean value where
        ``True`` indicates that ntpd should be running and ``False`` indicates that it
        should be stopped.

    ntp_servers
        A list of servers that should be added to the ESXi host's NTP configuration.

    service_policy
        The policy to set for the NTP service.

        .. note::

            When setting the service policy to ``off`` or ``on``, you *must* quote the
            setting. If you don't, the yaml parser will set the string to a boolean,
            which will cause trouble checking for stateful changes and will error when
            trying to set the policy on the ESXi host.


    service_restart
        If set to ``True``, the ntp daemon will be restarted, regardless of its previous
        running state. Default is ``False``.

    update_datetime
        If set to ``True``, the date/time on the given host will be updated to UTC.
        Default setting is ``False``. This option should be used with caution since
        network delays and execution delays can result in time skews.

    Example:

    .. code-block:: yaml

        configure-host-ntp:
          esxi.ntp_configured:
            - service_running: True
            - ntp_servers:
              - 192.174.1.100
              - 192.174.1.200
            - service_policy: 'on'
            - service_restart: True

    Fr#r$rr)r*�ntpdZget_ntp_config�get_service_running��service_namer+r,r(r1Zset_ntp_config)�ntp_serversr'rLr.T�
service_start�service_stop�service_running�get_service_policy�set_service_policy�rK�service_policyrSZupdate_host_datetime�update_datetimezHost datetime was updated.�service_restartzNTP Daemon Restarted.r&z$NTP is already in the desired state.NzNTP state will change.)r:rr;�setr<r=)r%rOrLrSrUrTrr?r*rHZ
ntp_configZntp_runningr@rA�current_service_policyrrr�ntp_configuredks�:

��
�
��
���



��
���


�
�
�
rXc	Cs|didd�}d}tdd}t|d��|�}|�d�}||krjtd	s^|d
urEt|d|d��|�}|�d
�}|rDd|��|d<|Snt|d��|�}|�d
�}|r^d|��|d<|S|d�d||d�i�d
|d<|dikrzd|d<|Std	r�d|d<d|d<|S)a�
    Configures a host's VMotion properties such as enabling VMotion and setting
    the device VirtualNic that VMotion will use.

    name
        Name of the state.

    enabled
        Ensures whether or not VMotion should be enabled on a host as a boolean
        value where ``True`` indicates that VMotion should be enabled and ``False``
        indicates that VMotion should be disabled.

    device
        The device that uniquely identifies the VirtualNic that will be used for
        VMotion for the host. Defaults to ``vmk0``.

    Example:

    .. code-block:: yaml

        configure-vmotion:
          esxi.vmotion_configured:
            - enabled: True
            - device: sample-device

    Fr#r$rr)r*Zget_vmotion_enabledzVMotion Enabledr1TZvmotion_enable)�devicer+r,r(Zvmotion_disabler'r-r.r&z6VMotion configuration is already in the desired state.Nz"VMotion configuration will change.)r:rr;r<r=)	r%r-rYrr?r*Zcurrent_vmotion_enabledrAr@rrr�vmotion_configured"s<

�
�rZcCs�|didd�}d}tdd}t|d��|�}|�d�}|r(d	|��|d
<|S|�d�}||krxtdsl|d
urSt|d��|�}|�d�}|rRd	|��|d
<|Snt|d��|�}|�d�}|rld	|��|d
<|S|d�d||d�i�|r�t|d��|�}	|	�d�}|r�d	|��|d
<|S|	�d�}
|
r�t|
t�r�tds�t|d��|�}|�d�}|r�d	|��|d
<|S|d�dd|
d�i�d
|d<|dikr�d|d
<|Stdr�d|d<d|d
<|S)a�
    Configures a host's VSAN properties such as enabling or disabling VSAN, or
    adding VSAN-eligible disks to the VSAN system for the host.

    name
        Name of the state.

    enabled
        Ensures whether or not VSAN should be enabled on a host as a boolean
        value where ``True`` indicates that VSAN should be enabled and ``False``
        indicates that VSAN should be disabled.

    add_disks_to_vsan
        If set to ``True``, any VSAN-eligible disks for the given host will be added
        to the host's VSAN system. Default is ``False``.

    Example:

    .. code-block:: yaml

        configure-host-vsan:
          esxi.vsan_configured:
            - enabled: True
            - add_disks_to_vsan: True

    Fr#r$rr)r*Zget_vsan_enabledr+r,r(zVSAN Enabledr1TZvsan_enableZvsan_disabler'r-r.Zget_vsan_eligible_disksZEligibleZvsan_add_disks�add_disks_to_vsanr&z3VSAN configuration is already in the desired state.NzVSAN configuration will change.)r:rr;r<r=�
isinstance�list)r%r-r[rr?r*Zcurrent_vsan_enabledr@rAZcurrent_eligible_disksZdisksrrr�vsan_configuredgsb


�
�


r^cCs�|durd}|didd�}d}tdd}	d	}
t|d
|
d��|	�}|�d�}|r2d
|��|d<|S|�|
�}||kr�tdsz|dur_t|d|
d��|	�}
|
�d�}|r^d
|��|d<|Snt|d|
d��|	�}|�d�}|rzd
|��|d<|S|d�d||d�i�d\}}|s�|r�t|d|d�}|�d�}|r�d
|��|d<|S|�d�}|r�t|��d�}|s�d}tjj	�
|d��}|D]}|�d�r�q�||}q�Wd�n1s�wYt|��d�}|d|dks�|d|dkr�d}nd}|�r/td�st|d|||d�}|�d�}|�rd
|��|d<|S|d�d ||�r)|n|d�i�|�r�t|d!|
d��|	�}|�d�}|�rNd
|��|d<|S|�|
�}||k�r�td�szt|d"|
|d#��|	�}|�d�}|�rzd
|��|d<|S|d�d$||d�i�|�r�td�s�t|d%|
d��|	�}|�d�}|�r�d
|��|d<|S|d�d%dd&d�i�d|d'<|dik�r�d(|d<|Std�r�d|d'<d)|d<|S)*a�
    Manage the SSH configuration for a host including whether or not SSH is running or
    the presence of a given SSH key. Note: Only one ssh key can be uploaded for root.
    Uploading a second key will replace any existing key.

    name
        Name of the state.

    service_running
        Ensures whether or not the SSH service should be running on a host. Represented
        as a boolean value where ``True`` indicates that SSH should be running and
        ``False`` indicates that SSH should stopped.

        In order to update SSH keys, the SSH service must be running.

    ssh_key
        Public SSH key to added to the authorized_keys file on the ESXi host. You can
        use ``ssh_key`` or ``ssh_key_file``, but not both.

    ssh_key_file
        File containing the public SSH key to be added to the authorized_keys file on
        the ESXi host. You can use ``ssh_key_file`` or ``ssh_key``, but not both.

    service_policy
        The policy to set for the NTP service.

        .. note::

            When setting the service policy to ``off`` or ``on``, you *must* quote the
            setting. If you don't, the yaml parser will set the string to a boolean,
            which will cause trouble checking for stateful changes and will error when
            trying to set the policy on the ESXi host.

    service_restart
        If set to ``True``, the SSH service will be restarted, regardless of its
        previous running state. Default is ``False``.

    certificate_verify
        If set to ``True``, the SSL connection must present a valid certificate.
        Default is ``True``.

    Example:

    .. code-block:: yaml

        configure-host-ssh:
          esxi.ssh_configured:
            - service_running: True
            - ssh_key_file: /etc/salt/ssh_keys/my_key.pub
            - service_policy: 'on'
            - service_restart: True
            - certificate_verify: True

    NTFr#r$rr)r*�sshrIrJr+r,r(r1rMrNr'rOr.)NFZget_ssh_key)�certificate_verify�key� �r�#r�Zupload_ssh_key)�ssh_key�ssh_key_filer`zSSH KeyrPrQrRrSrUzSSH service restarted.r&z,SSH service is already in the desired state.zSSH service state will change.)r:rr;r<r=�
_strip_key�splitrr�filesZfopen�
startswith)r%rOrfrgrSrUr`rr?r*r_Zssh_runningr@�enable�disableZcurrent_ssh_keyZssh_key_changedZclean_current_key�key_file�lineZ
clean_ssh_keyrArWrrr�ssh_configured�s�@


�
��



���
�
���
��



��
���


�
rpc	Cs�|didd�}d}tdd}|r[|sd}tdsOt|d	|d
��|�}	|	��D]&\}
}t|t�r2q(|�d�sN|�d�}|sCd
�|�}d|��|d<|Sq(|d�d	d|d�i�t|d��|�}
|
�d�}|rtd|��|d<|S|
�d��d�}
|
|kr�tds�t|d|dd��|�}|�d�dkr�|�d�}|�d�}d|r�|n|��|d<|S|d�d|
|d�i�t|d��|�}|��D]�\}
}zt	|
�}Wnt
y�d|
�d�|d<|YSw||}t|�t|�k�rItd�s*t|d |
|||d!��|�}|�|
��d�}|�s*|�|
��d�}|�s"d"�|
�}||d<|S|d�d#��s;|d�d#ii�|dd#�|
||d�i�q�d$|d%<|dik�r[d&|d<|Std�rhd'|d%<d(|d<|S))a|
    Ensures the specified syslog configuration parameters. By default,
    this state will reset the syslog service after any new or changed
    parameters are set successfully.

    name
        Name of the state.

    syslog_configs
        Name of parameter to set (corresponds to the command line switch for
        esxcli without the double dashes (--))

        Valid syslog_config values are ``logdir``, ``loghost``, ``logdir-unique``,
        ``default-rotate``, ``default-size``, and ``default-timeout``.

        Each syslog_config option also needs a configuration value to set.
        For example, ``loghost`` requires URLs or IP addresses to use for
        logging. Multiple log servers can be specified by listing them,
        comma-separated, but without spaces before or after commas

        (reference: https://blogs.vmware.com/vsphere/2012/04/configuring-multiple-syslog-servers-for-esxi-5.html)

    firewall
        Enable the firewall rule set for syslog. Defaults to ``True``.

    reset_service
        After a successful parameter set, reset the service. Defaults to ``True``.

    reset_syslog_config
        Resets the syslog service to its default settings. Defaults to ``False``.
        If set to ``True``, default settings defined by the list of syslog configs
        in ``reset_configs`` will be reset before running any other syslog settings.

    reset_configs
        A comma-delimited list of parameters to reset. Only runs if
        ``reset_syslog_config`` is set to ``True``. If ``reset_syslog_config`` is set
        to ``True``, but no syslog configs are listed in ``reset_configs``, then
        ``reset_configs`` will be set to ``all`` by default.

        See ``syslog_configs`` parameter above for a list of valid options.

    Example:

    .. code-block:: yaml

        configure-host-syslog:
          esxi.syslog_configured:
            - syslog_configs:
                loghost: ssl://localhost:5432,tcp://10.1.0.1:1514
                default-timeout: 120
            - firewall: True
            - reset_service: True
            - reset_syslog_config: True
            - reset_configs: loghost,default-timeout
    Fr#r$rr)r*�allr1�reset_syslog_config)�
syslog_configr7�messagezJThere was an error resetting a syslog config '{}'.Please check debug logs.r,r(r'r.Zget_firewall_statusr+ZrulesetsZsyslogZenable_firewall_ruleset)Zruleset_enableZruleset_name�retcoderr8r9�firewallZget_syslog_config�'z!' is not a valid config variable.Zset_syslog_config)rsZconfig_valuerv�
reset_servicezGThere was an error setting syslog config '{}'. Please check debug logs.rsTr&z'Syslog is already in the desired state.NzSyslog state will change.)r:r<rr;�itemsr\�bool�formatr=�_lookup_syslog_config�KeyErrorr>)r%Zsyslog_configsrvrxrrZ
reset_configsrr?r*�resetra�valrBZcurrent_firewallr@r-rF�outZcurrent_syslog_configZ
lookup_keyZcurrent_valrAr7rrr�syslog_configured�s�@��


��
�
��

�
�����
r�c#s�td�}|�d�s|dn|d}t�d||�|didd�}d}d}g}i}	d}
zZt�d	�t��}zt�||d
�|�Wntj	j
yQ}zt|��d}~wwtd�}
td|
d
���shtd|�d���dd��D�}
t�d|
�td|
d
�}dd�|D�}Wn2t
y�}z&t�d|�|
r�td|
�|�tds�dndt|�d��|WYd}~Sd}~wwt|�D�]d\}}|d|
vr�|�d�|d��t�|d�d}q�|
|dd}d�|d|�}g}g}g}|dD]#}||
v�r|�|�q�|�|
|d�|�|�d |d�d!��q�|�r6|�d"�|d#�d$d%�|D����t�|d�d}q�|�|��s�t�d&|�|�r�td�r^|�d'�|||d#�d(d%�|D����n*|g|D]�td)�|
d*��qc|�d+�||d#�d,d%�|D����t�|d�td�r�|�d-�|||d#�d.d%�|D����t�|d�d}q�ztd/||d|
d0�Wn(t�y�}z|�d1|�d2|�d3��t�|d�d}WYd}~q�d}~ww|�d4|�d5��t�|d�d6||d7�i|	t|�<d}q�t�d8||�|�|�}�fd9d%�|d:D�}g}g}g}g}|D]%��|d:v�rB�fd;d%��D�d<}|���|�|�d ��d!���q|d:D]#��|v�rj�fd=d%��D�d<}|���|�|�d ��d!���qHt�d>||||�|�r�|�d?�d#�d@d%�|D��|��t�|d�d}q�|�rd#�dAd%�|D��} td�r�|�dB�|| |��t�|d�d}q�ztdC||d|
d0�Wn&t�y�}z|�dD�| ||��t�|d�d}WYd}~q�d}~wwdE| �dF|��}!t�|!�|�|!�||d7�||d7�dG�|	t|�<d}q�dH|�dI�} t�| �|�| �q�td|
�|�s/|�s/dn
td�r6dn|�r;dnd}"|�|"dJ�|�|	dK��|S)La
    Configures the disk groups to use for vsan.

    This function will do the following:

    1. Check whether or not all disks in the diskgroup spec exist, and raises
       and errors if they do not.

    2. Create diskgroups with the correct disk configurations if diskgroup
       (identified by the cache disk canonical name) doesn't exist

    3. Adds extra capacity disks to the existing diskgroup

    Example:

    .. code:: python

        {
            'cache_scsi_addr': 'vmhba1:C0:T0:L0',
            'capacity_scsi_addrs': [
                'vmhba2:C0:T0:L0',
                'vmhba3:C0:T0:L0',
                'vmhba4:C0:T0:L0',
            ]
        }

    name
        Mandatory state name

    diskgroups
        Disk group representation containing scsi disk addresses.
        Scsi addresses are expected for disks in the diskgroup:

    erase_disks
        Specifies whether to erase all partitions on all disks member of the
        disk group before the disk group is created. Default value is False.
    �esxi.get_details�vcenterr*�	esxi_hostzRunning state %s for host '%s'N)r%r&r'�commentsFz&Validating diskgroups_configured input)�
diskgroups�erase_disks�&vsphere.get_service_instance_via_proxy�vsphere.list_disks��service_instancezNo disks retrieved from host 'rwcS�i|]}|d|�qS)�scsi_addressr��.0�drrr�
<dictcomp>�sz)diskgroups_configured.<locals>.<dictcomp>zscsi_addr_to_disk_map = %szvsphere.list_diskgroupscSr�)Z
cache_diskr)r��dgrrrr��s
�z	Error: %s�vsphere.disconnectr1�r&r(Zcache_scsi_addrz/No cache disk with scsi address '{}' was found.���T�id�
{} (id:{})Zcapacity_scsi_addrsz (id:�)zMError in diskgroup #{}: capacity disks with scsi addresses {} were not found.�, cS�g|]}d|�d��qS�rwr�r��arrr�
<listcomp>��z)diskgroups_configured.<locals>.<listcomp>zerase_disks = %szXState {} will erase all disks of disk group #{}; cache disk: '{}', capacity disk(s): {}.cSr�r�rr�rrrr��r��vsphere.erase_disk_partitions��disk_idr�zEErased disks of diskgroup #{}; cache disk: '{}', capacity disk(s): {}cSr�r�rr�rrrr��r�zPState {} will create the disk group #{}; cache disk: '{}', capacity disk(s): {}.cSr�r�rr�rrrr��r�zvsphere.create_diskgroup)Z
safety_checksr�zError creating disk group #z: �.zCreated disk group #'�'.r0)�cache�capacityz3Disk group #%s exists. Checking capacity disks: %s.cs*g|]�d��fdd��D�d���qS)r�c� g|]}|d�kr|d�qS�r�r�rr��r�rrr�s z4diskgroups_configured.<locals>.<listcomp>.<listcomp>r)r{)r�)�
host_disksr�rr�
s���Zcapacity_diskscr�r�rr�r�rrr��rcr�r�rr�r�rrr�r�zjDisk group #%s: existing capacity disk ids: %s; added capacity disk ids: %s; removed capacity disk ids: %szSError removing capacity disk(s) {} from disk group #{}; operation is not supported.cSr�r�r�r�r�rrrr�6r�cSr�r�rr�rrrr�Cr�z8State {} will add capacity disk(s) {} to disk group #{}.z!vsphere.add_capacity_to_diskgroupz7Error adding capacity disk(s) {} to disk group #{}: {}.zAdded capacity disk(s) z to disk group #)r0r/zDisk group #z- is correctly configured. Nothing to be done.�
)r&r(r')rr;�log�info�tracer�	serialize�
jsonschema�validate�
exceptions�ValidationErrorrr
rr@r=r<r>�	enumerate�appendr{�joinr�debug)#r%r�r��
proxy_details�hostnamer�errorsr'r�Zdiskgroup_changes�si�schema�excZscsi_addr_to_disk_mapZexisting_diskgroupsZ$cache_disk_to_existing_diskgroup_maprF�idxr�Z
cache_disk_idZcache_disk_displayZbad_scsi_addrsZcapacity_disk_idsZcapacity_disk_displaysZ	scsi_addrZexisting_diskgroupZexisting_capacity_disk_displaysZadded_capacity_disk_idsZadded_capacity_disk_displaysZremoved_capacity_disk_idsZremoved_capacity_disk_displaysZdisk_scsi_addr�sZcomr&r)r�r�r�diskgroups_configuredCs�
)
��

���

�
����
��

��
��
���	
��

���
��

�
��
�

��
���
��
��
�����


���

����	�r��100%c
Cst�d|�t�d|�t�d|�t�d|�td�}|�d�s&|dn|d}t�d	|�t�d
|�|ddid
�}tdrCdnd}	d}
g}i}d}
�z�t�d�t��}z
t	�
||||d�|�Wnt	jjyw}zt
|��d}~wwt�d|�}t|�d��}|�d�}t�d||�td�}
td|
d�}|d|kr�|�d|d|d�i�d}
d}|�d�r�td|dg|
d�}td |d!g|
d"�}|s�td#�|d!|���|d$}d%�|d&|d'�}t�d(|�d}|�s�|�r'td�r
|�d)�|||��t�|d*�ntd+|d'|
d,�|�d-�||��t�|d*�td�rA|�d.�||d||��t�|d*�nh|�ritd/|d'|
d,�}t�d0|�d1d2�|D�}t|�d$k�ritd3�|���td4|d|d$d'|d5|
d�|�d6�|d||��t�|d*�|�dd7|d|d8�ii�td|dg|
d�d$}d}
nR|d$�d9��s�td:�|d���|d'|d$d9v�r�td;�|d|d'd<�d=d2�|d$d9D�����|�d>�|d||��|d$}t�d?|�t�|d*�|�rc|d@k�r|dA|dBdCdC}n|dC}t�dD|�t|dC�dC}t�dE|�d$}|�dF��r<t�dG|�dF��nd}|�rHt|�d��}||k�scd}
|�dF|dC�dH�|dC�dH�d�i�|
�r�td�r||�dI�||||d|dJ���n8|dBdK|k�r�tdL�|d|dBdK|���tdM||d||
dN�|�dO|�dP��n
|�dQ�|��d}	tdR|
�t�|d*�|�dS�|�|	|dT��|WSt�y}z)t�dU|�|
�r�tdR|
�|�td�s�dnd|�dV�dW��|WYd}~Sd}~ww)XaV
    Configures the host cache used for swapping.

    It will do the following:

    1. Checks if backing disk exists

    2. Creates the VMFS datastore if doesn't exist (datastore partition will be
       created and use the entire disk)

    3. Raises an error if ``dedicated_backing_disk`` is ``True`` and partitions
       already exist on the backing disk

    4. Configures host_cache to use a portion of the datastore for caching
       (either a specific size or a percentage of the datastore)

    Examples

    Percentage swap size (can't be 100%)

    .. code:: python

        {
            'enabled': true,
            'datastore': {
                'backing_disk_scsi_addr': 'vmhba0:C0:T0:L0',
                'vmfs_version': 5,
                'name': 'hostcache'
                }
            'dedicated_backing_disk': false
            'swap_size': '98%',
        }

    Fixed sized swap size

    .. code:: python

        {
            'enabled': true,
            'datastore': {
                'backing_disk_scsi_addr': 'vmhba0:C0:T0:L0',
                'vmfs_version': 5,
                'name': 'hostcache'
                }
            'dedicated_backing_disk': true
            'swap_size': '10GiB',
        }

    name
        Mandatory state name.

    enabled
        Specifies whether the host cache is enabled.

    datastore
        Specifies the host cache datastore.

    swap_size
        Specifies the size of the host cache swap. Can be a percentage or a
        value in GiB. Default value is ``100%``.

    dedicated_backing_disk
        Specifies whether the backing disk is dedicated to the host cache which
        means it must have no other partitions. Default is False

    erase_backing_disk
        Specifies whether to erase all partitions on the backing disk before
        the datastore is created. Default value is False.
    zenabled = %szdatastore = %szswap_size = %szerase_backing_disk = %sr�r�r*r�z
hostname = %sz0Running host_cache_swap_configured for host '%s'zDefault commentsN)r%r(r&r'r1TFz&Validating host_cache_configured input)r-�	datastore�	swap_size�erase_backing_diskz(\d+)(%|GiB)rerz$swap_size_value = %s; swap_type = %sr�zvsphere.get_host_cacher�r-r.r�z!vsphere.list_datastores_via_proxyr%)Zdatastore_namesr�r�Zbacking_disk_scsi_addr)Zscsi_addressesr�z6Disk with scsi address '{}' was not found in host '{}'rr�r�r�zbacking_disk = %sz7State {} will erase the backing disk '{}' on host '{}'.r�r�r�z&Erased backing disk '{}' on host '{}'.zNState {} will create the datastore '{}', with backing disk '{}', on host '{}'.zvsphere.list_disk_partitionszpartitions = %scSsg|]
}|ddkr|�qS)r{Zmbrr)r��prrrr�Dsz)host_cache_configured.<locals>.<listcomp>z+Backing disk '{}' has unexpected partitionszvsphere.create_vmfs_datastoreZvmfs_versionz?Created vmfs datastore '{}', backed by disk '{}', on host '{}'.r0)r%�backing_diskZbacking_disk_idsz*Datastore '{}' doesn't have a backing diskzGDatastore '{}' is not backed by the correct disk: expected '{}'; got {}r�cSr�r�r)r�Zdiskrrrr�vs
��zZDatastore '{}' already exists on host '{}' and is backed by disk '{}'. Nothing to be done.zexisting_datastore = %s�%gY@r�izraw_size = %sMiBzadjusted swap_size = %sMiBr�z(\d+)MiBZGiBz;State {} will configure the host cache on host '{}' to: {}.)r-Zdatastore_namer�g0Az^Capacity of host cache datastore '{}' ({} MiB) is smaller than the required swap size ({} MiB)zvsphere.configure_host_cache)�
swap_size_MiBr�zHost cache configured on host 'r�zLHost cache on host '{}' is already correctly configured. Nothing to be done.r�r�)r(r&r'z
Error: %s.r�r�)r�r�rr;r�r<r�rr�r�r�r�r�r�re�match�int�groupr=r
r{r��lenr	rr�rrr@)r%r-r�r�Zdedicated_backing_diskr�r�r�rr&Z
needs_settingr�r'r�r�r��mZswap_size_valueZ	swap_typeZ
host_cacheZexisting_datastoresZexisting_disksr�Zbacking_disk_displayZexisting_datastoreZ
partitionsZnon_mbr_partitionsZraw_size_MiBr�Zexisting_swap_size_MiBrFrrr�host_cache_configured�s�P

���
��	��



�
����
�����
�����
�
������

����
�����
���
���	
���
���������r�cCsddddddd�}|�|�S)zl
    Helper function that looks up syslog_config keys available from
    ``vsphere.get_syslog_config``.
    zDefault Network Retry TimeoutzLocal Log Outputz#Local Logging Default Rotation SizezLog To Unique SubdirectoryzLocal Logging Default RotationszRemote Host)zdefault-timeoutZlogdirzdefault-sizez
logdir-uniquezdefault-rotateZloghost)r;)Zconfig�lookuprrrr|�s�
	r|cCs$|��|�dd�|�dd�|S)z�
    Strips an SSH key string of white space and line endings and returns the new string.

    key_string
        The string to be stripped.
    r�r#z
)�strip�replace)Z
key_stringrrrrh�srh)r!r")NNFF)r!)F)NNNFN)TTFN)r�FF)-�__doc__�loggingr��sys�	functoolsrZsalt.utils.filesrZsalt.config.schemas.esxirrZsalt.exceptionsrrrr	r
rZsalt.utils.decoratorsrr�ZHAS_JSONSCHEMA�ImportError�	getLogger�__name__r�ZpyVmomir
Z
versionMap�version_infor�ZHAS_PYVMOMIrr rCrGrXrZr^rpr�r�r�r|rhrrrr�<module>s�e �



��	
1�7D\�Y�&@�`