o
    ȷet                     @   s~   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r=edd	 e Ze  dS 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__ K/var/www/ideatree/venv/lib/python3.10/site-packages/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configinit_logging)r   fp	inst_datakey	user_datar   r   r   r	   -   s   

zBootstrap.write_metadatac                 C   s4   t jd| j  tj| jst| j d S 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   sH  t jddd}|dr5|ddkr |d\}}d| }nd}t jdd	d
}| d||f  d S |drt jdd	d}d}|dkrz|d8 }z| jd|d d}W n tyu } zt j	d| t
d W Y d }~nd }~ww |dksH|ddkr|d\}}nd}| jd| |d d S | d | d|  d S )NBotoboto_updatezsvn:HEADsvn:r   z-r%sz-rHEAD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   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locationnum_remaining_attemptser   r   r   	load_botoC   s8   



zBootstrap.load_botoc                 C   sj   z$ddl m} ||}tj| j|dd }t|d|	  W |S    t
jd|  d }Y |S )Nr   )
fetch_file/r   zProblem Retrieving file: %s)
boto.utilsr?   r   r   joinr   r3   r   r   readr   r    	exception)r   s3_filer?   fr   r   r   r   fetch_s3_fileb   s   zBootstrap.fetch_s3_filec                 C   sj   t jdd}|r1|d}|D ]"}| }|dr | |}|r0|ds0| jd| dd qd S d S )	NPyamipackages,zs3:z.pyzeasy_install -Z %sF)exit_on_error)	r   r   r0   r3   stripr1   rH   endswithr4   )r   package_strrJ   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$   r>   rQ   notifyr   r   get_instancer   r   r   r   main{   s   zBootstrap.main)__name__
__module____qualname____doc__r   r	   r$   r>   rH   rQ   rT   __classcell__r   r   r   r   r      s    
r   __main__	bootstrapz/var/log/boto.log)r   r   rB   r   r   boto.pyami.configr   r   boto.pyami.scriptbaser   r6   r   rU   set_file_loggerbsrT   r   r   r   r   <module>   s   d