U
    W¨+dgU  ã                   @   sî   d Z ddlZddlmZ ddlmZmZ 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 dd	lmZ ddlZddlZdd
lmZ ddlmZ ddlmZmZ dddddddgZG dd„ deƒZ G dd„ deƒZ!G dd„ de	ƒZ"dS )z)
High-level abstraction of an EC2 server
é    N)ÚIObject)ÚBotoConfigPathÚConfig)ÚModel)ÚStringPropertyÚIntegerPropertyÚBooleanPropertyÚCalculatedProperty)Úpropget)ÚZone)ÚKeyPair)Úclosing)ÚEC2ResponseError)ÚsixÚStringIOúm1.smallzm1.largez	m1.xlargeú	c1.mediumz	c1.xlargez
m2.2xlargez
m2.4xlargec                   @   s8   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zddd„Zd
S )ÚBundlerÚrootc                 C   s*   ddl m} || _|| _|||d| _d S )Nr   ©Ú	SSHClient)Úuname)Úboto.manage.cmdshellr   Úserverr   Ú
ssh_client)Úselfr   r   r   © r   ú6/tmp/pip-unpacked-wheel-dlxw5sjy/boto/manage/server.pyÚ__init__-   s    zBundler.__init__c                 C   sr   t dƒ | j ¡  tj |¡\}}d| | _| j || j¡ tj |¡\}}d| | _| j || j¡ t dƒ d S )Nz5	copying cert and pk over to /mnt directory on serverz/mnt/%sú...complete!)	Úprintr   Z	open_sftpÚosÚpathÚsplitÚremote_key_fileZput_fileÚremote_cert_file)r   Úkey_fileÚ	cert_filer"   Únamer   r   r   Ú	copy_x5093   s    


zBundler.copy_x509c                 C   sŽ   d}| j dkrd}|d7 }|d| j| jf 7 }|d| jjj 7 }|d| 7 }|d| 7 }|d	7 }| jjd
ksx| jjdkr‚|d7 }n|d7 }|S )NÚ r   úsudo zec2-bundle-vol z-c %s -k %s z-u %s z-p %s z-s %d z-d /mnt r   r   z-r i386z	-r x86_64)r   r%   r$   r   Ú_reservationZowner_idÚinstance_type)r   ÚprefixÚsizeÚssh_keyÚcommandr   r   r   Úbundle_image>   s    

zBundler.bundle_imagec                 C   sZ   d}| j dkrd}|d7 }|d| 7 }|d| 7 }|d| jjj 7 }|d| jjj 7 }|S )	Nr*   r   r+   zec2-upload-bundle z-m /mnt/%s.manifest.xml z-b %s z-a %s z-s %s )r   r   Úec2Úaws_access_key_idÚaws_secret_access_key)r   Úbucketr.   r0   r1   r   r   r   Úupload_bundleN   s    
zBundler.upload_bundleNTc	                 C   s`  t ƒ }	|s|	 d¡}|s"|	 d¡}|s0|	 d¡}|s>|	 d¡}|sL|	 d¡}|sZ| j ¡ }|  ||¡ |sptƒ }| dt	 ¡ | d¡ |r–| d¡ | |  
|||¡¡ | d	¡ | |  |||¡¡ | d	¡ | d
t	 ¡ | d¡ | ¡ }
tdƒ t|
ƒ | j |
¡}td|d  ƒ td|d  ƒ tdƒ tdƒ | jjj|d||f d| _| jS )NzName of S3 bucketzPrefix for AMI filezPath to RSA private key filezPath to RSA public cert filezSize (in MB) of bundled imagezsudo mv %s /mnt/boto.cfg; z0mv ~/.ssh/authorized_keys /mnt/authorized_keys; zhistory -c; z; zsudo mv /mnt/boto.cfg %s; z.mv /mnt/authorized_keys ~/.ssh/authorized_keysz3running the following command on the remote server:z	%sr   é   r   zregistering image...z%s/%s.manifest.xml)r(   Zimage_location)r   Z
get_stringÚget_filenameZget_intr   Úget_ssh_key_filer)   r   Úwriter   r2   r7   Úgetvaluer    r   Úrunr3   Zregister_imageZimage_id)r   r6   r.   r&   r'   r/   r0   ÚfpZclear_historyÚiobjectr1   Útr   r   r   ÚbundleY   sH    










zBundler.bundle)r   )NNNNNNNT)Ú__name__Ú
__module__Ú__qualname__r   r)   r2   r7   rA   r   r   r   r   r   +   s   
        ÿr   c                   @   sd   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚCommandLineGetterc                 C   s8   g }| j  ¡ D ]$}|j d¡dkr| |j|f¡ q|S )NZpyamir   )r3   Úget_all_imagesÚlocationÚfindÚappend)r   Zmy_amisÚamir   r   r   Úget_ami_list‚   s
    zCommandLineGetter.get_ami_listc                 C   sb   |  dd ¡}t|tƒr*tj |¡}||d< |sP| j d¡}tj |tjj	d|d< |d  
¡ | _d S )NÚregionÚregion_name)Úchoices)ÚgetÚ
isinstanceÚ
basestringÚbotor3   Ú
get_regionÚclsÚfind_propertyr
   ÚregionsÚconnect)r   ÚparamsrL   Úpropr   r   r   rS   Š   s    
zCommandLineGetter.get_regionc                 C   s*   |  dd ¡s&| j d¡}t  |¡|d< d S )Nr(   ©rO   rT   rU   r
   ©r   rX   rY   r   r   r   Úget_name”   s    zCommandLineGetter.get_namec                 C   s*   |  dd ¡s&| j d¡}t  |¡|d< d S )NÚdescriptionrZ   r[   r   r   r   Úget_description™   s    z!CommandLineGetter.get_descriptionc                 C   s,   |  dd ¡s(tddtd}t  |¡|d< d S )Nr-   úInstance Type©r(   Úverbose_namerN   )rO   r   ÚInstanceTypesr
   r[   r   r   r   Úget_instance_typež   s
    ÿz#CommandLineGetter.get_instance_typec                 C   s*   |  dd ¡s&tddd}t  |¡|d< d S )NÚquantityzNumber of Instances©r(   ra   )rO   r   r
   r[   r   r   r   Úget_quantity¤   s    zCommandLineGetter.get_quantityc                 C   s0   |  dd ¡s,tdd| jjd}t  |¡|d< d S )NÚzonezEC2 Availability Zoner`   )rO   r   r3   Zget_all_zonesr
   r[   r   r   r   Úget_zone©   s
    ÿzCommandLineGetter.get_zonec                 C   sx   d}|st|  dd ¡}|s.tddd}t  |¡}z.| j |g¡}t|ƒdkrZd}|d |d< W q tk
rp   Y qX qd S )NFrJ   ZAMIre   r8   Tr   )rO   r   r
   r3   rF   Úlenr   )r   rX   ZvalidrJ   rY   Úrsr   r   r   Ú
get_ami_id¯   s    
zCommandLineGetter.get_ami_idc                 C   sh   |  dd ¡}t|tƒr@| j ¡ }|D ]}|j|kr$|}||d< q$|sdtdd| jjd}t  |¡|d< d S )NÚgroupzEC2 Security Groupr`   )rO   rP   rQ   r3   Zget_all_security_groupsr(   r   r
   )r   rX   rl   Z
group_listÚgrY   r   r   r   Ú	get_group¾   s    



ÿzCommandLineGetter.get_groupc                 C   sn   |  dd ¡}t|tƒrD| j ¡ }|D ]}|j|kr$|j}|j|d< q$|sjtdd| jjd}t  |¡j|d< d S )NÚkeypairzEC2 KeyPairr`   )rO   rP   rQ   r3   Zget_all_key_pairsr(   r   r
   )r   rX   ro   Zkey_listÚkrY   r   r   r   Úget_keyË   s    


ÿzCommandLineGetter.get_keyc                 C   sr   || _ |  |¡ |d  ¡ | _|  |¡ |  |¡ |  |¡ |  |¡ |  |¡ |  	|¡ |  
|¡ |  |¡ d S )NrL   )rT   rS   rW   r3   r\   r^   rc   rh   rf   rk   rn   rq   )r   rT   rX   r   r   r   rO   Ø   s    







zCommandLineGetter.getN)rB   rC   rD   rK   rS   r\   r^   rc   rf   rh   rk   rn   rq   rO   r   r   r   r   rE   €   s   
rE   c                       s   e Zd ZedddZeddZeddZeddZeddZe	d	d
dZ
ededdZededdZededdZededdZededdZededdZededdZededdZededdZededdZededdZg Zg Zedd„ ƒZedUdd„ƒZedVdd„ƒZed d!„ ƒZ dW‡ fd"d#„	Z!d$d%„ Z"d&d'„ Z#d(d)„ Z$d*d+„ Z%d,d-„ Z&d.d/„ Z'd0d1„ Z(d2d3„ Z)d4d5„ Z*d6d7„ Z+d8d9„ Z,‡ fd:d;„Z-‡ fd<d=„Z.d>d?„ Z/d@dA„ Z0dBdC„ Z1dDdE„ Z2dFdG„ Z3dHdI„ Z4dJdK„ Z5dLdM„ Z6dXdOdP„Z7dYdQdR„Z8dSdT„ Z9‡  Z:S )ZÚServerTÚName)Úuniquera   ÚDescription)ra   zEC2 Region NamezEC2 Instance IDzEC2 Elastic IP AddresszIs This Server ProductionF)ra   ÚdefaultzAMI ID)ra   Zcalculated_typeZ
use_methodzAvailability Zone NamezPublic DNS NamezPrivate DNS NamezSecurity GroupszPrimary Security Group NamezKey Namer_   zCurrent StatuszServer Launch TimezConsole Outputc                 C   sj   |  d¡s| d¡ | dd|¡ | dd|¡ |  d¡sD| d¡ | ddd¡ | dd| jjj¡ d S )NZCredentialsr4   r5   Z	DB_ServerZdb_typeZSimpleDBZdb_name)Úhas_sectionÚadd_sectionÚsetÚ_managerÚdomainr(   )rT   Úcfgr4   r5   r   r   r   Úadd_credentials  s    



zServer.add_credentialsNc              	   K   s  |rt |d}| d¡r@| d¡D ]}||kr"| d|¡||< q"tƒ }| | |¡ | d¡}| ¡ }|  ||j|j¡ | d¡}	| d¡}
| d¡}| d¡}|dkr¶| 	d	d
|j
¡ tƒ }| |¡ t|tƒrÖ|j
}t|
tƒræ|
j
}
|	jd| dd¡|
|g| d¡|| ¡ d}g }d}| d¡}|j}|dk	rŽ| ¡ dkrŽ|d }tdƒ t d¡ | ¡ dkrxt d¡ q\| |¡ td| ƒ |D ]€}| ƒ }||_|dkrº| d¡d nt|ƒ|_
| d¡|_|j
|_|j|_|rö|dkrö||_|  ¡  | !|¡ |d7 }q’|S )aq  
        Create a new instance based on the specified configuration file or the specified
        configuration and the passed in parameters.

        If the config_file argument is not None, the configuration is read from there.
        Otherwise, the cfg argument is used.

        The config file may include other config files with a #import reference. The included
        config files must reside in the same directory as the specified file.

        The logical_volume argument, if supplied, will be used to get the current physical
        volume ID and use that as an override of the value specified in the config file. This
        may be useful for debugging purposes when you want to debug with a production config
        file but a test Volume.

        The dictionary argument may be used to override any EC2 configuration values in the
        config file.
        )r"   ZEC2rL   rJ   ro   rl   rg   NZEBSZlogical_volume_namer8   rd   r-   )Z	min_countÚ	max_countÚkey_nameZsecurity_groupsr-   Ú	placementÚ	user_datar   Ú
elastic_ipzEWaiting for instance to start so we can set its elastic IP address...é   Úrunningz.set the elastic IP of the first instance to %sr(   r*   r]   )"r   rw   ÚoptionsrO   rE   rW   r}   r4   r5   ry   r(   r   r;   rP   r   r   r=   r<   Ú	instancesÚ__len__r    ÚtimeÚsleepÚupdateZuse_ipr3   Ústrr]   rM   ÚidÚinstance_idr‚   ÚputrI   )rT   Úconfig_fileZlogical_volumer|   rX   ÚoptionÚgetterrL   r3   rJ   Zkprl   rg   Zcfg_fpÚreservationÚlÚir‚   r†   ÚinstanceÚsr   r   r   Úcreate  sp    










ú


"
zServer.creater*   c           
      C   s¬   t j ¡ }|D ]˜}| ¡ }z| |g¡}W n   g }Y nX t|ƒdkr| ƒ }||_||_||_|j|_||_	|d |_
|j
jD ]}	|	j|kr€|	|_q€| ¡  |  S qd S ©Nr8   r   )rR   r3   rV   rW   Úget_all_reservationsri   r(   r]   rM   r   r,   r†   rŒ   Ú	_instancerŽ   )
rT   r   r(   r]   rV   rL   r3   rj   r–   r•   r   r   r   Úcreate_from_instance_id^  s*    




zServer.create_from_instance_idc           	      C   s¼   g }t j ¡ }|D ]¤}| ¡ }| ¡ }|D ]Š}|jD ]~}z(ttj|j	dƒ t j
 d|j	 ¡ W q4 tk
r°   | ƒ }||_|j	|_|j|_|j	|_||_| ¡  | |¡ Y q4X q4q*q|S )N)r   zServer for %s already exists)rR   r3   rV   rW   r™   r†   Únextrr   rH   rŒ   ÚlogÚinfoÚStopIterationr(   rM   r   r,   rŽ   rI   )	rT   ZserversrV   rL   r3   rj   r’   r•   r–   r   r   r   Úcreate_from_current_instancesv  s(    

z$Server.create_from_current_instancesc                    s>   t t| ƒj|f|Ž d | _d | _d | _d | _d | _|  ¡  d S ©N)	Úsuperrr   r   Ússh_key_filer3   Ú	_cmdshellr,   rš   Ú
_setup_ec2)r   rŒ   Úkw©Ú	__class__r   r   r     s    zServer.__init__c              	   C   sº   | j r| jr| jrd S | jr¶| jr¶tj  ¡ D ]ˆ}|j| jkr,| ¡ | _ | j	r,| js,zL| j  
| j	g¡}t|ƒdkrœ|d jD ] }|j| j	krz|d | _|| _qzW q, tk
r²   Y q,X q,d S r˜   )r3   rš   r,   rŒ   rM   rR   rV   r(   rW   r   r™   ri   r†   r   )r   rL   rj   r•   r   r   r   r¥   –  s"    

zServer._setup_ec2c                 C   s    d}| j r| j  ¡  | j j}|S ©Nr*   )rš   rŠ   Ústate)r   Ústatusr   r   r   Ú_status©  s
    
zServer._statusc                 C   s   d}| j r| j j}|S r©   )rš   Zpublic_dns_name©r   Úhostnamer   r   r   Ú	_hostname°  s    zServer._hostnamec                 C   s   d}| j r| j j}|S r©   )rš   Zprivate_dns_namer­   r   r   r   Ú_private_hostname¶  s    zServer._private_hostnamec                 C   s   d}| j r| j j}|S r©   )rš   r-   )r   Úitr   r   r   Ú_instance_type¼  s    zServer._instance_typec                 C   s   d}| j r| j j}|S r©   )rš   Úlaunch_time)r   Últr   r   r   Ú_launch_timeÂ  s    zServer._launch_timec                 C   s   d}| j r| j  ¡ }|S r©   )rš   Zget_console_output)r   Úcor   r   r   Ú_console_outputÈ  s    
zServer._console_outputc                 C   s   g }| j r| j j}|S r¡   )r,   Úgroups)r   Zgnr   r   r   Ú_groupsÎ  s    zServer._groupsc                 C   s"   |   ¡ }t|ƒdkr|d jS dS )Nr8   r   r*   )r¹   ri   rŒ   )r   r¸   r   r   r   Ú_security_groupÔ  s    
zServer._security_groupc                 C   s   d }| j r| j j}|S r¡   )rš   r€   )r   rg   r   r   r   Ú_zoneÚ  s    zServer._zonec                 C   s   d }| j r| j j}|S r¡   )rš   r   )r   Úknr   r   r   Ú	_key_nameà  s    zServer._key_namec                    s   t t| ƒ ¡  |  ¡  d S r¡   )r¢   rr   rŽ   r¥   ©r   r§   r   r   rŽ   æ  s    z
Server.putc                    s    | j rtdƒ‚tt| ƒ ¡  d S ©Nz Can't delete a production server)Ú
productionÚ
ValueErrorr¢   rr   Údeleter¾   r§   r   r   rÂ   ê  s    zServer.deletec                 C   s"   | j rtdƒ‚| jr| j ¡  d S r¿   )rÀ   rÁ   rš   Ústopr¾   r   r   r   rÃ   ð  s    zServer.stopc                 C   s"   | j rtdƒ‚| jr| j ¡  d S r¿   )rÀ   rÁ   rš   Ú	terminater¾   r   r   r   rÄ   ö  s    zServer.terminatec                 C   s   | j r| j  ¡  d S r¡   )rš   Úrebootr¾   r   r   r   rÅ   ü  s    zServer.rebootc                 C   s   | j dkrt d¡ q d S )Nr„   rƒ   )r«   rˆ   r‰   r¾   r   r   r   Úwait   s    
zServer.waitc                 C   sb   | j s\tj d¡}tj |¡rDtj |d| j ¡}tj |¡rD|| _ | j s\tƒ }| 	d¡| _ | j S )Nz~/.sshz%s.pemzPath to OpenSSH Key file)
r£   r!   r"   Ú
expanduserÚisdirÚjoinr   Úisfiler   r9   )r   Zssh_dirZssh_filer?   r   r   r   r:     s    zServer.get_ssh_key_filec                 C   s,   | j s&ddlm} |  ¡  | | ¡| _ | j S )Nr   )Úcmdshell)r¤   Úboto.managerË   r:   Ústart)r   rË   r   r   r   Úget_cmdshell  s
    zServer.get_cmdshellc                 C   s
   d | _ d S r¡   )r¤   r¾   r   r   r   Úreset_cmdshell  s    zServer.reset_cmdshellc              	   C   s&   t |  ¡ ƒ}| |¡}W 5 Q R X |S r¡   )r   rÎ   r=   )r   r1   Úcmdr«   r   r   r   r=     s    z
Server.runr   c                 C   s   |   ¡  t| |ƒS r¡   )r:   r   )r   r   r   r   r   Úget_bundler  s    zServer.get_bundlerc                 C   s"   ddl m} |  ¡  || ||dS )Nr   r   )r   Ússh_pwd)r   r   r:   )r   r   rÒ   r   r   r   r   Úget_ssh_client#  s    zServer.get_ssh_clientc                 C   s   |   d| ¡S )Nzapt-get -y install %s)r=   )r   Úpkgr   r   r   Úinstall(  s    zServer.install)NNN)r*   )N)r   )r   N);rB   rC   rD   r   r(   r]   rM   r   r‚   r   rÀ   r	   r‹   Zami_idrg   r®   Zprivate_hostnameÚlistr¸   Zsecurity_groupr   r-   r«   r³   ÚopenZconsole_outputÚpackagesZpluginsÚclassmethodr}   r—   r›   r    r   r¥   r¬   r¯   r°   r²   rµ   r·   r¹   rº   r»   r½   rŽ   rÂ   rÃ   rÄ   rÅ   rÆ   r:   rÎ   rÏ   r=   rÑ   rÓ   rÕ   Ú__classcell__r   r   r§   r   rr   å   sh   





P
	

rr   )#Ú__doc__Zboto.ec2rR   Zboto.mashups.iobjectr   Zboto.pyami.configr   r   Zboto.sdb.db.modelr   Zboto.sdb.db.propertyr   r   r   r	   rÌ   r
   Zboto.ec2.zoner   Zboto.ec2.keypairr   r!   rˆ   Ú
contextlibr   Zboto.exceptionr   Zboto.compatr   r   rb   Úobjectr   rE   rr   r   r   r   r   Ú<module>   s*     þUe