U
    W¨+dt  ã                   @   sz   d dl Z d dlZd dlmZmZ d dlmZmZ d dlm	Z	 d dl
Z
G dd„ de	ƒZedkrve dd	¡ eƒ Ze ¡  dS )
é    N)Úget_instance_metadataÚget_instance_userdata)ÚConfigÚBotoConfigPath)Ú
ScriptBasec                       sP   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
‡  ZS )Ú	Bootstrapa\  
    The Bootstrap class is instantiated and run as part of the PyAMI
    instance initialization process.  The methods in this class will
    be run from the rc.local script of the instance and will be run
    as the root user.

    The main purpose of this class is to make sure the boto distribution
    on the instance is the one required.
    c                    s    d| _ |  ¡  tt| ƒ ¡  d S )Nz
/mnt/pyami)Úworking_dirÚwrite_metadataÚsuperr   Ú__init__©Úself©Ú	__class__© ú8/tmp/pip-unpacked-wheel-dlxw5sjy/boto/pyami/bootstrap.pyr   (   s    zBootstrap.__init__c                 C   sŒ   t tj t¡dƒ}| d¡ tƒ }|D ]}| d||| f ¡ q&tƒ }| d| ¡ | d¡ | d| j ¡ | 	¡  t
ƒ t_t ¡  d S )NÚwz[Instance]
z%s = %s
z
%s
z[Pyami]
zworking_dir = %s
)ÚopenÚosÚpathÚ
expanduserr   Úwriter   r   r   Úcloser   ÚbotoÚconfigZinit_logging)r   ÚfpZ	inst_dataÚkeyÚ	user_datar   r   r   r	   -   s    

zBootstrap.write_metadatac                 C   s0   t j d| j ¡ tj | j¡s,t | j¡ d S )NzWorking directory: %s)r   ÚlogÚinfor   r   r   ÚexistsÚmkdirr   r   r   r   Úcreate_working_dir>   s    zBootstrap.create_working_dirc              
   C   s@  t j ddd¡}| d¡rh| d¡dkr@| d¡\}}d| }nd}t j dd	d
¡}|  d||f ¡ nÔ| d¡r$t j dd	d¡}d}|dkrì|d8 }z| jd|d d}W qˆ tk
rè } zt j 	d|¡ t
 d¡ W 5 d }~X Y qˆX qˆ| d¡dkr| d¡\}}nd}| jd| |d n|  d¡ |  d| ¡ d S )NZBotoZboto_updatezsvn:HEADÚsvnú:r   z-r%sz-rHEADZboto_locationz/usr/local/botozsvn update %s %sÚgitz*/usr/share/python-support/python-boto/botoé
   é   zgit pull)ÚcwdzOgit pull attempt failed with the following exception. Trying again in a bit. %sé   Zmasterzgit checkout %sz.rm /usr/local/lib/python2.5/site-packages/botozeasy_install %s)r   r   ÚgetÚ
startswithÚfindÚsplitÚrunÚ	Exceptionr   r   ÚtimeÚsleep)r   ÚupdateÚmethodÚversionÚlocationZnum_remaining_attemptsÚer   r   r   Ú	load_botoC   s2    


zBootstrap.load_botoc                 C   sl   zFddl m} ||ƒ}tj | j| d¡d ¡}t|dƒ | 	¡ ¡ W n    t
j d| ¡ d }Y nX |S )Nr   )Ú
fetch_fileú/éÿÿÿÿr   zProblem Retrieving file: %s)Ú
boto.utilsr8   r   r   Újoinr   r-   r   r   Úreadr   r   Ú	exception)r   Zs3_filer8   Úfr   r   r   r   Úfetch_s3_fileb   s    
zBootstrap.fetch_s3_filec                 C   sf   t j dd¡}|rb| d¡}|D ]@}| ¡ }| d¡r@|  |¡}|r | d¡s | jd| dd q d S )	NZPyamiÚpackagesú,zs3:z.pyzeasy_install -Z %sF)Zexit_on_error)	r   r   r*   r-   Ústripr+   r@   Úendswithr.   )r   Zpackage_strrA   Úpackager   r   r   Úload_packagesm   s    



zBootstrap.load_packagesc                 C   s2   |   ¡  |  ¡  |  ¡  |  dtj d¡ ¡ d S )NzBootstrap Completed for %szinstance-id)r"   r7   rF   Únotifyr   r   Zget_instancer   r   r   r   Úmain{   s    zBootstrap.main)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r"   r7   r@   rF   rH   Ú__classcell__r   r   r   r   r      s   
r   Ú__main__Z	bootstrapz/var/log/boto.log)r   r   r;   r   r   Zboto.pyami.configr   r   Zboto.pyami.scriptbaser   r0   r   rI   Zset_file_loggerÚbsrH   r   r   r   r   Ú<module>   s   d