
    VhwZ                         d dl mZmZmZ eZdZdZd dlZ	d dl
Z
d dlZd dlmZ d dlmZ d dlmZ  G d d	      Zd
 ZddZd Zd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: zypper
author:
  - "Patrick Callahan (@dirtyharrycallahan)"
  - "Alexander Gubin (@alxgu)"
  - "Thomas O'Donnell (@andytom)"
  - "Robin Roth (@robinro)"
  - "Andrii Radyk (@AnderEnder)"
short_description: Manage packages on SUSE and openSUSE
description:
  - Manage packages on SUSE and openSUSE using the zypper and rpm tools.
  - Also supports transactional updates, by running zypper inside C(/sbin/transactional-update --continue --drop-if-no-change
    --quiet run).
extends_documentation_fragment:
  - community.general.attributes
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
options:
  name:
    description:
      - Package name V(name) or package specifier or a list of either.
      - Can include a version like V(name=1.0), V(name>3.4) or V(name<=2.7). If a version is given, V(oldpackage) is implied
        and zypper is allowed to update the package within the version range given.
      - You can also pass a URL or a local path to a rpm file.
      - When using O(state=latest), this can be V(*), which updates all installed packages.
    required: true
    aliases: ['pkg']
    type: list
    elements: str
  state:
    description:
      - V(present) makes sure the package is installed.
      - V(latest) makes sure the latest version of the package is installed.
      - V(absent) makes sure the specified package is not installed.
      - V(dist-upgrade) makes sure the latest version of all installed packages from all enabled repositories is installed.
      - When using V(dist-upgrade), O(name) should be V(*).
    required: false
    choices: [present, latest, absent, dist-upgrade, installed, removed]
    default: "present"
    type: str
  type:
    description:
      - The type of package to be operated on.
    required: false
    choices: [package, patch, pattern, product, srcpackage, application]
    default: "package"
    type: str
  extra_args_precommand:
    required: false
    description:
      - Add additional global target options to C(zypper).
      - Options should be supplied in a single line as if given in the command line.
    type: str
  disable_gpg_check:
    description:
      - Whether to disable to GPG signature checking of the package signature being installed. Has an effect only if O(state)
        is V(present) or V(latest).
    required: false
    default: false
    type: bool
  disable_recommends:
    description:
      - Corresponds to the C(--no-recommends) option for I(zypper). Default behavior (V(true)) modifies zypper's default behavior;
        V(false) does install recommended packages.
    required: false
    default: true
    type: bool
  force:
    description:
      - Adds C(--force) option to I(zypper). Allows to downgrade packages and change vendor or architecture.
    required: false
    default: false
    type: bool
  force_resolution:
    description:
      - Adds C(--force-resolution) option to I(zypper). Allows to (un)install packages with conflicting requirements (resolver
        chooses a solution).
    required: false
    default: false
    type: bool
    version_added: '0.2.0'
  update_cache:
    description:
      - Run the equivalent of C(zypper refresh) before the operation. Disabled in check mode.
    required: false
    default: false
    type: bool
    aliases: ["refresh"]
  oldpackage:
    description:
      - Adds C(--oldpackage) option to I(zypper). Allows to downgrade packages with less side-effects than force. This is
        implied as soon as a version is specified as part of the package name.
    required: false
    default: false
    type: bool
  extra_args:
    required: false
    description:
      - Add additional options to C(zypper) command.
      - Options should be supplied in a single line as if given in the command line.
    type: str
  allow_vendor_change:
    type: bool
    required: false
    default: false
    description:
      - Adds C(--allow_vendor_change) option to I(zypper) dist-upgrade command.
    version_added: '0.2.0'
  replacefiles:
    type: bool
    required: false
    default: false
    description:
      - Adds C(--replacefiles) option to I(zypper) install/update command.
    version_added: '0.2.0'
  clean_deps:
    type: bool
    required: false
    default: false
    description:
      - Adds C(--clean-deps) option to I(zypper) remove command.
    version_added: '4.6.0'
  simple_errors:
    type: bool
    required: false
    default: false
    description:
      - When set to V(true), provide a simplified error output (parses only the C(<message>) tag text in the XML output).
    version_added: '10.2.0'
  quiet:
    type: bool
    required: false
    default: true
    description:
      - Adds C(--quiet) option to I(zypper) install/update command.
    version_added: '10.2.0'
  skip_post_errors:
    type: bool
    required: false
    default: false
    description:
      - When set to V(true), ignore I(zypper) return code 107 (post install script errors).
    version_added: '10.6.0'
notes:
  - When used with a C(loop:) each package is processed individually, it is much more efficient to pass the list directly
    to the O(name) option.
requirements:
  - "zypper >= 1.0  # included in openSUSE >= 11.1 or SUSE Linux Enterprise Server/Desktop >= 11.0"
  - python-xml
  - rpm
aQ  
- name: Install nmap
  community.general.zypper:
    name: nmap
    state: present

- name: Install apache2 with recommended packages
  community.general.zypper:
    name: apache2
    state: present
    disable_recommends: false

- name: Apply a given patch
  community.general.zypper:
    name: openSUSE-2016-128
    state: present
    type: patch

- name: Remove the nmap package
  community.general.zypper:
    name: nmap
    state: absent

- name: Install the nginx rpm from a remote repo
  community.general.zypper:
    name: 'http://nginx.org/packages/sles/12/x86_64/RPMS/nginx-1.8.0-1.sles12.ngx.x86_64.rpm'
    state: present

- name: Install local rpm file
  community.general.zypper:
    name: /tmp/fancy-software.rpm
    state: present

- name: Update all packages
  community.general.zypper:
    name: '*'
    state: latest

- name: Install latest packages but dump error messages in a simplified format
  community.general.zypper:
    name: '*'
    state: latest
    simple_errors: true
    quiet: false

- name: Apply all available patches
  community.general.zypper:
    name: '*'
    state: latest
    type: patch

- name: Perform a dist-upgrade with additional arguments
  community.general.zypper:
    name: '*'
    state: dist-upgrade
    allow_vendor_change: true
    extra_args: '--allow-arch-change'

- name: Perform a installation of nmap with the install option replacefiles
  community.general.zypper:
    name: 'nmap'
    state: latest
    replacefiles: true

- name: Refresh repositories and update package openssl
  community.general.zypper:
    name: openssl
    state: present
    update_cache: true

- name: "Install specific version (possible comparisons: <, >, <=, >=, =)"
  community.general.zypper:
    name: 'docker>=1.10'
    state: present

- name: Wait 20 seconds to acquire the lock before failing
  community.general.zypper:
    name: mosh
    state: present
  environment:
    ZYPP_LOCK_TIMEOUT: 20

- name: Install the package with post-install error without failing
  community.general.zypper:
    name: <package_with_post_install_error>
    state: present
    skip_post_errors: true
N)parseString)	to_native)AnsibleModulec                       e Zd Zd Zd Zy)Packagec                 B    || _         || _        || _        |dk(  | _        y )N+)nameprefixversionshouldinstall)selfr   r   r   s       l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/zypper.py__init__zPackage.__init__  s#    	$m    c                 N    | j                   | j                  z   | j                  z   S N)r   r   r   )r   s    r   __str__zPackage.__str__  s    {{TYY&55r   N)__name__
__module____qualname__r   r    r   r   r
   r
     s    -6r   r
   c                     d}| d   dv r
| d   }| dd } |dk(  rd}t        j                  d      }	 |j                  |       }|j                         \  } }|d}|| |fS # t        $ r || dfcY S w xY w)	a  splits of the package name and desired version

    example formats:
        - docker>=1.10
        - apache=2.4

    Allowed version specifiers: <, >, <=, >=, =
    Allowed version format: [0-9.-]*

    Also allows a prefix indicating remove "-", "~" or install "+"
     r   )-~r      Nr   r   z!^(.*?)((?:<|>|<=|>=|=)[0-9.-]*)?$)recompilematchgroups	Exception)r   r   version_checkreresr   s        r   split_name_versionr(     s     FAw/!aABx}JJBCM ##D)g?GtW$$  tR s   ,A A/.A/c                     g }g }| D ]_  }d|v s|j                  d      r|j                  |       *t        |      \  }}}|dvr|rd}nd}|j                  t        |||             a ||fS )Nz://z.rpm)r   r   r   r   )endswithappendr(   r
   )namesremovepackagesurlsr   r   pnamer   s           r   get_want_stater1   >  s    HD 
=D=DMM&1KK%7%="FE7Z' F FOOGE67;<
= T>r   c                     t        | d      }|j                  g d       |j                  |D cg c]  }|j                   c}       t        | |d      d   S c c}w )zget installed state of packagessearch)z--match-exactz	--detailsz--installed-onlyF)fail_not_foundr   )get_cmdextendr   parse_zypper_xml)mr.   cmdps       r   get_installed_stater;   O  sQ     !X
CJJABJJ)1)*As59!<< *s   Ac                    | j                  |d      \  }}}	 t        |      }|dk(  rK|rCj                  d      d   j                  d   j                  }	| j                  |	||||       ni |||fS |d	v r|d
}
i }nd}
j                  d      }|D ]~  }|j                  d      }i ||<   |j                  d      ||   d<   |j                  d      ||   d<   |j                  d      }|dk(  ||   d<   |j                  j                  ||   d<    |dk(  r|
rt        | |||      S |dk(  r | j                  d   r|
rt        | |||      S | j                  d   rt!        |      xs |}||||fS | j                  d   rt!              xs |}| j                  d|z  ||||       y # t        j                  j                  j
                  $ r-}| j                  dt        |      z  ||||       Y d }~d }~ww xY w)NF)check_rcz%Failed to parse zypper xml output: %s)msgrcstdoutstderrr9   h   messager   )r   f   g   j   k   Tsolvabler   editionr   zedition-old
oldversionstatus	installedgrouprF   )r4   r.   rH   skip_post_errorssimple_errorsz.Zypper run command failed with return code %s.)run_commandparseXMLxmlparsersexpat
ExpatError	fail_jsonr   getElementsByTagName
childNodesdatagetAttribute
parentNodenodeNamer7   paramsget_simple_errors)r8   r9   r4   r.   r?   r@   rA   domexcerrmsgfirstrunsolvable_listrI   r   rL   s                  r   r7   r7   X  sM   sU;BBv
 
Sy--i8<GGJOOFKKFr&SKQr66))	&	& HHH00<% 	CH((0DHTN(0(=(=i(HHTN9%+3+@+@+OHTN<(**84F*0K*?HTN;'&.&9&9&B&BHTN7#	C 9 $As>T\]]9"45( $As>T\]] 88O$&s+5vFVV++ 	xx "3'16KKDrIbY_hntwKxg ;;'' B	?)C.P&S 	 	B 	BBs   F, ,'H "G;;H c                     g }| j                  d      }|y |D ]'  }|j                  |j                  j                         ) dj	                  |      S )NrC   z 
)rX   r+   
firstChildrZ   join)r`   rP   message_xml_tagsxs       r   r_   r_     sW    M//	: 0Q\\../0::m$$r   c                    |dv }|dk(  }| j                  dd      ddg}| j                  d   r|j                  d	       t               r| j                  d
d      ddd	dg|z   }| j                  d   r.| j                  d   j	                         }|j                  |       |s|r | j                  d   r|j                  d       |dk(  r|j                  d       |j                  |       |dvr"|s |j                  d| j                  d   g       | j                  r|dk7  r|j                  d       |r|j                  d       | j                  d   r|j                  d       | j                  d   r|j                  d       | j                  d   r|j                  d       | j                  d   r|j                  d       | j                  d    r|j                  d!       |d"k(  r | j                  d#   r|j                  d$       |d%k(  r | j                  d&   r|j                  d'       | j                  d(   r/| j                  d(   j	                  d)      }|j                  |       |S )*zPputs together the basic zypper command arguments with those passed to the module)installupdatepatchdist-upgraderefreshzypperT)requiredz--non-interactivez--xmloutquietz--quietztransactional-updatez
--continuez--drop-if-no-changerunextra_args_precommanddisable_gpg_checkz--no-gpg-checksr3   z--disable-repositories)rm   rn   z--typetypez	--dry-runz--auto-agree-with-licensesdisable_recommendsz--no-recommendsforcez--forceforce_resolutionz--force-resolution
oldpackagez--oldpackagereplacefilesz--replacefilesr-   
clean_depsz--clean-depsrn   allow_vendor_changez--allow-vendor-change
extra_args )get_bin_pathr^   r+   transactional_updatessplitr6   
check_mode)r8   
subcommand
is_install
is_refreshr9   	args_lists         r   r5   r5     s0   MMJy(J>>(T>24G
TCxx

9~~4t~DlTiktv{|  @C  Cxx'(HH45;;=	

9jahh/B&C

$%X

+,JJz22:

Hahhv./0||
h.

;

/088()JJ()88GJJy!88&'JJ+,88L!JJ~&88N#JJ'(X88L!JJ~&^#1F(G

*+xxHH\*005	

9Jr   c                    g g g d}|rz|D ]u  }||   d   }|dk(  r2d||   d   z   dz   ||   d   z   dz   }|d	   j                  ||z          B|d
k(  r|d   j                  |       \|dk(  sb|d   j                  |       w d}|D ](  }||   s	||dz   dj                  ||         z   dz   z  }* d|vri |d<   d|d   vr	||d   d<   y |d   dxx   d|z   z  cc<   y )N)rM   removedupgradedrN   z
to-upgradez (rK   z => r   )r   z
to-installrM   z	to-remover   r   z: z, 
diffprepared)r+   rg   )	r8   retvalsresultr.   r:   rN   versionsoutputstates	            r   set_diffr     s8   BB?H 	.A1Ig&E$&)L"99FBVAYyEYY\__$++AL9,&%,,Q/+%#**1-	. F GE?edlTYYx%??$FFFG W(&,
#
#tf}4#r   c                 >   dddd}t        |      \  }}t        d |D              rd| j                  d<   |sW|D cg c]  }|j                  r| }}i }|rt	        | |      }|D cg c]   }|j
                  |j                  |v k7  s|" }}|s|sd|fS t        | d      }	|	j                  d	       |	j                  |       |	j                  |D cg c]  }t        |       c}       |	|d
<   t        | |	      \  }
|d<   |d<   |d<   |
|fS c c}w c c}w c c}w )zlinstall and update (if want_latest) the packages in name_install, while removing the packages in name_remover   r   r?   r@   rA   c              3   4   K   | ]  }|j                     y wr   )r   .0r:   s     r   	<genexpr>z"package_present.<locals>.<genexpr>  s     
'199
's   Trz   Nrk   z--r9   r?   r@   rA   )r1   anyr^   r   r;   r   r   r5   r+   r6   strr7   )r8   r   want_latestr   r.   r/   r:   packageswithoutversionprerun_stater9   r   s              r   package_presentr     s0   "3G#D)NHd 
'h
''!% .6!GQYY!!G!G!.q2HIL'W!1??qvv?U+VAWWDW} !Y
CJJtJJt JJ)1A)*GENBRSTVYBZ?FGDM78,gh.?7?3 "H
 X *s   DD( D	DDc                     dddd}| j                   d   dk(  rd}n| j                   d   dk(  rd}nd}t        | |      }||d	<   t        | |      \  }|d
<   |d<   |d<   ||fS )z-run update or patch on all available packagesr   r   r   rv   rm   r   rn   rl   r9   r?   r@   rA   )r^   r5   r7   )r8   r   cmdnamer9   r   s        r   package_update_allr     s     "3Gxx7"	
'	n	, 
!W
CGENBRSTVYBZ?FGDM78,gh.?7?r   c                 $   dddd}t        |d      \  }}t        d |D              r| j                  d       |r| j                  d	       | j                  d
   dk(  r| j                  d       t	        | |      }|D cg c]  }|j
                  |v s| }}|sd|fS t        | d      }|j                  |D cg c]  }|j
                  |j                  z    c}       ||d<   t        | |      \  }|d<   |d<   |d<   ||fS c c}w c c}w )zremove the packages in namer   r   r   T)r-   c              3   :   K   | ]  }|j                   d k(    yw)r   N)r   r   s     r   r   z!package_absent.<locals>.<genexpr>(  s     
-q188s?
-s   z4Can not combine '+' prefix with state=remove/absent.r>   zCan not remove via URL.rv   rm   zCan not remove patches.Nr-   r9   r?   r@   rA   )
r1   r   rW   r^   r;   r   r5   r6   r   r7   )	r8   r   r   r.   r/   r   r:   r9   r   s	            r   package_absentr   #  s   "3G#D6NHd

-H
--	NO	12xx7"	12&q(3L#>aqvv'=>H>W}
!X
CJJH5q"56GENBRSTVYBZ?FGDM78,gh.?7? ? 6s   DD> Dc                 h    dddd}t        | d      }||d<   t        | |      \  }|d<   |d<   |d<   |S )	zupdate the repositoriesr   r   r   ro   r9   r?   r@   rA   )r5   r7   )r8   r   r9   r   s       r   repo_refreshr   <  sM    "3G
!Y
CGENBRSTVYBZ?FGDM78,gh.?Nr   c                     t        dd      5 }|j                         D ]F  }|j                         }|d   }|| k(  s|d   }|d   }|d|j                  d      v fc cd d d        S  	 d d d        y # 1 sw Y   y xY w)Nz/proc/mountsrr          ro,)open	readlinesr   )mount_pointfilelinefieldspathfsoptss          r   get_fs_type_and_readonly_stater   H  s    	nc	" 3dNN$ 	3DZZ\F!9D{"AYay44::c?2223 3	33 3 s   .A1 A1&A11A:c                  `    t         j                  j                  d      xr t        d      dk(  S )Nz/usr/sbin/transactional-update/)btrfsT)osr   existsr   r   r   r   r   r   T  s(    77>>:;v@^_b@cgv@vvr   c                     t        t        d7i dt        ddgdd      dt        dd	g d
      dt        ddg d      dt        dd       dt        ddd      dt        ddd      dt        ddd      dt        ddd      dt        ddgdd      dt        ddd      dt        dd       dt        ddd      dt        ddd      dt        ddd      dt        ddd      d t        ddd      d!t        ddd      d"      } t        d#d#d#$      | _        | j                  d   }| j                  d   }| j                  d   }t	        t        d |            }|r3| j                  s't        |       }|d%   d&k7  r | j                  d7d'd(i| |d)gk(  r|d*v rt        |       \  }}nI|d)gk7  r|d+k(  r| j                  d,-       n+|d.v rt        | |      \  }}n|d/v rt        | ||d0k(        \  }}d%   d1v xr t              |d2<   | j                  rt        | |       |d%   d1vr | j                  d7d'd3i| |d2   s|d4= |d5=  | j                  d7|||d6| y )8Nr   Tpkglistr   )rq   aliasesrv   elementsr   Fpresent)absentrM   latestr   r   rn   )rq   defaultchoicesrv   package)r   rm   patternproduct
srcpackageapplicationrt   )rq   r   ru   bool)rq   r   rv   rw   rx   ry   update_cachero   )rq   r   r   rv   rz   r~   r}   r{   r|   rP   rr   rO   )argument_specsupports_check_modeC)LANGLC_ALLLC_MESSAGESr?   r   r>   zZypper refresh run failed.*)r   rn   rn   z'Can not dist-upgrade specific packages.r   )r   r   )rM   r   r   r   )r   rE   changedzZypper run failed.r@   rA   )r   r   r   r   )r   dictrun_command_environ_updater^   r   filterr   r   rW   r   r   r   r   _diffr   	exit_json)moduler   r   r   r   packages_changeds         r   mainr   \  s    
teW6ER
y  CJ  K
 ui  BI  J
 #'t"D	

 #E5vN
  $UDvN
 u6B
 "5%fM
 uyk5W]^
 UEG
 UD9
 !%eU P
 ue&I
 UEG
 u6J
  t&A!
" "5%fM#
& !)F. )-#cs(SF%== DMM'"E==0L tT"#D F--v&4=AFI!=II u}"<<$6v$>!'	#5N2FG))(6vt(D%g88(7exFW(X%g (2Mt<L7MGI||"23t}H$=1=W=9HHFR$e,R'Rr   __main__)F)TN)#
__future__r   r   r   rv   __metaclass__DOCUMENTATIONEXAMPLESos.pathr   rS   r!   xml.dom.minidomr   rR   +ansible.module_utils.common.text.convertersr   ansible.module_utils.basicr   r
   r(   r1   r;   r7   r_   r5   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s   " A @ZxWr  
 	 3 A 56 6 >"=8yv	%-`54&R"2		w?SD zF r   