o
    3'fX                  
   @   s   U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ eeZd	d
gedgdZeed< dZdd Zdd Zdede	dededdf
ddZdS )z<Snap: Install, configure and manage snapd and snap packages.    N)subputil)Cloud)Config)
MetaSchema)PER_INSTANCE)prepend_base_commandcc_snapubuntusnap)iddistros	frequencyactivate_by_schema_keysmetac                 C   s   | sdS t d t| tr|  } nt| ts tdj| dtd|g}d	| }| D ]}t d|
ddd	  q,t||d
 tj|dd dS )a  Import list of assertions.

    Import assertions by concatenating each assertion into a
    string separated by a '\n'.  Write this string to a instance file and
    then invoke `snap ack /path/to/file` and check for errors.
    If snap exits 0, then all assertions are imported.
    Nz'Importing user-provided snap assertionsz8assertion parameter was not a list or dict: {assertions})
assertionsack
zSnap acking: %sr      zutf-8T)capture)LOGdebug
isinstancedictvalueslist	TypeErrorformatSNAP_CMDjoinsplitr   
write_fileencoder   )r   assertions_filesnap_cmdcombinedasrt r'   :/usr/lib/python3/dist-packages/cloudinit/config/cc_snap.pyadd_assertions   s"   





r)   c                 C   s   | sdS t d t| trdd t|  D } nt| ts'tdj| dt	d| }g }|D ]*}t|t
}z	tj||d W q0 tjyZ } z|t
| W Y d}~q0d}~ww |rmd	j|d
}tt | t|dS )aH  Run the provided commands provided in snap:commands configuration.

    Commands are run individually. Any errors are collected and reported
    after attempting all commands.

    @param commands: A list or dict containing commands to run. Keys of a
        dict will be used to order the commands provided as dict values.
    Nz#Running user-provided snap commandsc                 S   s   g | ]\}}|qS r'   r'   ).0_vr'   r'   r(   
<listcomp>I   s    z run_commands.<locals>.<listcomp>z5commands parameter was not a list or dict: {commands})commandsr   )shellz.Failures running snap commands:
{cmd_failures})cmd_failures)r   r   r   r   sorteditemsr   r   r   r   strr   ProcessExecutionErrorappendr   logexcRuntimeError)r.   fixed_snap_commandsr0   commandr/   emsgr'   r'   r(   run_commands;   s8   	




r<   namecfgcloudargsreturnc                 C   sV   | di }|std|  d S t| dg tj|j d t	| dg  d S )Nr   z8Skipping module named %s, no 'snap' key in configurationr   zsnapd.assertionsr.   )
getr   r   r)   ospathr   pathsget_ipath_curr<   )r=   r>   r?   r@   cfginr'   r'   r(   handleb   s   
rH   )__doc__loggingrC   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   cloudinit.subpr   	getLogger__name__r   r   __annotations__r   r)   r<   r3   r   rH   r'   r'   r'   r(   <module>   s&   
"'