U
    Ld]                  
   @   s  U d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ dgZeZeeZdddedeededededgedgdZeed< eeZ dZdd Zdd Zeeee ddddZ!dS )z<Snap: Install, configure and manage snapd and snap packages.    N)dedent)log)subputil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCE)prepend_base_commandZubuntuZcc_snapZSnapz5Install, configure and manage snapd and snap packagesah          This module provides a simple configuration namespace in cloud-init to
        both setup snapd and install snaps.

        .. note::
            Both ``assertions`` and ``commands`` values can be either a
            dictionary or a list. If these configs are provided as a
            dictionary, the keys are only used to order the execution of the
            assertions or commands and the dictionary is merged with any
            vendor-data snap configuration provided. If a list is provided by
            the user instead of a dict, any vendor-data snap configuration is
            ignored.

        The ``assertions`` configuration option is a dictionary or list of
        properly-signed snap assertions which will run before any snap
        ``commands``. They will be added to snapd's assertion database by
        invoking ``snap ack <aggregate_assertion_file>``.

        Snap ``commands`` is a dictionary or list of individual snap
        commands to run on the target system. These commands can be used to
        create snap users, install snaps and provide snap configuration.

        .. note::
            If 'side-loading' private/unpublished snaps on an instance, it is
            best to create a snap seed directory and seed.yaml manifest in
            **/var/lib/snapd/seed/** which snapd automatically installs on
            startup.
        ax          snap:
            assertions:
              00: |
                signed_assertion_blob_here
              02: |
                signed_assertion_blob_here
            commands:
              00: snap create-user --sudoer --known <snap-user>@mydomain.com
              01: snap install canonical-livepatch
              02: canonical-livepatch enable <AUTH_TOKEN>
    ay          # Convenience: the snap command can be omitted when specifying commands
        # as a list and 'snap' will automatically be prepended.
        # The following commands are equivalent:
        snap:
          commands:
            00: ['install', 'vlc']
            01: ['snap', 'install', 'vlc']
            02: snap install vlc
            03: 'snap install vlc'
    z        # You can use a list of commands
        snap:
          commands:
            - ['install', 'vlc']
            - ['snap', 'install', 'vlc']
            - snap install vlc
            - 'snap install vlc'
    z        # You can use a list of assertions
        snap:
          assertions:
            - signed_assertion_blob_here
            - |
              signed_assertion_blob_here
    snap)idnametitleZdescriptiondistrosZexamples	frequencyZ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	  qXt||d
 tj|dd dS )a  Import list of assertions.

    Import assertions by concatenating each assertion into a
    string separated by a '
'.  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})
assertionsZack
zSnap acking: %sr      zutf-8T)Zcapture)LOGdebug
isinstancedictvalueslist	TypeErrorformatSNAP_CMDjoinsplitr   Z
write_fileencoder   )r   Zassertions_fileZsnap_cmdZcombinedZasrt r"   :/usr/lib/python3/dist-packages/cloudinit/config/cc_snap.pyadd_assertionsw   s"    





r$   c                 C   s   | sdS t d t| tr4dd t|  D } nt| tsNtdj| dt	d| }g }|D ]Z}t|t
}ztj||tjjd W q` tjk
r } z|t
| W 5 d}~X Y q`X q`|rd	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>   s     z run_commands.<locals>.<listcomp>z5commands parameter was not a list or dict: {commands})commandsr   )shellZ	status_cbz.Failures running snap commands:
{cmd_failures})cmd_failures)r   r   r   r   sorteditemsr   r   r   r   strr   sysstderrwriteZProcessExecutionErrorappendr   ZlogexcRuntimeError)r)   Zfixed_snap_commandsr+   Zcommandr*   emsgr"   r"   r#   run_commands   s2    	




"r6   )r   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   pathsZget_ipath_curr6   )r   r7   r8   r9   Zcfginr"   r"   r#   handle   s     
r?   )"__doc__r<   r/   textwrapr   Z	cloudinitr   Zloggingr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r	   Zcloudinit.settingsr
   Zcloudinit.subpr   r   r   Z	getLogger__name__r   r   __annotations__r   r$   r6   r.   r   r?   r"   r"   r"   r#   <module>   sR   
2Z'