Xc@sddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z ddlmZmZddlmZmZddlZddlZddlmZmZddlmZmZmZddlmZdd lm Z d d d d ddgZ!ej"j#eddZ$ej"j#eddZ%de eefdYZ&dZ'dZ(e)dZ*dZ+dZ,dZ-dS(iN(tArgtMutuallyExclusiveArgList(t BaseCommand(t ArgumentErrort ClientError(tFileTransferProgressBarMixintRegionConfigurableMixin(t Euca2oolst SYSCONFDIR(tdelimited_listtfilesizetmanifest_block_device_mappings(tBootstrapRequest(t BundleImagetbtrfstext2text3text4tjfstxfss bundle-voltexcludestfstabt BundleVolumec#BseZeZdZdzZeddddeddd d dd ed d dddd{eddd ddedddeddd ddedddedd d d!ded"d#dd$ed%d&d'dd(ed)d&d'dd*e ed+d,d-d&d'dd.ed/d,d-d&d0dd1ed2d3d d d"d4dd5ed6d7dd|dd;ed<d d=dd>e ed?d d=dd@edAd&d'ddBedCd d=ddDedEdFd d=ddGedHdId d=ddJedKd d=ddLedMdNd dOddPedQd dRddSedTd dRddUedVdWd}ddXedYdWd}de j edZdWd}de j e jjed[d\d d]dedd^ed_d d`dedd"gddaedbded"dcde j eddddede j edfddgde j gZdhZdiZdjZd}dkZdlZdmZdnZdoZdpZdqZdrZdsZdtZduZdvZ dwZ!dxZ"dyZ#RS(~sbPrepare this machine's filesystem for use in the cloud This command must be run as the superuser.tEUCA_DEFAULT_REGIONtAWS_DEFAULT_REGIONs-ps--prefixthelpsPthe file name prefix to give the bundle's files (default: image)s-ds --destinationtmetavartDIRslocation to place the bundle's files (default: dir named by TMPDIR, TEMP, or TMP environment variables, or otherwise /var/tmp)s-rs--archs#the image's architecture (required)tchoicesti386tx86_64tarmhftppctppc64tppc64les-es --excludesPATH,...ttypet,s(comma-separated list of paths to excludes-is --includes(comma-separated list of paths to includes-ss--sizetMiBtdefaulti(s0size of the image to create (default: 10240 MiB)s --no-filtertactiont store_trues(do not filter out sensitive/system filess--allsdinclude all filesystems regardless of type (default: only include local filesystems)s --inherittdesttinheritseuse the metadata service to provide metadata for the bundle (this is the default)s --no-inheritt store_falsesGdo not use the metadata service for bundle metadatas-vs--volumet/sSlocation of the volume from which to create the bundle (default: /)s-Ps --partitiontmbrtgpttnoneslthe type of partition table to create (default: attempt to guess based on the existing disk)s--scripttFILEslocation of a script to run immediately before bundling. It will receive the volume's mount point as its only argument.s--fstabsOlocation of an fstab(5) file to copy into the bundled images--generate-fstabsQautomatically generate an fstab(5) file for the bundled images --grub-configs{location of a GRUB 1 configuration file to copy to /boot/grub/menu.lst on the bundled images-ks --privatekeysfile containing your private key to sign the bundle's manifest with. This private key will also be required to unbundle the image in the future.s-cs--certs&file containing your X.509 certificates --ec2certs=file containing the cloud's X.509 certificates-us--usertACCOUNTsyour account IDs--kerneltIMAGEsKID of the kernel image to associate with this machine images --ramdisksLID of the ramdisk image to associate with this machine images--bootstrap-urltroute_tos|[Eucalyptus only] bootstrap service endpoint URL (used for obtaining --ec2cert automaticallys--bootstrap-services--bootstrap-auths-Bs--block-device-mappingss%VIRTUAL1=DEVICE1,VIRTUAL2=DEVICE2,...s`block device mapping scheme with which to launch instances of this machine images--productcodessCODE1,CODE2,...s3comma-separated list of product codes for the images --part-sizeis --enc-keycCs t|dS(Ni(tint(ts((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pytss--enc-ivcCs t|dS(Ni(R4(R5((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR6scCs1tjdkr!tdn|jjdsBtdn|jdddt}|jjdr}|jn|jjd st ||jd <|jd s|j j d |td j |n|j j d |jd n|jjd  r-|jjd r-d|jd R?t euca2oolstutiltmkdtemp_for_large_filesR;tpathtjointtempfiletmkdtempt _BundleVolume__create_disk_imaget*_BundleVolume__create_and_mount_filesystemt+_BundleVolume__get_exclude_and_include_argstextendt!_BundleVolume__copy_to_target_dirt_BundleVolume__insert_fstabt!_BundleVolume__insert_grub_configRDRGt _quote_cmdt subprocesst check_calltKeyboardInterrupttsyststderrttimetsleept!_BundleVolume__unmount_filesystemtrmdirt$_BundleVolume__update_filesystem_idst _BundleVolume__detach_disk_imageR@tmaintremove( RHtdestdirRMt mountpointtdevicetfsinfot exclude_optstcmdtmsgt bundle_cmdtresult((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyRksF'              cCs"x|dD]}dG|GHq WdS(NisWrote manifest((RHRutmanifest_filename((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt print_resultscsDd}tfd|D}tjd|d|dd|S(NRLRKR7t privatekeytcerttec2certtusertkerneltramdisktblock_device_mappingst productcodest part_sizetenc_keytenc_ivt show_progresstkey_idt secret_keytsecurity_tokent bootstrap_urltbootstrap_servicetbootstrap_authtregionc3s'|]}|jj|fVqdS(N(R>R?(t.0tkey(RH(sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pys sRMR8t image_typetmachine(sprefixs destinationsarchRxRyRzR{R|R}R~RRRRRRRRRRRR(tdictR t from_other(RHtimage_filenameR8t bundle_argstbundle_args_dict((RHsJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__build_bundle_commands cCsL|jjd|d|}tj|dd}|jdkrH|jSdS(Ns#reading metadata service value '%s's,http://169.254.169.254/2012-01-12/meta-data/ttimeoutii(RDtdebugtrequestsR?t status_codettexttNone(RHRTturltresponse((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__read_metadata_values  cCsE|j|}|rAg|jD]}|r"|jd^q"SgS(NR,(t"_BundleVolume__read_metadata_valuet splitlinestrstrip(RHRTtvaluetline((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__read_metadata_lists,cCsfi}|jds"|d7}n|j|}x.|D]&}|r8|j||||R?RRDRGRRUt!_BundleVolume__read_metadata_dicttitemst startswithRt exceptionstTimeoutR(RHRtval((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__populate_args_from_metadata s0      c Cstjdddj|dddjt|dg|jdd kr|d }tjdd|ddj|gns|jddkrtjddddddd|gtjdddddd|gtjdd|gn|j|}|S(!Ntdds if=/dev/zerosof={0}sbs=1Mscount=1sseek={0}iR9R-sunit ss mklabel msdossmkpart primary 64 -1ss set 1 boot ontprinttquittparteds-ss--t R.tsgdisks--news 128:1M:+1Ms --typecodes128:ef02s --change-names 128:BIOS Boots--largest-new=1s1:8300s1:Images--print(sunit ss mklabel msdossmkpart primary 64 -1ss set 1 boot onsprintsquit(R`RaRFR4R>RUt_BundleVolume__map_disk_image(RHRMt size_in_mbt parted_scripttmapped((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__create_disk_image,s$ c Cs:|jddkr|jjd|tjdddd|gd tj}zxb|jjD]Q}|j}|d d d gkr`|d }|j d rdj |Sq`q`W|jj dt dWd|j XnS|jjd|tjdd|dgd tj}|j \}}|jSdS(NR9R-R.smapping partitioned image %stkpartxs-ss-vs-atstdoutitaddtmaptp1s/dev/mapper/{0}s+failed to get usable map output from kpartxsdevice mapping failedsmapping unpartitioned image %stlosetups-fs--show(smbrsgpt(R>RDRR`tPopentPIPERt readlinestsplitRRFterrorR=t communicatetstrip( RHRMRRt line_splitRoRtloopdevt_((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__map_disk_imageFs&    cCst}t|}|jjd|||dd|dgg}xA|D]9}|j||jjdt|tj|qKW|jjd|d||tjdd|d||g|S(Ns4creating filesystem on %s using metadata from %s: %stmkfss-tR#sformatting with ``%s''smounting %s filesystem %s at %stmount(RAt_get_filesystem_infoRDRGtappendR_R`Ra(RHRoRnRIRptfs_cmdstfs_cmd((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__create_and_mount_filesystembs        cCsG|jjd|tjdgtjdtjd|gdS(Ns unmounting %stsyncg?tumount(RDRGR`RaReRf(RHRo((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__unmount_filesystemrs cCs/g}|jdr/|jd|dfn|jdrX|jd|dfn|jddkr}|jdn|r+|jddkrd g|}nJ|jdd krd g|}n%|jddkrdg|}n|j||jjd|t|tj|ndS(s Apply filesystem identifiers to an unmounted filesystem. To avoid UUID conflicts, run this only after the filesystem no longer needs to be mounted on the running system. tlabels-Ltuuids-UR#RRRs-it0ttune2fsRt jfs_adminRt xfs_admins-updating device %s filesystem IDs with ``%s''N(RRR(s-iR(RRR(R?R[RRDRGR_R`Ra(RHRoRptoptionsRr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__update_filesystem_idsxs$  cCs~tjdg|jdd krK|jjd|ddd|g}n"|jjd |d d|g}tj|dS( NRR9R-R.sunmapping partitioned image %sRs-ss-ds!unmapping unpartitioned device %sR(smbrsgpt(R`RaR>RDR(RHRMRoRr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__detach_disk_imagescCs@g}x3|jjdpgD]}|jd|gqWx3|jjdpTgD]}|jd|gqUW|jjdsxgtD]Y\}}}|tkr|jjd||||jdtjj |dgqqWn|jjd r<tjj t r<|jjd t |jd t gn|S( Ntexcludes --excludetincludes --includetalls excluding %s filesystem %s at %ss**t no_filtersadding path exclusions from %ss--exclude-from( R>R?R[t_get_all_mountstALLOWED_FILESYSTEM_TYPESRDRR;RTRUtisfilet EXCLUDES_FILE(RHR>RRRoRntfstype((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__get_exclude_and_include_argss   ,%c Cs|jjdpd}|jds4|d7}n|jdsP|d7}ndg}|jjdrtjddgdtj}|j\}}|jdd pd jd }|d kr|j d q|j dn |j dddg||||g} |j j dt | dGHtj| }|j |jdkr|j j d|jdGHdg||||g} tj| }|j n|jdkr|j jd|jtj|jdndS(NtvolumeR,s -rHlpogDtSRtrsyncs --versionRsversion itis3.1.0s--info=progress2s --progresss--quiets-Xscopying files with ``%s''sCopying files...is2rsync exited with code %i; retrying without xattrss$Retrying without extended attributesisrsync exited with code %i(ii(R>R?RR`RRRR9RRRDRGR_twaitt returncodeRtCalledProcessError( RHR)Rqtsourcet rsync_optsRtoutRt rsync_versionRr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__copy_to_target_dirs<    #      cCstjj|dd}tjj|rW|d}|jjd|t||n|jjdr|jj d|jdtt |nC|jjdr|jj d|jdt|jd|ndS(NtetcRs.baks$backing up original fstab file as %sR:sgenerating fstab file from %ssusing fstab file %s( R;RTRUtexistsRDRt_copy_with_xattrsR>R?RGtFSTAB_TEMPLATE_FILE(RHRntfstab_filenamet fstab_bak((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__insert_fstabs cCs|jjdrtjj|ddd}tjj|rl|d}|jjd|t||n|jj d|jdt|jd|ndS(Nt grub_configtboottgrubsmenu.lsts.baks+backing up original grub1 config file as %ssusing grub1 config file %s( R>R?R;RTRURRDRRRG(RHRnt grub_filenamet grub_back((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__insert_grub_configs    (RR(RRRR R!R"(smbrsgptR/N($t__name__t __module__RtSUITEt DESCRIPTIONt REGION_ENVVARRR R4RRtargparsetSUPPRESSR t AUTH_CLASStARGSR R RJRkRwR@RRRRBRXRRYRgRiRjRZR\R]R^(((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR7s           0     !       % c Csi}tdF}x<|D]4}|jdd\}}}}||f||Bs(RU(Rr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR_AscCsHytjdd||gWn$tjk rCtj||nXdS(s shutil.copy2 doesn't preserve xattrs until python 3.3, so here we attempt to leverage the cp command to do it for us. tcps-aN(R`RaRtshutiltcopy2(RR)((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyREs(.Rtos.pathR;RR!R`RcRVRetrequestbuilderRRtrequestbuilder.commandRtrequestbuilder.exceptionsRRtrequestbuilder.mixinsRRRRQteuca2ools.commandsRRteuca2ools.commands.argtypesR R R teuca2ools.commands.bootstrapR t%euca2ools.commands.bundle.bundleimageR RRTRURRRRRtFalseRCRAR_R(((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyts: