3 \�me< � @ s U d Z ddlZddlZddlmZ ddlmZ ddlmZ yddl Z dZ W n ek rf dZ dZ Y nX ddlm Z mZmZmZ dd lmZ dd lmZmZ ddlmZ eje�ZdgZd dded�eed�gedgd�Zeee�Z dZdZdZed�dd�Z e!eee"dd�dd�Z#dS )z<Ubuntu Drivers: Interact with third party drivers in Ubuntu.� N)�dedent)�Cloud)�DistroTF)�subp� temp_utils� type_utils�util)�Config)� MetaSchema�get_meta_doc)�PER_INSTANCEZubuntuZcc_ubuntu_driverszUbuntu 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�title�description�distrosZexamplesZ frequencyZactivate_by_schema_keyszDubuntu-drivers: error: argument <command>: invalid choice: 'install'z�Template: 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 tdtj| �| f ��d}tjtj| |��}|sLtj d||� d S t jd�sjtj d� |dg� d}tj| d�}|r�|d j|�7 }tj d |||r�|nd� t j|j� dd �}tjj|d�}zpy2tj|t� tjd��} | jt|� W d Q R X W n8 tk �r0 } ztjtdt| �� � W Y d d } ~ X nX W d tjj|��rNtj|� X yt j ddd|g� W nV t jk �r� } z6t|j k�r�tj!d� nd|j"k�r�tj!d� � W Y d d }~X nX d S )Nz.'drivers' config expected dict, found '%s': %sznvidia/license-acceptedz$Not installing NVIDIA drivers. %s=%szubuntu-driverszI'ubuntu-drivers' command not available. Installing ubuntu-drivers-commonzubuntu-drivers-commonZnvidiaznvidia/versionz:{}z<Installing and activating NVIDIA drivers (%s=%s, version=%s)ZlatestT)�dirZ needs_exeznvidia.templatez cloud-initz.Failed to register NVIDIA debconf template: %sZinstallz--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 Zobj_namer Ztranslate_boolZget_cfg_by_path�LOG�debugr Zwhich�formatr ZmkdtempZget_tmp_exec_path�os�path�joinZ write_file�NVIDIA_DEBCONF_CONTENT�debconfZDebconfCommunicatorZcommandr � ExceptionZlogexc�str�isdirZdel_dirZProcessExecutionError� OLD_UBUNTU_DRIVERS_STDERR_NEEDLE�stderr�warning�stdout)�cfgZpkg_install_funcr ZcfgpathZnv_accZ driver_argZversion_cfgZtdirZdebconf_fileZdc�e�exc� r+ �'/usr/lib/python3.6/cc_ubuntu_drivers.py�install_driversN sV r- )r r( �cloud�args�returnc C sF d|krt jd| � d S ts,t jd| � d S t|d |jj|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&