
    Vh<1                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ d d	lmZmZ d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Ze dk(  r e        yy)    )absolute_importdivisionprint_functiona[  
module: nxos_rpm
extends_documentation_fragment:
- cisco.nxos.nxos
short_description: Install patch or feature rpms on Cisco NX-OS devices.
description:
- Install software maintenance upgrade (smu) RPMS and 3rd party RPMS on Cisco NX-OS
  devices.
version_added: 1.0.0
author: Sai Chintalapudi (@saichint)
notes:
- Tested against NXOSv 7.0(3)I2(5), 7.0(3)I4(6), 7.0(3)I5(3), 7.0(3)I6(1), 7.0(3)I7(3)
- Unsupported for Cisco MDS
- For patches, the minimum platform version needed is 7.0(3)I2(5)
- For feature rpms, the minimum platform version needed is 7.0(3)I6(1)
- The module manages the entire RPM lifecycle (Add, activate, commit, deactivate,
  remove)
- For reload patches, this module is NOT idempotent until the patch is committed.
options:
  pkg:
    description:
    - Name of the RPM package.
    type: str
  file_system:
    description:
    - The remote file system of the device. If omitted, devices that support a file_system
      parameter will use their default values.
    default: bootflash
    type: str
  aggregate:
    description:
    - List of RPM/patch definitions.
    type: list
    elements: dict
    suboptions:
      pkg:
        description:
        - Name of the RPM package.
        required: True
        type: str
      file_system:
        description:
        - The remote file system of the device. If omitted, devices that support a file_system
          parameter will use their default values.
        type: str
      state:
        description:
        - If the state is present, the rpm will be installed, If the state is absent,
          it will be removed.
        choices:
        - present
        - absent
        type: str
  state:
    description:
    - If the state is present, the rpm will be installed, If the state is absent,
      it will be removed.
    default: present
    choices:
    - present
    - absent
    type: str
zV
- cisco.nxos.nxos_rpm:
    pkg: nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000.rpm
ag  
commands:
    description: commands sent to the device
    returned: always
    type: list
    sample: ["install add bootflash:nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000.rpm forced",
             "install activate nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000 forced",
             "install commit nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000"]
N)deepcopy)AnsibleModule)remove_default_spec)load_configrun_commandsc                     d}| ddg}|dk  r4t        ||      d   }|r|S t        j                  d       |dz  }|dk  r3y y )Nr   text)commandoutput
         )r
   timesleep)r   module	iterationcmdsbodys        g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_rpm.pyexecute_show_commandr   w   sQ    I623D
b.FD)!,KJJqMNI b.    c                 J    dj                  ||      }t        ||       }d|v ryy)Nzdir {0}:/{1}zNo such fileFT)formatr   )r   dstfile_systemr   r   s        r   remote_file_existsr      s.    ##K5G0Dr   c                     d}|dk  r^t        | |gd      }|rFd|d   j                         v sd|d   j                         v rt        j                  d       |dz  }ny y |dk  r]y y )Nr   r   T(another install operation is in progressfailedr   r   )r	   lowerr   r   )r   cmdr   msgs       r   config_cmd_operationr&      sh    I
b.&3%.:c!fllnLs1v||~-

1Q	 b.r   c                     d}|dk  r8t        ||       }|r||vry ||v ry t        j                  d       |dz  }|dk  r8dj                  |      }| j	                  |       y )Nr   r   r   r   zOperation "{0}" Failedr%   )r   r   r   r   	fail_json)r   show_cmdcfg_cmdpkgpkg_not_presentr   r   errs           r   validate_operationr/      sq    I
b.#Hf5$d{

1Q	 b. #
)
)'
2C
r   c                 `    dj                  ||      }t        | |       t        | |||d       |S )Nzinstall add {0}:{1}Fr   r&   r/   )r   r*   r   full_pkgr,   r$   s         r   add_operationr3      s3    

&
&{H
=C%vxc59Jr   c                 ^    dj                  |      }t        | |       t        | |||d       |S )Ninstall activate {0} forcedFr1   )r   r*   r,   r$   s       r   activate_operationr6      s1    
'
.
.s
3C%vxc59Jr   c                    d}|rdj                  |      }ndj                  |      }ddi}|dk  rt        | |gd|      }|rt        |d   t              r
|d   dk(  rk|S t        |d   t              rVd|d   j                         v r|S d	|d   j                         v sd
|d   j                         v rt        j                  d       |dz  }|dk  ry y )Nr   r5   install deactivate {0} forcedignore_timeoutTr   izsocket is closedr!   r"   r   r   )r   r	   
isinstanceintstrr#   r   r   )r   r,   flagr   r$   optsr%   s          r   activate_reloadr?      s    I+2237-44S9d#D
b.&3%t4#a&#&q6V#JCFC(%Q7J>#a&,,.P3q6<<>1JJqMQ	 b.r   c                 ^    dj                  |      }t        | |       t        | ||||       |S )Nzinstall commit {0}r1   r   r*   r,   r=   r$   s        r   commit_operationrB      s1    

%
%c
*C%vxc48Jr   c                 ^    dj                  |      }t        | |       t        | ||||       |S )Nr8   r1   rA   s        r   deactivate_operationrD      s1    
)
0
0
5C%vxc48Jr   c                 ,    |rd}nd}t        | |       |S )Nzterminal dont-askzno terminal dont-ask)r&   )r   configr$   s      r   terminal_operationrG      s    !$%Jr   c                     g }|j                  t        | d             dj                  |      }t        | |       t	        | |||d       |j                  |       |j                  t        | d             |S )NTzinstall remove {0} forcedF)appendrG   r   r&   r/   )r   r*   r,   commandsr$   s        r   remove_operationrK      sj    HOO&vt45
%
,
,S
1C%vxc48OOCOO&vu56Or   c           	         g }d}|j                  d      }dj                  |dd       }d}d}	d}
d}d	j                  |      }|d
k(  rt        ||       }t        |	|       }||vr"||vr|j	                  t        | ||||             t        ||       }|rd|v rd}||vr<|r|j	                  t        | |d             |S |j	                  t        | |	|             t        |
|       }||vrRt        ||       }||v r|j	                  t        | |
|d             |S dj                  |      }| j                  |       |S t        |
|       }t        |	|       }t        ||       }|rd|v rd}||v r||v r|r|j	                  t        | |d             |S |j	                  t        | |	|d             t        |
|       }||v r|j	                  t        | |
|d             |j                  t        | ||             |S ||v r;|j	                  t        | |
|d             |j                  t        | ||             |S ||v r[|r|j	                  t        | |d             |S |j	                  t        | ||d             |j                  t        | ||             |S t        ||       }||v r|j                  t        | ||             |S )NF.r   zshow install inactivezshow install activezshow install committedzshow install patcheszshow install pkg-info {0}presentzPatch Type    :  reloadTz.Operation "install activate {0} forced" Failedr(   )splitjoinr   r   rI   r3   r?   r6   rB   r)   rD   extendrK   )r   r2   r   staterJ   reload_patchsplitted_pkgr,   show_inactiveshow_activeshow_commitshow_patchesshow_pkg_infoinactive_bodyactive_bodypatch_type_bodycommit_body
patch_bodyr.   s                      r   install_remove_rpmr`      s   HL>>#&L
((<"%
&C+M'K*K)L/66s;M	,]FC*;?m#;(>OOM&-hX[\].}fE8OKLk!T BC 26; LM*;?k!-lFCJj  0c5 QRZ OW GMMcR  S )T OO +;?*;?.}fE8OKL+#"4U CD 4V[#t TU2;G+%OO$4V[#t$TU 0 LM* O' KOO,V[#tLMOO,V]CHI  O KU CD 4V]CQV WX 0 LM O	 1GMm# 0 LMOr   c                     t        t        d      t        dd      t        ddgd            } t        |       }t        d	
      |d<   t        |       t        t        dd|            }|j                  |        ddgg}ddgg}t	        |||d      }t               }dg |d}|j                  j                  d      }g }	|rT|D ]N  }
|
D ]&  }|
j                  |      |j                  |   |
|<   ( |
j                         }|	j                  |       P n<|	j                  |j                  d   |j                  d   |j                  d   d       |	D ]e  }|d   dk(  r(t        ||d   |d         }|s|j                  d       t        ||d   |d   |d         }|sMd	|d<   |d   j                  |       g  |j                  di | y )Nr<   )type	bootflash)rb   defaultabsentrO   )choicesrd   )r,   r   rS   T)requiredr,   listdict)rb   elementsoptions)	aggregaterl   F)argument_specrequired_one_ofmutually_exclusivesupports_check_mode)changedrJ   warningsr   rS   )r   z1The requested package doesn't exist on the devicer(   rq   rJ    )ri   r   r   updater   rh   paramsgetcopyrI   r   r)   r`   rR   	exit_json)element_specaggregate_specrm   rn   ro   r   rr   resultsrl   objectsitemkeydobjremote_existsr   s                   r   mainr   K  s   ee[9Hi0)DL l+N $/N5 '4Vfn#]^M&{+,O +./#'-!	F vHRXFG!!+.IG 	D 388C=( &c 2DI3 		ANN1	 	}}U+%}}];w/	
  -w<9$.vs5zsS`OabM   %X Y!&#e*c-6H#g,W!%GIJ&&t,- Fwr   __main__)!
__future__r   r   r   rb   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rw   r   ansible.module_utils.basicr   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr	   r
   r   r   r&   r/   r3   r6   r?   rB   rD   rG   rK   r`   r   __name__rs   r   r   <module>r      s   $ A @ >@

   4
""0Rj= @ zF r   