
    x[h                        % S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
JrJrJrJrJr  S SKJrJr  \R&                  " \5      rSSSSSS	S
SSSSSSS.rSSSS.rSrSrSrS\/r/ SQrSrSrSr\\\\/ SSS 0S!.r SS"SSS#.0SS$SS%./S&.r!S'r"S(r# " S) S*\RH                  5      r% " S+ S,\&5      r' " S- S.\'5      r( " S/ S05      r) " S1 S2\)5      r* " S3 S4\)5      r+ " S5 S6\+5      r,S\\\S4S7 jr-S8 r. SGS9 jr/SHS: jr0 SIS; jr1\%\Rd                  44/r3S< r4\S=:X  a  S SK5r5\-" 5       r6\6c  \7" S>5        \5Rp                  " S?5        \9" \5Rt                  5      S?:X  a2  \;" \Ry                  5       5      \;" \Ry                  5       5      -   S@/-   r<O\5Rt                  S?S r<SA r=0 r>\?\@SB'   \< H  rA\=" \6\A\>SC9  M     \7" \R                  " \>S?SDSESF95        gg)J    N)atomic_helperdmisourcessubputil)
EventScope	EventType)zsdc:uuidT)hostnameT)root_authorized_keysT)user-scriptF)	user-dataF)zcloud-init:user-dataF)iptables_disableT)motd_sys_infoT)sdc:datacenter_nameT)zsdc:vendor-dataF)zsdc:operator-scriptF)zsdc:hostnameT)zsdc:dns_domainT)instance-idlocal-hostnamezpublic-keysr   legacy-user-datar   r   r   availability_zonevendor-dataoperator-scriptr
   
dns_domainzsdc:nicszsdc:resolversz
sdc:routes)network-datadns_serversrouteszlx-brandkvmSmartOS
datasource)r   r   r   r   zsdc:uuiduser-datar   z"/native/.zonecontrol/metadata.sockz
/dev/ttyS1<   F
ephemeral0z/dev/vdb)serial_deviceserial_timeoutmetadata_sockfileno_base64_decodebase64_keys
base64_alldisk_aliasesmbr)
table_typelayout	overwriteext4)label
filesystemdevice)
disk_setupfs_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 <key>'
#
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                       \ rS rSrSr\R                  r\R                  r\	R                  \R                  \R                  \R                  10rS rS rS rS rS rS rS	 rS
 rS r\S 5       rSrg)DataSourceSmartOS   Joyentc                    [         R                  R                  XX#5        [        R                  " U R
                  [        R                  " U[        0 5      [        /5      U l        0 U l	        S U l
        S U l        S U l        [        R                  R                  U R                   R#                  S5      5      U l        U R'                  5         g )Nscripts)r   
DataSource__init__r   mergemanydictds_cfgget_cfg_by_pathDS_CFG_PATHBUILTIN_DS_CONFIGmetadatanetwork_data_network_configroutes_dataospathjoinpaths	get_cpathscript_base_d_init)selfsys_cfgdistrorE   s       E/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceSmartOS.pyr8   DataSourceSmartOS.__init__   s    ##D6A(($$Wk2>!
  #WW\\$***>*>y*IJ

    c                 h    [         R                  R                  U 5      nU< SU R                  < S3$ )Nz	 [client=])r   r7   __str__	md_client)rI   roots     rL   rQ   DataSourceSmartOS.__str__   s'    !!))$/#'88rN   c                 H   U R                   [        R                  :X  a#  [        5       U l         U R                   c  S U l        U R                  [        R                  :X  aC  [        U R                   U R                  S   U R                  S   U R                  S   S9U l        g g )Nr"   r    r!   )smartos_typer"   r    r!   )rV   r   UNSETget_smartos_environrR   jmc_client_factoryr:   rI   s    rL   rH   DataSourceSmartOS._init   s    - 3 5D  (!%>>W]]*/!.."&++.A"B"kk/:#{{+;<	DN +rN   c                    [         R                  S5        Sn[        R                  R	                  SR                  US/5      5      (       a:  [        R                  " SR                  US/5      SR                  US/5      5        gg)aS  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/provisioningprovision_successN)LOGdebugrB   rC   existsrD   rename)rI   svc_paths     rL   _set_provisioned"DataSourceSmartOS._set_provisioned   sl     			AB77>>#((Hn#=>??II(N34($789 @rN   c                    U R                  5         0 nSnU R                  (       d  [        R                  S5        gU R                  R                  5       (       d!  [        R                  SU R                  5        gU R                  R                  5         [        R                  5        H$  u  p4Uu  pVU R                  R                  XVS9X'   M&     [        R                  5        H"  u  p5U R                  R                  U5      X'   M$     U R                  R                  5         [        R                  R                  U R                   R#                  5       SUS   S5      n[        R                  R                  US	5      nS
[$        -  n	['        UR                  S	5      UU	SSS9  [        R                  R                  US5      n
['        UR                  S5      U
SSS9  UR                  S5      nS[$        -  n['        X5        US   (       d  US   (       a	  US   US'   OUS   US'   S nUS   (       a  US   nUS   (       dG  [(        UU
[        R                  R                  U R                   R#                  S5      S5      S.-  US'   [*        R,                  " XR.                  /5      U l        X l        US   U l        US   U l        US   U l        U R9                  5         g)N zNot running on smartosFz4No metadata device '%r' found for SmartOS datasourcestrip	instancesr   datar   z%s/user-scriptTi  )	content_flinkshebangmoder   )rm   ro   rp   r   z%s/mdata-user-datar   r
   r   r   r6   zper-boot)user_scriptoperator_script
per_boot_dr   r   )rH   rV   r`   ra   rR   rb   open_transportSMARTOS_ATTRIB_MAPitemsgetSMARTOS_ATTRIB_JSONget_jsonclose_transportrB   rC   rD   rE   rF   LEGACY_USER_Dwrite_boot_contentBUILTIN_VENDOR_DATAr   r9   r>   userdata_rawvendordata_rawr?   rA   re   )rI   mdudci_noun	attributesmartos_nounrj   data_drq   
u_script_lrr   u_datau_data_fs                rL   	_get_dataDataSourceSmartOS._get_data   s   

  II./~~$$&&IIF  	%%'"4":":"<G"+L..,,\,GBK #= &9%>%>%@!G..11,?BK &A 	&&( JJ  "KM1BF
 ggll6=9%5
FF=!!	
 '',,v/@AFF$%%		
 *+'-76, "#*~')*~#$')-'8#$k?KB-  3*#2 ggllJJ((3Z7 !B} **B+>? /~.h<rN   c                     S[         -  $ )Nzserial (%s))SERIAL_DEVICErZ   s    rL   _get_subplatform"DataSourceSmartOS._get_subplatformL  s    },,rN   c                 >    U R                   S   R                  U5      $ )Nr&   )r:   rw   )rI   names     rL   device_name_to_device'DataSourceSmartOS.device_name_to_deviceO  s    {{>*..t44rN   c                 :    U R                   [        :X  a  [        $ 0 $ N)rV   SMARTOS_ENV_KVMBUILTIN_CLOUD_CONFIGrZ   s    rL   get_config_obj DataSourceSmartOS.get_config_objR  s    /''	rN   c                      U R                   S   $ )Nr   )r>   rZ   s    rL   get_instance_id!DataSourceSmartOS.get_instance_idW  s    }}]++rN   c                    U R                   [        R                  :X  a  S U l         U R                   cL  U R                  b?  [	        U R                  U R
                  S   U R
                  S   U R                  S9U l         U R                   $ )Nr   r   r?   r   r   r   )r@   r   rW   r?   convert_smartos_network_datar>   rA   rZ   s    rL   network_config DataSourceSmartOS.network_configZ  s{     7==0#'D '  ,'C!%!2!2 $m <#}}\:++	($ ###rN   )
r@   r:   rR   r>   r?   rA   rG   rV   r~   r   N)__name__
__module____qualname____firstlineno__dsnamer   rW   rV   rR   r   NETWORKr	   BOOT_NEW_INSTANCEBOOTBOOT_LEGACYdefault_update_eventsr8   rQ   rH   re   r   r   r   r   r   propertyr   __static_attributes__ rN   rL   r2   r2      s    F==LI''NN!!
&9$aF-5
, $ $rN   r2   c                       \ rS rSrSrg)JoyentMetadataFetchExceptionik  r   Nr   r   r   r   r   r   rN   rL   r   r   k      rN   r   c                       \ rS rSrSrg)JoyentMetadataTimeoutExceptionio  r   Nr   r   rN   rL   r   r   o  r   rN   r   c                       \ rS rSrSr\R                  " S5      rSS jrS r	S r
S rS	 rS
 rSS jrSS jrSS jrS rS rS rS rS rS rSrg)JoyentMetadataClientis  z
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<length>\d+) (?P<checksum>[0-9a-f]+) (?P<body>(?P<request_id>[0-9a-f]+) (?P<status>SUCCESS|NOTFOUND)( (?P<payload>.+))?)Nc                 6    Uc
  [        5       nXl        X l        g r   )rX   rV   fp)rI   rV   r   s      rL   r8   JoyentMetadataClient.__init__  s    .0L(rN   c                 p    SR                  [        R                  " UR                  S5      5      S-  5      $ )N{0:08x}zutf-8    )formatbinasciicrc32encode)rI   bodys     rL   	_checksumJoyentMetadataClient._checksum  s/    NN4;;w/0:=
 	
rN   c           	      `   U R                   R                  U5      R                  5       n[        US   5      [	        US   5      :w  a*  [        SR                  US   [	        US   5      5      5      eU R                  US   5      nUS   U:w  a  [        SR                  XCS   5      5      eUS   U:w  a  [        SR                  XS   5      5      eUR                  SS 5      (       d  [        R                  S	5        g [        R                  " US   5      n[        R                  S
U5        U$ )Nlengthr   z*Incorrect frame length given ({0} != {1}).checksumz*Invalid checksum (expected: {0}; got {1}).
request_idz-Request ID mismatch (expected: {0}; got {1}).payloadzNo value found.zValue "%s" found.)
line_regexmatch	groupdictintlenr   r   r   rw   r`   ra   r   b64d)rI   expected_request_idframe
frame_dataexpected_checksumvalues         rL   _get_value_from_frame*JoyentMetadataClient._get_value_from_frame  s0   __**51;;=
z(#$Jv,>(??.<CCx(#j.@*A 
 !NN:f+=>j!%66.<CC%*'= 
 l#'::.?FF'L)A 
 ~~i..II'("":i#89		%u-rN   c                 L  ^ / mU4S jnSn  U R                   R                  S5      nU(       d  [        X!" 5       -  5      eUS:X  a  U" 5       $ TR                  U5        MU  ! [         a7  nUR
                  [
        R                  :X  a  [        X!" 5       -  5      Uee SnAff = f)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.
c                  D   > SR                  T 5      R                  S5      $ )NrN   ascii)rD   decode)responses   rL   as_ascii0JoyentMetadataClient._readline.<locals>.as_ascii  s    88H%,,W55rN   zPartial response: '%s'      
N)r   readr   appendOSErrorerrnoEAGAIN)rI   r   msgbyteexcr   s        @rL   	_readlineJoyentMetadataClient._readline  s     	6 'ww||A8xz9IJJ5=#:%%   99,8hj( s   A A" A" "
B#,2BB#c                     U R                   R                  UR                  S5      5        U R                   R                  5         g )Nr   )r   writer   flush)rI   r   s     rL   _writeJoyentMetadataClient._write  s(    cjj)*rN   c                     [         R                  S5        U R                  S5        U R                  5       n[         R                  SU5        US:w  a  [	        SU-  5      e[         R                  S5        g )NzNegotiating protocol V2zNEGOTIATE V2
z	read "%s"V2_OKz'Invalid response "%s" to "NEGOTIATE V2"zNegotiation complete)r`   ra   r   r   r   )rI   r   s     rL   
_negotiateJoyentMetadataClient._negotiate  sb    		+,$%>>#		+x(w.9HD  			()rN   c                    SR                  [        R                  " SS5      5      nSR                  UU45      nU(       a8  US[        R
                  " UR                  5       5      R                  5       -   -  nSR                  [        U5      U R                  U5      U5      n[        R                  SU5        SnU R                  (       d  U R                  5         SnU R                  U5        U R                  5       nU(       a  U R!                  5         [        R                  S	U5        S
U;  a  g U R#                  X75      nU$ )Nr   r   r    zV2 {0} {1} {2}
z#Writing "%s" to metadata transport.FTz"Read "%s" from metadata transport.SUCCESS)r   randomrandintrD   base64	b64encoder   r   r   r   r`   ra   r   rt   r   r   rz   r   )	rI   rtypeparamr   message_bodyr   
need_closer   r   s	            rL   requestJoyentMetadataClient.request  s   %%fnnQ
&CD
xx
 C&"2"25<<>"B"I"I"KKKL ''t~~l;\
 			7=
ww!JC>>#  "		6AH$**:@rN   c                 l    U R                  SUS9nUc  U$ U(       a  U(       a  UR                  5       nU$ )NGETr   r   )r   rj   )rI   keydefaultrj   results        rL   rw   JoyentMetadataClient.get  s3    E5>Ne\\^FrN   c                 V    U R                  XS9nUc  U$ [        R                  " U5      $ )N)r  )rw   jsonloads)rI   r  r  r  s       rL   ry   JoyentMetadataClient.get_json  s+    #/>Nzz&!!rN   c                 T    U R                  SS9nU(       d  / $ UR                  S5      $ )NKEYS)r   
)r   split)rI   r  s     rL   listJoyentMetadataClient.list  s)    F+I||D!!rN   c           	          SR                  X4 Vs/ s H'  n[        R                  " UR                  5       5      PM)     sn5      R	                  5       nU R                  SUS9$ s  snf )N    PUTr   )rD   r   r   r   r   r   )rI   r  valir   s        rL   putJoyentMetadataClient.put	  sY    		47:>:aVahhj):>

&( 	 ||%u|55 ?s   .A$c                 j    U R                   (       a"  U R                   R                  5         S U l         g g r   )r   closerZ   s    rL   rz   $JoyentMetadataClient.close_transport  s"    77GGMMODG rN   c                 L    U R                   (       a  U $ U R                  5         U $ r   )r   rt   rZ   s    rL   	__enter__JoyentMetadataClient.__enter__  s    77KrN   c                 $    U R                  5         g r   )rz   )rI   exc_type	exc_value	tracebacks       rL   __exit__JoyentMetadataClient.__exit__  s    rN   c                     [         er   )NotImplementedErrorrZ   s    rL   rt   #JoyentMetadataClient.open_transport  s    !!rN   )r   rV   NNr   NF)r   r   r   r   __doc__recompiler   r8   r   r   r   r   r   r   rw   ry   r  r  rz   r  r   rt   r   r   rN   rL   r   r   s  sh     	 J

8:	*B""6
"rN   r   c                   @   ^  \ rS rSr\4U 4S jjrS rS rS rSr	U =r
$ )JoyentMetadataSocketClienti"  c                 8   > [         [        U ]  U5        Xl        g r   )superr+  r8   
socketpath)rI   r.  rV   	__class__s      rL   r8   #JoyentMetadataSocketClient.__init__#  s    ($8F$rN   c                     [         R                   " [         R                  [         R                  5      nUR                  U R                  5        UR                  S5      U l        U R                  5         g )Nrwb)socketAF_UNIXSOCK_STREAMconnectr.  makefiler   r   )rI   socks     rL   rt   )JoyentMetadataSocketClient.open_transport'  sF    }}V^^V-?-?@T__%--&rN   c                 T    [         R                  R                  U R                  5      $ r   )rB   rC   rb   r.  rZ   s    rL   rb   !JoyentMetadataSocketClient.exists-  s    ww~~doo..rN   c                 R    U R                   R                  < SU R                  < S3$ )Nz(socketpath=))r/  r   r.  rZ   s    rL   __repr__#JoyentMetadataSocketClient.__repr__0  s    &*nn&=&=tOOrN   )r   r.  )r   r   r   r   SMARTOS_ENV_LX_BRANDr8   rt   rb   r>  r   __classcell__r/  s   @rL   r+  r+  "  s!    0D %/P PrN   r+  c                   J   ^  \ rS rSrS\S4U 4S jjrS rS rS rS r	S	r
U =r$ )
JoyentMetadataSerialClienti4  
   Nc                 D   > [         [        U ]  X45        Xl        X l        g r   )r-  rD  r8   r.   timeout)rI   r.   rG  rV   r   r/  s        rL   r8   #JoyentMetadataSerialClient.__init__5  s     	($8JrN   c                 T    [         R                  R                  U R                  5      $ r   )rB   rC   rb   r.   rZ   s    rL   rb   !JoyentMetadataSerialClient.exists<  s    ww~~dkk**rN   c                    U R                   c   SS KnUR	                  U R
                  U R                  S9nUR                  5       (       d  [        SU R
                  -  5      eX0l         [        R                  " U[        R                  5        U R                  5         U R                  5         g ! [         a  n[        S5      UeS nAff = f)Nr   zserial support is not available)rG  zUnable to open %s)r   serialImportErrorr#  Serialr.   rG  isOpenSystemErrorfcntllockfLOCK_EX_flushr   )rI   rL  esers       rL   rt   )JoyentMetadataSerialClient.open_transport?  s    77?
 --T\\-BC::<<!"5"CDDGKKU]]+  )5s   B0 0
C:CCc                    [         R                  S5        U R                  R                  nSU R                  l          U R	                  5         M  ! [
         a     Of = f[         R                  S5        US:  a  SU R                  l        OXR                  l         [         R                  S5        U R                  S5         U R	                  5       nUS:X  a  O[US:X  a  [         R                  S	5        M\  [         R                  S
U5        O%! [
         a    [         R                  S5         Of = fM  [         R                  S5        XR                  l        g )NzFlushing inputg?zInput empty   z,Writing newline, expecting "invalid command"r  zinvalid commandFAILUREzGot "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`   ra   r   rG  r   r   r   warning)rI   rG  r   s      rL   rT  !JoyentMetadataSerialClient._flushO  s   		"#''//   2 		-  Q;DGGO%GGOIIDEKK>>+00y(II9:CXN1 <   			;<!s/   A 
AAD D :D D32D3c                 n    U R                   R                  < SU R                  < SU R                  < S3$ )Nz(device=z
, timeout=r=  )r/  r   r.   rG  rZ   s    rL   r>  #JoyentMetadataSerialClient.__repr__v  s'    NN##KKLL
 	
rN   )r.   r   rG  )r   r   r   r   r   r8   rb   rt   rT  r>  r   rA  rB  s   @rL   rD  rD  4  s)     4+ %"N
 
rN   rD  c                   ^   ^  \ rS rSrSrS	U 4S jjrS
S jrSU 4S jjrS
S jrSS jr	Sr
U =r$ ) JoyentMetadataLegacySerialClienti~  a  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-<keyname> with a boolean indicating that
<keyname> is base64 encoded.c                 f   > [        [        U 5      nUR                  XU5        S U l        S U l        g r   )r-  r`  r8   r$   r%   )rI   r.   rG  rV   sr/  s        rL   r8   )JoyentMetadataLegacySerialClient.__init__  s-    2D9	

6L1rN   c                    U(       a  S U l         S U l        S nU R                  cH  U R                  5       nSU;   a+  [        R                  " U R                  S5      5      U l        OSU l        U R                  (       a  g U R                   c  Uc  U R                  5       n[        5       nSU;   a)  [        U R                  S5      R                  S5      5      nU Vs/ s H  oDR                  S5      (       d  M  USS  PM!     sn HX  n[        R                  " U R                  U5      5      (       a  UR                  U5        M@  XS;   d  MG  UR                  U5        MZ     X0l         g g s  snf )Nr%   Fr$   ,zb64-   )r$   r%   r  r   is_true_getsetr  
startswithaddremove)rI   resetkeysb64_keyskr  s         rL   _init_base64_keys2JoyentMetadataLegacySerialClient._init_base64_keys  s   #D"DO??"99;Dt#"&,,tyy/F"G"'??#|yy{uH$tyy7==cBC (,Dt!||F/C!"tD<<		#//LL% , E  ( $ Es   E)9	E)c                 (   > [         [        U ]  XUS9$ )N)r  rj   )r-  r`  rw   )rI   r  r  rj   r/  s       rL   rh  %JoyentMetadataLegacySerialClient._get  s#    5t@ A 
 	
rN   c                 v    U[         ;   a  gU R                  US9  U R                  (       a  gXR                  ;   $ )NF)rm  T)NO_BASE64_DECODErq  r%   r$   )rI   r  rm  s      rL   is_b64_encoded/JoyentMetadataLegacySerialClient.is_b64_encoded  s8    ""U+??&&&&rN   c                 h   [        5       nU R                  USUS9nXTL a  U$ U R                  U5      (       a3   [        R                  " UR                  5       5      R                  5       nU(       a  UR                  5       nU$ ! [        R                   a    [        R                  SX5         NEf = f)NF)rj   r  z#Failed base64 decoding key '%s': %s)objectrh  rw  r   	b64decoder   r   r   Errorr`   r[  rj   )rI   r  r  rj   mdefaultr  s         rL   rw   $JoyentMetadataLegacySerialClient.get  s    8ii5(i;?Ns##M&&szz|4;;= ))+C
 >> MA3LMs   2B *B10B1)r%   r$   )rE  N)Fr&  )r   r   r   r   r'  r8   rq  rh  rw  rw   r   rA  rB  s   @rL   r`  r`  ~  s'    $ (D

' rN   r`  c                     U c  [        U5      n U c  g U [        :X  a  [        UUU S9$ U [        :X  a	  [	        XS9$ [        SU -  5      e)N)r.   rG  rV   )r.  rV   z"Unknown value for smartos_type: %s)rX   r   r`  r@  r+  
ValueError)rV   r"   r    r!   uname_versions        rL   rY   rY     si     *=9		(/ "%
 	

 
-	-)(
 	
 9LH
IIrN   c                    SSSU /nS n [         R                   " U5      u  p#[        R                  SX5        Uc  S $ UR                  5       $ ! [         R                   a*  n[        R
                  " [        SU -  U45         S nANNS nAff = f)Nfilez--briefz--mime-typezscript %s mime type is %sz%Failed to identify script type for %s)r   r`   ra   ProcessExecutionErrorr   logexcrj   )rm   cmdf_type_errrU  s        rL   identify_filer    s    9mY
7CF
3		-yA
 >45v||~5	 %% 
9IEqI	
 	

s   .A B! BBc                    U (       d:  [         R                  R                  U5      (       a  [         R                  " U5        U(       a:  [         R                  R	                  U5      (       a  [         R                  " U5        U (       d  g[
        R                  " XUS9  U(       ad  U R                  S5      (       dN  [        U5      nUS:X  a=  [
        R                  " USR                  SU /5      US9  [        R                  SU5        U(       a   [         R                  R	                  U5      (       a  [         R                  " U5        U (       ao  [         R                  R                  U5      (       aJ  [
        R                  " [         R                  R                  U5      5        [         R                  " X5        gggg! [         a&  n[
        R                   " [        SU5         SnAgSnAff = f)	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)rp   z#!z
text/plainr  z#!/bin/bashzadded shebang to file %sz$failed establishing content link: %s)rB   rC   rb   unlinkislinkr   
write_filerj  r  rD   r`   ra   
ensure_dirdirnamesymlinkIOErrorr  )contentrm   rn   ro   rp   r  rU  s          rL   r|   r|     sE   . rww~~i00
		)t$$
		$OOIT2w))$//y)\!OO499mW%=>T II0)<	Hww~~d##		$277>>)44 56

9+ 5w	   	HKKCQGG	Hs   B.G 
G5G00G5c                     [         R                  " 5       nU c  US   n U S:X  a  [        $ Uc  [        R                  " S5      nOUnU(       a  UR                  S5      (       a  [        $ g )Nrf  zBrandZ virtual linuxzsystem-product-nameSmartDC)rB   unamer@  r   read_dmi_datarj  r   )r  product_namer  system_types       rL   rX   rX   1  se    HHJE a..##''(=>"{--i88rN   c                 X  ^ / SQ/ SQSS/S.mU(       a  [        U[        [        45      (       d  U/nO/ nU(       a  [        U[        [        45      (       d  U/nO/ nU(       d  / nS nS nUSS	.USS	.S
.n/ nU  GH  n[        U4S jUR	                  5        5       5      n	U	R                  SUS   S.5        SU;   a  U	R                  SUS   05        / n
UR                  S/ 5       GHn  nUS:X  a  SS0nGOMUS:X  a  SS0nGOA/ n[        U4S jUR	                  5        5       5      nUR                  SUS.5        U" U5      (       a  SOSnSU;   a  UR                  SS5      (       at  Xn   S   (       dh  UR                  S/ 5       Vs/ s H  nXn   S    " U5      (       d  M  UPM     nn[        U5      (       a"  US!   Xn   S'   UR                  SXn   S   05        U H`  n[        U4S" jUR	                  5        5       5      nUR                  SUS#   05        UR                  U5        UR                  S$U05        Mb     U
R                  U5        GMq     U	R                  S%U
05        UR                  U	5        GM     U(       a  UR                  S&UUS'.5        S(US).$ s  snf )*a  Return 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_addressmtur   paramssubnetstype)	address	broadcastdns_nameservers
dns_searchmetricpointopointr   scoper  networkgateway)physicalsubnetroutec                     SU ;   $ )N.r   addrs    rL   is_valid_ipv43convert_smartos_network_data.<locals>.is_valid_ipv4      d{rN   c                     SU ;   $ )N:r   r  s    rL   is_valid_ipv63convert_smartos_network_data.<locals>.is_valid_ipv6  r  rN   N)r   gw)ipv4ipv6c              3   D   >#    U  H  u  pUTS    ;   d  M  X4v   M     g7f)r  Nr   .0rp  v
valid_keyss      rL   	<genexpr>/convert_smartos_network_data.<locals>.<genexpr>  s&      
*tqa:j3I.IFQF{    
 r  	interface)r  r   macr  ipsdhcpr  dhcp4addrconfdhcp6c              3   D   >#    U  H  u  pUTS    ;   d  M  X4v   M     g7f)r  Nr   r  s      rL   r  r    s&      '2tqa:h;O6OFQF{r  static)r  r  r  r  primaryFr  gatewaysr   r   c              3   D   >#    U  H  u  pUTS    ;   d  M  X4v   M     g7f)r  Nr   r  s      rL   r  r    s+       $1DA
7 33 $1r  dstr   r  
nameserver)r  r  searchr   )versionconfig)	
isinstancer  tupledictrv   updaterw   r   r   )r?   r   r   r   r  r  pgwsr  niccfgr  ipr  	routeentsprotor  r  r  rcfgr  s                      @rL   r   r   G  s   6


 
)J4 +e}55&-K*tUm44$J
 (t4't4D
 F 
"yy{
 
 	

JK0@ABC<JJs5z23''%$BV| '*z! '*	 '*yy{   (#% #0"3"3#	5(A(A
  ;t, '*ggj"&=$&=#{73B7 &= ! $
 x==08DK-"MM9dk$6G*HI#E  $)KKM  D" KKE%L 9:$$T*MM8Y"78) $, NN6"q %r 	

Iw'(cG J $&$	
 F++Y$s   J''J'c                 8    [         R                  " U [        5      $ r   )r   list_from_dependsdatasources)dependss    rL   get_datasource_listr    s    $$Wk::rN   __main__zDo not appear to be on smartos.r   r   c                 6   X;   a  X!   $ U[         ;   a   [         U   nU R                  U5      X!'   X!   $ US:X  a3  S H  n[        XU5        M     [        US   US   US   US   S9X!'   X!   $ U[        ;   a  [        U   u  p5OUSpSU R                  X5S	9X!'   X!   $ )
Nr   )r   r   r   r   r   r   r   r   r   Fri   )rx   ry   load_keyr   ru   rw   )clientr  rl   keynamedepkeyrj   s         rL   r  r    s    ;9%%)#.G0DI, y+ $$ . 5!.1 /-H~	DI y ((!3C!8"%u

7
8DIyrN   rl   )r  r  rl   T)re  z: )indent	sort_keys
separators)NF   r%  )NNNN)Cr   r   r   rQ  r  loggingrB   r   r(  r3  	cloudinitr   r   r   r   r   cloudinit.eventr   r	   	getLoggerr   r`   ru   rx   r@  r   DS_NAMEr<   rv  METADATA_SOCKFILEr   SERIAL_TIMEOUTr=   r   r}   r{   r7   r2   	Exceptionr   r   r   r+  rD  r`  rY   r  r|   rX   r   DEP_FILESYSTEMr  r  sysjmcprintexitr   argvr  rn  r  rl   r  __annotations__r  dumpsr   rN   rL   <module>r     s  .       	  	  = = 1! &(1),02,6-5&* & "	  " 
W%  9 " #$*(!:.  	
 fM 8 8 D$** D$N	9 		%A 	l" l"^P!5 P$G
!5 G
TU'A Ur '!J6
6 8=0Hf. BF],D //12; z

C
{/0
388}$))+,%**,-. ! 	 xx|< D$O40  
$**T!t
LMe rN   