B
    è?F[ô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   sf   g }d}xH| D ]@}y$t ||ƒ}t|ƒr*d}| |¡ W q   | d ¡ Y qX qW |rb|| _| jS |S )NFT)ÚgetattrÚcallableÚappendÚmap_listÚmap)ÚselfÚnameÚresultsZis_callableÚserverÚval© r   ú2lib/python3.7/site-packages/boto/mashups/server.pyÚ__getattr__&   s    

zServerSet.__getattr__c             G   s&   g }x| j D ]}| ||Ž ¡ qW |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 )N)Ú_ec2ÚbotoÚconnect_ec2)r   r   r   r   Úec2>   s    

z
Server.ec2c             C   s*   t ƒ }|  ¡ }x|D ]}| |¡ qW |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 )N)r   Úinstance_idÚdescriptionZ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 )N)	Ú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)r7   zAMI IDzAvailability ZonezSecurity GroupÚdefault)r7   r8   zKey Namez
Elastic IPzInstance TypeZDescriptionc             C   s   t ‚d S )N)ÚAttributeError)r   Úvaluer   r   r   ÚsetReadOnlyn   s    zServer.setReadOnlyc             C   sV   | j sP| jrPy| j | jg¡}W n
   d S t|ƒdkrP|d | _| jjd | _ | j S )Nr   )r.   r'   r    Zget_all_reservationsÚlenr-   Ú	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 )N)ÚinstanceÚimage_id)r   r   r   r   ÚgetAMI   s    zServer.getAMIzThe AMI for the serverc             C   s   | j r| j  ¡  | j jS d S )N)r?   ÚupdateÚstate)r   r   r   r   Ú	getStatus…   s    
zServer.getStatuszThe status of the serverc             C   s   | j r| j jS d S )N)r?   Úpublic_dns_name)r   r   r   r   ÚgetHostname   s    zServer.getHostnamez!The public DNS name of the serverc             C   s   | j r| j jS d S )N)r?   Zprivate_dns_name)r   r   r   r   ÚgetPrivateHostname”   s    zServer.getPrivateHostnamez"The private DNS name of the serverc             C   s   | j r| j jS d S )N)r?   Úlaunch_time)r   r   r   r   ÚgetLaunchTime›   s    zServer.getLaunchTimezThe time the Server was startedc             C   s   | j r| j  ¡ S d S )N)r?   Zget_console_output)r   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 )N)r-   Úgroups)r   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)r1   r   r?   r2   Úget_filer   )r   Zremote_fileÚ
local_filer   r   r   Ú	getConfig²   s    
zServer.getConfigc             C   s<   d| j j }t|ƒ}| |¡ | ¡  |  |t¡ || _d S )Nz%s.ini)r?   r2   ÚopenÚwriteÚcloseÚput_filer   r1   )r   ÚconfigrN   Ú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)r1   Zdump_to_sdbr2   )r   rT   r   r   r   Ú
set_configÅ   s    zServer.set_configc             C   s    t dd| _| j d| j¡ d S )NF)Zdo_loadrW   )r   r1   Zload_from_sdbr2   )r   r   r   r   Úload_configÌ   s    zServer.load_configc             C   s   | j r| j  ¡  d S )N)r?   Ústop)r   r   r   r   rZ   Ð   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_countZ	max_countÚkey_nameZsecurity_groupsÚinstance_typeZ	placementZ	user_data)rZ   r   r   Zget_all_imagesÚstrÚami_idZget_all_security_groupsÚsecurity_groupr1   rY   Zhas_sectionZadd_sectionÚsetr[   r\   Z_managerZdomainr   r   rQ   ÚgetvalueÚrunr^   r_   Úzoner=   r2   r'   ÚputÚ
elastic_ipZassociate_address)r   r    ÚamirK   ZcfgÚrÚir   r   r   ÚstartÔ   s<    
zServer.startc             C   s   | j r| j  ¡  d S )N)r?   Úreboot)r   r   r   r   rm   ÷   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)ZusernameZpkey)Úparamikor?   Úprintr/   r   Úget_filenameZRSAKeyZfrom_private_key_filer0   Z	SSHClientZload_system_host_keysZload_host_keysÚosÚpathÚ
expanduserZset_missing_host_key_policyZAutoAddPolicyZconnectrE   )r   Úkey_fileZhost_key_fileÚunamerp   Úiobjectr   r   r   Úget_ssh_clientû   s     


zServer.get_ssh_clientc             C   s    |   ¡ }| ¡ }| ||¡ d S )N)ry   Ú	open_sftpÚget)r   Ú
remotepathÚ	localpathÚ
ssh_clientÚsftp_clientr   r   r   rM     s    zServer.get_filec             C   s    |   ¡ }| ¡ }| ||¡ d S )N)ry   rz   rg   )r   r}   r|   r~   r   r   r   r   rS     s    zServer.put_filec             C   s   |   ¡ }| ¡ }| |¡S )N)ry   rz   Úlistdir)r   r|   r~   r   r   r   r   r€     s    zServer.listdirc             C   s   |   |¡}| ¡ }t|ƒ d S )N)ry   Zinvoke_shellr   )r   rv   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 ƒ y| 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_64z	%sr]   é   z...complete!)rq   ry   rz   rs   rt   ÚsplitrS   r   Úremover-   Zowner_idr?   r_   Úexec_commandÚread)r   Úprefixrv   Ú	cert_fileÚsizer~   r   rt   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 z	%sr]   r‚   z...complete!)rq   r    r[   r\   ry   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_stringrr   Zget_intr   r   rq   r    Zregister_imager@   )r   rŽ   r‡   rv   rˆ   r‰   rx   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
        r2   )Ú	volume_idr'   Údevice)Úhasattrr2   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
        r2   )r’   r'   )r”   r2   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 %sz	%sr]   r‚   z...complete!)rq   ry   r…   r†   )r   Zpackage_namerŠ   r~   r‹   rŒ   r   r   r   Úinstall_package‚  s    
zServer.install_package)r&   )N)Nrn   ro   )N)NNNNN)r‘   ):r   r   r   Úpropertyr    Úclassmethodr%   r*   r,   r   r   r'   Z
config_urira   rf   rb   r^   rh   r_   r(   Úlogr;   r>   r?   rA   ri   rD   ZstatusrF   ZhostnamerG   Zprivate_hostnamerI   rH   rJ   Zconsole_outputrL   rK   rO   rV   rT   rX   rY   rZ   rl   rm   ry   rM   rS   r€   r   r   r   r   r•   r—   r˜   Ú__classcell__r   r   )r4   r   r   <   sx   	






# 

"

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   rs   Úlistr	   r   r   r   r   r   Ú<module>   s   