o
    x[h‰  ã                   @   sž  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZmZmZmZ d dlmZmZ e e¡Zdddddd	d
dddddddœZddddœZdZdZdZdegZg d¢ZdZdZdZeeeeg ddd id!œZ dd"ddd#œidd$dd%œgd&œZ!d'Z"d(Z#G d)d*„ d*ej$ƒZ%G d+d,„ d,e&ƒZ'G d-d.„ d.e'ƒZ(G d/d0„ d0ƒZ)G d1d2„ d2e)ƒZ*G d3d4„ d4e)ƒZ+G d5d6„ d6e+ƒZ,deeedfd7d8„Z-d9d:„ Z.	;dOd<d=„Z/dPd>d?„Z0	dQd@dA„Z1e%ej2ffgZ3dBdC„ Z4edDk�rMd dl5Z5e-ƒ Z6e6du �r
e7dEƒ e5 8dF¡ e9e5j:ƒdFk�r"e;e <¡ ƒe;e <¡ ƒ dGg Z<ne5j:dFd… Z<dHdI„ Z=i Z>e?e@dJ< e<D ]
ZAe=e6eAe>dK� �q5e7ejBe>dFdLdMdN�ƒ dS dS )Ré    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                   @   s†   e Zd ZdZejZejZej	e
je
je
jhiZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )ÚDataSourceSmartOSÚJoyentc                 C   sj   t j | |||¡ t | jt |ti ¡tg¡| _i | _	d | _
d | _d | _tj | j d¡¡| _|  ¡  d S )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ÚdistrorA   © rH   úE/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceSmartOS.pyr4   ²   s   ýÿzDataSourceSmartOS.__init__c                 C   s   t j | ¡}d|| jf S )Nz%s [client=%s])r   r3   Ú__str__Ú	md_client)rE   ÚrootrH   rH   rI   rJ   Å   s   zDataSourceSmartOS.__str__c                 C   s^   | j tjkrtƒ | _ | j d u rd | _| jtjkr-t| j | jd | jd | jd d�| _d S d S )Nr!   r   r    )Úsmartos_typer!   r   r    )rM   r   ÚUNSETÚget_smartos_environrK   Újmc_client_factoryr6   ©rE   rH   rH   rI   rD   É   s   
üÿzDataSourceSmartOS._initc                 C   sL   t  d¡ d}tj d |dg¡¡r$t d |dg¡d |dg¡¡ dS dS )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ú/ÚprovisioningÚprovision_successN)ÚLOGÚdebugr>   r?   Úexistsr@   Úrename)rE   Úsvc_pathrH   rH   rI   Ú_set_provisioned×   s   

þÿz"DataSourceSmartOS._set_provisionedc                 C   sÜ  |   ¡  i }d}| jst d¡ dS | j ¡ s t d| j¡ dS | j ¡  t ¡ D ]\}}|\}}| jj	||d�||< q)t
 ¡ D ]\}}| j |¡||< q@| j ¡  tj | j ¡ d|d d¡}tj |d	¡}d
t }	t| 	d	¡||	ddd� tj |d¡}
t| 	d¡|
ddd� | 	d¡}dt }t||ƒ |d s¬|d r¦|d |d< n|d |d< d }|d r¶|d }|d sÍt||
tj | j d¡d¡dœ |d< t || jg¡| _|| _|d | _|d | _|d | _|  ¡  dS )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   )r`   rb   rc   r   z%s/mdata-user-datar   r	   r   r   r2   zper-boot)Úuser_scriptÚoperator_scriptÚ
per_boot_dr   r   )rD   rM   rU   rV   rK   rW   Úopen_transportÚSMARTOS_ATTRIB_MAPÚitemsÚgetÚSMARTOS_ATTRIB_JSONÚget_jsonÚclose_transportr>   r?   r@   rA   rB   ÚLEGACY_USER_DÚwrite_boot_contentÚBUILTIN_VENDOR_DATAr   r5   r:   Úuserdata_rawÚvendordata_rawr;   r=   rZ   )rE   ÚmdÚudÚci_nounÚ	attributeÚsmartos_nounr]   Údata_drd   Ú
u_script_lre   Úu_dataÚu_data_frH   rH   rI   Ú	_get_dataé   s~   

þ


ÿûü

ÿý


zDataSourceSmartOS._get_datac                 C   s   dt  S )Nzserial (%s))ÚSERIAL_DEVICErQ   rH   rH   rI   Ú_get_subplatformL  ó   z"DataSourceSmartOS._get_subplatformc                 C   s   | j d  |¡S )Nr%   )r6   rj   )rE   ÚnamerH   rH   rI   Údevice_name_to_deviceO  ó   z'DataSourceSmartOS.device_name_to_devicec                 C   s   | j tkrtS i S ©N)rM   ÚSMARTOS_ENV_KVMÚBUILTIN_CLOUD_CONFIGrQ   rH   rH   rI   Úget_config_objR  s   
z DataSourceSmartOS.get_config_objc                 C   s
   | j d S )Nr   )r:   rQ   rH   rH   rI   Úget_instance_idW  s   
z!DataSourceSmartOS.get_instance_idc                 C   sN   | j tjkr	d | _ | j d u r$| jd ur$t| j| jd | jd | jd�| _ | j S )Nr   r   ©r;   r   r   r   )r<   r   rN   r;   Úconvert_smartos_network_datar:   r=   rQ   rH   rH   rI   Únetwork_configZ  s   

üz DataSourceSmartOS.network_configN)Ú__name__Ú
__module__Ú__qualname__Údsnamer   rN   rM   rK   r   ÚNETWORKr   ÚBOOT_NEW_INSTANCEÚBOOTÚBOOT_LEGACYÚdefault_update_eventsr4   rJ   rD   rZ   r|   r~   r�   r†   r‡   ÚpropertyrŠ   rH   rH   rH   rI   r0   ¤   s*    ýÿcr0   c                   @   ó   e Zd ZdS )ÚJoyentMetadataFetchExceptionN©r‹   rŒ   r�   rH   rH   rH   rI   r–   k  ó    r–   c                   @   r•   )ÚJoyentMetadataTimeoutExceptionNr—   rH   rH   rH   rI   r™   o  r˜   r™   c                   @   sš   e Zd ZdZe d¡Zd#dd„Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zd$dd„Zd%dd„Zd$dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ ZdS )&Ú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<length>\d+) (?P<checksum>[0-9a-f]+) (?P<body>(?P<request_id>[0-9a-f]+) (?P<status>SUCCESS|NOTFOUND)( (?P<payload>.+))?)Nc                 C   s   |d u rt ƒ }|| _|| _d S rƒ   )rO   rM   Úfp)rE   rM   r›   rH   rH   rI   r4   �  s   
zJoyentMetadataClient.__init__c                 C   s   d  t | d¡¡d@ ¡S )Nú{0:08x}zutf-8ì   ÿÿ )ÚformatÚbinasciiÚcrc32Úencode)rE   ÚbodyrH   rH   rI   Ú	_checksum‡  s   ÿzJoyentMetadataClient._checksumc                 C   sÊ   | j  |¡ ¡ }t|d ƒt|d ƒkr"td |d t|d ƒ¡ƒ‚|  |d ¡}|d |kr9td ||d ¡ƒ‚|d |krItd ||d ¡ƒ‚| dd ¡sVt	 
d	¡ d S t |d ¡}t	 
d
|¡ |S )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£   rj   rU   rV   r   Úb64d)rE   Úexpected_request_idÚframeÚ
frame_dataÚexpected_checksumÚvaluerH   rH   rI   Ú_get_value_from_frameŒ  s4   ÿÿÿÿÿÿ
z*JoyentMetadataClient._get_value_from_framec              
      sŒ   g ‰ ‡ fdd„}d}	 z| j  d¡}|st||ƒ  ƒ‚|dkr#|ƒ W S ˆ  |¡ W n tyD } z|jtjkr?t||ƒ  ƒ|‚‚ d}~ww q)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                      s   d  ˆ ¡ d¡S )Nó    Úascii)r@   ÚdecoderH   ©ÚresponserH   rI   Úas_ascii²  r‚   z0JoyentMetadataClient._readline.<locals>.as_asciizPartial response: '%s'Té   ó   
N)r›   Úreadr™   ÚappendÚOSErrorÚerrnoÚEAGAIN)rE   r¹   ÚmsgÚbyteÚexcrH   r·   rI   Ú	_readline¨  s,   ÿþ€ûøzJoyentMetadataClient._readlinec                 C   s    | j  | d¡¡ | j  ¡  d S )Nrµ   )r›   Úwriter¡   Úflush)rE   rÁ   rH   rH   rI   Ú_writeÅ  s   zJoyentMetadataClient._writec                 C   sJ   t  d¡ |  d¡ |  ¡ }t  d|¡ |dkrtd| ƒ‚t  d¡ d S )NzNegotiating protocol V2zNEGOTIATE V2
z	read "%s"ÚV2_OKz'Invalid response "%s" to "NEGOTIATE V2"zNegotiation complete)rU   rV   rÇ   rÄ   r–   )rE   r¸   rH   rH   rI   Ú
_negotiateÉ  s   

ÿzJoyentMetadataClient._negotiatec           	      C   s¾   d  t dd¡¡}d ||f¡}|r|dt | ¡ ¡ ¡  7 }d  t|ƒ|  	|¡|¡}t
 d|¡ d}| js<|  ¡  d}|  |¡ |  ¡ }|rK|  ¡  t
 d	|¡ d
|vrWd S |  ||¡}|S )Nrœ   r   r�   ú zV2 {0} {1} {2}
z#Writing "%s" to metadata transport.FTz"Read "%s" from metadata transport.ÚSUCCESS)rž   ÚrandomÚrandintr@   Úbase64Ú	b64encoder¡   r¶   r¬   r£   rU   rV   r›   rg   rÇ   rÄ   rm   r³   )	rE   ÚrtypeÚparamr¦   Úmessage_bodyrÁ   Ú
need_closer¸   r²   rH   rH   rI   ÚrequestÔ  s2   þÿÿ
zJoyentMetadataClient.requestFc                 C   s.   | j d|d�}|d u r|S |r|r| ¡ }|S )NÚGET©rÐ   rÑ   )rÔ   r]   )rE   ÚkeyÚdefaultr]   ÚresultrH   rH   rI   rj   õ  s   zJoyentMetadataClient.getc                 C   s$   | j ||d�}|d u r|S t |¡S )N)rØ   )rj   ÚjsonÚloads)rE   r×   rØ   rÙ   rH   rH   rI   rl   ý  s   
zJoyentMetadataClient.get_jsonc                 C   s   | j dd�}|s
g S | d¡S )NÚKEYS)rÐ   Ú
)rÔ   Úsplit)rE   rÙ   rH   rH   rI   Úlist  s   
zJoyentMetadataClient.listc                 C   s*   d  dd„ ||fD ƒ¡ ¡ }| jd|d�S )Nó    c                 S   s   g | ]	}t  | ¡ ¡‘qS rH   )rÎ   rÏ   r¡   )Ú.0ÚirH   rH   rI   Ú
<listcomp>  s    z,JoyentMetadataClient.put.<locals>.<listcomp>ÚPUTrÖ   )r@   r¶   rÔ   )rE   r×   ÚvalrÑ   rH   rH   rI   Úput	  s   ÿþzJoyentMetadataClient.putc                 C   s   | j r| j  ¡  d | _ d S d S rƒ   )r›   ÚcloserQ   rH   rH   rI   rm     s   

þz$JoyentMetadataClient.close_transportc                 C   s   | j r| S |  ¡  | S rƒ   )r›   rg   rQ   rH   rH   rI   Ú	__enter__  s   zJoyentMetadataClient.__enter__c                 C   s   |   ¡  d S rƒ   )rm   )rE   Úexc_typeÚ	exc_valueÚ	tracebackrH   rH   rI   Ú__exit__  s   zJoyentMetadataClient.__exit__c                 C   s   t ‚rƒ   )ÚNotImplementedErrorrQ   rH   rH   rI   rg     s   z#JoyentMetadataClient.open_transport©NNrƒ   ©NF)r‹   rŒ   r�   Ú__doc__ÚreÚcompiler¨   r4   r£   r³   rÄ   rÇ   rÉ   rÔ   rj   rl   rß   ræ   rm   rè   rì   rg   rH   rH   rH   rI   rš   s  s(    ÿ


!
rš   c                       s8   e Zd Zef‡ fdd„	Zdd„ Zdd„ Zdd„ Z‡  ZS )	ÚJoyentMetadataSocketClientc                    s   t t| ƒ |¡ || _d S rƒ   )Úsuperró   r4   Ú
socketpath)rE   rõ   rM   ©Ú	__class__rH   rI   r4   #  s   
z#JoyentMetadataSocketClient.__init__c                 C   s4   t   t jt j¡}| | j¡ | d¡| _|  ¡  d S )NÚrwb)ÚsocketÚAF_UNIXÚSOCK_STREAMÚconnectrõ   Úmakefiler›   rÉ   )rE   ÚsockrH   rH   rI   rg   '  s   z)JoyentMetadataSocketClient.open_transportc                 C   ó   t j | j¡S rƒ   )r>   r?   rW   rõ   rQ   rH   rH   rI   rW   -  ó   z!JoyentMetadataSocketClient.existsc                 C   s   d| j j| jf S )Nz%s(socketpath=%s))r÷   r‹   rõ   rQ   rH   rH   rI   Ú__repr__0  s   z#JoyentMetadataSocketClient.__repr__)	r‹   rŒ   r�   ÚSMARTOS_ENV_LX_BRANDr4   rg   rW   r  Ú__classcell__rH   rH   rö   rI   ró   "  s
    ró   c                       sD   e Zd Zdedf‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡  Z	S )ÚJoyentMetadataSerialClienté
   Nc                    s"   t t| ƒ ||¡ || _|| _d S rƒ   )rô   r  r4   r-   Útimeout)rE   r-   r  rM   r›   rö   rH   rI   r4   5  s   
z#JoyentMetadataSerialClient.__init__c                 C   rÿ   rƒ   )r>   r?   rW   r-   rQ   rH   rH   rI   rW   <  r   z!JoyentMetadataSerialClient.existsc              
   C   sŠ   | j d u r;zdd l}W n ty } ztdƒ|‚d }~ww |j| j| jd�}| ¡ s1td| j ƒ‚|| _ t	 
|t	j¡ |  ¡  |  ¡  d S )Nr   zserial support is not available)r  zUnable to open %s)r›   ÚserialÚImportErrorrí   ÚSerialr-   r  ÚisOpenÚSystemErrorÚfcntlÚlockfÚLOCK_EXÚ_flushrÉ   )rE   r  ÚeÚserrH   rH   rI   rg   ?  s$   
ÿþ€ÿz)JoyentMetadataSerialClient.open_transportc                 C   sè   t  d¡ | jj}d| j_	 z|  ¡  W n	 ty   Y nw qt  d¡ |dkr-d| j_n|| j_	 t  d¡ |  d¡ z|  ¡ }|dkrGW n"|d	krRt  d
¡ W q1t  d|¡ W n tyg   t  d¡ Y nw q2t  d¡ || j_d S )NzFlushing inputgš™™™™™¹?Tz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.)rU   rV   r›   r  rÄ   r™   rÇ   Úwarning)rE   r  r¸   rH   rH   rI   r  O  sB   
ÿý




ÿÿõ
z!JoyentMetadataSerialClient._flushc                 C   s   d| j j| j| jf S )Nz%s(device=%s, timeout=%s))r÷   r‹   r-   r  rQ   rH   rH   rI   r  v  s
   ýz#JoyentMetadataSerialClient.__repr__)
r‹   rŒ   r�   r„   r4   rW   rg   r  r  r  rH   rH   rö   rI   r  4  s    ÿ'r  c                       sN   e Zd ZdZd‡ fdd„	Zddd„Zd‡ fd	d
„	Zddd„Zdd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-<keyname> with a boolean indicating that
    <keyname> is base64 encoded.r  Nc                    s(   t t| ƒ}| |||¡ d | _d | _d S rƒ   )rô   r  r4   r#   r$   )rE   r-   r  rM   Úsrö   rH   rI   r4   �  s   

z)JoyentMetadataLegacySerialClient.__init__Fc                 C   sÞ   |rd | _ d | _d }| jd u r$|  ¡ }d|v r!t |  d¡¡| _nd| _| jr)d S | j d u rm|d u r6|  ¡ }tƒ }d|v rGt|  d¡ d¡ƒ}dd„ |D ƒD ]}t |  |¡¡r^| |¡ qN||v rg| 	|¡ qN|| _ d S d S )Nr$   Fr#   ú,c                 S   s"   g | ]}|  d ¡r|dd… ‘qS )zb64-é   N)Ú
startswith)rá   ÚkrH   rH   rI   rã   ¬  s   " zFJoyentMetadataLegacySerialClient._init_base64_keys.<locals>.<listcomp>)
r#   r$   rß   r   Úis_trueÚ_getÚsetrÞ   ÚaddÚremove)rE   ÚresetÚkeysÚb64_keysr×   rH   rH   rI   Ú_init_base64_keys“  s2   


€
ñz2JoyentMetadataLegacySerialClient._init_base64_keysc                    s   t t| ƒj|||d�S )N)rØ   r]   )rô   r  rj   )rE   r×   rØ   r]   rö   rH   rI   r  µ  s   
ÿz%JoyentMetadataLegacySerialClient._getc                 C   s,   |t v rdS | j|d� | jrdS || jv S )NF)r   T)ÚNO_BASE64_DECODEr#  r$   r#   )rE   r×   r   rH   rH   rI   Úis_b64_encodedº  s   
z/JoyentMetadataLegacySerialClient.is_b64_encodedc                 C   sv   t ƒ }| j|d|d�}||u r|S |  |¡r3zt | ¡ ¡ ¡ }W n tjy2   t	 
d||¡ Y nw |r9| ¡ }|S )NF)r]   rØ   z#Failed base64 decoding key '%s': %s)Úobjectr  r%  rÎ   Ú	b64decoder¡   r¶   rŸ   ÚErrorrU   r  r]   )rE   r×   rØ   r]   Úmdefaultrå   rH   rH   rI   rj   Ä  s   
ÿz$JoyentMetadataLegacySerialClient.get)r  N)Frï   )
r‹   rŒ   r�   rð   r4   r#  r  r%  rj   r  rH   rH   rö   rI   r  ~  s    
"

r  c                 C   sR   | d u rt |ƒ} | d u rd S | tkrt||| d�S | tkr#t|| d�S td|  ƒ‚)N)r-   r  rM   )rõ   rM   z"Unknown value for smartos_type: %s)rO   r„   r  r  ró   Ú
ValueError)rM   r!   r   r    Úuname_versionrH   rH   rI   rP   Ö  s   ýÿrP   c              
   C   s€   ddd| g}d }zt   |¡\}}t d| |¡ W n t jy5 } zt td|  |f¡ W Y d }~nd }~ww |d u r<d S | ¡ S )NÚfilez--briefz--mime-typezscript %s mime type is %sz%Failed to identify script type for %s)r   rU   rV   ÚProcessExecutionErrorr   Úlogexcr]   )r`   ÚcmdÚf_typeÚ_errr  rH   rH   rI   Úidentify_fileñ  s   ÿ€ÿr2  é   c              
   C   s$  | st j |¡rt  |¡ |rt j |¡rt  |¡ | sdS tj|| |d� |rH|  d¡sHt|ƒ}|dkrHtj|d 	d| g¡|d� t
 d|¡ |r�z+t j |¡rVt  |¡ | rpt j |¡rst t j |¡¡ t  ||¡ W dS W dS W dS  ty� } zt t
d|¡ W Y d}~dS d}~ww dS )	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)rc   z#!z
text/plainrÝ   z#!/bin/bashzadded shebang to file %sz$failed establishing content link: %s)r>   r?   rW   ÚunlinkÚislinkr   Ú
write_filer  r2  r@   rU   rV   Ú
ensure_dirÚdirnameÚsymlinkÚIOErrorr.  )Úcontentr`   ra   rb   rc   r0  r  rH   rH   rI   ro   þ  s6   

ÿ
þ€ÿùro   c                 C   sR   t  ¡ }| d u r|d } | dkrtS |d u rt d¡}n|}|r'| d¡r'tS d S )Nr  zBrandZ virtual linuxzsystem-product-nameÚSmartDC)r>   Úunamer  r   Úread_dmi_datar  r„   )r+  Úproduct_namer=  Úsystem_typerH   rH   rI   rO   1  s   rO   c                    sL  g d¢g d¢ddgdœ‰|rt |ttfƒs|g}ng }|r(t |ttfƒs'|g}ng }|s.g }dd„ }dd	„ }|d
dœ|d
dœdœ‰ g }| D ]Ï}t‡fdd„| ¡ D ƒƒ}| d|d dœ¡ d|v rk| d|d i¡ g }	| dg ¡D ]”}
|
dkr~ddi}n„|
dkr‡ddi}n{g }t‡fdd„| ¡ D ƒƒ}| d|
dœ¡ ||
ƒr¤dnd‰d|v rÛ| dd ¡rÛˆ ˆ d! sÛ‡ ‡fd"d#„| d$g ¡D ƒ}t|ƒrÛ|d% ˆ ˆ d!< | dˆ ˆ d! i¡ |D ]$}t‡fd&d„| ¡ D ƒƒ}| d|d' i¡ | |¡ | d(|i¡ qÝ|	 |¡ qs| d)|	i¡ | |¡ qE|�r!| d*||d+œ¡ d,|d-œS ).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   ÚscoperE  ÚnetworkÚgateway)ÚphysicalÚsubnetÚroutec                 S   ó   d| v S )NÚ.rH   ©ÚaddrrH   rH   rI   Úis_valid_ipv4Š  r   z3convert_smartos_network_data.<locals>.is_valid_ipv4c                 S   rR  )Nú:rH   rT  rH   rH   rI   Úis_valid_ipv6�  r   z3convert_smartos_network_data.<locals>.is_valid_ipv6N)r©   Úgw)Úipv4Úipv6c                 3   ó(   � | ]\}}|ˆ d  v r||fV  qdS )rO  NrH   ©rá   r  Úv©Ú
valid_keysrH   rI   Ú	<genexpr>—  ó   € ÿz/convert_smartos_network_data.<locals>.<genexpr>rO  Ú	interface)rE  r€   ÚmacrA  ÚipsÚdhcprE  Údhcp4ÚaddrconfÚdhcp6c                 3   r\  )rP  NrH   r]  r_  rH   rI   ra  ¦  rb  Ústatic)rE  rF  rZ  r[  ÚprimaryFrY  c                    s    g | ]}ˆ ˆ d  |ƒr|‘qS )r©   rH   )rá   rY  )ÚpgwsÚprotorH   rI   rã   ¸  s    ýÿz0convert_smartos_network_data.<locals>.<listcomp>Úgatewaysr   c                 3   r\  )rQ  NrH   r]  r_  rH   rI   ra  Â  s   € þýÚdstr   rD  Ú
nameserver)rE  rF  Úsearchrº   )ÚversionÚconfig)	Ú
isinstancerß   ÚtupleÚdictri   Úupdaterj   r¬   r½   )r;   r   r   r   rV  rX  rs  ÚnicÚcfgrD  ÚiprP  Ú	routeentsrn  rQ  ÚrcfgrH   )rl  rm  r`  rI   r‰   G  s’   þì€€þÿ

ÿþÿ
þþ
ýÿ
r‰   c                 C   s   t  | t¡S rƒ   )r   Úlist_from_dependsÚdatasources)ÚdependsrH   rH   rI   Úget_datasource_listî  s   r€  Ú__main__zDo not appear to be on smartos.rº   rŠ   c                 C   sº   ||v r|| S |t v rt | }|  |¡||< || S |dkr@dD ]}t| ||ƒ q!t|d |d |d |d d�||< || S |tv rKt| \}}n|d}}| j||d	�||< || S )
NrŠ   )r   r   r   r   r   r   r   r   rˆ   Fr\   )rk   rl   Úload_keyr‰   rh   rj   )Úclientr×   r_   ÚkeynameÚdepkeyr]   rH   rH   rI   r‚    s*   ë
üú
r‚  r_   )rƒ  r×   r_   T)r  z: )ÚindentÚ	sort_keysÚ
separators)NFr3  rî   )NNNN)CrÎ   rŸ   r¿   r  rÚ   Úloggingr>   rÌ   rñ   rù   Ú	cloudinitr   r   r   r   r   Úcloudinit.eventr   r   Ú	getLoggerr‹   rU   rh   rk   r  r„   ÚDS_NAMEr8   r$  ÚMETADATA_SOCKFILEr}   ÚSERIAL_TIMEOUTr9   r…   rp   rn   r3   r0   Ú	Exceptionr–   r™   rš   ró   r  r  rP   r2  ro   rO   r‰   ÚDEP_FILESYSTEMr~  r€  ÚsysÚjmcÚprintÚexitr¬   Úargvrß   r!  r‚  r_   rv  Ú__annotations__r×   ÚdumpsrH   rH   rH   rI   Ú<module>   sÎ   

òü	ùýÿ
ÿø H 0JY
û
ÿ
3
ÿ 
#ÿ




ÿþÿÎ