U
    3ìdô6  ã                   @   sŒ   d 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l
mZ ddlmZ ddlmZ ddlZG d	d
„ d
eƒZG dd„ deƒZdS )z)
High-level abstraction of an EC2 server
é    N)ÚStringIO)ÚIObject)ÚConfigÚBotoConfigPath)Úinteractive_shell)ÚModel)ÚStringPropertyc                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú	ServerSetc                 C   sb   g }d}| D ]@}z$t ||ƒ}t|ƒr(d}| |¡ W q   | d ¡ Y qX q|r^|| _| jS |S )NFT)ÚgetattrÚcallableÚappendÚmap_listÚmap)ÚselfÚnameÚresultsZis_callableÚserverÚval© r   ú7/tmp/pip-unpacked-wheel-d7dsrkjd/boto/mashups/server.pyÚ__getattr__&   s    
zServerSet.__getattr__c                 G   s"   g }| j D ]}| ||Ž ¡ q
|S ©N)r   r   )r   Úargsr   Úfnr   r   r   r   6   s    
zServerSet.mapN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r	   $   s   r	   c                       s  e Zd Zedd„ ƒZedd„ ƒZed[dd„ƒZd\‡ fd	d
„	Ze	dddZ
e	ddZe	ƒ Ze	ddZe	ddZe	dddZe	ddZe	ddZe	ddZe	ddZe	ƒ Zdd„ Zdd„ ZeeeddƒZdd„ Zeeedd ƒZd!d"„ Zeeedd#ƒZd$d%„ Zeeedd&ƒZd'd(„ Zeeedd)ƒZd*d+„ Z ee edd,ƒZ!d-d.„ Z"ee"edd/ƒZ#d0d1„ Z$ee$edd2ƒZ%d3d4„ Z&d5d6„ Z'ee&e'dd7ƒZ(d8d9„ Z)d:d;„ Z*d<d=„ Z+d>d?„ Z,d@dA„ Z-d]dDdE„Z.dFdG„ Z/dHdI„ Z0dJdK„ Z1d^dLdM„Z2dNdO„ Z3dPdQ„ Z4d_dRdS„Z5d`dUdV„Z6dWdX„ Z7dYdZ„ Z8‡  Z9S )aÚServerc                 C   s   | j d krt ¡ | _ | j S r   )Ú_ec2ÚbotoÚconnect_ec2©r   r   r   r   Úec2>   s    

z
Server.ec2c                 C   s&   t ƒ }|  ¡ }|D ]}| |¡ q|S )zl
        Returns a list of Server instances, one for each Server object
        persisted in the db
        )r	   Úfindr   )ÚclsÚlÚrsr   r   r   r   Ú	InventoryD   s
    zServer.InventoryÚ c                 C   s$   | ƒ }||_ ||_||_| ¡  |S r   )r   Úinstance_idÚdescriptionÚsave)r$   r   r)   r*   Úsr   r   r   ÚRegisterP   s    zServer.RegisterNc                    s<   t t| ƒj|f|Ž d | _d | _d | _d | _d | _d | _d S r   )	Úsuperr   Ú__init__Ú_reservationÚ	_instanceÚ_ssh_clientÚ_pkeyÚ_configr   )r   ÚidÚkw©Ú	__class__r   r   r/   Y   s    zServer.__init__TÚName)ÚuniqueÚverbose_namezInstance ID)r;   zAMI IDzAvailability ZonezSecurity GroupÚdefault)r;   r<   zKey Namez
Elastic IPzInstance TypeÚDescriptionc                 C   s   t ‚d S r   )ÚAttributeError)r   Úvaluer   r   r   ÚsetReadOnlyn   s    zServer.setReadOnlyc                 C   sZ   | j sT| jrTz| j | jg¡}W n   Y d S X t|ƒdkrT|d | _| jjd | _ | j S )Nr   )r1   r)   r"   Zget_all_reservationsÚlenr0   Ú	instances)r   r&   r   r   r   ÚgetInstanceq   s    
zServer.getInstancezThe Instance for the serverc                 C   s   | j r| j jS d S r   )ÚinstanceÚimage_idr!   r   r   r   ÚgetAMI   s    zServer.getAMIzThe AMI for the serverc                 C   s   | j r| j  ¡  | j jS d S r   )rD   ÚupdateÚstater!   r   r   r   Ú	getStatus…   s    
zServer.getStatuszThe status of the serverc                 C   s   | j r| j jS d S r   )rD   Úpublic_dns_namer!   r   r   r   ÚgetHostname   s    zServer.getHostnamez!The public DNS name of the serverc                 C   s   | j r| j jS d S r   )rD   Zprivate_dns_namer!   r   r   r   ÚgetPrivateHostname”   s    zServer.getPrivateHostnamez"The private DNS name of the serverc                 C   s   | j r| j jS d S r   )rD   Úlaunch_timer!   r   r   r   ÚgetLaunchTime›   s    zServer.getLaunchTimezThe time the Server was startedc                 C   s   | j r| j  ¡ S d S r   )rD   Zget_console_outputr!   r   r   r   ÚgetConsoleOutput¢   s    zServer.getConsoleOutputz&Retrieve the console output for serverc                 C   s   | j r| j jS d S d S r   )r0   Úgroupsr!   r   r   r   Ú	getGroups©   s    zServer.getGroupsz5The Security Groups controlling access to this serverc                 C   s2   | j s,t}d| jj }|  ||¡ t|ƒ| _ | j S ©Nz%s.ini)r4   r   rD   r5   Úget_filer   )r   Zremote_fileÚ
local_filer   r   r   Ú	getConfig²   s    
zServer.getConfigc                 C   s<   d| j j }t|ƒ}| |¡ | ¡  |  |t¡ || _d S rR   )rD   r5   ÚopenÚwriteÚcloseÚput_filer   r4   )r   ÚconfigrT   Úfpr   r   r   Ú	setConfigº   s    
zServer.setConfigz!The instance data for this serverc                 C   s   || _ | j  d| j¡ dS )z&
        Set SDB based config
        ÚbotoConfigsN)r4   Zdump_to_sdbr5   )r   rZ   r   r   r   Ú
set_configÅ   s    zServer.set_configc                 C   s    t dd| _| j d| j¡ d S )NF)Zdo_loadr]   )r   r4   Zload_from_sdbr5   r!   r   r   r   Úload_configÌ   s    zServer.load_configc                 C   s   | j r| j  ¡  d S r   )rD   Ústopr!   r   r   r   r`   Ð   s    zServer.stopc              	   C   s<  |   ¡  t ¡ }|jt| jƒgdd }|jt| jƒgd}| jsJ|  	¡  | j 
d¡s†| j d¡ | j dd|j¡ | j dd|j¡ | j 
d¡sž| j d¡ | jjrÎ| j dd| jjj¡ | j dd	| j¡ tƒ }| j |¡ | ¡ }|jd
d
| j|| j| j|d}|jd }|j| _|  ¡  | jr8| | j| j¡ d S )N)Z	image_idsr   )Z
groupnamesZCredentialsÚaws_access_key_idÚaws_secret_access_keyZPyamiZserver_sdb_domainZserver_sdb_nameé   )Z	min_countÚ	max_countÚkey_nameZsecurity_groupsÚinstance_typeZ	placementÚ	user_data)r`   r   r    Zget_all_imagesÚstrÚami_idZget_all_security_groupsÚsecurity_groupr4   r_   Úhas_sectionÚadd_sectionÚsetra   rb   Ú_managerÚdomainr   r   rW   ÚgetvalueÚrunre   rf   ÚzonerB   r5   r)   ÚputÚ
elastic_ipZassociate_address)r   r"   ÚamirP   ÚcfgÚrÚir   r   r   ÚstartÔ   s>    ú
zServer.startc                 C   s   | j r| j  ¡  d S r   )rD   Úrebootr!   r   r   r   rz   ÷   s    zServer.rebootú~/.ssh/known_hostsÚrootc                 C   s˜   dd l }| jstdƒ d S | js’|s4tƒ }| d¡}|j |¡| _| 	¡ | _| j 
¡  | j tj |¡¡ | j | ¡ ¡ | jj| jj|| jd | jS )Nr   zNo instance yet!zPath to OpenSSH Key file)ÚusernameZpkey)ÚparamikorD   Úprintr2   r   Úget_filenameZRSAKeyZfrom_private_key_filer3   Z	SSHClientZload_system_host_keysZload_host_keysÚosÚpathÚ
expanduserZset_missing_host_key_policyZAutoAddPolicyÚconnectrJ   )r   Úkey_fileZhost_key_fileÚunamer~   Úiobjectr   r   r   Úget_ssh_clientû   s$    


 ÿzServer.get_ssh_clientc                 C   s    |   ¡ }| ¡ }| ||¡ d S r   )rˆ   Ú	open_sftpÚget)r   Ú
remotepathÚ	localpathÚ
ssh_clientÚsftp_clientr   r   r   rS     s    zServer.get_filec                 C   s    |   ¡ }| ¡ }| ||¡ d S r   )rˆ   r‰   rs   )r   rŒ   r‹   r   rŽ   r   r   r   rY     s    zServer.put_filec                 C   s   |   ¡ }| ¡ }| |¡S r   )rˆ   r‰   Úlistdir)r   r‹   r   rŽ   r   r   r   r     s    zServer.listdirc                 C   s   |   |¡}| ¡ }t|ƒ d S r   )rˆ   Zinvoke_shellr   )r   r…   r   Zchannelr   r   r   Úshell  s    
zServer.shellc                 C   sJ  t dƒ t dƒ |  ¡ }| ¡ }tj |¡\}}d| }	|  ||	¡ tj |¡\}}d| }
|  ||
¡ t dt ƒ z| t¡ W n   Y nX d}|d|
|	f 7 }|d| j	j
 7 }|d| 7 }|d	| 7 }|d
7 }| jjdksê| jdkrô|d7 }n|d7 }t d| ƒ | |¡}|d  ¡ }t d| ƒ t d|d  ¡  ƒ t dƒ d S )Nzbundling image...z5	copying cert and pk over to /mnt directory on serverz/mnt/%sz	deleting %szsudo ec2-bundle-vol z-c %s -k %s z-u %s z-p %s z-s %d z-d /mnt zm1.smallz	c1.mediumz-r i386z	-r x86_64ú	%src   é   ú...complete!)r   rˆ   r‰   r   r‚   ÚsplitrY   r   Úremover0   Zowner_idrD   rf   Úexec_commandÚread)r   Úprefixr…   Ú	cert_fileÚsizer   rŽ   r‚   r   Zremote_key_fileZremote_cert_fileÚcommandÚtÚresponser   r   r   Úbundle_image"  s<    

zServer.bundle_imagec                 C   sš   t dƒ d}|d| 7 }|d| 7 }|d| jj 7 }|d| jj 7 }t d| ƒ |  ¡ }| |¡}|d  ¡ }t d| ƒ t d|d	  ¡  ƒ t d
ƒ d S )Nzuploading bundle...zec2-upload-bundle z-m /mnt/%s.manifest.xml z-b %s z-a %s z-s %s r‘   rc   r’   r“   )r   r"   ra   rb   rˆ   r–   r—   )r   Úbucketr˜   r›   r   rœ   r   r   r   r   Úupload_bundleD  s    
zServer.upload_bundlec                 C   sŒ   t ƒ }|s| d¡}|s"| d¡}|s0| d¡}|s>| d¡}|sL| d¡}|  ||||¡ |  ||¡ tdƒ | j d||f ¡| _	| 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registering image...z%s/%s.manifest.xml)
r   Z
get_stringr€   Zget_intrž   r    r   r"   Zregister_imagerE   )r   rŸ   r˜   r…   r™   rš   r‡   r   r   r   Úcreate_imageS  s     




zServer.create_imageú/dev/sdpc                 C   s*   t |dƒr|j}n|}| jj|| j|dS )zî
        Attach an EBS volume to this server

        :param volume: EBS Volume to attach
        :type volume: boto.ec2.volume.Volume

        :param device: Device to attach to (default to /dev/sdp)
        :type device: string
        r5   )Ú	volume_idr)   Údevice)Úhasattrr5   r"   Úattach_volumer)   )r   Úvolumer¤   r£   r   r   r   r¦   e  s    

zServer.attach_volumec                 C   s(   t |dƒr|j}n|}| jj|| jdS )z‘
        Detach an EBS volume from this server

        :param volume: EBS Volume to detach
        :type volume: boto.ec2.volume.Volume
        r5   )r£   r)   )r¥   r5   r"   Údetach_volumer)   )r   r§   r£   r   r   r   r¨   u  s    
zServer.detach_volumec                 C   sj   t d| ƒ d| }t d| ƒ |  ¡ }| |¡}|d  ¡ }t d| ƒ t d|d  ¡  ƒ t dƒ d S )Nzinstalling %s...zyum -y install %sr‘   rc   r’   r“   )r   rˆ   r–   r—   )r   Úpackage_namer›   r   rœ   r   r   r   r   Úinstall_package‚  s    
zServer.install_package)r(   )N)Nr{   r|   )N)NNNNN)r¢   ):r   r   r   Úpropertyr"   Úclassmethodr'   r-   r/   r   r   r)   Z
config_uriri   rr   rj   re   rt   rf   r*   Úlogr@   rC   rD   rF   ru   rI   ÚstatusrK   ÚhostnamerL   Zprivate_hostnamerN   rM   rO   Zconsole_outputrQ   rP   rU   r\   rZ   r^   r_   r`   ry   rz   rˆ   rS   rY   r   r   rž   r    r¡   r¦   r¨   rª   Ú__classcell__r   r   r7   r   r   <   sŒ   

	






ÿÿÿÿÿÿÿ#  ÿ

"

r   )Ú__doc__r   Z
boto.utilsZboto.compatr   Zboto.mashups.iobjectr   Zboto.pyami.configr   r   Zboto.mashups.interactiver   Zboto.sdb.db.modelr   Zboto.sdb.db.propertyr   r   Úlistr	   r   r   r   r   r   Ú<module>   s   