
    VhS                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlmZmZmZ 	 d dlZdZdZ G d	 d
e      Zd Zedk(  r e        yy# e$ r dZ ej(                         ZY 6w xY w)    )absolute_importdivisionprint_functionaW  
author:
  - Masayoshi Mizuma (@mizumm)
module: pmem
short_description: Configure Intel Optane Persistent Memory modules
version_added: 4.5.0
description:
  - This module allows Configuring Intel Optane Persistent Memory modules (PMem) using C(ipmctl) and C(ndctl) command line
    tools.
requirements:
  - C(ipmctl) and C(ndctl) command line tools
  - xmltodict
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  appdirect:
    description:
      - Percentage of the total capacity to use in AppDirect Mode (V(0)-V(100)).
      - Create AppDirect capacity utilizing hardware interleaving across the requested PMem modules if applicable given the
        specified target.
      - Total of O(appdirect), O(memorymode) and O(reserved) must be V(100).
    type: int
  appdirect_interleaved:
    description:
      - Create AppDirect capacity that is interleaved any other PMem modules.
    type: bool
    required: false
    default: true
  memorymode:
    description:
      - Percentage of the total capacity to use in Memory Mode (V(0)-V(100)).
    type: int
  reserved:
    description:
      - Percentage of the capacity to reserve (V(0)-V(100)). O(reserved) will not be mapped into the system physical address
        space and will be presented as reserved capacity with Show Device and Show Memory Resources Commands.
      - O(reserved) will be set automatically if this is not configured.
    type: int
    required: false
  socket:
    description:
      - This enables to set the configuration for each socket by using the socket ID.
      - Total of O(appdirect), O(memorymode) and O(reserved) must be V(100) within one socket.
    type: list
    elements: dict
    suboptions:
      id:
        description: The socket ID of the PMem module.
        type: int
        required: true
      appdirect:
        description:
          - Percentage of the total capacity to use in AppDirect Mode (V(0)-V(100)) within the socket ID.
        type: int
        required: true
      appdirect_interleaved:
        description:
          - Create AppDirect capacity that is interleaved any other PMem modules within the socket ID.
        type: bool
        required: false
        default: true
      memorymode:
        description:
          - Percentage of the total capacity to use in Memory Mode (V(0)-V(100)) within the socket ID.
        type: int
        required: true
      reserved:
        description:
          - Percentage of the capacity to reserve (V(0)-V(100)) within the socket ID.
        type: int
  namespace:
    description:
      - This enables to set the configuration for the namespace of the PMem.
    type: list
    elements: dict
    suboptions:
      mode:
        description:
          - The mode of namespace. The detail of the mode is in the man page of ndctl-create-namespace.
        type: str
        required: true
        choices: ['raw', 'sector', 'fsdax', 'devdax']
      type:
        description:
          - The type of namespace. The detail of the type is in the man page of ndctl-create-namespace.
        type: str
        required: false
        choices: ['pmem', 'blk']
      size:
        description:
          - The size of namespace. This option supports the suffixes V(k) or V(K) or V(KB) for KiB, V(m) or V(M) or V(MB)
            for MiB, V(g) or V(G) or V(GB) for GiB and V(t) or V(T) or V(TB) for TiB.
          - This option is required if multiple namespaces are configured.
          - If this option is not set, all of the available space of a region is configured.
        type: str
        required: false
  namespace_append:
    description:
      - Enable to append the new namespaces to the system.
      - The default is V(false) so the all existing namespaces not listed in O(namespace) are removed.
    type: bool
    default: false
    required: false
a  
reboot_required:
  description: Indicates that the system reboot is required to complete the PMem configuration.
  returned: success
  type: bool
  sample: true
result:
  description:
    - Shows the value of AppDirect, Memory Mode and Reserved size in bytes.
    - If O(socket) argument is provided, shows the values in each socket with C(socket) which contains the socket ID.
    - If O(namespace) argument is provided, shows the detail of each namespace.
  returned: success
  type: list
  elements: dict
  contains:
    appdirect:
      description: AppDirect size in bytes.
      type: int
    memorymode:
      description: Memory Mode size in bytes.
      type: int
    reserved:
      description: Reserved size in bytes.
      type: int
    socket:
      description: The socket ID to be configured.
      type: int
    namespace:
      description: The list of the detail of namespace.
      type: list
  sample: [
    {
      "appdirect": 111669149696,
      "memorymode": 970662608896,
      "reserved": 3626500096,
      "socket": 0
    },
    {
      "appdirect": 111669149696,
      "memorymode": 970662608896,
      "reserved": 3626500096,
      "socket": 1
    }
  ]
a  
- name: Configure the Pmem as AppDirect 10, Memory Mode 70, and the Reserved 20 percent.
  community.general.pmem:
    appdirect: 10
    memorymode: 70

- name: Configure the Pmem as AppDirect 10, Memory Mode 80, and the Reserved 10 percent.
  community.general.pmem:
    appdirect: 10
    memorymode: 80
    reserved: 10

- name: Configure the Pmem as AppDirect with not interleaved 10, Memory Mode 70, and the Reserved 20 percent.
  community.general.pmem:
    appdirect: 10
    appdirect_interleaved: false
    memorymode: 70

- name: Configure the Pmem each socket.
  community.general.pmem:
    socket:
      - id: 0
        appdirect: 10
        appdirect_interleaved: false
        memorymode: 70
        reserved: 20
      - id: 1
        appdirect: 10
        memorymode: 80
        reserved: 10

- name: Configure the two namespaces.
  community.general.pmem:
    namespace:
      - size: 1GB
        type: pmem
        mode: raw
      - size: 320MB
        type: pmem
        mode: sector
N)AnsibleModulemissing_required_libhuman_to_bytesTFc                   p    e Zd Zd ZddZddZddZd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZddZd Zy)PersistentMemoryc                    t        t        t        d      t        dd      t        d      t        d      t        ddt        t        dd      t        dd      t        dd      t        dd      t        d      	      
      t        ddt        t        ddg d      t        dddg      t        d            
      t        dd            ddgfg dfddgddgddgddgddgddgddgddgf      }t        s |j                  t	        d      t
               |j                  dd      | _        |j                  dd      | _        |j                  d   | _
        |j                  d   | _        |j                  d   | _        |j                  d    | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        || _        d| _        g | _        y )!Nint)typeboolT)r   defaultlistdict)requiredr   )id	appdirectappdirect_interleaved
memorymodereserved)r   elementsoptionsstr)rawsectorfsdaxdevdax)r   r   choicespmemblk)r   r   )moder   sizeF)r   r   r   r   socket	namespacenamespace_appendr   r   )r   r   r$   r%   r$   r%   r&   )argument_specrequired_togetherrequired_one_ofmutually_exclusive	xmltodict)msg	exceptionipmctlndctlr   r   )r   r   HAS_XMLTODICT_LIBRARY	fail_jsonr   XMLTODICT_LIBRARY_IMPORT_ERRORget_bin_pathipmctl_exec
ndctl_execparamsr   interleavedmemmoder   r$   r%   r&   modulechangedresult)selfr9   s     j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pmem.py__init__zPersistentMemory.__init__   s   E*&*&EU+5)& E:"&5"A.2.M#'E#B!%5!1	 & !4eEij!uvuoF!u- "&65!A/4 l+ C h'x(k*{+;'0112-.	 A*
X %(58  : "..x> --gt<{3!==)@A}}\2j1mmH-{3 &.@ A    c                 L   |D cg c]  }t        |       }}| j                  j                  d|z         | j                  j                  |      \  }}}| j                  j                  d|z         |r'|dk7  r"| j                  j	                  d|z  |||       |S c c}w )Nzpmem_run_command: execute: %sr,   zpmem_run_command: result: %sr   zError while running: %s)r,   rcouterr)r   r9   logrun_commandr1   )r<   commandreturnCheckpartcmdrB   rC   rD   s           r=   pmem_run_commandz!PersistentMemory.pmem_run_command!  s    %,-Ts4y--;cAB{{..s3C:S@A27KK!!&?"%'&*,#3 " @ 
 .s   B!c                 F    | j                   g|z   }| j                  ||      S N)r4   rK   r<   rG   rH   s      r=   pmem_run_ipmctlz PersistentMemory.pmem_run_ipmctl1  s(    ##$w.$$Wk::r?   c                 F    | j                   g|z   }| j                  ||      S rM   )r5   rK   rN   s      r=   pmem_run_ndctlzPersistentMemory.pmem_run_ndctl7  s&    ??#g-$$Wk::r?   c                 ,    g d}| j                  |      S )N)showz-systemz-capabilitiesrO   r<   rG   s     r=   pmem_is_dcpmm_installedz(PersistentMemory.pmem_is_dcpmm_installed=  s    6##G,,r?   c                 P    g }|D ]  }|d   |vs|j                  |d            |S )Nalignappend)r<   regionalignsrgs       r=   pmem_get_region_align_sizez+PersistentMemory.pmem_get_region_align_sizeB  s9     	+B'{&(bk*	+ r?   c                 @    g }|D ]  }|j                  |d           |S )Navailable_sizerY   )r<   r[   r`   r]   s       r=   pmem_get_available_region_sizez/PersistentMemory.pmem_get_available_region_sizeJ  s2     	8B!!"%5"67	8 r?   c                 P    g }|D ]  }|d   |vs|j                  |d            |S )Nr   rY   )r<   r[   typesr]   s       r=   pmem_get_available_region_typez/PersistentMemory.pmem_get_available_region_typeQ  s9     	)B&z&RZ(	) r?   c                    d }dd}d }| j                   r ||       S | j                  ) || | j                  | j                  | j                        S  ||       }||S | j                  D ]  } || |d   |d   |d         }||c S  y )Nc                 Z   ddg}| j                  |      }|syt        j                  |      }| j                  |      }t	        |      dk7  ry| j                  |      }| j                  |      }| j                  D ]  }|d   rh	 t        |d         }||d   z  dk7  rd	|d   |d   fz  c S d
}	t        |      D ]  \  }
}||kD  s||
xx   |z  cc<   d}	 n |	d
u r
d|d   z  c S ||d<   nt	        | j                        dk7  r y|d   |vsd|d   d|c S  y # t        $ r Y  yw xY w)Nr   z-Rz*Available region(s) is not in this system.   z<Not supported the regions whose alignment size is different.r#   z-The format of size: NNN TB|GB|MB|KB|T|G|M|K|Br   z size: %s should be align with %dFTz*There is not available region for size: %s	size_bytez8size option is required to configure multiple namespacesr   ztype z2 is not supported in this system. Supported type: )rQ   jsonloadsr^   lenra   rd   r%   r   
ValueError	enumerate)r<   rG   rC   r[   r\   r`   rc   nsrh   is_space_enoughiavails               r=   namespace_checkz=PersistentMemory.pmem_argument_check.<locals>.namespace_checkZ  sx   tnG%%g.CCZZ_F44V<F6{aU!@@HN77?Enn of:O$22f:$>	 !6!9,1ARZQWXYQZD[[[&+O$-n$= "5 9,*1-:-.2O!	" '%/KbQWjXX&/B{O(A-Uf:U*\^_e\fhmnn5o8 1 & ONOs   D	D*)D*c                 ~    |
|dk  s|dkD  ry|
|dk  s|dkD  ry|
||z   dkD  ryy |dk  s|dkD  ry||z   |z   dk7  ryy )Nr   d   z*appdirect percent should be from 0 to 100.z+memorymode percent should be from 0 to 100.z'Total percent should be less equal 100.z)reserved percent should be from 0 to 100.zTotal percent should be 100. )r<   r   r8   r   s       r=   percent_checkz;PersistentMemory.pmem_argument_check.<locals>.percent_check  sr     Y]i#oC7Q;'C-Dw&,D - a<8c>Fw&1S89 9r?   c                    g d}| j                  |      }t        j                  |t              d   d   }g }|D ]   }|j	                  t        |d   d             " | j                  D ]  }|d   |vsd|d   z  c S  y )	N)rS   -onvmxml-socketdict_constructor
SocketListSocketSocketID   r   zInvalid socket number: %d)rO   r+   parser   rZ   r   r$   )r<   rG   rC   sockets_dict
socket_idsslskts          r=   socket_id_checkz=PersistentMemory.pmem_argument_check.<locals>.socket_id_check  s    9G&&w/C$??3F|TU]^LJ" ;!!#bnb"9:; {{ Ct9J.6TBBC r?   r   r   r   rM   )r%   r$   r   r8   r   )r<   rr   rv   r   retr   s         r=   pmem_argument_checkz$PersistentMemory.pmem_argument_checkY  s    )	V	:	 >>"4(([[  t~~t||T]]SS!$'C
{{ ##k*C,=s:P?J	 r?   c                     ddg}| j                  |      }|sy t        j                  |      }|D ]2  }d|d   g}| j                  |       d|d   g}| j                  |       4 y )Nr   -Nzdisable-namespacedevzdestroy-namespace)rQ   ri   rj   )r<   rG   rC   
namespacesrn   s        r=   pmem_remove_namespacesz'PersistentMemory.pmem_remove_namespaces  s~    4.!!'* ZZ_
  	)B*BuI6G(*BuI6G(	) 	r?   c                 .    ddg}| j                  |       y )Ndelete-goalrT   rU   s     r=   pmem_delete_goalz!PersistentMemory.pmem_delete_goal  s    W%W%r?   c                     | j                   | j                   r| j                  du r| j                          | j                   | j                          y y )NF)r%   r&   r   r   )r<   s    r=   pmem_init_envzPersistentMemory.pmem_init_env  sG    >>!dnn9N9NRW9W'')>>!!!# "r?   Nc                    g d}|r
|d|d   gz  }| j                  |      }t        j                  |t              d   d   }d}|D ]?  }|j	                         D ]*  \  }}|dk(  s|t        |j                         d         z  }, A |S )	N)rS   z-dCapacity-uBrx   ry   z-dimmrz   r   r{   DimmListDimmr   r   )rO   r+   r   r   itemsr   split)	r<   r   rG   rC   	dimm_listcapacityentrykeyvs	            r=   pmem_get_capacityz"PersistentMemory.pmem_get_capacity  s    P	3t9--G""7+OOC$?
KFS	 	2E++- 2Q*$AGGIaL 11H2	2
 r?   c                     dd}d }dd}d} || |      }ddg|z   }| j                  |d      } || ||      \  }	}
|	du r|i |
fS g d|z   }| j                  |      } || ||      }d	}||d
fS )Nc                    g }|r|d   }|d   }|d   }|d   }|d|gz  }n$| j                   }| j                  }| j                  }|d|z
  |z
  }|d|z  d|z  gz  }n|d|z  d|z  gz  }| j                  r|d	gz  }|S |d
gz  }|S )Nr   r   r   r   rz   rt   zmemorymode=%dzreserved=%dzPersistentMemoryType=AppDirectz,PersistentMemoryType=AppDirectNotInterleaved)r   r8   r   r7   )r<   r   ipmctl_optsr   r8   r   	socket_idress           r=   build_ipmctl_creation_optszRPersistentMemory.pmem_create_memory_allocation.<locals>.build_ipmctl_creation_opts  s    K,	l+z?I		955 NN	,,==Gmi/' 9=3;NOO' 9=8;STT @AA   NOOr?   c                 @   t        j                  d      }t        j                  d      }t        j                  d      }d}d}|j                         D ]E  }|j                  |      r|d|d}d} ||fS |j                  |      s5|r8|d|d}d} ||fS  ||fS )	NWARNINGz	.*Error.*z9Do you want to continue? [y/n] Error: Invalid data input. Tz (command: )F)recompile
splitlinesmatch)	r<   
ipmctl_outrG   warningerrorignore_errorerrmsgrB   lines	            r=   is_allocation_goodzJPersistentMemory.pmem_create_memory_allocation.<locals>.is_allocation_good  s    jj+GJJ{+E::KML FB"--/ 	==&37AFB v: [[&'7;W!E"v:	 v:r?   c           	         ddd}|r|d   |d<   t        j                  |t              d   d   }|D ]  }|r|d   t        |d   d	      k7  r|j	                         D ]d  \  }}|d
k(  r(|dxx   t        |j                         d         z  cc<   3|dk(  s|dk(  s>|dxx   t        |j                         d         z  cc<   f  | j                  |      }||d   z
  |d   z
  |d<   |S )Nr   )r   r   r   r$   r{   ConfigGoalList
ConfigGoalr   r   
MemorySizer   AppDirect1SizeAapDirect2Sizer   r   )r+   r   r   r   r   r   r   )	r<   goalr   r   rC   r   r   r   r   s	            r=   get_allocation_resultzMPersistentMemory.pmem_create_memory_allocation.<locals>.get_allocation_result  s
    !3C #D	H//$>?OPQ]^C >
 3t9E*,=r(BB#kkm >FCl*L)S1->>) 00C;K4KK(C	!,==(	>> --c2H&[)99C<MMC
OJr?   Fcreater   )rH   )r   r   r   rx   ry   z-forcer   Tr   rM   rT   )r<   r   r   r   r   reboot_requiredr   rG   rC   rB   r   r   r   s                r=   pmem_create_memory_allocationz.PersistentMemory.pmem_create_memory_allocation  s    	6	*	4  0s; W%3""7">'c7;
F;"B.. K[X##G,#D$4R''r?   c                 p    dd|d   g}|d   r
|d|d   gz  }d|v r
|d|d   gz  }| j                  |       y )Nzcreate-namespacez-mr"   r   z-trh   z-s)rQ   )r<   r%   rG   s      r=   pmem_config_namespacesz'PersistentMemory.pmem_config_namespaces?  s\    %tYv->?Vi/00G)#i455GG$r?   )TrM   )__name__
__module____qualname__r>   rK   rO   rQ   rV   r^   ra   rd   r   r   r   r   r   r   r   ru   r?   r=   r
   r
      sR    ?B ;;-
Xt(&
$\(|	r?   r
   c                  d   t               } | j                          | j                         }|r| j                  j	                  |       | j                          d| _        | j                  rV| j                  D ]  }| j                  |        ddg}| j                  |      }t        j                  |      }|| _        d}n| j                  N| j                         \  }}}|r| j                  j	                  |       | j                  j                  |       nc| j                  D ]T  }	| j                  |	      \  }
}}|r| j                  j	                  |       |
rd}| j                  j                  |       V | j                  j!                  | j                  | j                         y )NrA   Tr   r   F)r:   r   r;   )r
   rV   r   r9   r1   r   r:   r%   r   rQ   ri   rj   r;   r$   r   rZ   	exit_json)r    r   rn   rG   rC   all_nsr   r   r   r   skt_reboot_requiredskt_ret
skt_errmsgs                r=   mainr   K  s|   D  "$$&E%(DL~~.. 	,B''+	, 4.!!'*C		'+'I'I'K$fKK!!f!-3;; 		(C7;7Y7YZ]7^4*%%*%5""&KKw'		( 	KK'{{  r?   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONRETURNEXAMPLESri   r   	tracebackansible.module_utils.basicr   r   r   r+   r0   r2   ImportError
format_excobjectr
   r   r   ru   r?   r=   <module>r      s    C Bl\,
\(T  	  Z Z*
 !%)"iv iX,^ zF G  <!%9Y%9%9%;"<s   A A)(A)