o
    ȷe6                     @   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   s^   g }d}| D ]}zt ||}t|rd}|| W q   |d  Y q|r-|| _| jS |S )NFT)getattrcallableappendmap_listmap)selfnameresultsis_callableserverval r   J/var/www/ideatree/venv/lib/python3.10/site-packages/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-	B	Cd]dDdEZ.dFdG Z/dHdI Z0dJdK Z1d\dLdMZ2dNdO Z3dPdQ Z4d^dRdSZ5d_dUdVZ6dWdX Z7dYdZ Z8  Z9S )`Serverc                 C   s   | j d u 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i | d | _d | _d | _d | _d | _d | _d S r   )	superr   __init___reservation	_instance_ssh_client_pkey_configr   )r   idkw	__class__r   r   r0   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 r   )AttributeError)r   valuer   r   r   setReadOnlyn   s   zServer.setReadOnlyc                 C   sX   | j s)| jr)z
| j| jg}W n   Y d S t|dkr)|d | _| jjd | _ | j S )Nr   )r2   r*   r#   get_all_reservationslenr1   	instances)r   r'   r   r   r   getInstanceq   s   
zServer.getInstancezThe Instance for the serverc                 C      | j r| j jS d S r   )instanceimage_idr"   r   r   r   getAMI      zServer.getAMIzThe AMI for the serverc                 C   s   | j r| j   | j jS d S r   )rG   updatestater"   r   r   r   	getStatus   s   
zServer.getStatuszThe status of the serverc                 C   rF   r   )rG   public_dns_namer"   r   r   r   getHostname   rJ   zServer.getHostnamez!The public DNS name of the serverc                 C   rF   r   )rG   private_dns_namer"   r   r   r   getPrivateHostname   rJ   zServer.getPrivateHostnamez"The private DNS name of the serverc                 C   rF   r   )rG   launch_timer"   r   r   r   getLaunchTime   rJ   zServer.getLaunchTimezThe time the Server was startedc                 C   s   | j r| j  S d S r   )rG   get_console_outputr"   r   r   r   getConsoleOutput   s   
zServer.getConsoleOutputz&Retrieve the console output for serverc                 C   rF   r   )r1   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)r5   r   rG   r6   get_filer   )r   remote_file
local_filer   r   r   	getConfig   s   
zServer.getConfigc                 C   s<   d| j j }t|}|| |  | |t || _d S rX   )rG   r6   openwritecloseput_filer   r5   )r   configr[   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)r5   dump_to_sdbr6   )r   ra   r   r   r   
set_config   s   zServer.set_configc                 C   s    t dd| _| jd| j d S )NF)do_loadrd   )r   r5   load_from_sdbr6   r"   r   r   r   load_config   s   zServer.load_configc                 C      | j r
| j   d S d S r   )rG   stopr"   r   r   r   rk         zServer.stopc              	   C   s>  |    t }|jt| jgdd }|jt| jgd}| js%| 	  | j
dsC| jd | jdd|j | jdd|j | j
dsO| jd | jjrg| jdd| jjj | jdd	| j t }| j| | }|jd
d
| j|| j| j|d}|jd }|j| _|   | jr|| j| j d S d S )N)	image_idsr   )
groupnamesCredentialsaws_access_key_idaws_secret_access_keyPyamiserver_sdb_domainserver_sdb_name   )	min_count	max_countkey_namesecurity_groupsinstance_type	placement	user_data)rk   r    r!   get_all_imagesstrami_idget_all_security_groupssecurity_groupr5   ri   has_sectionadd_sectionsetrp   rq   _managerdomainr   r   r^   getvaluerunrx   rz   zonerD   r6   r*   put
elastic_ipassociate_address)r   r#   amirV   cfgrir   r   r   start   s@   
zServer.startc                 C   rj   r   )rG   rebootr"   r   r   r   r      rl   zServer.reboot~/.ssh/known_hostsrootc                 C   s   dd l }| jstd d S | jsI|st }|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)usernamepkey)paramikorG   printr3   r   get_filenameRSAKeyfrom_private_key_filer4   	SSHClientload_system_host_keysload_host_keysospath
expanduserset_missing_host_key_policyAutoAddPolicyconnectrN   )r   key_filehost_key_fileunamer   iobjectr   r   r   get_ssh_client   s"   


zServer.get_ssh_clientc                 C       |   }| }||| d S r   )r   	open_sftpget)r   
remotepath	localpath
ssh_clientsftp_clientr   r   r   rY        zServer.get_filec                 C   r   r   )r   r   r   )r   r   r   r   r   r   r   r   r`     r   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   invoke_shellr   )r   r   r   channelr   r   r   shell  s   
zServer.shellc                 C   sF  t d t d |  }| }tj|\}}d| }	| ||	 tj|\}}d| }
| ||
 t dt  z|t W n   Y d}|d|
|	f 7 }|d| j	j
 7 }|d| 7 }|d	| 7 }|d
7 }| jjdkss| jdkrx|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	%sru      ...complete!)r   r   r   r   r   splitr`   r   remover1   owner_idrG   rz   exec_commandread)r   prefixr   	cert_filesizer   r   r   r   remote_key_file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   ru   r   r   )r   r#   rp   rq   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}|s|d}|s|d}|s&|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   
get_stringr   get_intr   r   r   r#   register_imagerH   )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
        r6   )	volume_idr*   device)hasattrr6   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
        r6   )r   r*   )r   r6   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   ru   r   r   )r   r   r   r   )r   package_namer   r   r   r   r   r   r   install_package  s   
zServer.install_package)r)   r   )Nr   r   )NNNNN)r   ):r   r   r   propertyr#   classmethodr(   r.   r0   r   r   r*   
config_urir   r   r   rx   r   rz   r+   logrA   rE   rG   rI   r   rM   statusrO   hostnamerQ   private_hostnamerS   rR   rU   console_outputrW   rV   r\   rc   ra   rf   ri   rk   r   r   r   rY   r`   r   r   r   r   r   r   r   r   __classcell__r   r   r8   r   r   <   s    

	






#

"

r   )__doc__r    
boto.utilsboto.compatr   boto.mashups.iobjectr   boto.pyami.configr   r   boto.mashups.interactiver   boto.sdb.db.modelr   boto.sdb.db.propertyr   r   listr	   r   r   r   r   r   <module>   s   