
    f-                     F   U d Z ddlZddl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j*                  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 Zd Zdedede	deddf
dZy)z<Snap: Install, configure and manage snapd and snap packages.    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCE)prepend_base_commandubuntucc_snap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titledescriptiondistrosexamples	frequencyactivate_by_schema_keysmetac                    | syt         j                  d       t        | t              r| j	                         } n+t        | t
              st        dj                  |             t        d|g}dj                  |       }| D ]*  }t         j                  d|j                  d      dd	        , t        j                  ||j                  d
             t        j                  |d       y)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})
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asrts        :/usr/lib/python3/dist-packages/cloudinit/config/cc_snap.pyadd_assertionsr1   v   s     II78*d#&&(

D)FMM% N 
 	
 %1Hyy$H <		#TZZ%5a%:;< 	OOOX__W%=>IIh%    c                 n   | syt         j                  d       t        | t              r,t	        | j                               D cg c]  \  }}|	 } }}n+t        | t              st        dj                  |             t        d|       }g }|D ]*  }t        |t              }	 t        j                  ||       , |r7dj                  |      }t        j                  t         |       t!        |      yc c}}w # t        j                  $ r$}|j                  t        |             Y d}~d}~ww xY w)	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 commandsz5commands 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)	r4   _vfixed_snap_commandsr6   commandr5   emsgs	            r0   run_commandsrD      s$    II34(D!"()9":;$!QA;;$'CJJ! K 
 	
 /vx@L& (7C(	(IIgU+( ?FF% G 
 	C3 # < )) 	(A''	(s   C7$C==D4D//D4r   cfgcloudargsreturnc                 ^   |j                  di       }|st        j                  d|        y t        j                  |       t        |j                  dg       t        j                  j                  |j                  j                         d             t        |j                  dg              y )Nr   z8Skipping module named %s, no 'snap' key in configurationr   zsnapd.assertionsr4   )getr   r    r   wait_for_snap_seededr1   ospathr(   pathsget_ipath_currD   )r   rE   rF   rG   cfgins        r0   handlerQ      s    GGFBE		F	
 	e$		,#
U[[..02DE :r*+r2   ) __doc__loggingrL   textwrapr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r	   cloudinit.settingsr
   cloudinit.subpr   r   r   	getLogger__name__r   r   __annotations__r'   r1   rD   r9   r$   rQ    r2   r0   <module>r_      s  
 C  	    ! # < + /*	g! D	< 	
 	
	
 	
	
 			
O1d  &xmWj Wt t
&<$ N, ,6 ,% ,t , ,r2   