o ckF[6@sdZddlZddlZddlmZddlmZddlmZm Z ddl m Z ddl m Z ddlmZddlZGd d d eZGd d d e ZdS) z) High-level abstraction of an EC2 server N)StringIO)IObject)ConfigBotoConfigPath)interactive_shell)Model)StringPropertyc@seZdZddZddZdS) ServerSetcCs^g}d}|D]}zt||}t|rd}||Wq|dYq|r-||_|jS|S)NFT)getattrcallableappendmap_listmap)selfnameresults is_callableservervalr5/usr/lib/python3/dist-packages/boto/mashups/server.py __getattr__&s zServerSet.__getattr__cGs"g}|jD] }|||q|SN)r r )rargsrfnrrrr6s z ServerSet.mapN)__name__ __module__ __qualname__rrrrrrr $s r cseZdZeddZeddZed[ddZd\fd d Ze d d d Z e ddZ e Z e ddZ e ddZe dddZe ddZe ddZe ddZe ddZe ZddZddZeeeddZddZeeedd Zd!d"Zeeedd#Zd$d%Zeeedd&Zd'd(Zeeedd)Zd*d+Z ee edd,Z!d-d.Z"ee"edd/Z#d0d1Z$ee$edd2Z%d3d4Z&d5d6Z'ee&e'dd7Z(d8d9Z)d:d;Z*dd?Z,d@dAZ- B Cd]dDdEZ.dFdGZ/dHdIZ0dJdKZ1d\dLdMZ2dNdOZ3dPdQZ4d^dRdSZ5d_dUdVZ6dWdXZ7dYdZZ8Z9S)`ServercCs|jdur t|_|jSr)_ec2boto connect_ec2rrrrec2>s  z Server.ec2cCs&t}|}|D]}||q |S)zl Returns a list of Server instances, one for each Server object persisted in the db )r findr )clslrsrrrr InventoryDs  zServer.InventorycCs$|}||_||_||_||Sr)r instance_id descriptionsave)r%rr*r+srrrRegisterPs zServer.RegisterNc s@tt|j|fi|d|_d|_d|_d|_d|_d|_dSr) superr__init__ _reservation _instance _ssh_client_pkey_configr)ridkw __class__rrr0Ys zServer.__init__TName)unique verbose_namez Instance ID)r<zAMI IDzAvailability ZonezSecurity Groupdefault)r<r=zKey Namez Elastic IPz Instance Type DescriptioncCstr)AttributeError)rvaluerrr setReadOnlynszServer.setReadOnlycCsX|js)|jr)z |j|jg}WnYdSt|dkr)|d|_|jjd|_|jS)Nr)r2r*r#get_all_reservationslenr1 instances)rr'rrr getInstanceqs  zServer.getInstancezThe Instance for the servercC|jr|jjSdSr)instanceimage_idr"rrrgetAMIz Server.getAMIzThe AMI for the servercCs|jr |j|jjSdSr)rGupdatestater"rrr getStatuss zServer.getStatuszThe status of the servercCrFr)rGpublic_dns_namer"rrr getHostnamerJzServer.getHostnamez!The public DNS name of the servercCrFr)rGprivate_dns_namer"rrrgetPrivateHostnamerJzServer.getPrivateHostnamez"The private DNS name of the servercCrFr)rG launch_timer"rrr getLaunchTimerJzServer.getLaunchTimezThe time the Server was startedcCs|jr|jSdSr)rGget_console_outputr"rrrgetConsoleOutputs zServer.getConsoleOutputz&Retrieve the console output for servercCrFr)r1groupsr"rrr getGroupsszServer.getGroupsz5The Security Groups controlling access to this servercCs2|jst}d|jj}|||t||_|jSNz%s.ini)r5rrGr6get_filer)r remote_file local_filerrr getConfigs    zServer.getConfigcCs<d|jj}t|}|||||t||_dSrX)rGr6openwritecloseput_filerr5)rconfigr[fprrr setConfigs    zServer.setConfigz!The instance data for this servercCs||_|jd|jdS)z& Set SDB based config botoConfigsN)r5 dump_to_sdbr6)rrarrr set_configszServer.set_configcCs tdd|_|jd|jdS)NF)do_loadrd)rr5 load_from_sdbr6r"rrr load_configs zServer.load_configcC|jr |jdSdSr)rGstopr"rrrrkz Server.stopc Cs>|t}|jt|jgdd}|jt|jgd}|js%| |j dsC|j d|j dd|j |j dd|j|j dsO|j d|jjrg|j dd|jjj|j dd |jt}|j||}|jd d |j||j|j|d }|jd}|j|_||jr||j|jdSdS) N) image_idsr) groupnames Credentialsaws_access_key_idaws_secret_access_keyPyamiserver_sdb_domainserver_sdb_name) min_count max_countkey_namesecurity_groups instance_type placement user_data)rkr r!get_all_imagesstrami_idget_all_security_groupssecurity_groupr5ri has_section add_sectionsetrprq_managerdomainrrr^getvaluerunrxrzzonerDr6r*put elastic_ipassociate_address)rr#amirVcfgrirrrstarts@      z Server.startcCrjr)rGrebootr"rrrrrlz Server.reboot~/.ssh/known_hostsrootcCsddl}|js tddS|jsI|st}|d}|j||_| |_|j |j t j ||j||jj|jj||jd|jS)NrzNo instance yet!zPath to OpenSSH Key file)usernamepkey)paramikorGprintr3r get_filenameRSAKeyfrom_private_key_filer4 SSHClientload_system_host_keysload_host_keysospath expanduserset_missing_host_key_policy AutoAddPolicyconnectrN)rkey_file host_key_fileunameriobjectrrrget_ssh_clients"    zServer.get_ssh_clientcC |}|}|||dSr)r open_sftpget)r remotepath localpath ssh_client sftp_clientrrrrYzServer.get_filecCrr)rrr)rrrrrrrrr`rzServer.put_filecCs|}|}||Sr)rrlistdir)rrrrrrrrs zServer.listdircCs||}|}t|dSr)r invoke_shellr)rrrchannelrrrshells  z Server.shellcCsFtdtd|}|}tj|\}}d|} ||| tj|\}}d|} ||| tdtz|tWnYd} | d| | f7} | d|j j 7} | d|7} | d |7} | d 7} |j j d kss|j d krx| d 7} n| d7} td| | | } | d} td| td| dtddS)Nzbundling image...z5 copying cert and pk over to /mnt directory on serverz/mnt/%sz deleting %szsudo ec2-bundle-vol z -c %s -k %s z-u %s z-p %s z-s %d z-d /mnt zm1.smallz c1.mediumz-r i386z -r x86_64 %sru ...complete!)rrrrrsplitr`rremover1owner_idrGrz exec_commandread)rprefixr cert_filesizerrrrremote_key_fileremote_cert_filecommandtresponserrr bundle_image"s<           zServer.bundle_imagecCstdd}|d|7}|d|7}|d|jj7}|d|jj7}td||}||}|d}td|td|d td dS) Nzuploading bundle...zec2-upload-bundle z-m /mnt/%s.manifest.xml z-b %s z-a %s z-s %s rrurr)rr#rprqrrr)rbucketrrrrrrrr upload_bundleDs       zServer.upload_bundlecCst}|s |d}|s|d}|s|d}|s|d}|s&|d}||||||||td|jd||f|_ |j S)NzName of S3 bucketzPrefix for AMI filezPath to RSA private key filezPath to RSA public cert filezSize (in MB) of bundled imagezregistering image...z%s/%s.manifest.xml) r get_stringrget_intrrrr#register_imagerH)rrrrrrrrrr create_imageSs       zServer.create_image/dev/sdpcCs*t|dr |j}n|}|jj||j|dS)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)hasattrr6r# attach_volumer*)rvolumerrrrrres zServer.attach_volumecCs(t|dr |j}n|}|jj||jdS)z Detach an EBS volume from this server :param volume: EBS Volume to detach :type volume: boto.ec2.volume.Volume r6)rr*)rr6r# detach_volumer*)rrrrrrrus zServer.detach_volumecCsjtd|d|}td||}||}|d}td|td|dtddS)Nzinstalling %s...zyum -y install %srrurr)rrrr)r package_namerrrrrrrinstall_packages      zServer.install_package)r)r)Nrr)NNNNN)r):rrrpropertyr# classmethodr(r.r0rrr* config_urirrrrxrrzr+logrArErGrIrrMstatusrOhostnamerQprivate_hostnamerSrRrUconsole_outputrWrVr\rcrarfrirkrrrrYr`rrrrrrrr __classcell__rrr8rr<s            #  "   r)__doc__r boto.utils boto.compatrboto.mashups.iobjectrboto.pyami.configrrboto.mashups.interactiverboto.sdb.db.modelrboto.sdb.db.propertyrrlistr rrrrrs