a )g @sddlmZmZmZeZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZddlmZddlmZddlmZdd Zd d Zd d ZddZddZGdddeZGdddeZ dS))absolute_importdivisionprint_functionN)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)Zprotocol s\rs%d s%s N) r dumpsreplacerhashlibZsha1Z hexdigestoswritelen)fdobjsrcZ data_hashrC/usr/lib/python3.9/site-packages/ansible/module_utils/connection.pywrite_to_file_descriptor/s   rcCstdt|}|||S)N!Q)structpackrZsendall)sdataZ packed_lenrrr send_dataFsrcCsd}td}t||kr<||t|}|s2dS||7}q td|d|d}||d}t||kr||t|}|sdS||7}q`|S)Nrr)rrZrecvrunpack)rZ header_lenrdZdata_lenrrr recv_dataKs     r"c Csrt|j}z||}WnNtyf}z6t|dd}t|d|}|dt|ddfWYd}~Sd}~00d|dfS)Ncodeerrrsurrogate_then_replaceerrorsr) ConnectionZ _socket_path exec_commandConnectionErrorgetattrr)moduleZcommandZ connectionoutexcr#messagerrrr*]s   (r*cOs(tt}d||d}||f|d<|S)Nz2.0)Zjsonrpcmethodidparams)struuidZuuid4)Zmethod_argskwargsreqidreqrrrrequest_builderhs   r:cseZdZfddZZS)r+cs2tt||t|D]\}}t|||qdS)N)superr+__init__r setattr)selfr0r6r7kv __class__rrr<rszConnectionError.__init__)__name__ __module__ __qualname__r< __classcell__rrrArr+psr+c@s4eZdZddZddZddZddZd d Zd S) r)cCs|durtd||_dS)Nzsocket_path must be a value)AssertionError socket_path)r>rHrrrr<zszConnection.__init__cCsNz |j|WStyH|dr8td|jj|ft|j|YS0dS)N_z!'%s' object has no attribute '%s')__dict__KeyError startswithAttributeErrorrBrCr__rpc__)r>namerrr __getattr__s    zConnection.__getattr__c Ost|g|Ri|}|d}tj|js:td|jztj|tdd}Wn4t y}ztdt |WYd}~n d}~00z| |}WnFt j y} z,td|jt | ddtd WYd} ~ n d} ~ 00zt|} WnltyT|d rtd ||d d |Ddd t|D} d| } td|| |Yn0| d|krltdd| vrtt| d| d<| S)Nr2zsocket path %s does not exist or cannot be found. See Troubleshooting socket path issues in the Network Debug and Troubleshooting GuideT)clsZ 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% exceptionZ set_optionz;Unable to decode JSON from response to {0}. Received '{1}'.cSsg|] }t|qSr)repr).0argrrr z,Connection._exec_jsonrpc..cSsg|]\}}d||qS)z {0}={1!r})format)rUr?r@rrrrWrXz, z@Unable to decode JSON from response to {0}({1}). Received '{2}'.zinvalid json-rpc id receivedZ result_typeresult)r:rpathexistsrHr+jsonr r TypeErrorrsendsocketerror traceback format_excloads ValueErrorrLrYr joinr r) r>rOr6r7r9r8rr/r.eresponser3rrr _exec_jsonrpcsT      zConnection._exec_jsonrpccOs`|j|g|Ri|}d|vrX|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. rarr0r#r&r')r#rZ)rigetr+r)r>rOr6r7rhr%msgr#rrrrNs  zConnection.__rpc__c Csz6ttjtj}||jt|t|t|}WnNtjy}z4| t d|jt |ddt dWYd}~n d}~00| t |ddS)Nzmunable to connect to socket %s. See the socket path issue category in Network Debug and Troubleshooting Guider&r'rRZsurrogate_or_strict)r`ZAF_UNIXZ SOCK_STREAMZconnectrHrrr"racloser+rrbrc)r>rZsfrhrgrrrr_s  zConnection.sendN)rCrDrEr<rPrirNr_rrrrr)xs 1r))!Z __future__rrrtypeZ __metaclass__rrr]r`rrbr5 functoolsrZansible.module_utils._textrrZ ansible.module_utils.common.jsonrZansible.module_utils.sixr Zansible.module_utils.six.movesr rrr"r*r: Exceptionr+objectr)rrrrs(