o ckF[gU@sdZddlZddlmZddlmZmZddlm Z ddl m Z m Z m Z mZddlmZddlmZdd lmZddlZddlZdd lmZdd lmZdd lmZmZgd ZGdddeZ GdddeZ!Gddde Z"dS)z) High-level abstraction of an EC2 server N)IObject)BotoConfigPathConfig)Model)StringPropertyIntegerPropertyBooleanPropertyCalculatedProperty)propget)Zone)KeyPair)closing)EC2ResponseError)sixStringIO)m1.smallzm1.largez m1.xlarge c1.mediumz c1.xlargez m2.2xlargez m2.4xlargec@s<eZdZdddZddZddZdd Z dd d Zd S)BundlerrootcCs*ddlm}||_||_|||d|_dS)Nr SSHClient)uname)boto.manage.cmdshellrserverr ssh_client)selfrrrr4/usr/lib/python3/dist-packages/boto/manage/server.py__init__-s zBundler.__init__cCsrtd|jtj|\}}d||_|j||jtj|\}}d||_|j||jtddS)Nz5 copying cert and pk over to /mnt directory on serverz/mnt/%s ...complete!) printr open_sftpospathsplitremote_key_fileput_fileremote_cert_file)rkey_file cert_filer#namerrr copy_x5093s    zBundler.copy_x509cCsd}|jdkr d}|d7}|d|j|jf7}|d|jjj7}|d|7}|d|7}|d 7}|jjd ks<|jjd krB|d 7}|S|d 7}|S)Nrsudo zec2-bundle-vol z -c %s -k %s z-u %s z-p %s z-s %d z-d /mnt rrz-r i386z -r x86_64)rr'r%r _reservationowner_id instance_type)rprefixsizessh_keycommandrrr bundle_image>s   zBundler.bundle_imagecCsZd}|jdkr d}|d7}|d|7}|d|7}|d|jjj7}|d|jjj7}|S) Nr,rr-zec2-upload-bundle z-m /mnt/%s.manifest.xml z-b %s z-a %s z-s %s )rrec2aws_access_key_idaws_secret_access_key)rbucketr1r3r4rrr upload_bundleNs   zBundler.upload_bundleNTc Cs`t} |s | d}|s| d}|s| d}|s| d}|s&| d}|s-|j}||||s8t}|dt |d|rK|d|| ||||d || ||||d |d t |d | } t d t | |j| } t d | dt d | dt dt d|jjj|d||fd|_|jS)NzName of S3 bucketzPrefix for AMI filezPath to RSA private key filezPath to RSA public cert filezSize (in MB) of bundled imagezsudo mv %s /mnt/boto.cfg; z0mv ~/.ssh/authorized_keys /mnt/authorized_keys; z history -c; z; zsudo mv /mnt/boto.cfg %s; z.mv /mnt/authorized_keys ~/.ssh/authorized_keysz3running the following command on the remote server:z %srrzregistering image...z%s/%s.manifest.xml)r*image_location)r get_string get_filenameget_intrget_ssh_key_filer+rwriterr5r:getvaluer rrunr6register_imageimage_id) rr9r1r(r)r2r3fp clear_historyiobjectr4trrrbundleYsH             zBundler.bundler)NNNNNNNT)__name__ __module__ __qualname__rr+r5r:rJrrrrr+s   rc@sdeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ dS)CommandLineGettercCs8g}|jD]}|jddkr||j|fq|S)Npyamir)r6get_all_imageslocationfindappend)rmy_amisamirrr get_ami_lists zCommandLineGetter.get_ami_listcCsb|dd}t|trtj|}||d<|s(|jd}tj|tjj d|d<|d |_dS)Nregion region_name)choices) get isinstance basestringbotor6 get_regioncls find_propertyr regionsconnect)rparamsrXproprrrr_s    zCommandLineGetter.get_regioncC.|dds|jd}t||d<dSdS)Nr*r[r`rar rrdrerrrget_name  zCommandLineGetter.get_namecCrf)N descriptionrgrhrrrget_descriptionrjz!CommandLineGetter.get_descriptioncCs0|ddstddtd}t||d<dSdS)Nr0 Instance Typer* verbose_namerZ)r[r InstanceTypesr rhrrrget_instance_types z#CommandLineGetter.get_instance_typecCs.|ddstddd}t||d<dSdS)NquantityzNumber of Instancesr*ro)r[rr rhrrr get_quantityrjzCommandLineGetter.get_quantitycCs4|ddstdd|jjd}t||d<dSdS)NzonezEC2 Availability Zonern)r[rr6 get_all_zonesr rhrrrget_zones zCommandLineGetter.get_zonecCs|d}|s<|dd}|stddd}t|}z|j|g}t|dkr-d}|d|d<Wn ty7Ynw|rdSdS)NFrVAMIrsr;Tr)r[rr r6rQlenr)rrdvalidrVrersrrr get_ami_ids        zCommandLineGetter.get_ami_idcCsl|dd}t|tr |j}|D] }|j|kr|}||d<q|s4tdd|jjd}t||d<dSdS)NgroupzEC2 Security Grouprn)r[r\r]r6get_all_security_groupsr*rr )rrdr} group_listgrerrr get_groups    zCommandLineGetter.get_groupcCsr|dd}t|tr"|j}|D]}|j|kr!|j}|j|d<q|s7tdd|jjd}t|j|d<dSdS)Nkeypairz EC2 KeyPairrn)r[r\r]r6get_all_key_pairsr*rr )rrdrkey_listkrerrrget_keys     zCommandLineGetter.get_keycCsr||_|||d|_||||||||||| || || |dS)NrX) r`r_rcr6rirlrqrwrtr|rr)rr`rdrrrr[s        zCommandLineGetter.getN)rLrMrNrWr_rirlrqrtrwr|rrr[rrrrrOs  rOcseZdZedddZeddZeddZeddZeddZe d d d Z e d e dd Z e de dd Ze de dd Ze de dd Ze dedd Ze de dd Ze de dd Ze de dd Ze de dd Ze de dd Ze dedd ZgZgZeddZedUddZedVddZed d!Z dWfd"d# Z!d$d%Z"d&d'Z#d(d)Z$d*d+Z%d,d-Z&d.d/Z'd0d1Z(d2d3Z)d4d5Z*d6d7Z+d8d9Z,fd:d;Z-fdd?Z/d@dAZ0dBdCZ1dDdEZ2dFdGZ3dHdIZ4dJdKZ5dLdMZ6dXdOdPZ7dYdQdRZ8dSdTZ9Z:S)ZServerTName)uniquero Description)rozEC2 Region NamezEC2 Instance IDzEC2 Elastic IP AddresszIs This Server ProductionF)rodefaultzAMI ID)rocalculated_type use_methodzAvailability Zone NamezPublic DNS NamezPrivate DNS NamezSecurity GroupszPrimary Security Group NamezKey NamermzCurrent StatuszServer Launch TimezConsole OutputcCsj|ds |d|dd||dd||ds"|d|ddd|dd|jjjdS)N Credentialsr7r8 DB_Serverdb_typeSimpleDBdb_name) has_section add_sectionset_managerdomainr*)r`cfgr7r8rrradd_credentialss    zServer.add_credentialsNc Ks|rt|d}|dr |dD]}||vr|d|||<qt}||||d}|}|||j|j|d} |d} |d} |d} |dkr[| d d |j t } | | t | trk| j } t | trs| j } | jd |d d | | g|d | | d}g}d}|d}|j}|dur|dkr|d}tdtd|dkrtd |dks||td||D]<}|}||_|dkr|ddnt||_ |d|_|j |_|j|_|r|dkr||_| |!||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#EC2rXrVrr}ruNEBSlogical_volume_namer;rrr0) min_count max_countkey_namesecurity_groupsr0 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,rk)"rroptionsr[rOrcrr7r8rr*rrAr\r r rCrB instances__len__r timesleepupdateuse_ipr6strrkrYid instance_idrputrT)r` config_filelogical_volumerrdoptiongetterrXr6rVkpr}rucfg_fp reservationlirrinstancesrrrcreate st                       z Server.creater,c Cstj}|D]J}|}z||g}Wng}Yt|dkrQ|}||_||_||_|j|_||_ |d|_ |j j D] } | j |krH| |_ q>||SqdSNr;r)r^r6rbrcget_all_reservationsryr*rkrYrr.rr _instancer) r`rr*rkrbrXr6r{rrrrrcreate_from_instance_id^s.      zServer.create_from_instance_idc Csg}tj}|D]P}|}|}|D]C}|jD]=}zttj|j dtj d|j Wqt yW|}||_|j |_ |j |_|j |_||_|||Yqwqq |S)N)rzServer for %s already exists)r^r6rbrcrrnextrrSrloginfo StopIterationr*rYrr.rrT) r`serversrbrXr6r{rrrrrrcreate_from_current_instancesvs.   z$Server.create_from_current_instancesc sBtt|j|fi|d|_d|_d|_d|_d|_|dSN) superrr ssh_key_filer6 _cmdshellr.r _setup_ec2)rrkw __class__rrrs zServer.__init__c Cs|jr |jr |jr dS|jrZ|jr\tjD]G}|j|jkrY||_|j rY|jsYz&|j |j g}t |dkrN|dj D]}|j|j krM|d|_||_q=Wqt yXYqwqdSdSdSr)r6rr.rrYr^rbr*rcrrryrr)rrXr{rrrrrs.       zServer._setup_ec2cCs d}|jr|j|jj}|SNr,)rrstate)rstatusrrr_statuss  zServer._statuscCd}|jr |jj}|Sr)rpublic_dns_namerhostnamerrr _hostnamezServer._hostnamecCrr)rprivate_dns_namerrrr_private_hostnamerzServer._private_hostnamecCrr)rr0)ritrrr_instance_typerzServer._instance_typecCrr)r launch_time)rltrrr _launch_timerzServer._launch_timecCsd}|jr |j}|Sr)rget_console_output)rcorrr_console_outputs zServer._console_outputcCsg}|jr |jj}|Sr)r.groups)rgnrrr_groupsrzServer._groupscCs"|}t|dkr|djSdS)Nr;rr,)rryr)rrrrr_security_groups  zServer._security_groupcCd}|jr |jj}|Sr)rr)rrurrr_zonerz Server._zonecCrr)rr)rknrrr _key_namerzServer._key_namecstt||dSr)rrrrrrrrrs z Server.putcs |jrtdtt|dSNz Can't delete a production server) production ValueErrorrrdeleterrrrrsz Server.deletecC&|jrtd|jr|jdSdSr)rrrstoprrrrr z Server.stopcCrr)rrr terminaterrrrrrzServer.terminatecCs|jr |jdSdSr)rrebootrrrrrsz Server.rebootcCs&|jdkrtd|jdksdSdS)Nrr)rrrrrrrwaits  z Server.waitcCsb|js.tjd}tj|r"tj|d|j}tj|r"||_|js.t}| d|_|jS)Nz~/.sshz%s.pemzPath to OpenSSH Key file) rr"r# expanduserisdirjoinrisfilerr>)rssh_dirssh_filerHrrrr@s    zServer.get_ssh_key_filecCs,|jsddlm}||||_|jS)Nr)cmdshell)r boto.managerr@start)rrrrr get_cmdshells   zServer.get_cmdshellcCs d|_dSr)rrrrrreset_cmdshells zServer.reset_cmdshellcCs<t|}||}Wd|S1swY|Sr)r rrC)rr4cmdrrrrrCs   z Server.runrcCs|t||Sr)r@r)rrrrr get_bundlers zServer.get_bundlercCs"ddlm}|||||dS)Nrr)rssh_pwd)rrr@)rrrrrrrget_ssh_client#s zServer.get_ssh_clientcCs|d|S)Nzapt-get -y install %s)rC)rpkgrrrinstall(szServer.install)NNN)r,rrK)rN);rLrMrNrr*rkrYrrrrr rami_idrurprivate_hostnamelistrsecurity_grouprr0rropenconsole_outputpackagesplugins classmethodrrrrrrrrrrrrrrrrrrrrrrr@rrrCrrr __classcell__rrrrrsj        P        r)#__doc__boto.ec2r^boto.mashups.iobjectrboto.pyami.configrrboto.sdb.db.modelrboto.sdb.db.propertyrrrr rr boto.ec2.zoner boto.ec2.keypairr r"r contextlibr boto.exceptionr boto.compatrrrpobjectrrOrrrrrs"       Ue