܍~c]UddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZmZmZddlmZmZe jeZdddd d d d d dddddd ZddddZdZdZdZdegZgdZdZdZdZ ee eegd d!d"id#Z!d!d$d d d%id!d&d!d'gd(Z"d)Z#d*Z$Gd+d,ej%Z&Gd-d.e'Z(Gd/d0e(Z)Gd1d2Z*Gd3d4e*Z+Gd5d6e*Z,Gd7d8e,Z-deee dfd9Z.d:Z/ dJd<Z0dKd=Z1 dLd>Z2e&ej3ffgZ4d?Z5ed@krddl6Z6e.Z7e7e8dAe6j9dBe:e6j;dBkr@eiZ?e@eAdE<e=D]ZBe>e7eBe?Fe8ejCe?dBdGdHIdSdS)MN)dmi)log)sourcessubputil) 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/dbceZdZdZejZejZej e j e j e j hiZdZdZdZdZdZdZdZd Zd Zed Zd S) DataSourceSmartOSJoyentctj||||tj|jtj|titg|_i|_ d|_ d|_ tj |jd|_|dS)Nscripts)r DataSource__init__r mergemanydictds_cfgget_cfg_by_path DS_CFG_PATHBUILTIN_DS_CONFIGmetadata network_data_network_configospathjoinpaths get_cpath script_base_d_init)selfsys_cfgdistrorCs E/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceSmartOS.pyr7zDataSourceSmartOS.__init__s##D'65AAA( $Wk2>>!      #W\\$**>*>y*I*IJJ cZtj|}|d|jdS)Nz [client=])rr6__str__ md_client)rGroots rJrNzDataSourceSmartOS.__str__s-!))$//#'4488rKc|jtjkr!t|_|jd|_|jtjkr@t |j|jd|jd|jd|_dSdS)Nr"r r!) smartos_typer"r r!)rRrUNSETget_smartos_environrOjmc_client_factoryr9rGs rJrFzDataSourceSmartOS._inits   - - 3 5 5D  (!% >W] * */!."&+.A"B"k/:#{+;< DNNN + *rKc(tdd}tjd|dgrAtjd|dgd|dgdSdS)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)LOGdebugr@rAexistsrBrename)rGsvc_paths rJ_set_provisionedz"DataSourceSmartOS._set_provisioneds ABBB 7>>#((Hn#=>> ? ?  I(N344($7899       rKc2|i}d}|jstddS|js"td|jdS|jtD])\}}|\}}|j ||||<*tD]"\}}|j |||<#|j tj|jd|dd}tj|d }d t$z} t'| d || d d tj|d} t'| d| dd | d} dt$z} t'| | |ds|dr |d|d<n |d|d<d}|dr|d}|dsGt(|| tj|jdddz|d<t+j||jg|_||_|d|_|d|_|d|_|d S)NzNot running on smartosFz4No metadata device '%r' found for SmartOS datasourcestrip instancesrdatar z%s/user-scriptTi) content_flinkshebangmoder)rgrirjrz%s/mdata-user-datarr r rr5zper-boot) user_scriptoperator_script per_boot_drr)rFrRr[r\rOr]open_transportSMARTOS_ATTRIB_MAPitemsgetSMARTOS_ATTRIB_JSONget_jsonclose_transportr@rArBrCrD LEGACY_USER_Dwrite_boot_contentBUILTIN_VENDOR_DATArr8r= userdata_rawvendordata_rawr> routes_datar`) rGmdudci_noun attribute smartos_nounrddata_drk u_script_lrlu_datau_data_fs rJ _get_datazDataSourceSmartOS._get_datas2     II. / / /5~$$&&  IIF   5 %%'''"4":":"<"< H H GY"+ L%.,,\,GGBwKK%8%>%>%@%@ @ @ !G\.11,??BwKK &&((( J " "KM1BF  gll6=99 % 5  FF= ! !!     ',,v/@AA FF$ % %%     *++'-768,,, "# 9*~ 9')*~#$$')-'8#$  k? !KB-   3*#2 gllJ((33Z77!B} *B +>??  /~.h< trKcdtzS)Nz serial (%s)) SERIAL_DEVICErVs rJ_get_subplatformz"DataSourceSmartOS._get_subplatformNs },,rKcB|jd|S)Nr&)r9rq)rGnames rJdevice_name_to_devicez'DataSourceSmartOS.device_name_to_deviceQs{>*..t444rKc4|jtkrtSiSN)rRSMARTOS_ENV_KVMBUILTIN_CLOUD_CONFIGrVs rJget_config_objz DataSourceSmartOS.get_config_objTs   / /' ' rKc|jdS)Nr)r=rVs rJget_instance_idz!DataSourceSmartOS.get_instance_idYs}]++rKc|jtjkrd|_|j?|j8t |j|jd|jd|j|_|jS)Nrrr>rrr)r?rrSr>convert_smartos_network_datar=rzrVs rJnetwork_configz DataSourceSmartOS.network_config\sp  7= 0 0#'D   ' ,'C!%!2 $ m <#}\:+ ((($ ##rKN)__name__ __module__ __qualname__dsnamerrSrRrOrNETWORKr BOOT_NEW_INSTANCEBOOT BOOT_LEGACYdefault_update_eventsr7rNrFr`rrrrrpropertyrrKrJr2r2s F=L I  ' N  ! $999   $aaaF---555 ,,, $ $X $ $ $rKr2ceZdZdS)JoyentMetadataFetchExceptionNrrrrrKrJrrmDrKrceZdZdS)JoyentMetadataTimeoutExceptionNrrrKrJrrqrrKrceZdZdZejdZddZdZdZ dZ dZ d Z dd Z dd Zdd ZdZdZdZdZdZdZdZdS)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.+))?)NcB|t}||_||_dSr)rTrRfp)rGrRrs rJr7zJoyentMetadataClient.__init__s&  .00L(rKc|dtj|ddzS)N{0:08x}zutf-8)formatbinasciicrc32encode)rGbodys rJ _checksumzJoyentMetadataClient._checksums8 N4;;w// 0 0: =   rKc |j|}t|dt |dkr(?(? ? ?.<CCx(#j.@*A*A  !NN:f+=>> j !%6 6 6.<CC%z*'=  l #': : :.?FF'L)A  ~~i..  II' ( ( (4 *Y/00 %u--- rKc|gfd}d} |jd}t|dkrt||z|dkr |S|nC#t $r6}|jt jkrt||z|d}~wwxYw)z Reads a line a byte at a time until 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. cTddS)NrKascii)rBdecode)responsesrJas_asciiz0JoyentMetadataClient._readline..as_asciis#88H%%,,W55 5rKzPartial response: '%s'Tr N)rreadrrappendOSErrorerrnoEAGAIN)rGrmsgbyteexcrs @rJ _readlinezJoyentMetadataClient._readlines 6 6 6 6 6'  w||At99>>8xxzz9IJJJ5==#8::%%%%%   9 ,,8hhjj(   sAA:$A:: B:1B55B:c|j|d|jdS)Nr)rwriterflush)rGrs rJ_writezJoyentMetadataClient._writes5  cjj))***  rKc&td|d|}td||dkrt d|ztddS)NzNegotiating protocol V2z NEGOTIATE V2 z read "%s"V2_OKz'Invalid response "%s" to "NEGOTIATE V2"zNegotiation complete)r[r\rrr)rGrs rJ _negotiatezJoyentMetadataClient._negotiates +,,, $%%%>>## +x((( w  .9HD  ()))))rKcdtjdd}d||f}|r>|dt j|zz }dt|| ||}t d|d}|j s| d}|||}|r|t d |d |vrdS|||}|S) Nrrr zV2 {0} {1} {2} z#Writing "%s" to metadata transport.FTz"Read "%s" from metadata transport.SUCCESS)rrandomrandintrBbase64 b64encoderrrrr[r\rrnrrrtr) rGrtypeparamr message_bodyr need_closerrs rJrequestzJoyentMetadataClient.requests_%%fnQ &C&CDD xx    L C&"25<<>>"B"B"I"I"K"KK KL ''   t~~l;;\   7=== w     ! ! !J C>>##  #  " " " 6AAA H $ $4**:x@@ rKFcl|d|}||S|r|r|}|S)NGETrr)rrd)rGkeydefaultrdresults rJrqzJoyentMetadataClient.getsBE55 >N  $e $\\^^F rKc`|||}||Stj|S)N)r)rqjsonloads)rGrrrs rJrszJoyentMetadataClient.get_jsons1#w// >Nz&!!!rKc`|d}|sgS|dS)NKEYS)r )rsplit)rGrs rJlistzJoyentMetadataClient.lists4F++ I||D!!!rKcdd||fD}|d|S)N cZg|](}tj|)Sr)rrr).0is rJ z,JoyentMetadataClient.put.. s+ > > >aV ahhjj ) ) > > >rKPUTr)rBrr)rGrvalrs rJputzJoyentMetadataClient.put sN > >C: > > >  &(( ||%u|555rKc0|d|S)NDELETEr)r)rGrs rJdeletezJoyentMetadataClient.deletes||(#|666rKcX|jr"|jd|_dSdSr)rcloserVs rJrtz$JoyentMetadataClient.close_transports0 7  GMMOOODGGG  rKc@|jr|S||Sr)rrnrVs rJ __enter__zJoyentMetadataClient.__enter__s( 7 K  rKc.|dSr)rt)rGexc_type exc_value tracebacks rJ__exit__zJoyentMetadataClient.__exit__s rKctr)NotImplementedErrorrVs rJrnz#JoyentMetadataClient.open_transport#s!!rKNNrNF)rrr__doc__recompilerr7rrrrrrrqrsrrrrtrr rnrrKrJrrus= J     8: * * *B"""" """ 666 777  """""rKrc4eZdZeffd ZdZdZdZxZS)JoyentMetadataSocketClientcftt||||_dSr)superrr7 socketpath)rGrrR __class__s rJr7z#JoyentMetadataSocketClient.__init__(s- ($//88FFF$rKctjtjtj}||j|d|_|dS)Nrwb)socketAF_UNIX SOCK_STREAMconnectrmakefilerr)rGsocks rJrnz)JoyentMetadataSocketClient.open_transport,sU}V^V-?@@ T_%%%--&& rKcJtj|jSr)r@rAr]rrVs rJr]z!JoyentMetadataSocketClient.exists2sw~~do...rKc0|jjd|jdS)Nz (socketpath=))rrrrVs rJ__repr__z#JoyentMetadataSocketClient.__repr__5s&*n&=&=&=tOOrK) rrrSMARTOS_ENV_LX_BRANDr7rnr]r$ __classcell__rs@rJrr'st0D%%%%%% ///PPPPPPPrKrc>eZdZdedffd ZdZdZdZdZxZ S)JoyentMetadataSerialClient Ncvtt|||||_||_dSr)rr)r7r.timeout)rGr.r,rRrrs rJr7z#JoyentMetadataSerialClient.__init__:s8 ($//88rJJJ  rKcJtj|jSr)r@rAr]r.rVs rJr]z!JoyentMetadataSerialClient.existsAsw~~dk***rKcF|jqtj|j|j}|st d|jz||_tj|tj | | dS)N)r,zUnable to open %s) rserialSerialr.r,isOpen SystemErrorfcntllockfLOCK_EX_flushr)rGsers rJrnz)JoyentMetadataSerialClient.open_transportDs 7?- T\BBBC::<< E!"5 "CDDDDG KU] + + +  rKctd|jj}d|j_ |n#t $rYnwxYw'td|dkr d|j_n ||j_ td|d |}|dkrnh|d krtd mtd |n*#t $rtd YnwxYwtd ||j_dS)NzFlushing inputg?Tz 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)rGr,rs rJr6z!JoyentMetadataSerialClient._flushNs "###'/      1      -   Q;;DGOO%DGO  IID E E E KK    >>++000y((II9::: CXNNNN1    <   ;<<<!s/A AAD DD$EEc@|jjd|jd|jdS)Nz(device=z , timeout=r#)rrr.r,rVs rJr$z#JoyentMetadataSerialClient.__repr__us, N # # # KKK LLL  rK) rrrrr7r]rnr6r$r&r's@rJr)r)9s 4+++%"%"%"N       rKr)cHeZdZdZd fd Zd dZd fd Zd dZd d ZxZ 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.r*Nc|tt|}||||d|_d|_dSr)rr>r7r$r%)rGr.r,rRsrs rJr7z)JoyentMetadataLegacySerialClient.__init__s< 2D 9 9 67L111rKFc|rd|_d|_d}|jL|}d|vr-tj|d|_nd|_|jrdS|j||}t }d|vr5t |dd}d|DD]X}tj||r||?||vr| |Y||_dSdS)Nr%Fr$,cLg|]!}|d|dd"S)zb64-N) startswith)rks rJrzFJoyentMetadataLegacySerialClient._init_base64_keys..s1DDD!q||F/C/CD!""DDDrK) r$r%rris_true_getsetraddremove)rGresetkeysb64_keysrs rJ_init_base64_keysz2JoyentMetadataLegacySerialClient._init_base64_keyssL  ##D "DO ? "99;;Dt##"&,tyy/F/F"G"G"' ?  F   #|yy{{uuH$$tyy77==cBBCCEDtDDD - -< #//-LL%%%%h ,,,'D    $ #rKcZtt||||S)N)rrd)rr>rq)rGrrrdrs rJrHz%JoyentMetadataLegacySerialClient._gets35t<<@@ A   rKch|tvrdS|||jrdS||jvS)NF)rLT)NO_BASE64_DECODErOr%r$)rGrrLs rJis_b64_encodedz/JoyentMetadataLegacySerialClient.is_b64_encodedsG " " "5 U+++ ? 4d&&&rKct}||d|}||ur|S||rk tj|}n1#tj$rt d||YnwxYw|r| }|S)NF)rdrz#Failed base64 decoding key '%s': %s) objectrHrSr b64decoderrrErrorr[r;rd)rGrrrdmdefaultrs rJrqz$JoyentMetadataLegacySerialClient.gets88ii5(i;; (??N   s # # M M&szz||44;;==> M M M A3LLLLL M  ))++C s8A<<+B*)B*)r*N)Fr) rrrrr7rOrHrSrqr&r's@rJr>r>}s * * ( ( ( (D      ''''rKr>c|t|}|dS|tkrt|||S|tkrt ||St d|z)N)r.r,rR)rrRz"Unknown value for smartos_type: %s)rTrr>r%r ValueError)rRr"r r! uname_versions rJrUrUs*=99 t  ( (/ "%    - - -)(|     9LH I IIrKc"ddd|g}d} tj|\}}td||n;#tj$r)}t jtd|z|fYd}~nd}~wwxYw|dn|S)Nfilez--briefz --mime-typezscript %s mime type is %sz%Failed to identify script type for %s)rr[r\ProcessExecutionErrorrlogexcrd)rgcmdf_type_erres rJ identify_filerds 9mY 7C F 3 -y&AAAA  %    9IEqI         >44v||~~5s3>A6 A11A6c|s3tj|rtj||r3tj|rtj||sdSt j||||rq|ds\t|}|dkrGt j|d d|g|t d||r tj|rtj||rgtj|rJt j tj |tj||dSdSdS#t$r&}t jtd|Yd}~dSd}~wwxYwdS) 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 Becuase 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)rjz#!z text/plainrz #!/bin/bashzadded shebang to file %sz$failed establishing content link: %s)r@rAr]unlinkislinkr write_filerErdrBr[r\ ensure_dirdirnamesymlinkIOErrorr_)contentrgrhrirjrarcs rJrvrvs. rw~~i00 ) t$$ $ OIwT2222=w))$//=y)) \ ! ! O499mW%=>>T     II0) < < < H Hw~~d##  $ ,27>>)44 , 5 5666 9d+++++ , , , , H H H KCQ G G G G G G G G G HHHs> " {--i88 4rKcgdgdddgd|r t|ttfs|g}ng}|r t|ttfs|g}ng}|sg}d}d}|dd |dd d g}|D]8}tfd |D}|d |d dd|vr|d|dig} |dgD]} | dkrddi} n_g} tfd|D} | d| d|| rdndd|vr|ddrtdsffd|dgD} t| r4| dd<| ddi|D]x}tfd |D}|d|d!i| || d"| iy| | |d#| i||:|r|d$||d%d&|d'S)(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 pointopointrscoperznetworkgateway)physicalsubnetroutec d|vS)N.raddrs rJ is_valid_ipv4z3convert_smartos_network_data..is_valid_ipv4 d{rKc d|vS)N:rrs rJ is_valid_ipv6z3convert_smartos_network_data..is_valid_ipv6rrKN)rgw)ipv4ipv6c3:K|]\}}|dv||fVdS)rNrrrFv valid_keyss rJ z/convert_smartos_network_data..sC  q!a:j3I.I.IQF.I.I.I.I  rKr interface)rzrmacrvipsdhcprzdhcp4c3:K|]\}}|dv||fVdS)rNrrs rJrz/convert_smartos_network_data..sC#q!a:h;O6O6OQF6O6O6O6OrKstatic)rzr{rrprimaryFrcBg|]}d||S)rr)rrpgwsprotos rJrz0convert_smartos_network_data..sB$$$ "3tE{73B77$$$$rKgatewaysrc3:K|]\}}|dv||fVdS)rNrrs rJrz/convert_smartos_network_data..sG   Aq 7 333A3333  rKdstrry nameserver)rzr{searchr)versionconfig) isinstancertupledictrpupdaterqrr)r>rrrrrrniccfgryipr routeentsrrrcfgrrrs @@@rJrrFs6          )J4+e}55 (&-K *tUm44 &$J (t44't44  D FAA    "yy{{      JK0@AABBB C<< JJ s5z2 3 3 3''%$$6 #6 #BV|| '* '*yy{{ (#%#0-"3"3?## 5(A(A#  ;t,J$$$$$&)ggj"&=&=$$$ x==J08 DK-"MM9d5k$6G*HIII#99E    $)KKMM   D"KKE%L 9:::$$T***MM8Y"78888 NN6 " " " " Iw'((( c  $&$     F + ++rKc6tj|tSr)rlist_from_depends datasources)dependss rJget_datasource_listrs  $Wk : ::rK__main__zDo not appear to be on smartos.rrc||vr||S|tvr&t|}||||<n|dkrEdD]}t|||t|d|d|d|d||<n8|tvrt|\}}n|d}}||| ||<||S) Nr)rrrrrrrrrFrc)rrrsload_keyrrorq)clientrrfkeynamedepkeyrds rJrrs $;;9  % % %)#.G00DII $ $ $ / / ....4!.1 / -H~ DII(((!3C!8"%u 7% 88DICyrKrf)rrrfT)rBz: )indent sort_keys separators)NFrer)NNNN)Drrrr3rr@rrrr/ cloudinitrrloggingrrrcloudinit.eventrr getLoggerrr[rorrr%rDS_NAMEr;rRMETADATA_SOCKFILErSERIAL_TIMEOUTr<rrwrur6r2 Exceptionrrrrr)r>rUrdrvrTrDEP_FILESYSTEMrrsysjmcprintexitrargvrrMrrfr__annotations__rdumpsrrKrJrs.  $$$$$$))))))))))11111111g!!&(1),02,6-5&*&" " W% 9 "#$*(!:.   f MM  88 C$C$C$C$C$*C$C$C$L     9        %A   o"o"o"o"o"o"o"o"dPPPPP!5PPP$A A A A A !5A A A HUUUUU'AUUUr'! JJJJ6 6 6 68=0H0H0H0Hf.BF[,[,[,[,@/12 ;;; zJJJ    C { /000  s38}} D$))++ , ,d%**,,-- .  !  x|<D$OOO11400000 E*$*T!t L L LMMMMMerK