U
    w7eR                  	   @   s"  U d Z ddlZddlmZ ddlmZ ddlmZ zddlZdZ	W n e
k
r^   dZdZ	Y nX ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZmZ ddlmZ eeZdgZdddedeedgedgdZeed< eeZ dZdZ dZ!edddZ"e#eee$ddddZ%dS )z<Ubuntu Drivers: Interact with third party drivers in Ubuntu.    N)dedent)Cloud)DistroTF)log)subp
temp_utils
type_utilsutil)Config)
MetaSchemaget_meta_doc)PER_INSTANCEZubuntuZcc_ubuntu_driverszUbuntu Driversz,Interact with third party drivers in Ubuntu.zo        This module interacts with the 'ubuntu-drivers' command to install
        third party driver packages.zN        drivers:
          nvidia:
            license-accepted: true
        drivers)idnametitleZdescriptiondistrosZexamplesZ	frequencyZactivate_by_schema_keysmetazDubuntu-drivers: error: argument <command>: invalid choice: 'install'zTemplate: linux/nvidia/latelink
Type: boolean
Default: true
Description: Late-link NVIDIA kernel modules?
 Enable this to link the NVIDIA kernel modules in cloud-init and
 make them available for use.
X_LOADTEMPLATEFILE)distroc              
   C   s  t | ts tdt| | f d}tt| |}|sLt	d|| d S t
dsjt	d |dg d}t| d}|r|d	|7 }t	d
|||r|nd tj| dd}tj|d}zpz2t|t td}	|	t| W 5 Q R X W n8 tk
r0 }
 zttdt|
  W 5 d }
~
X Y nX W 5 tj|rNt| X zt

ddd|g W nV t
jk
r } z4t|j krt!d nd|j"krt!d  W 5 d }~X Y nX d S )Nz.'drivers' config expected dict, found '%s': %sznvidia/license-acceptedz$Not installing NVIDIA drivers. %s=%szubuntu-driverszI'ubuntu-drivers' command not available.  Installing ubuntu-drivers-commonzubuntu-drivers-commonZnvidiaznvidia/versionz:{}z<Installing and activating NVIDIA drivers (%s=%s, version=%s)ZlatestT)dirZ	needs_exeznvidia.templatez
cloud-initz.Failed to register NVIDIA debconf template: %sinstallz--gpgpuz[the available version of ubuntu-drivers is too old to perform requested driver installationz"No drivers found for installation.z0ubuntu-drivers found no drivers for installation)#
isinstancedict	TypeErrorr   Zobj_namer	   Ztranslate_boolZget_cfg_by_pathLOGdebugr   Zwhichformatr   ZmkdtempZget_tmp_exec_pathospathjoinisdirZdel_dirZ
write_fileNVIDIA_DEBCONF_CONTENTdebconfZDebconfCommunicatorZcommandr   	ExceptionZlogexcstrZProcessExecutionError OLD_UBUNTU_DRIVERS_STDERR_NEEDLEstderrwarningstdout)cfgZpkg_install_funcr   ZcfgpathZnv_accZ
driver_argZversion_cfgZtdirZdebconf_fileZdceexc r-   D/usr/lib/python3/dist-packages/cloudinit/config/cc_ubuntu_drivers.pyinstall_driversN   sf    



  
r/   )r   r*   cloudargsreturnc                 C   sF   d|krt d|  d S ts,t d|  d S t|d |jj|j d S )Nr   z4Skipping module named %s, no 'drivers' key in configz<Skipping module named %s, 'python3-debconf' is not installed)r   r   HAS_DEBCONFr(   r/   r   Zinstall_packages)r   r*   r0   r1   r-   r-   r.   handle   s      r4   )&__doc__r   textwrapr   Zcloudinit.cloudr   Zcloudinit.distrosr   r#   r3   ImportErrorZ	cloudinitr   Zloggingr   r   r   r	   Zcloudinit.configr
   Zcloudinit.config.schemar   r   Zcloudinit.settingsr   Z	getLogger__name__r   r   r   __annotations__r&   r"   r   r/   r%   listr4   r-   r-   r-   r.   <module>   sL   

	

@