x[hUddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZddlmZmZej&eZdddddd d d d d dddd ZddddZdZdZdZdegZgdZdZdZdZeeeegddd id!Z dd"ddd#idd$dd%gd&Z!d'Z"d(Z#Gd)d*e jHZ%Gd+d,e&Z'Gd-d.e'Z(Gd/d0Z)Gd1d2e)Z*Gd3d4e)Z+Gd5d6e+Z,deeedfd7Z-d8Z. dGd9Z/dHd:Z0 dId;Z1e%e jdffgZ3d<Z4ed=k(rddl5Z5e-Z6e6e7d>e5jpd?e9e5jtd?k(r2e;ejye;ejyzd@gzZe?e@dB<eCe7eje>d?dDdEFyy)JN) 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 rravailability_zone vendor-dataoperator-scriptr dns_domainzsdc:nicsz sdc:resolversz sdc:routes) network-data dns_serversrouteszlx-brandkvmSmartOS datasource)rrr rzsdc:uuiduser-datar z"/native/.zonecontrol/metadata.sockz /dev/ttyS1<F ephemeral0z/dev/vdb) serial_deviceserial_timeoutmetadata_sockfileno_base64_decode base64_keys base64_all disk_aliasesmbr) table_typelayout overwriteext4)label filesystemdevice) disk_setupfs_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/dbceZdZdZej Zej Zeje je je jhiZdZdZdZdZdZdZdZd Zd Zed Zy ) DataSourceSmartOSJoyentctjj||||tj|j tj |titg|_i|_ d|_ d|_ d|_ tjj|j j#d|_|j'y)Nscripts)r DataSource__init__r mergemanydictds_cfgget_cfg_by_path DS_CFG_PATHBUILTIN_DS_CONFIGmetadata network_data_network_config routes_dataospathjoinpaths get_cpath script_base_d_init)selfsys_cfgdistrorDs E/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceSmartOS.pyr7zDataSourceSmartOS.__init__s##D'65A(( $$Wk2>!     #WW\\$***>*>y*IJ cdtjj|}|d|jdS)Nz [client=])rr6__str__ md_client)rHroots rKrOzDataSourceSmartOS.__str__s'!!))$/#'88rLcH|jtjk(r"t|_|jd|_|jtjk(rFt |j|j d|j d|j d|_yy)Nr"r r!) smartos_typer"r r!)rSrUNSETget_smartos_environrPjmc_client_factoryr9rHs rKrGzDataSourceSmartOS._inits    - 3 5D   (!% >>W]] */!.."&++.A"B"kk/:#{{+;< DN +rLctjdd}tjj dj |dgr9tj dj |dgdj |dgyy)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/ provisioningprovision_successN)LOGdebugrArBexistsrCrename)rHsvc_paths rK_set_provisionedz"DataSourceSmartOS._set_provisionedsi AB 77>>#((Hn#=> ? II(N34($789  @rLc|ji}d}|jstjdy|jj s!tjd|jy|jj tjD]*\}}|\}}|jj||||<,tjD]#\}}|jj|||<%|jjtjj|j j#d|dd}tjj|d }d t$z} t'|jd || d d tjj|d} t'|jd| dd |jd} dt$z} t'| | |ds|dr |d|d<n|d|d<d}|dr|d}|dsGt(|| tjj|j j#dddz|d<t+j,||j.g|_||_|d|_|d|_|d|_|j9y )NzNot running on smartosFz4No metadata device '%r' found for SmartOS datasourcestrip instancesrdatar z%s/user-scriptTi) content_flinkshebangmoder)rhrjrkrz%s/mdata-user-datarr r rr5zper-boot) user_scriptoperator_script per_boot_drr)rGrSr\r]rPr^open_transportSMARTOS_ATTRIB_MAPitemsgetSMARTOS_ATTRIB_JSONget_jsonclose_transportrArBrCrDrE LEGACY_USER_Dwrite_boot_contentBUILTIN_VENDOR_DATArr8r= userdata_rawvendordata_rawr>r@ra) rHmdudci_noun attribute smartos_nounredata_drl u_script_lrmu_datau_data_fs rK _get_datazDataSourceSmartOS._get_datas    II. /~~$$& IIF  %%'"4":":"< H GY"+ L%..,,\,GBwK H&9%>%>%@ @ !G\..11,?BwK @ &&( JJ "KM1BF ggll6=9 % 5  FF= !!  '',,v/@A FF$ %%  *+'-768, "#*~')*~#$')-'8#$  k?KB- 3*#2 ggllJJ((3Z7!B} **B +>?  /~.h< rLcdtzS)Nz serial (%s)) SERIAL_DEVICErWs rK_get_subplatformz"DataSourceSmartOS._get_subplatformLs },,rLc>|jdj|S)Nr&)r9rr)rHnames rKdevice_name_to_devicez'DataSourceSmartOS.device_name_to_deviceOs{{>*..t44rLc8|jtk(rtSiSN)rSSMARTOS_ENV_KVMBUILTIN_CLOUD_CONFIGrWs rKget_config_objz DataSourceSmartOS.get_config_objRs    /' ' rLc |jdS)Nr)r=rWs rKget_instance_idz!DataSourceSmartOS.get_instance_idWs}}]++rLc|jtjk(rd|_|jN|jBt |j|j d|j d|j |_|jS)Nrrr>rrr)r?rrTr>convert_smartos_network_datar=r@rWs rKnetwork_configz DataSourceSmartOS.network_configZs{   7== 0#'D    '  ,'C!%!2!2 $ m <#}}\:++ ($ ###rLN)__name__ __module__ __qualname__dsnamerrTrSrPrNETWORKr BOOT_NEW_INSTANCEBOOT BOOT_LEGACYdefault_update_eventsr7rOrGrarrrrrpropertyrrLrKr2r2s F==L I  ' ' NN  ! ! &9 $aF-5 , $ $rLr2c eZdZy)JoyentMetadataFetchExceptionNrrrrrLrKrrkrLrc eZdZy)JoyentMetadataTimeoutExceptionNrrrLrKrrorrLrceZdZdZej dZddZdZdZ dZ dZ d Z dd Z dd Zdd Zd ZdZdZdZdZdZy)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.+))?)Nc8| t}||_||_yr)rUrSfp)rHrSrs rKr7zJoyentMetadataClient.__init__s  .0L(rLcndjtj|jddzS)N{0:08x}zutf-8)formatbinasciicrc32encode)rHbodys rK _checksumzJoyentMetadataClient._checksums/ NN4;;w/ 0: =  rLc R|jj|j}t|dt |dk7r*t dj |dt |d|j|d}|d|k7rt dj ||d|d|k7rt dj ||d|jddstjd ytj|d}tjd ||S) Nlengthrz*Incorrect frame length given ({0} != {1}).checksumz*Invalid checksum (expected: {0}; got {1}). request_idz-Request ID mismatch (expected: {0}; got {1}).payloadzNo value found.zValue "%s" found.) line_regexmatch groupdictintlenrrrrrr\r]rb64d)rHexpected_request_idframe frame_dataexpected_checksumvalues rK_get_value_from_framez*JoyentMetadataClient._get_value_from_frames1__**51;;= z(# $Jv,>(? ?.<CCx(#j.@*A  !NN:f+=> j !%6 6.<CC%z*'=  l #': :.?FF'L)A  ~~i. II' ("":i#89 %u- rLc<gfd}d} |jjd}|st||z|dk(r|Sj|O#t$r7}|j t j k(rt||z|d}~wwxYw)z Reads a line a byte at a time until \n is encountered. Returns an ascii string with the trailing newline removed. If a timeout (per-byte) is set and it expires, a JoyentMetadataFetchException will be thrown. cDdjjdS)NrLascii)rCdecode)responsesrKas_asciiz0JoyentMetadataClient._readline..as_asciis88H%,,W5 5rLzPartial response: '%s' N)rreadrappendOSErrorerrnoEAGAIN)rHrmsgbyteexcrs @rK _readlinezJoyentMetadataClient._readlines 6' ww||A8xz9IJJ5=#:%% 99 ,8hj(  s;A A B$2BBc|jj|jd|jjy)Nr)rwriterflush)rHrs rK_writezJoyentMetadataClient._writes(  cjj)*  rLctjd|jd|j}tjd||dk7rt d|ztjdy)NzNegotiating protocol V2z NEGOTIATE V2 z read "%s"V2_OKz'Invalid response "%s" to "NEGOTIATE V2"zNegotiation complete)r\r]rrr)rHrs rK _negotiatezJoyentMetadataClient._negotiatesb +, $%>># +x( w .9HD  ()rLcjdjtjdd}dj||f}|r7|dt j |j jzz }djt||j||}tjd|d}|js|jd}|j||j}|r|j!tjd |d |vry|j#||}|S) Nrrr zV2 {0} {1} {2} z#Writing "%s" to metadata transport.FTz"Read "%s" from metadata transport.SUCCESS)rrandomrandintrCbase64 b64encoderrrrr\r]rrorrrur) rHrtypeparamr message_bodyr need_closerrs rKrequestzJoyentMetadataClient.requests%%fnnQ &CD xx   C&"2"25<<>"B"I"I"KK KL ''  t~~l;\  7= ww    !J C>>#   " 6A H $**:x@ rLc\|jd|}||S|r|r|j}|S)NGETrr)rre)rHkeydefaultreresults rKrrzJoyentMetadataClient.gets3E5 >N e\\^F rLcZ|j||}||Stj|S)N)r)rrjsonloads)rHrrrs rKrtzJoyentMetadataClient.get_jsons-#w/ >Nzz&!!rLcP|jd}|sgS|jdS)NKEYS)r )rsplit)rHrs rKlistzJoyentMetadataClient.lists)F+I||D!!rLc dj||fDcgc]%}tj|j'c}j }|j d|Scc}w)N PUTr)rCrrrrr)rHrvalirs rKputzJoyentMetadataClient.put sW 47: >aV  ahhj ) > &( ||%u|55 ?s*A$c`|jr"|jjd|_yyr)rcloserWs rKruz$JoyentMetadataClient.close_transports" 77 GGMMODG rLcB|jr|S|j|Sr)rrorWs rK __enter__zJoyentMetadataClient.__enter__s 77K  rLc$|jyr)ru)rHexc_type exc_value tracebacks rK__exit__zJoyentMetadataClient.__exit__s rLctr)NotImplementedErrorrWs rKroz#JoyentMetadataClient.open_transports!!rLNNrNF)rrr__doc__recompilerr7rrrrrrrrrtrrrurrrorrLrKrrssj J   8: *B" " 6   "rLrc4eZdZeffd ZdZdZdZxZS)JoyentMetadataSocketClientc:tt| |||_yr)superrr7 socketpath)rHrrS __class__s rKr7z#JoyentMetadataSocketClient.__init__#s ($8F$rLctjtjtj}|j|j|j d|_|jy)Nrwb)socketAF_UNIX SOCK_STREAMconnectrmakefilerr)rHsocks rKroz)JoyentMetadataSocketClient.open_transport'sF}}V^^V-?-?@ T__%--& rLcTtjj|jSr)rArBr^rrWs rKr^z!JoyentMetadataSocketClient.exists-sww~~doo..rLcN|jjd|jdS)Nz (socketpath=))rrrrWs rK__repr__z#JoyentMetadataSocketClient.__repr__0s&*nn&=&=tOOrL) rrrSMARTOS_ENV_LX_BRANDr7ror^r __classcell__rs@rKrr"s0D% /PrLrc>eZdZdedffd ZdZdZdZdZxZ S)JoyentMetadataSerialClient NcJtt| ||||_||_yr)rr#r7r.timeout)rHr.r&rSrrs rKr7z#JoyentMetadataSerialClient.__init__5s% ($8rJ  rLcTtjj|jSr)rArBr^r.rWs rKr^z!JoyentMetadataSerialClient.exists<sww~~dkk**rLc|j ddl}|j |j |j }|jstd|j z||_tj|tj|j|jy#t$r}td|d}~wwxYw)Nrzserial support is not available)r&zUnable to open %s)rserial ImportErrorrSerialr.r&isOpen SystemErrorfcntllockfLOCK_EX_flushr)rHr)esers rKroz)JoyentMetadataSerialClient.open_transport?s 77?  -- T\\-BC::<!"5 "CDDDG KKU]] +   )5 sB-- C6 CCctjd|jj}d|j_ |j #t $rYnwxYwtjd|dkDrd|j_n||j_ tjd|j d |j }|dk(rnW|dk(rtjd Ytjd |n$#t $rtjd YnwxYwtjd ||j_y) 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]rr&rrrwarning)rHr&rs rKr1z!JoyentMetadataSerialClient._flushOs "#''//  2   - Q;DGGO%DGGO IID E KK  >>+00y(II9: CXN1  <  ;<!s/A AAD D 5D D-,D-ch|jjd|jd|jdS)Nz(device=z , timeout=r)rrr.r&rWs rKrz#JoyentMetadataSerialClient.__repr__vs' NN # # KK LL  rL) rrrrr7r^ror1rr r!s@rKr#r#4s$ 4+ %"N rLr#cHeZdZdZdfd ZddZd fd ZddZd dZxZ 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.chtt|}|j|||d|_d|_yr)rr:r7r$r%)rHr.r&rSsrs rKr7z)JoyentMetadataLegacySerialClient.__init__s/ 2D 9 67L1rLc|rd|_d|_d}|jE|j}d|vr*tj|j d|_nd|_|jry|j||j}t }d|vr)t |j djd}|Dcgc]}|jds|ddc}D]N}tj|j |r|j|9||vs>|j|P||_yycc}w)Nr%Fr$,zb64-) r$r%rris_true_getsetr startswithaddremove)rHresetkeysb64_keyskrs rK_init_base64_keysz2JoyentMetadataLegacySerialClient._init_base64_keyss #D "DO ?? "99;Dt#"&,,tyy/F"G"' ??     #|yy{uH$tyy7==cBC(,D!q||F/C!"D -<< #/LL%h ,  - (D  $Es E!Ec0tt| |||S)N)rre)rr:rr)rHrrrers rKrAz%JoyentMetadataLegacySerialClient._gets%5t@ A  rLcn|tvry|j||jry||jvS)NF)rFT)NO_BASE64_DECODErJr%r$)rHrrFs rKis_b64_encodedz/JoyentMetadataLegacySerialClient.is_b64_encodeds: " " U+ ??d&&&&rLcXt}|j|d|}||ur|S|j|r2 tj|j j }|r|j}|S#tj$rtjd||Y@wxYw)NF)rerz#Failed base64 decoding key '%s': %s) objectrArNr b64decoderrrErrorr\r7re)rHrrremdefaultrs rKrrz$JoyentMetadataLegacySerialClient.gets8ii5(i; (?N   s # M&&szz|4;;= ))+C >> M A3L Ms1A<<*B)(B))r$N)Fr ) rrrr r7rJrArNrrr r!s@rKr:r:~s" $ (D 'rLr:c| t|}|y|tk(rt|||S|tk(r t ||St d|z)N)r.r&rS)rrSz"Unknown value for smartos_type: %s)rUrr:rr ValueError)rSr"r r! uname_versions rKrVrVsk*=9   (/ "%  - -)(|   9LH IIrLcddd|g}d} tj|\}}tjd|||dS|j S#tj$r)}t j td|z|fYd}~Kd}~wwxYw)Nfilez--briefz --mime-typezscript %s mime type is %sz%Failed to identify script type for %s)rr\r]ProcessExecutionErrorrlogexcre)rhcmdf_type_errr2s rK identify_filer^s 9mY 7C F 3 -y&A >45v||~5  % %  9IEqI   s/A B  BB c~|s4tjj|rtj||r4tjj |rtj||syt j ||||r`|jdsOt|}|dk(r?t j |djd|g|tjd||r tjj |rtj||ritjj|rIt jtjj|tj||yyyy#t$r%}t j td|Yd}~yd}~wwxYw) a  Write the content to content_f. Under the following rules: 1. If no content, remove the file 2. Write the content 3. If executable and no file magic, add it 4. If there is a link, create it @param content: what to write @param content_f: the file name @param backup_d: the directory to save the backup at @param link: if defined, location to create a symlink to @param shebang: if no file magic, set shebang @param mode: file mode Because of the way that Cloud-init executes scripts (no shell), a script will fail to execute if does not have a magic bit (shebang) set for the file. If shebang=True, then the script will be checked for a magic bit and to the SmartOS default of assuming that bash. N)rkz#!z text/plainrz #!/bin/bashzadded shebang to file %sz$failed establishing content link: %s)rArBr^unlinkislinkr write_filerCr^rCr\r] ensure_dirdirnamesymlinkIOErrorrZ)contentrhrirjrkr\r2s rKrwrws:. rww~~i0 ) t$ $ OOIwT2w))$/y) \ ! OO499mW%=>T  II0) <  Hww~~d# $277>>)4 56 9d+5w   H KKCQ G G Hs-BF F<F77F<ctj}||d}|dk(rtS|tjd}n|}|r|j drt Sy)Nr?zBrandZ virtual linuxzsystem-product-nameSmartDC)rAunamerr read_dmi_datarCr)rV product_namerj system_types rKrUrU1sb HHJEa ..##''(=> " {--i8 rLcgdgdddgd|rt|ttfs|g}ng}|rt|ttfs|g}ng}|sg}d}d}|dd |dd d }g}|D]}tfd |j D} | j d |d dd|vr| j d|dig} |j dgD]K} | dk(rddi} n,| dk(rddi} n!g} tfd|j D} | j d| d|| rdnd}d|vrv|j ddrd||ds\|j dgDcgc]}||d |r|}}t|r$|d!||d<| j d||di|D]^}tfd"|j D}|j d|d#i| j|| j d$| i`| j| N| j d%| i|j| |r|jd&||d'd(|d)Scc}w)*aReturn a dictionary of network_config by parsing provided SMARTOS sdc:nics configuration data sdc:nics data is a dictionary of properties of a nic and the ip configuration desired. Additional nic dictionaries are appended to the list. Converting the format is straightforward though it does include duplicate information as well as data which appears to be relevant to the hostOS rather than the guest. For each entry in the nics list returned from query sdc:nics, we create a type: physical entry, and extract the interface properties: 'mac' -> '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_addressmturparamssubnetstype) address broadcastdns_nameservers dns_searchmetric pointopointrscopersnetworkgateway)physicalsubnetroutec d|vS)N.raddrs rK is_valid_ipv4z3convert_smartos_network_data..is_valid_ipv4 d{rLc d|vS)N:rrs rK is_valid_ipv6z3convert_smartos_network_data..is_valid_ipv6rrLN)rgw)ipv4ipv6c3<K|]\}}|dvs||fyw)r}Nr.0rIv valid_keyss rK z/convert_smartos_network_data..s* q!a:j3I.IQF  r} interface)rsrmacroipsdhcprsdhcp4addrconfdhcp6c3<K|]\}}|dvs||fyw)r~Nrrs rKrz/convert_smartos_network_data..s*#q!a:h;O6OQFrstatic)rsrtrrprimaryFrgatewaysrrc3:K|]\}}|dvr||fyw)rNrrs rKrz/convert_smartos_network_data..s.  Aq 7 33A sdstrrr nameserver)rsrtsearchr)versionconfig) isinstancertupledictrqupdaterrrr)r>rrrrrpgwsrniccfgrripr~ routeentsprotorrrrcfgrs @rKrrGs6      )J4+e}5&-K *tUm4$J (t4't4 D FC "yy{   JK0@AB C< JJ s5z2 3''%$8 #BV| '*z! '* '*yy{ (#%#0"3# 5(A  ;t,'*ggj"&=$ "3tE{73B7$$ x=08 DK-"MM9d5k$6G*HI#9E $)KKM D"KKE%L 9:$$T*MM8Y"78)9, NN6 "q8 #r Iw'( cGCJ $&$  F ++Y$sI c6tj|tSr)rlist_from_depends datasources)dependss rKget_datasource_listrs  $ $Wk ::rL__main__zDo not appear to be on smartos.rrcH||vr||S|tvr"t|}|j|||<||S|dk(r7dD]}t|||t|d|d|d|d||<||S|tvr t|\}}n|d}}|j || ||<||S) Nr)rrrrrrrrrFrd)rsrtload_keyrrprr)clientrrgkeynamedepkeyres rKrrs $;9  % %)#.G0DI,Cy+$ $ / .  /5!.1 / -H~ DICy ((!3C!8"%u 7% 8DICyrLrg)rrrgT)r>z: )indent sort_keys separators)NFr)NNNN)Crrrr.rloggingrArr r cloudinitrrrrrcloudinit.eventrr getLoggerrr\rprsrrDS_NAMEr;rMMETADATA_SOCKFILErSERIAL_TIMEOUTr<rrxrvr6r2 Exceptionrrrrr#r:rVr^rwrUrDEP_FILESYSTEMrrsysjmcprintexitrargvrrGrrgr__annotations__rdumpsrrLrKrs.  ==1g!&(1),02,6-5&*&" " W% 9 "#$*(!:.  f M 88 D$**D$N 9  %A l"l"^P!5P$G !5G TU'AUr'! J6 68=0Hf.BF],D//12 ; z  C { /0  388} $))+ ,%**,- .  ! xx|<D$O1401 *$**T!t LMerL