← Back
Editing: gcp.cpython-38.pyc
U ��g( � @ s� d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl m Z mZmZm Z mZ d dlmZ e�e�e��ZdZdZdZdZd Zd Zdgdd gddgddgddgd�ZG dd� de�ZdS )� N)�Any�Dict�List�Optional)� exceptions�http�secret_manager�system�util)�PublicCloudAutoAttachInstancez�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 EngineZ8045211386737108299Z6022427724719891830Z7427070211152946628Z599959289349842382Z7818678586103571931Z2592866803419978320Z160084055690847662Z2176054482269786025Z4168323874790319525)ZxenialZbionicZfocalZjammyZnoblec @ s� e Zd Zdd� Zeejejdddgd�e e ef d�dd���Zee d�d d ��Z eed�dd��Zee d�d d�Zed�dd�Zeed�dd�ZdS )�GCPAutoAttachInstancec C s d | _ d S )N��etag��self� r �5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py�__init__% s zGCPAutoAttachInstance.__init__g �?� )Zretry_sleeps)�returnc C sx t jtddidd�}|jdkr6tj�|j� d|jiS |j� d�}|rbd|� � krbtj|j|d ��tj |j|jd ��d S )N�Metadata-Flavor�Googler )�headersZtimeout�� � identityTokenZerror_descriptionzservice account)Zstatus_codeZ error_msg)�code�body)r �readurl� TOKEN_URLr r ZsecretsZ add_secretr Z json_dict�get�lowerr ZGCPServiceAccountError�CloudMetadataError)r �responseZ error_descr r r �identity_doc, s$ � � �z"GCPAutoAttachInstance.identity_docc C s dS )NZgcpr r r r r � cloud_type? s z GCPAutoAttachInstance.cloud_typec C s* t j�t�r&t�t�}t|�� kr&dS dS )z$This machine is a viable GCPInstanceTF)�os�path�exists�DMI_PRODUCT_NAMEr Z load_file�GCP_PRODUCT_NAME�strip)r Zproduct_namer r r � is_viableC s zGCPAutoAttachInstance.is_viablec C sN | j d }t�|�d�d d �}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 �.r z===zutf-8ZgoogleZcompute_engineZ license_id)r# �base64Zurlsafe_b64decode�split�json�loads�decoder )r �tokenZidentityZ identity_dictr r r �get_licenses_from_identityM s � ��z0GCPAutoAttachInstance.get_licenses_from_identityc C s$ t �� j}|tkr t�d� dS dS )Nz0This series isn't supported for GCP auto-attach.FT)r �get_release_info�series�GCP_LICENSES�LOG�info)r r5 r r r �should_poll_for_pro_license^ s z1GCPAutoAttachInstance.should_poll_for_pro_license)�wait_for_changer c C s� t }|r(|t7 }| jr(|tj| jd�7 }tj|ddid�}|jdkr�dd� |jD �}|j � d�| _t�� j }t� |g �D ]}||krx d S qxd S t�|j� |jdkr�t�� �nt�� �d S )Nr r r )r r c S s g | ]}|d �qS )�idr )�.0�licenser r r � <listcomp>o s z@GCPAutoAttachInstance.is_pro_license_present.<locals>.<listcomp>r TFi� )�LICENSES_URL�WAIT_FOR_CHANGEr � LAST_ETAG�formatr r r Z json_listr r r r4 r5 r6 r7 �errorr r ZCancelProLicensePollingZDelayProLicensePolling)r r: Zurlr"