3 \�me� � @ s� U d Z ddlZddlZddlZddlmZ ddlmZ ddlm Z m Z ddlmZ ddl mZ ddlmZmZ dd lmZ dd lmZ eje�ZdZdd deegeed�ed�gg d�Zeee�Z ddd�Zddd�Zeeeedd�dd�ZdS )z%Seed Random: Provide random seed data� N)�BytesIO)�dedent)�subp�util)�Cloud)�Config)� MetaSchema�get_meta_doc)�ALL_DISTROS)�PER_INSTANCEaP All cloud instances started from the same image will produce very similar data when they are first booted as they are all starting with the same seed for the kernel's entropy keyring. To avoid this, random seed data can be provided to the instance either as a string or by specifying a command to run to generate the data. Configuration for this module is under the ``random_seed`` config key. If the cloud provides its own random seed data, it will be appended to ``data`` before it is written to ``file``. If the ``command`` key is specified, the given command will be executed. This will happen after ``file`` has been populated. That command's environment will contain the value of the ``file`` key as ``RANDOM_SEED_FILE``. If a command is specified that cannot be run, no error will be reported unless ``command_required`` is set to true. Zcc_seed_randomzSeed RandomzProvide random seed dataz� random_seed: file: /dev/urandom data: my random string encoding: raw command: ['sh', '-c', 'dd if=/dev/urandom of=$RANDOM_SEED_FILE'] command_required: true a_ # To use 'pollinate' to gather data from a remote entropy # server and write it to '/dev/urandom', the following # could be used: random_seed: file: /dev/urandom command: ["pollinate", "--server=http://local.polinate.server"] command_required: true )�id�name�title�descriptionZdistrosZ frequencyZexamplesZactivate_by_schema_keysc C sf | sdS | s|j � d kr$tj| �S |j � dkr:tj| �S |j � dkrVtj| dd d�S td | ��d S ) N� �raw�base64�b64�gzip�gzF)�quiet�decodez Unknown random_seed encoding: %s)r )r r )r r )�lowerr �encode_textr Z b64decodeZdecomp_gzip�IOError)�data�encoding� r �$/usr/lib/python3.6/cc_seed_random.py�_decodeP s r c C sr | r|rt d��n| s&tjd� d S | d }tj|�s^|rNt dj|d���ntjd|� d S tj| |dd� d S ) Nz"no command found but required=truezno command providedr z+command '{cmd}' not found but required=true)�cmdz'command '%s' not found for seed_commandF)�envZcapture)� ValueError�LOG�debugr Zwhich�format)�command�requiredr! r r r r �handle_random_seed_command] s r( )r �cfg�cloud�args�returnc C s |j di �}|j dd�}|j dd�}t� }|rF|jt||j d�d�� |jj}|rnd|krn|jtj|d �� |j� }t |�r�t jd| t |�|� tj||� |j d d �} |j d d�} y$t jj� }||d<