U
    Ad                  	   @   s   U d 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ZddgZdddeeeedgg dZeed< eeZ dd Zeee
eeddddZdS )z5Grub Dpkg: Configure grub debconf installation device    N)Logger)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCE)ProcessExecutionErrora  Configure which device is used as the target for grub installation. This module
can be enabled/disabled using the ``enabled`` config key in the ``grub_dpkg``
config dict. The global config key ``grub-dpkg`` is an alias for ``grub_dpkg``.
If no installation device is specified this module will execute grub-probe to
determine which disk the /boot directory is associated with.

The value which is placed into the debconf database is in the format which the
grub postinstall script expects. Normally, this is a /dev/disk/by-id/ value,
but we do fallback to the plain disk name if a by-id name is not present.

If this module is executed inside a container, then the debconf database is
seeded with empty values, and install_devices_empty is set to true.
ZubuntuZdebianZcc_grub_dpkgz	Grub Dpkgz*Configure grub debconf installation devicez            grub_dpkg:
              enabled: true
              grub-pc/install_devices: /dev/sda
              grub-pc/install_devices_empty: false
            )idnametitleZdescriptiondistrosZ	frequencyZexamplesZactivate_by_schema_keysmetac              
   C   sT  d}g }z"t j ddddgddd  }W np tk
r| } z4t|jtrT| d	 nd
|jkrj| d n W 5 d}~X Y n  tk
r   t	
| d Y nX |rtj|sdS z(t j dddd|gddd   }W n" tk
r   t	
| d| Y nX | dd| dd |D }| dd| |r@t|d n|}| d| |S )z
    Fetches the /dev/disk/by-id device grub is installed to.
    Falls back to plain disk name if no by-id entry is present.
     z
grub-probez-tdiskz/bootT)Zcapturer   z'grub-probe' not found in $PATHzfailed to get canonical pathz)grub-probe 'failed to get canonical path'Nz*grub-probe failed to execute for grub-dpkgZudevadminfoz--rootz--query=symlinkz3udevadm DEVLINKS symlink query failed for disk='%s'z%considering these device symlinks: %s,c                 S   s   g | ]}d |kr|qS )z
disk/by-id ).0Zdevr   r   ?/usr/lib/python3/dist-packages/cloudinit/config/cc_grub_dpkg.py
<listcomp>o   s      zfetch_idevs.<locals>.<listcomp>z)filtered to these disk/by-id symlinks: %szselected %s)r   stripr   
isinstancereasonFileNotFoundErrordebugstderr	Exceptionr   logexcospathexistssplitjoinsorted)logr   Zdeviceseidevsr   r   r   fetch_idevs<   sH    

  
r*   )r   cfgcloudr'   argsreturnc           
      C   s   | d| di }|si }| dd}t|rD|d| | d S t|dd }|d krbt|}| d}|d kr|| }nt|tst|}t	|
 }d||f }	|d	||f  ztd
g|	 W n  tk
r   t|d Y nX d S )NZ	grub_dpkgz	grub-dpkgenabledTz*%s disabled by config grub_dpkg/enabled=%szgrub-pc/install_deviceszgrub-pc/install_devices_emptyz[grub-pc grub-pc/install_devices string %s
grub-pc grub-pc/install_devices_empty boolean %s
z2Setting grub debconf-set-selections with '%s','%s'zdebconf-set-selectionsz2Failed to run debconf-set-selections for grub-dpkg)getr   Zis_falser   Zget_cfg_option_strr*   r   boolZtranslate_boolstrlowerr   r   r    )
r   r+   r,   r'   r-   Zmycfgr/   r)   Zidevs_emptyZ	dconf_selr   r   r   handlex   s:    



r4   )__doc__r!   Zloggingr   textwrapr   Z	cloudinitr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r	   Zcloudinit.settingsr
   Zcloudinit.subpr   ZMODULE_DESCRIPTIONr   r   __annotations__r*   r2   listr4   r   r   r   r   <module>	   s@   
=    