a 'Dg @sUddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZddlmZmZeeZdddddd d d d d dddd ZddddZdZdZdZdegZgdZdZdZdZeeeegddd 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/d0d0Z)Gd1d2d2e)Z*Gd3d4d4e)Z+Gd5d6d6e+Z,deeedfd7d8Z-d9d:Z.dOdd?Z0dQd@dAZ1e%e j2ffgZ3dBdCZ4edDkrddl5Z5e-Z6e6dure7dEe58dFe9e5j:dFkr@e;e<e;e<dGgZe?e@dJ<edKqfe7ejBe>dFdLdMdNdS)RN) atomic_helperdmisourcessubputil) 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-idlocal-hostnamez public-keysr legacy-user-datar r rZavailability_zone vendor-dataoperator-scriptr dns_domainzsdc:nicsz sdc:resolversz sdc:routes) network-data dns_serversrouteszlx-brandZkvmZSmartOSZ datasource)r rr rzsdc:uuiduser-datar z"/native/.zonecontrol/metadata.sockz /dev/ttyS1<FZ ephemeral0z/dev/vdb) serial_deviceserial_timeoutmetadata_sockfileZno_base64_decode base64_keys base64_all disk_aliasesZmbr)Z table_typeZlayoutZ overwriteZext4)ZlabelZ filesystemdevice)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@seZdZdZejZejZej e j e j e j hiZddZddZddZdd Zd d Zd d ZddZddZddZeddZdS)DataSourceSmartOSZJoyentcCsjtj||||t|jt|titg|_i|_ d|_ d|_ d|_ t j|jd|_|dS)Nscripts)r DataSource__init__r mergemanydictds_cfgZget_cfg_by_path DS_CFG_PATHBUILTIN_DS_CONFIGmetadata network_data_network_config routes_dataospathjoinpaths get_cpathZ script_base_d_init)selfZsys_cfgZdistror0r4G/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceSmartOS.pyr$s zDataSourceSmartOS.__init__cCstj|}d||jfS)Nz%s [client=%s])rr#__str__ md_client)r3rootr4r4r5r6s zDataSourceSmartOS.__str__cCsZ|jtjkr$t|_|jdur$d|_|jtjkrVt|j|jd|jd|jdd|_dS)Nrrr) smartos_typerrr)r9rUNSETget_smartos_environr7jmc_client_factoryr&r3r4r4r5r2s   zDataSourceSmartOS._initcCsHtdd}tjd|dgrDtd|dgd|dgdS)aMark 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)LOGdebugr-r.existsr/rename)r3Zsvc_pathr4r4r5_set_provisioneds   z"DataSourceSmartOS._set_provisionedc Cs|i}d}|js$tddS|js@td|jdS|jtD]$\}}|\}}|jj ||d||<qRt D]\}}|j |||<q|j t j|jd|dd}t j|d }d t} t| d || d d d t j|d} t| d| dd d| d} dt} t| | |ds\|drP|d|d<n |d|d<d}|drr|d}|dst|| t j|jddd|d<t||jg|_||_|d|_|d|_|d|_|d S)NzNot running on smartosFz4No metadata device '%r' found for SmartOS datasourcestripZ instancesrdatar z%s/user-scriptTi) content_flinkshebangmoder)rHrJrKrz%s/mdata-user-datarr r rr"zper-boot) user_scriptoperator_scriptZ per_boot_drr)r2r9r?r@r7rAopen_transportSMARTOS_ATTRIB_MAPitemsgetSMARTOS_ATTRIB_JSONget_jsonclose_transportr-r.r/r0r1 LEGACY_USER_Dwrite_boot_contentBUILTIN_VENDOR_DATArr%r)Z userdata_rawZvendordata_rawr*r,rC) r3ZmdZudZci_nounZ attributeZ smartos_nounrFZdata_drLZ u_script_lrMZu_dataZu_data_fr4r4r5 _get_datas~                zDataSourceSmartOS._get_datacCsdtS)Nz serial (%s)) SERIAL_DEVICEr=r4r4r5_get_subplatformLsz"DataSourceSmartOS._get_subplatformcCs|jd|S)Nr)r&rQ)r3namer4r4r5device_name_to_deviceOsz'DataSourceSmartOS.device_name_to_devicecCs|jtkrtSiSN)r9SMARTOS_ENV_KVMBUILTIN_CLOUD_CONFIGr=r4r4r5get_config_objRs z DataSourceSmartOS.get_config_objcCs |jdS)Nr)r)r=r4r4r5get_instance_idWsz!DataSourceSmartOS.get_instance_idcCsN|jtjkrd|_|jdurH|jdurHt|j|jd|jd|jd|_|jS)Nrrr*rrr)r+rr:r*convert_smartos_network_datar)r,r=r4r4r5network_configZs   z DataSourceSmartOS.network_configN)__name__ __module__ __qualname__Zdsnamerr:r9r7rZNETWORKrZBOOT_NEW_INSTANCEZBOOTZ BOOT_LEGACYZdefault_update_eventsr$r6r2rCrXrZr\r`rapropertyrdr4r4r4r5r!s(cr!c@s eZdZdS)JoyentMetadataFetchExceptionNrerfrgr4r4r4r5riksric@s eZdZdS)JoyentMetadataTimeoutExceptionNrjr4r4r4r5rkosrkc@seZdZdZedZd#ddZddZdd Z d d Z d d Z ddZ d$ddZ d%ddZd&ddZddZddZddZddZdd 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;r9fp)r3r9rmr4r4r5r$szJoyentMetadataClient.__init__cCsdt|dd@S)N{0:08x}zutf-8)formatbinasciicrc32encode)r3bodyr4r4r5 _checksumszJoyentMetadataClient._checksumcCs|j|}t|dt|dkrDtd|dt|d||d}|d|krrtd||d|d|krtd||d|ddst d dSt |d}t d ||S) Nlengthrtz*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_regexmatch groupdictintlenrirprurQr?r@rZb64d)r3Zexpected_request_idframeZ frame_dataZexpected_checksumvaluer4r4r5_get_value_from_frames4     z*JoyentMetadataClient._get_value_from_framec sgfdd}d}zD|jd}t|dkr.as_asciizPartial response: '%s'r N)rmreadr|rkappendOSErrorerrnoZEAGAIN)r3rmsgZbyteexcr4rr5 _readlines$    zJoyentMetadataClient._readlinecCs |j|d|jdS)Nr)rmwritersflush)r3rr4r4r5_writeszJoyentMetadataClient._writecCsJtd|d|}td||dkr|dt|7}dt|| ||}t d|d}|j sx| d}|||}|r|t d |d |vrdS|||}|S) Nrnrro zV2 {0} {1} {2} z#Writing "%s" to metadata transport.FTz"Read "%s" from metadata transport.SUCCESS)rprandomZrandintr/base64 b64encodersrr|rur?r@rmrNrrrTr) r3rtypeparamrwZ message_bodyrZ need_closerr~r4r4r5requests2    zJoyentMetadataClient.requestFcCs.|jd|d}|dur|S|r*|r*|}|S)NZGETrr)rrF)r3keydefaultrFresultr4r4r5rQs zJoyentMetadataClient.getcCs$|j||d}|dur|St|S)N)r)rQjsonloads)r3rrrr4r4r5rSszJoyentMetadataClient.get_jsoncCs|jdd}|sgS|dS)NZKEYS)r )rsplit)r3rr4r4r5lists zJoyentMetadataClient.listcCs*ddd||fD}|jd|dS)N cSsg|]}t|qSr4)rrrs).0ir4r4r5 rz,JoyentMetadataClient.put..ZPUTr)r/rr)r3rvalrr4r4r5put szJoyentMetadataClient.putcCs|jr|jd|_dSr])rmcloser=r4r4r5rTs z$JoyentMetadataClient.close_transportcCs|jr |S||Sr])rmrNr=r4r4r5 __enter__szJoyentMetadataClient.__enter__cCs |dSr])rT)r3exc_type exc_value tracebackr4r4r5__exit__szJoyentMetadataClient.__exit__cCstdSr])NotImplementedErrorr=r4r4r5rNsz#JoyentMetadataClient.open_transport)NN)N)NF)N)rerfrg__doc__recompilerxr$rurrrrrrQrSrrrTrrrNr4r4r4r5rlss&  !  rlcs8eZdZeffdd ZddZddZddZZS) JoyentMetadataSocketClientcstt||||_dSr])superrr$ socketpath)r3rr9 __class__r4r5r$#sz#JoyentMetadataSocketClient.__init__cCs4ttjtj}||j|d|_|dS)NZrwb)socketZAF_UNIXZ SOCK_STREAMZconnectrZmakefilermr)r3Zsockr4r4r5rN's  z)JoyentMetadataSocketClient.open_transportcCstj|jSr])r-r.rArr=r4r4r5rA-sz!JoyentMetadataSocketClient.existscCsd|jj|jfS)Nz%s(socketpath=%s))rrerr=r4r4r5__repr__0sz#JoyentMetadataSocketClient.__repr__) rerfrgSMARTOS_ENV_LX_BRANDr$rNrAr __classcell__r4r4rr5r"srcsDeZdZdedffdd ZddZddZd d Zd d ZZ S) JoyentMetadataSerialClient Ncs"tt|||||_||_dSr])rrr$r timeout)r3r rr9rmrr4r5r$5sz#JoyentMetadataSerialClient.__init__cCstj|jSr])r-r.rAr r=r4r4r5rA<sz!JoyentMetadataSerialClient.existsc Cs|jdurz ddl}Wn.tyD}ztd|WYd}~n d}~00|j|j|jd}|sntd|j||_t |t j | | dS)Nrzserial support is not available)rzUnable to open %s)rmserial ImportErrorrZSerialr rZisOpen SystemErrorfcntllockfZLOCK_EX_flushr)r3reZserr4r4r5rN?s   z)JoyentMetadataSerialClient.open_transportcCstd|jj}d|j_z |Wqty<Yq@Yq0qtd|dkr\d|j_n||j_td|dz:|}|dkrWq|dkrtd Wqdtd |Wqdtytd Yqd0qdtd ||j_dS) NzFlushing inputg?z Input emptyz,Writing newline, expecting "invalid command"rzinvalid commandFAILUREzGot "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@rmrrrkrwarning)r3rrr4r4r5rOs6            z!JoyentMetadataSerialClient._flushcCsd|jj|j|jfS)Nz%s(device=%s, timeout=%s))rrer rr=r4r4r5rvs z#JoyentMetadataSerialClient.__repr__) rerfrgr^r$rArNrrrr4r4rr5r4s 'rcsNeZdZdZdfdd ZdddZdfd d Zdd d Zdd dZZ S) JoyentMetadataLegacySerialClientaV1 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.rNcs(tt|}||||d|_d|_dSr])rrr$rr)r3r rr9srr4r5r$s z)JoyentMetadataLegacySerialClient.__init__FcCs|rd|_d|_d}|jdurH|}d|vrBt|d|_nd|_|jrRdS|jdur|durl|}t}d|vrt|dd}dd|DD]2}t||r||q||vr| |q||_dS)NrFr,cSs"g|]}|dr|ddqS)zb64-N) startswith)rkr4r4r5rrzFJoyentMetadataLegacySerialClient._init_base64_keys..) rrrrZis_true_getsetraddremove)r3resetkeysZb64_keysrr4r4r5_init_base64_keyss.    z2JoyentMetadataLegacySerialClient._init_base64_keyscstt|j|||dS)N)rrF)rrrQ)r3rrrFrr4r5rs z%JoyentMetadataLegacySerialClient._getcCs,|tvr dS|j|d|jr"dS||jvS)NF)rT)NO_BASE64_DECODErrr)r3rrr4r4r5is_b64_encodeds  z/JoyentMetadataLegacySerialClient.is_b64_encodedcCsvt}|j|d|d}||ur"|S||rfzt|}Wn"tjydt d||Yn0|rr| }|S)NF)rFrz#Failed base64 decoding key '%s': %s) objectrrrZ b64decodersrrqErrorr?rrF)r3rrrFZmdefaultrr4r4r5rQs z$JoyentMetadataLegacySerialClient.get)rN)F)NF)F)NF) rerfrgrr$rrrrQrr4r4rr5r~s  " rcCsV|durt|}|durdS|tkr2t|||dS|tkrFt||dStd|dS)N)r rr9)rr9z"Unknown value for smartos_type: %s)r;r^rrr ValueError)r9rrr uname_versionr4r4r5r<sr<c Csddd|g}d}z t|\}}td||Wn:tjyj}z ttd||fWYd}~n d}~00|durxdS|S)Nfilez--briefz --mime-typezscript %s mime type is %sz%Failed to identify script type for %s)rr?r@ZProcessExecutionErrorrlogexcrF)rHcmdf_typeZ_errrr4r4r5 identify_files  rc 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[paramssubnetstype) address broadcastZdns_nameserversZ dns_searchZmetricZ pointopointrZscopernetworkZgateway)physicalsubnetroutecSsd|vS)N.r4addrr4r4r5 is_valid_ipv4sz3convert_smartos_network_data..is_valid_ipv4cSsd|vS)N:r4rr4r4r5 is_valid_ipv6sz3convert_smartos_network_data..is_valid_ipv6N)rygw)ipv4ipv6c3s&|]\}}|dvr||fVqdS)rNr4rrv valid_keysr4r5 sz/convert_smartos_network_data..rZ interface)rr[ZmacripsZdhcprZdhcp4ZaddrconfZdhcp6c3s&|]\}}|dvr||fVqdS)rNr4rrr4r5rsZstatic)rrrrZprimaryFrcs g|]}d|r|qS)ryr4)rr)pgwsprotor4r5rsz0convert_smartos_network_data..gatewaysrc3s&|]\}}|dvr||fVqdS)rNr4rrr4r5rs dstrrZ nameserver)rrsearchr)versionconfig) isinstancertupledictrPupdaterQr|r)r*rrrrrrZnicZcfgriprZ routeentsrrZrcfgr4)rrrr5rcGs               rccCs t|tSr])rZlist_from_depends datasources)Zdependsr4r4r5get_datasource_listsr__main__zDo not appear to be on smartos.rrdcCs||vr||S|tvr0t|}||||<nv|dkrtdD]}t|||qs     H0JY  3  #