a '¡Dg ‰ã@s–UddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZddlmZmZe e¡Zdddddd d d d d ddddœ ZddddœZdZdZdZdegZgd¢ZdZdZdZeeeegddd id!œZ dd"ddd#œidd$dd%œgd&œZ!d'Z"d(Z#Gd)d*„d*e j$ƒZ%Gd+d,„d,e&ƒZ'Gd-d.„d.e'ƒZ(Gd/d0„d0ƒZ)Gd1d2„d2e)ƒZ*Gd3d4„d4e)ƒZ+Gd5d6„d6e+ƒZ,deeedfd7d8„Z-d9d:„Z.dOdd?„Z0dQd@dA„Z1e%e j2ffgZ3dBdC„Z4edDk�r’ddl5Z5e-ƒZ6e6du�re7dEƒe5 8dF¡e9e5j:ƒdFk�r@e;e <¡ƒe;e <¡ƒdGgZe?e@dJ<edK��qfe7ejBe>dFdLdMdN�ƒdS)RéN)Ú atomic_helperÚdmiÚsourcesÚsubpÚutil)Ú EventScopeÚ EventType)zsdc:uuidT)ÚhostnameT)Úroot_authorized_keysT)ú user-scriptF)ú user-dataF)zcloud-init:user-dataF)Úiptables_disableT)Ú motd_sys_infoT)úsdc:datacenter_nameT)zsdc:vendor-dataF)zsdc:operator-scriptF)z sdc:hostnameT)zsdc:dns_domainT) ú instance-idúlocal-hostnamez public-keysr úlegacy-user-datar r rZavailability_zoneú vendor-dataúoperator-scriptr Ú dns_domainzsdc:nicsz sdc:resolversz sdc:routes)ú network-dataÚ dns_serversÚrouteszlx-brandZkvmZSmartOSZ datasource)r rr rzsdc:uuiduser-datar z"/native/.zonecontrol/metadata.sockz /dev/ttyS1é<FZ ephemeral0z/dev/vdb)Ú serial_deviceÚserial_timeoutÚmetadata_sockfileZno_base64_decodeÚ base64_keysÚ base64_allÚ disk_aliasesZmbr)Z table_typeZlayoutZ overwriteZext4)ZlabelZ filesystemÚdevice)Z disk_setupZfs_setupaU#cloud-boothook #!/bin/sh fname="%(per_boot_d)s/01_smartos_vendor_data.sh" mkdir -p "${fname%%/*}" cat > "$fname" <<"END_SCRIPT" #!/bin/sh ## # This file is written as part of the default vendor data for SmartOS. # The SmartOS datasource writes the listed file from the listed metadata key # sdc:operator-script -> %(operator_script)s # user-script -> %(user_script)s # # You can view content with 'mdata-get ' # for script in "%(operator_script)s" "%(user_script)s"; do [ -x "$script" ] || continue echo "executing '$script'" 1>&2 "$script" done END_SCRIPT chmod +x "$fname" z/var/dbc@s†eZdZdZejZejZej e j e j e j hiZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Zdd„Zdd„Zedd„ƒZdS)ÚDataSourceSmartOSZJoyentcCsjtj ||||¡t |jt |ti¡tg¡|_i|_ d|_ d|_ d|_ t j |j d¡¡|_| ¡dS)NÚscripts)rÚ DataSourceÚ__init__rÚ mergemanydictÚds_cfgZget_cfg_by_pathÚ DS_CFG_PATHÚBUILTIN_DS_CONFIGÚmetadataÚ network_dataÚ_network_configÚ routes_dataÚosÚpathÚjoinÚpathsÚ get_cpathZ script_base_dÚ_init)ÚselfZsys_cfgZdistror0©r4úG/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceSmartOS.pyr$²s ýÿzDataSourceSmartOS.__init__cCstj |¡}d||jfS)Nz%s [client=%s])rr#Ú__str__Ú md_client)r3Úrootr4r4r5r6Ås zDataSourceSmartOS.__str__cCsZ|jtjkr$tƒ|_|jdur$d|_|jtjkrVt|j|jd|jd|jdd�|_dS)Nrrr)Ú smartos_typerrr)r9rÚUNSETÚget_smartos_environr7Újmc_client_factoryr&©r3r4r4r5r2És   üzDataSourceSmartOS._initcCsHt d¡d}tj d |dg¡¡rDt d |dg¡d |dg¡¡dS)aƒMark the instance provisioning state as successful. When run in a zone, the host OS will look for /var/svc/provisioning to be renamed as /var/svc/provision_success. This should be done after meta-data is successfully retrieved and from this point the host considers the provision of the zone to be a success and keeps the zone running. z-Instance provisioning state set as successfulz/var/svcú/Z provisioningZprovision_successN)ÚLOGÚdebugr-r.Úexistsr/Úrename)r3Zsvc_pathr4r4r5Ú_set_provisioned×s   þz"DataSourceSmartOS._set_provisionedc Csä| ¡i}d}|js$t d¡dS|j ¡s@t d|j¡dS|j ¡t ¡D]$\}}|\}}|jj ||d�||<qRt  ¡D]\}}|j  |¡||<q€|j  ¡t j |j ¡d|dd¡}t j |d ¡}d t} t|  d ¡|| d d d �t j |d¡} t|  d¡| dd d�|  d¡} dt} t| | ƒ|d�s\|d�rP|d|d<n |d|d<d}|d�rr|d}|d�s¢t|| t j |j d¡d¡dœ|d<t ||jg¡|_||_|d|_|d|_|d|_| ¡d S)NÚzNot running on smartosFz4No metadata device '%r' found for SmartOS datasource©ÚstripZ instancesrÚdatar z%s/user-scriptTiÀ)Ú content_fÚlinkÚshebangÚmoder)rHrJrKrz%s/mdata-user-datarr r rr"zper-boot)Ú user_scriptÚoperator_scriptZ per_boot_drr)r2r9r?r@r7rAÚopen_transportÚSMARTOS_ATTRIB_MAPÚitemsÚgetÚSMARTOS_ATTRIB_JSONÚget_jsonÚclose_transportr-r.r/r0r1Ú LEGACY_USER_DÚwrite_boot_contentÚBUILTIN_VENDOR_DATArr%r)Z userdata_rawZvendordata_rawr*r,rC) r3ZmdZudZci_nounZ attributeZ smartos_nounrFZdata_drLZ u_script_lrMZu_dataZu_data_fr4r4r5Ú _get_dataés~  þ  ÿûü        ÿý    zDataSourceSmartOS._get_datacCsdtS)Nz serial (%s))Ú SERIAL_DEVICEr=r4r4r5Ú_get_subplatformLsz"DataSourceSmartOS._get_subplatformcCs|jd |¡S)Nr)r&rQ)r3Únamer4r4r5Údevice_name_to_deviceOsz'DataSourceSmartOS.device_name_to_devicecCs|jtkrtSiS©N)r9ÚSMARTOS_ENV_KVMÚBUILTIN_CLOUD_CONFIGr=r4r4r5Úget_config_objRs z DataSourceSmartOS.get_config_objcCs |jdS)Nr)r)r=r4r4r5Úget_instance_idWsz!DataSourceSmartOS.get_instance_idcCsN|jtjkrd|_|jdurH|jdurHt|j|jd|jd|jd�|_|jS)Nrr©r*rrr)r+rr:r*Úconvert_smartos_network_datar)r,r=r4r4r5Únetwork_configZs   üz DataSourceSmartOS.network_configN)Ú__name__Ú __module__Ú __qualname__Zdsnamerr:r9r7rZNETWORKrZBOOT_NEW_INSTANCEZBOOTZ BOOT_LEGACYZdefault_update_eventsr$r6r2rCrXrZr\r`raÚpropertyrdr4r4r4r5r!¤s(ýÿcr!c@s eZdZdS)ÚJoyentMetadataFetchExceptionN©rerfrgr4r4r4r5riksric@s eZdZdS)ÚJoyentMetadataTimeoutExceptionNrjr4r4r4r5rkosrkc@sšeZdZdZe d¡Zd#dd„Zdd„Zdd „Z d d „Z d d „Z dd„Z d$dd„Z d%dd„Zd&dd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„ZdS)'ÚJoyentMetadataClientz« A client implementing v2 of the Joyent Metadata Protocol Specification. The full specification can be found at http://eng.joyent.com/mdata/protocol.html z~V2 (?P\d+) (?P[0-9a-f]+) (?P(?P[0-9a-f]+) (?PSUCCESS|NOTFOUND)( (?P.+))?)NcCs|durtƒ}||_||_dSr])r;r9Úfp)r3r9rmr4r4r5r$�szJoyentMetadataClient.__init__cCsd t | d¡¡d@¡S)Nú{0:08x}zutf-8ìÿÿ)ÚformatÚbinasciiÚcrc32Úencode)r3Úbodyr4r4r5Ú _checksum‡sÿzJoyentMetadataClient._checksumcCsÊ|j |¡ ¡}t|dƒt|dƒkrDtd |dt|dƒ¡ƒ‚| |d¡}|d|krrtd ||d¡ƒ‚|d|kr’td ||d¡ƒ‚| dd¡s¬t   d ¡dSt   |d¡}t   d |¡|S) NÚlengthrtz*Incorrect frame length given ({0} != {1}).Zchecksumz*Invalid checksum (expected: {0}; got {1}).Ú request_idz-Request ID mismatch (expected: {0}; got {1}).ZpayloadzNo value found.zValue "%s" found.) Ú line_regexÚmatchÚ groupdictÚintÚlenrirprurQr?r@rZb64d)r3Zexpected_request_idÚframeZ frame_dataZexpected_checksumÚvaluer4r4r5Ú_get_value_from_frameŒs4ÿÿ ÿÿ ÿÿ   z*JoyentMetadataClient._get_value_from_framec s¢g‰‡fdd„}d}zD|j d¡}t|ƒdkr.as_asciizPartial response: '%s'éró N)rmÚreadr|rkÚappendÚOSErrorÚerrnoZEAGAIN)r3r…ÚmsgZbyteÚexcr4rƒr5Ú _readline¨s$    ÿþzJoyentMetadataClient._readlinecCs |j | d¡¡|j ¡dS)Nr�)rmÚwritersÚflush)r3rŒr4r4r5Ú_writeÅszJoyentMetadataClient._writecCsJt d¡| d¡| ¡}t d|¡|dkr|dt | ¡¡ ¡7}d t|ƒ|  |¡|¡}t   d|¡d}|j sx|  ¡d}| |¡| ¡}|r–| ¡t   d |¡d |vr®dS| ||¡}|S) Nrnrroú zV2 {0} {1} {2} z#Writing "%s" to metadata transport.FTz"Read "%s" from metadata transport.ÚSUCCESS)rpÚrandomZrandintr/Úbase64Ú b64encodersr‚r|rur?r@rmrNr‘rŽrTr) r3ÚrtypeÚparamrwZ message_bodyrŒZ need_closer„r~r4r4r5ÚrequestÔs2þÿÿ    zJoyentMetadataClient.requestFcCs.|jd|d�}|dur|S|r*|r*| ¡}|S)NZGET©r˜r™)ršrF)r3ÚkeyÚdefaultrFÚresultr4r4r5rQõs zJoyentMetadataClient.getcCs$|j||d�}|dur|St |¡S)N)r�)rQÚjsonÚloads)r3rœr�ržr4r4r5rSýszJoyentMetadataClient.get_jsoncCs|jdd�}|sgS| d¡S)NZKEYS)r˜Ú )ršÚsplit)r3ržr4r4r5Úlists zJoyentMetadataClient.listcCs*d dd„||fDƒ¡ ¡}|jd|d�S)Nó cSsg|]}t | ¡¡‘qSr4)r–r—rs)Ú.0Úir4r4r5Ú r€z,JoyentMetadataClient.put..ZPUTr›)r/r‚rš)r3rœÚvalr™r4r4r5Úput sÿzJoyentMetadataClient.putcCs|jr|j ¡d|_dSr])rmÚcloser=r4r4r5rTs z$JoyentMetadataClient.close_transportcCs|jr |S| ¡|Sr])rmrNr=r4r4r5Ú __enter__szJoyentMetadataClient.__enter__cCs | ¡dSr])rT)r3Úexc_typeÚ exc_valueÚ tracebackr4r4r5Ú__exit__szJoyentMetadataClient.__exit__cCst‚dSr])ÚNotImplementedErrorr=r4r4r5rNsz#JoyentMetadataClient.open_transport)NN)N)NF)N)rerfrgÚ__doc__ÚreÚcompilerxr$rurrŽr‘r’ršrQrSr£r©rTr«r¯rNr4r4r4r5rlss&ÿ  !  rlcs8eZdZef‡fdd„ Zdd„Zdd„Zdd„Z‡ZS) ÚJoyentMetadataSocketClientcstt|ƒ |¡||_dSr])Úsuperr´r$Ú socketpath)r3r¶r9©Ú __class__r4r5r$#sz#JoyentMetadataSocketClient.__init__cCs4t tjtj¡}| |j¡| d¡|_| ¡dS)NZrwb)ÚsocketZAF_UNIXZ SOCK_STREAMZconnectr¶Zmakefilermr’)r3Zsockr4r4r5rN's  z)JoyentMetadataSocketClient.open_transportcCstj |j¡Sr])r-r.rAr¶r=r4r4r5rA-sz!JoyentMetadataSocketClient.existscCsd|jj|jfS)Nz%s(socketpath=%s))r¸rer¶r=r4r4r5Ú__repr__0sz#JoyentMetadataSocketClient.__repr__) rerfrgÚSMARTOS_ENV_LX_BRANDr$rNrArºÚ __classcell__r4r4r·r5r´"sr´csDeZdZdedf‡fdd„ Zdd„Zdd„Zd d „Zd d „Z‡Z S) ÚJoyentMetadataSerialClienté Ncs"tt|ƒ ||¡||_||_dSr])rµr½r$r Útimeout)r3r r¿r9rmr·r4r5r$5sz#JoyentMetadataSerialClient.__init__cCstj |j¡Sr])r-r.rAr r=r4r4r5rA<sz!JoyentMetadataSerialClient.existsc Cs–|jdur‚z ddl}Wn.tyD}ztdƒ|‚WYd}~n d}~00|j|j|jd�}| ¡sntd|jƒ‚||_t   |t j ¡|  ¡|  ¡dS)Nrzserial support is not available)r¿zUnable to open %s)rmÚserialÚ ImportErrorr°ZSerialr r¿ZisOpenÚ SystemErrorÚfcntlÚlockfZLOCK_EXÚ_flushr’)r3rÀÚeZserr4r4r5rN?s   ÿþz)JoyentMetadataSerialClient.open_transportcCsèt d¡|jj}d|j_z | ¡Wqty<Yq@Yq0qt d¡|dkr\d|j_n||j_t d¡| d¡z:| ¡}|dkrŽWqÒ|dkr¤t d ¡Wqdt d |¡WqdtyÎt d ¡Yqd0qdt d ¡||j_dS) NzFlushing inputgš™™™™™¹?z Input emptyéz,Writing newline, expecting "invalid command"r¡zinvalid commandÚFAILUREzGot "FAILURE". Retrying.z%Unexpected response "%s" during flushzQTimeout while initializing metadata client. Is the host metadata service running?z'Got "invalid command". Flush complete.)r?r@rmr¿rŽrkr‘Úwarning)r3r¿r„r4r4r5rÅOs6          ÿ  z!JoyentMetadataSerialClient._flushcCsd|jj|j|jfS)Nz%s(device=%s, timeout=%s))r¸rer r¿r=r4r4r5rºvs ýz#JoyentMetadataSerialClient.__repr__) rerfrgr^r$rArNrÅrºr¼r4r4r·r5r½4s ÿ'r½csNeZdZdZd‡fdd„ Zddd„Zd‡fd d „ Zdd d „Zdd d„Z‡Z S)Ú JoyentMetadataLegacySerialClienta½V1 of the protocol was not safe for all values. Thus, we allowed the user to pass values in as base64 encoded. Users may still reasonably expect to be able to send base64 data and have it transparently decoded. So even though the V2 format is now used, and is safe (using base64 itself), we keep legacy support. The way for a user to do this was: a.) specify 'base64_keys' key whose value is a comma delimited list of keys that were base64 encoded. b.) base64_all: string interpreted as a boolean that indicates if all keys are base64 encoded. c.) set a key named b64- with a boolean indicating that is base64 encoded.r¾Ncs(tt|ƒ}| |||¡d|_d|_dSr])rµrÊr$rr)r3r r¿r9Úsr·r4r5r$�s z)JoyentMetadataLegacySerialClient.__init__FcCsÚ|rd|_d|_d}|jdurH| ¡}d|vrBt | d¡¡|_nd|_|jrRdS|jdurÖ|durl| ¡}tƒ}d|vrŽt| d¡ d¡ƒ}dd„|DƒD]2}t | |¡¡r¼| |¡qœ||vrœ|  |¡qœ||_dS)NrFrú,cSs"g|]}| d¡r|dd…‘qS)zb64-éN)Ú startswith)r¥Úkr4r4r5r§¬r€zFJoyentMetadataLegacySerialClient._init_base64_keys..) rrr£rZis_trueÚ_getÚsetr¢ÚaddÚremove)r3ÚresetÚkeysZb64_keysrœr4r4r5Ú_init_base64_keys“s.    z2JoyentMetadataLegacySerialClient._init_base64_keyscstt|ƒj|||d�S)N)r�rF)rµrÊrQ)r3rœr�rFr·r4r5rеs ÿz%JoyentMetadataLegacySerialClient._getcCs,|tvr dS|j|d�|jr"dS||jvS)NF)rÔT)ÚNO_BASE64_DECODErÖrr)r3rœrÔr4r4r5Úis_b64_encodedºs  z/JoyentMetadataLegacySerialClient.is_b64_encodedcCsvtƒ}|j|d|d�}||ur"|S| |¡rfzt | ¡¡ ¡}Wn"tjydt   d||¡Yn0|rr|  ¡}|S)NF)rFr�z#Failed base64 decoding key '%s': %s) ÚobjectrÐrØr–Z b64decodersr‚rqÚErrorr?rÉrF)r3rœr�rFZmdefaultr¨r4r4r5rQÄs z$JoyentMetadataLegacySerialClient.get)r¾N)F)NF)F)NF) rerfrgr±r$rÖrÐrØrQr¼r4r4r·r5rÊ~s  " rÊcCsV|durt|ƒ}|durdS|tkr2t|||d�S|tkrFt||d�Std|ƒ‚dS)N)r r¿r9)r¶r9z"Unknown value for smartos_type: %s)r;r^rÊr»r´Ú ValueError)r9rrrÚ uname_versionr4r4r5r<Ösýÿr<c Cs€ddd|g}d}z t |¡\}}t d||¡Wn:tjyj}z t td||f¡WYd}~n d}~00|durxdS| ¡S)NÚfilez--briefz --mime-typezscript %s mime type is %sz%Failed to identify script type for %s)rr?r@ZProcessExecutionErrorrÚlogexcrF)rHÚcmdÚf_typeZ_errrÆr4r4r5Ú identify_fileñs  ÿráéc Cs|stj |¡rt |¡|r4tj |¡r4t |¡|s 'mac_address', 'mtu', 'interface' -> 'name'. The remaining keys are related to ip configuration. For each ip in the 'ips' list we create a subnet entry under 'subnets' pairing the ip to a one in the 'gateways' list. Each route in sdc:routes is mapped to a route on each interface. The sdc:routes properties 'dst' and 'gateway' map to 'network' and 'gateway'. The 'linklocal' sdc:routes property is ignored. )Ú mac_addressZmtur[ÚparamsÚsubnetsÚtype) ÚaddressÚ broadcastZdns_nameserversZ dns_searchZmetricZ pointopointrZscoperìÚnetworkZgateway)ÚphysicalÚsubnetÚroutecSsd|vS)NÚ.r4©Úaddrr4r4r5Ú is_valid_ipv4Šsz3convert_smartos_network_data..is_valid_ipv4cSsd|vS)Nú:r4rôr4r4r5Ú is_valid_ipv6�sz3convert_smartos_network_data..is_valid_ipv6N)ryÚgw)Úipv4Úipv6c3s&|]\}}|ˆdvr||fVqdS)rðNr4©r¥rÏÚv©Ú valid_keysr4r5Ú —sz/convert_smartos_network_data..rðZ interface)rìr[ZmacréÚipsZdhcprìZdhcp4ZaddrconfZdhcp6c3s&|]\}}|ˆdvr||fVqdS)rñNr4rürþr4r5r¦sZstatic)rìrírúrûZprimaryFrùcs g|]}ˆˆd|ƒr|‘qS)ryr4)r¥rù)ÚpgwsÚprotor4r5r§¸sþz0convert_smartos_network_data..Úgatewaysrc3s&|]\}}|ˆdvr||fVqdS)ròNr4rürþr4r5rÂs þÚdstrrëZ nameserver)rìríÚsearchr†)ÚversionÚconfig) Ú isinstancer£ÚtupleÚdictrPÚupdaterQr|r‰)r*rrrrörørZnicZcfgrëÚiprñZ routeentsrròZrcfgr4)rrrÿr5rcGsŽ þìþ  ÿ     ÿþÿ  þ  þ   ýÿrccCs t |t¡Sr])rZlist_from_dependsÚ datasources)Zdependsr4r4r5Úget_datasource_listîsrÚ__main__zDo not appear to be on smartos.r†rdcCs®||vr||S|tvr0t|}| |¡||<nv|dkrtdD]}t|||ƒqsÊ  òü ù ýÿ ÿøH0JYû ÿ 3 ÿ # ÿ     ÿþÿ