o nnhR@sddlmZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddlmZmZddlmZddlmZdd lmZmZdd lmZmZdd lmZdd lmZdd lm Z ddl!m"Z"ddl#m$Z$m%Z%e"Z&ej'dddZ(Gddde)Z*dS)) annotationsN)MutableSequence)rmtree)context) AnsibleErrorAnsibleParserError) GalaxyAPI) user_agent) to_nativeto_text) yaml_dump yaml_load) LooseVersion)open_url)RoleRequirement)Display) is_subpath unfrackpathreturnboolcCsVd}ttdr)td}tj|_d|_zt|dWn tjy&Y|Swd}|S)zj Check if tarfile.data_filter implementation is working for the current Python version or not F data_filterzdocs/README.mdz ../README.mdz/fooT)hasattrtarfileTarInfoSYMTYPEtypelinknamerLinkOutsideDestinationError)rettir >/usr/local/lib/python3.10/dist-packages/ansible/galaxy/role.py_check_working_data_filter1s  r"c@seZdZeddgZejddejddfZejddZ ejddejddfZ d Z d'd d Z d dZ ddZeddZeddZeddZeddZeddZddZddZdd Zd!d"Zed#d$Zed%d&Zd S)( GalaxyRolegithgmetazmain.ymlz main.yamlz.galaxy_install_infozrequirements.ymlzrequirements.yaml)defaultsfileshandlersr&tasks templatesvarstestsNc sd_d_d_d_tjd _tdj|_ |_ |_ |_ |p*|_ d_|_fdd|jD_|durx|tjtjjj sWtj|j }njD]}tjtj|||rrtj|j }nqZ|_dSjd_dS)N ignore_certszValidate TLS certificates: %scsg|] }tj|jqSr )ospathjoinname).0xselfr r! esz'GalaxyRole.__init__..r) _metadata_metadata_dependencies _requirements _install_inforCLIARGS_validate_certsdisplaydebuggalaxy_apir2versionsrc download_urlscm roles_pathspathsendswithr/r0r1sep META_MAINexists) r6r@apir2rCrBrEr0 meta_mainr r5r!__init__Ss0   zGalaxyRole.__init__cCs|jr d|j|jfS|jS)zj Returns "rolename (version)" if version is not null Returns "rolename" otherwise z%s (%s))rBr2r5r r r!__repr__yszGalaxyRole.__repr__cCs |j|jkSN)r2)r6otherr r r!__eq__s zGalaxyRole.__eq__cCst|jts |jjS|jSrP) isinstancerArrLr5r r r!rLs zGalaxyRole.apic Cs|jdurQ|jD]H}|jD]B}tj||}tj|rOzt|d }t||_Wdn1s2wYWnt yLt d|j YdSwnq q|jS)z' Returns role metadata NrzUnable to load metadata for %sF) r8rGrJr/r0r1isfileopenr Exceptionr>vvvvvr2)r6r0rM meta_pathfr r r!metadatas$         zGalaxyRole.metadatacCsT|jdurg|_|jdur|jdpg|_t|jts'td|d|j|jS)zC Returns a list of dependencies from role metadata N dependencies.Expected role dependencies to be a list. Role z% has meta/main.yml with dependencies )r9r[getrSrrr5r r r!metadata_dependenciess   z GalaxyRole.metadata_dependenciescCs|jdurFtj|j|j}tj|rFz,z t|d}t||_Wnty8t d|j YW| dSwW| |jS| w|jS)z+ Returns role install info NrTz)Unable to load Galaxy install info for %sF) r;r/r0r1 META_INSTALLrUrVr rWr>rXr2close)r6 info_pathrZr r r! install_infos      zGalaxyRole.install_infocCs"|jD] }tj|rdSqdS)NTF)rGr/r0isdir)r6r0r r r!_existss  zGalaxyRole._existsc Cst|jtjtjjdd}tj tj |jds't tj |jdtj |j|j }t |d"}zt|||_WntyNYWddSwWddS1sZwYdS)z Writes a YAML-formatted file to the role's meta/ directory (named .galaxy_install_info) which contains some information we can use later for commands like 'list' and 'info'. z%c)rB install_dater&zw+NFT)dictrBdatetimenowtimezoneutcstrftimer/r0rKr1makedirsr`rVr r;rW)r6inforbrZr r r!_write_galaxy_install_infos&   z%GalaxyRole._write_galaxy_install_infocCs0|jrzt|jWdStyYdSwdS)z Removes the specified role from the roles path. There is a sanity check to make sure there's a meta/main.yml file at this path so the user doesn't blow away random directories. TF)r[rr0rWr5r r r!removes  zGalaxyRole.removec Cs|ro|jdur |j}nd|vr d|vr d|d|d|jf}n|j}td|z(t||jtd}tjdd}| }|rK| || }|s@| |j WSt yn}ztd t|WYd}~dSd}~wwdS) zS Downloads the archived role to a temp location based on role data N github_user github_repoz*https://github.com/%s/%s/archive/%s.tar.gzz- downloading role from %s)validate_certs http_agentF)deletezfailed to download the file: %s)rDrBrCr>rr=r tempfileNamedTemporaryFilereadwriterar2rWerrorr )r6 role_data archive_urlurl_file temp_filedataer r r!fetchs.   zGalaxyRole.fetchc s|jrtjd2dtjdi|j}n|jrtj |jr |j}nd|jvr.|j}| |}n|j |j}|sBt d|j|j jf|ddkrQtd|j|j d|d}|jst|d krfd d |D}z|Wntyt d d dd |Dwt|d|_n.|ddr|d|_n"d|_n|jdkr|rt|jdd |Dvrt d|j|j|f|D]"}|d|jkrd|vr|d|_|d|jkrd|vr|d|_q| |}nt d|rtd|t|st dt|d}d}|}|D]1} |j D]*} | | jvr=tj!tj!| j} |s0| } | }qt| t| kr=| } | }qq|sHt dz t"|#||_$Wn t%y^t dw|j&} |j| d krq|j| dd <t| }t'| D]\}}||_td|j|jfztj(|jrtj)|jst dtjdd st d!|j|*st d"|jnt+|jt,| d d#}|D]} | -s| .sِqd$D]l}t/| |d}sq|d%krtj!t/| dd&}n|0| d&d'}tjj|1tj2}d&}tj|||}t3||d(d)s*d*|d+|d,|}t |tj||}tjj|0|d&d'1tj2}t4| ||qt5rY|j6| t7|jd-d.q|6| t7|jq|8Wn-t9y}z |j:t:j;kr||d'krWYd}~qyt d/|jt7|fd}~wwtd0t<||jrtj |jszt=|Wd(St9t>fy}ztd1|t|fWYd}~d(Sd}~wwd(Sd S)3N keep_scm_metaz://z - sorry, %s was not found on %s. role_typeAPPzP%s is a Container App role, and should only be installed using Ansible Containerversionsidrcs2g|]}tr|dpddurqS)r2N)rparser^r3avr r!r7,s2z&GalaxyRole.install..zUnable to compare role versions (%s) to determine the most recent version due to incompatible version formats. Please contact the role author to resolve versioning conflicts, or specify an explicit role version to install.z, cSsg|]}|jqSr )vstring)r3rr r r!r73s github_branchmastercSsg|]}|ddqS)r2N)r^rr r r!r7;szX- the specified version (%s) of %s was not found in the list of available versions (%s).r2sourcerDzNo valid role data foundzinstalling from %sz>the downloaded file does not appear to be a valid tar archive.rTz7this role does not appear to have a meta/main.yml file.z=this role does not appear to have a valid meta/main.yml file.z- extracting %s to %sz7the specified roles path exists and is not a directory.forceFzJthe specified role %s appears to already exist. Use --force to replace it.zu%s doesn't appear to contain a role. please remove this directory manually if you really want to put the role here.)follow)r2rrrT)realzInvalid z for tarfile member: path z is not a subpath of the role r)filterz Could not update files in %s: %sz- %s was installed successfullyz"Unable to remove tmp file (%s): %sr )?rErscm_archive_rolerr<specrCr/r0rUrrLlookup_role_by_namer api_serverr^r>warningr2fetch_role_relatedrBlensort TypeErrorr1r rDr?r is_tarfilerV getmembersrJdirnamer extractfiler8rWrG enumeraterKrdrprmrisregissymgetattrreplacesplitrIrsetattrr"extractr roOSErrorerrnoEACCESstrunlinkIOError)r6tmp_filer{ role_versionsloose_versions role_version role_tar_file meta_filemembersmemberrMmeta_parent_dirarchive_parent_dirrG paths_lenidxr0resolved_archiveattr attr_value relative_to full_patherrrelative_path_dir relative_pathrr rr!installs                       zGalaxyRole.installcCst|j|j|j|jdS)z Returns role spec info { 'scm': 'git', 'src': 'http://git.example.com/repos/repo.git', 'version': 'v1.0', 'name': 'repo' } )rErCrBr2)rgrErCrBr2r5r r r!rs zGalaxyRole.specc Cs|jdurHg|_|jD]<}tj|j|}tj|rGz$z t|d}t||_Wnty9t d|j YnwW| n| wnq t |jtsYtd|d|j|jS)z+ Returns role requirements NrTz"Unable to load requirements for %sr]z has meta/requirements.yml )r:META_REQUIREMENTSr/r0r1rUrVr rWr>rXr2rarSrr)r6meta_requirementsrYrZr r r! requirementss$      zGalaxyRole.requirements)NNNN)__name__ __module__ __qualname__setSUPPORTED_SCMSr/r0r1rJr`r ROLE_DIRSrNrOrRpropertyrLr[r_rcrerorprrrrr r r r!r#Ks8  &      2  r#)rr)+ __future__rrrh functoolsr/rrvcollections.abcrshutilransibleransible.errorsrransible.galaxy.apiransible.galaxy.user_agentr +ansible.module_utils.common.text.convertersr r ansible.module_utils.common.yamlr r #ansible.module_utils.compat.versionransible.module_utils.urlsr!ansible.playbook.role.requirementransible.utils.displayransible.utils.pathrrr>cacher"objectr#r r r r!s0