U
    W¨+dÙ  ã                   @   sV   d dl Zd dlZd dlZd dlZd dlZd dlZd dlmZ G dd„ dejj	j
ƒZ
dS )é    N)Úgetpwnamc                   @   sJ   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )Ú	Installerz5
    Base Installer class for Ubuntu-based AMI's
    Ú*ÚrootNc
              
   C   sŠ   |dkrt t d¡ƒ}|dkr,t t d¡ƒ}td| dƒ}
|	rb|	 ¡ D ]\}}|
 d||f ¡ qF|
 d|||||||f ¡ |
 ¡  dS )	zÐ
        Write a file to /etc/cron.d to schedule a command
            env is a dict containing environment variables you want to set in the file
            name will be used as the name of the file
        Úrandomé<   é   z/etc/cron.d/%sÚwz%s=%s
z%s %s %s %s %s %s %s
N)Ústrr   Ú	randrangeÚopenÚitemsÚwriteÚclose)ÚselfÚnameÚcommandÚminuteÚhourÚmdayÚmonthZwdayZwhoÚenvÚfpÚkeyÚvalue© r   úJ/tmp/pip-unpacked-wheel-dlxw5sjy/boto/pyami/installers/ubuntu/installer.pyÚadd_cron"   s    zInstaller.add_cronc                 C   sV   t j d|¡}t|dƒ}| |¡ | ¡  t  |tjtj	B tj
B ¡ |  d| ¡ dS )z7
        Add this file to the init.d directory
        z/etc/init.dr	   z!/usr/sbin/update-rc.d %s defaultsN)ÚosÚpathÚjoinr   r   r   ÚchmodÚstatÚS_IREADÚS_IWRITEÚS_IEXECÚrun)r   Úfiler   Zf_pathÚfr   r   r   Úadd_init_script3   s    

zInstaller.add_init_scriptc                 C   sb   t j d||f ¡ tj d¡s0| jdddd tddƒ}| d||f ¡ | 	¡  |tj
|< d	S )
z
        Add an environemnt variable
        For Ubuntu, the best place is /etc/environment.  Values placed here do
        not need to be exported.
        zAdding env variable: %s=%sz/etc/environment.origz)cp /etc/environment /etc/environment.origF)ÚnotifyZexit_on_errorz/etc/environmentÚaz
%s="%s"N)ÚbotoÚlogÚinfor   r   Úexistsr&   r   r   r   Úenviron)r   r   r   r   r   r   r   Úadd_env>   s    
zInstaller.add_envc                 C   s   |   d| ¡ d S )Nz/etc/init.d/%s stop©r&   ©r   Zservice_namer   r   r   ÚstopL   s    zInstaller.stopc                 C   s   |   d| ¡ d S )Nz/etc/init.d/%s startr2   r3   r   r   r   ÚstartO   s    zInstaller.startc                 C   s   |   d| ¡ t|ƒ}|S )z3
        Create a user on the local system
        zuseradd -m %s)r&   r   )r   ÚuserZusrr   r   r   Úcreate_userR   s    zInstaller.create_userc                 C   s   t ‚dS )z>
        This is the only method you need to override
        N)ÚNotImplementedError)r   r   r   r   ÚinstallZ   s    zInstaller.install)r   r   r   r   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   r1   r4   r5   r7   r9   r   r   r   r   r      s   
r   )Zboto.pyami.installersr,   r   Úos.pathr"   r   Úpwdr   ZpyamiZ
installersr   r   r   r   r   Ú<module>   s   