Your IP : 216.73.216.218


Current Path : /usr/lib/python3/dist-packages/uaclient/clouds/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/clouds/__pycache__/gcp.cpython-38.pyc

U

8�-dt�@s�ddlZddlZddlZddlZddlmZmZmZmZddl	m
Z
ddlmZm
Z
mZmZddlmZe�d�ZdZdZd	Zd
ZdZdZd
dddd�ZGdd�de�ZdS)�N)�Any�Dict�List�Optional)�	HTTPError)�
exceptions�messages�system�util)�AutoAttachCloudInstancezpro.clouds.gcpz�http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=contracts.canonical.com&format=full&licenses=TRUEzThttp://metadata.google.internal/computeMetadata/v1/instance/licenses/?recursive=truez&wait_for_change=truez&last_etag={etag}z/sys/class/dmi/id/product_namezGoogle Compute EngineZ8045211386737108299Z6022427724719891830Z599959289349842382Z2592866803419978320)ZxenialZbionicZfocalZjammyc@s�eZdZdd�Zeejejdddgd�e	e
efd�dd���Zee
d�d	d
��Z
eed�dd��Zee
d�d
d�Zed�dd�Zeed�dd�ZdS)�UAAutoAttachGCPInstancecCs
d|_dS)N��etag��self�r�5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py�__init__%sz UAAutoAttachGCPInstance.__init__g�?�)Zretry_sleeps)�returnc
Cs�z ddi}tjt|dd�\}}Wn�tk
r�}z�t|dd�}d}|r�ztj|tjd�}Wntk
rrd}YnX|r�|�	dd�}|r�|n|j
}d}	|r�d|��kr�tj
jj|d	�}tj
j}	tj||	t|d
d�d��W5d}~XYnXd
|iS)N�Metadata-Flavor�Googler)�headersZtimeout�body)�clsZerror_descriptionzservice account)Z	error_msg�coder)�msg�msg_coder�
identityToken)r
�readurl�	TOKEN_URLr�getattr�json�loadsZDatetimeAwareJSONDecoder�
ValueError�get�reason�lowerrZ"GCP_SERVICE_ACCT_NOT_ENABLED_ERRORr�format�namer�GCPProAccountError)
rrZurl_responseZ_headers�erZ
error_descZ
error_detailsrrrrr�identity_doc,sB��

�
�z$UAAutoAttachGCPInstance.identity_doccCsdS)NZgcprrrrr�
cloud_typeLsz"UAAutoAttachGCPInstance.cloud_typecCs*tj�t�r&t�t�}t|��kr&dSdS)z$This machine is a viable GCPInstanceTF)�os�path�exists�DMI_PRODUCT_NAMEr	Z	load_file�GCP_PRODUCT_NAME�strip)rZproduct_namerrr�	is_viablePs

z!UAAutoAttachGCPInstance.is_viablecCsN|jd}t�|�d�dd�}t�|�d��}|�di��di��dg�S)	z�Get a list of licenses from the GCP metadata.

        Instance identity token (jwt) carries a list of licenses
        associated with the instance itself.

        Returns an empty list if licenses are not present in the metadata.
        r�.rz===zutf-8ZgoogleZcompute_engineZ
license_id)r,�base64Zurlsafe_b64decode�splitr"r#�decoder%)r�tokenZidentityZ
identity_dictrrr�get_licenses_from_identityZs
���z2UAAutoAttachGCPInstance.get_licenses_from_identitycCs&t��d}|tkr"t�d�dSdS)N�seriesz0This series isn't supported for GCP auto-attach.FT)r	�get_platform_info�GCP_LICENSES�LOG�info)rr;rrr�should_poll_for_pro_licenseks

z3UAAutoAttachGCPInstance.should_poll_for_pro_license)�wait_for_changerc
Cs�t}|r(|t7}|jr(|tj|jd�7}ztj|ddid�\}}WnHtk
r�}z*t�	|�|j
dkrrt���nt�
��W5d}~XYnXdd�|D�}|�dd�|_t��d	}t�|�|kS)
Nr
rr)ri�cSsg|]}|d�qS)�idr)�.0�licenserrr�
<listcomp>�szBUAAutoAttachGCPInstance.is_pro_license_present.<locals>.<listcomp>ZETagr;)�LICENSES_URL�WAIT_FOR_CHANGEr�	LAST_ETAGr(r
rrr>�errorrrZCancelProLicensePollingZDelayProLicensePollingr%r	r<r=)rrAZurlZlicensesrr+Zlicense_idsr;rrr�is_pro_license_presentrs&�


z.UAAutoAttachGCPInstance.is_pro_license_presentN)�__name__�
__module__�__qualname__r�propertyr
Zretryrr*r�strrr,r-�boolr4rr:r@rJrrrrr$s	r)r6r"Zloggingr.�typingrrrrZurllib.errorrZuaclientrrr	r
Zuaclient.cloudsrZ	getLoggerr>r rFrGrHr1r2r=rrrrr�<module>s,
���