o nnh @sddlmZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z ddlmZddlmZddlmZdd Zd d Zd d ZddZddZGdddeZGdddeZdS)) annotationsN)partial)to_bytesto_text)AnsibleJSONEncoder) iteritems)cPicklecCs`tj|dd}|dd}tt|}t|dt |t||t|d|dS)zHandles making sure all data is properly written to file descriptor fd. In particular, that data is encoded in a character stream-friendly way and that all data gets written before returning. r)protocol s\rs%d s%s N) rdumpsreplacerhashlibsha1 hexdigestoswritelen)fdobjsrc data_hashrJ/usr/local/lib/python3.10/dist-packages/ansible/module_utils/connection.pywrite_to_file_descriptor.s   rcCstdt|}|||S)N!Q)structpackrsendall)sdata packed_lenrrr send_dataEsr!cCsd}td}t||kr#||t|}|sdS||7}t||ks td|d|d}||d}t||krR||t|}|sHdS||7}t||ks;|S)Nrr)rrrecvrunpack)r header_lenrddata_lenrrr recv_dataJs"     r)c Csrt|j}z||}Wn'ty3}zt|dd}t|d|}|dt|ddfWYd}~Sd}~wwd|dfS)Ncodeerrr#surrogate_then_replaceerrorsr) Connection _socket_path exec_commandConnectionErrorgetattrr)modulecommand connectionoutexcr*messagerrrr2\s    r2cOs(tt}d||d}||f|d<|S)Nz2.0)jsonrpcmethodidparams)struuiduuid4)method_argskwargsreqidreqrrrrequest_buildergs   rGcseZdZfddZZS)r3cs2tt||t|D] \}}t|||q dS)N)superr3__init__rsetattr)selfr:rCrDkv __class__rrrIqszConnectionError.__init__)__name__ __module__ __qualname__rI __classcell__rrrNrr3osr3c@s4eZdZddZddZddZddZd d Zd S) r0cCs|durtd||_dS)Nzsocket_path must be a value)AssertionError socket_path)rKrUrrrrIys zConnection.__init__cCsJz|j|WSty$|drtd|jj|ft|j|YSw)N_z!'%s' object has no attribute '%s')__dict__KeyError startswithAttributeErrorrOrPr__rpc__)rKnamerrr __getattr__~s   zConnection.__getattr__c Oslt|g|Ri|}|d}tj|jstd|jz tj|tdd}Wnt y;}ztdt |d}~wwz| |}Wnt j y`} ztd|jt | ddtd d} ~ wwzt|} Wn2ty|d r|td ||d d |Ddd t|D} d| } td|| |w| d|krtdd| vrtt| d| d<| S)Nr=zsocket path %s does not exist or cannot be found. See Troubleshooting socket path issues in the Network Debug and Troubleshooting GuideT)cls vault_to_textzqFailed to encode some variables as JSON for communication with ansible-connection. The original exception was: %szuunable to connect to socket %s. See Troubleshooting socket path issues in the Network Debug and Troubleshooting Guider-r.r, exception set_optionz;Unable to decode JSON from response to {0}. Received '{1}'.cSsg|]}t|qSr)repr).0argrrr sz,Connection._exec_jsonrpc..cSsg|] \}}d||qS)z {0}={1!r})format)rdrLrMrrrrfsz, z@Unable to decode JSON from response to {0}({1}). Received '{2}'.zinvalid json-rpc id received result_typeresult)rGrpathexistsrUr3jsonr r TypeErrorrsendsocketerror traceback format_excloads ValueErrorrYrgrjoinrr) rKr\rCrDrFrErr9r8eresponser>rrr _exec_jsonrpcs^       zConnection._exec_jsonrpccOs`|j|g|Ri|}d|vr,|d}|dp|d}|d}tt|dd|d|dS) aExecutes the json-rpc and returns the output received from remote device. :name: rpc method to be executed over connection plugin that implements jsonrpc 2.0 :args: Ordered list of params passed as arguments to rpc method :kwargs: Dict of valid key, value pairs passed as arguments to rpc method For usage refer the respective connection plugin docs. rprr:r*r-r.)r*ri)rxgetr3r)rKr\rCrDrwr,msgr*rrrr[s  zConnection.__rpc__c Cszttjtj}||jt|t|t|}Wn!tjy<}z| t d|jt |ddt dd}~ww| t |ddS)Nzmunable to connect to socket %s. See the socket path issue category in Network Debug and Troubleshooting Guider-r.r`surrogate_or_strict)roAF_UNIX SOCK_STREAMconnectrUr!rr)rpcloser3rrqrr)rKrsfrwrvrrrrns"   zConnection.sendN)rPrQrRrIr]rxr[rnrrrrr0ws 1 r0) __future__rrr rlrorrqr@ functoolsr+ansible.module_utils.common.text.convertersrr ansible.module_utils.common.jsonransible.module_utils.sixransible.module_utils.six.movesrrr!r)r2rG Exceptionr3objectr0rrrrs(