Xc@sddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z ddlZddlZddlZddlmZmZmZmZddlmZddlmZddlmZddlmZdd lm Z dd l!m"Z"dd l#m$Z$d d dZ%de&fdYZ'de&fdYZ(de&fdYZ)dZ*dZ+dS(iN(tArgtMutuallyExclusiveArgList(t ArgumentError(tb64encoded_file_contentstdelimited_listtfilesizetmanifest_block_device_mappings(tDescribeServiceCertificates(t CheckBucket(t CreateBucket(t GetObject(t PostObject(t PutObject(tAWSErrori iitBundleCreatingMixincBsreZedddddeddedddd ed d dd dd edddeddOddeddddddeddddddeddddded d!dd"dd#ed$dd%dd&ed'dd%dd(ed)d*dPdd+ed,d*dPdejed-d*dPdejed.d/dd0d1edd2ed3dd4d1ed5d6gdd7ed8ddQd6d9dejed<d1e dd=ed>d1e d6d?dejed@d1dAdejedBd1dCdejedDdEdFdejgZ dGZ dHZ dIZ dJZdKZdLZedMZedNZRS(Rs-is--imagetmetavartFILEtrequiredthelps.file containing the image to bundle (required)s-ps--prefixsthe file name prefix to give the bundle's files (required when bundling stdin; otherwise defaults to the image's file name)s-ds --destinationtDIRslocation to place the bundle's files (default: dir named by TMPDIR, TEMP, or TMP environment variables, or otherwise /var/tmp)s-rs--archtchoicesti386tx86_64tarmhftppctppc64tppc64les#the image's architecture (required)s-ks --privatekeysfile containing your private key to sign the bundle's manifest with. If one is not available the bundle will not be signed.s-cs--certsfile containing your X.509 certificate. If one is not available it will not be possible to unbundle the bundle without cloud administrator assistance.s --ec2certsfile containing the cloud's X.509 certificate. If one is not available locally it must be available from the bootstrap service.s-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,...ttypes`block device mapping scheme with which to launch instances of this machine images--productcodessCODE1,CODE2,...t,tdefaults3comma-separated list of product codes for the images --image-typetmachinetkerneltramdisks --image-sizes?the image's size (required when bundling stdin)s --part-sizeis --enc-keycCs t|dS(Ni(tint(ts((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytwss--enc-ivcCs t|dS(Ni(R$(R%((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyR&yss--batchtactiont store_truecCsD|jjds|jjd}dtjkr\|jjdtjd|jd %i)(R9R:R=RCtexiststisdirRtformattfdopentduptsyststdintfilenot isinstanceRLt string_typestbasenamet euca2oolstutilt get_filesizetopentEC2_BUNDLE_SIZE_LIMITR?twarn(RN((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytconfigure_bundle_outputs8 ( #  cCsl|jjddkr(d|jd|jjd|jd}n|jd}|jjdr|jjd|jd}n|jd}dj||jd32x}(trandomt SystemRandomR9R:R?tinfot getrandbitsRZ(RNtsrandt enc_key_itenc_iv_i((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytgenerate_encryption_keyss c Cs|jjdtd|jd|jjd|d|dddd }|j}x|jd pggD]}|jd d krh|jd dkrhtjdt }|j |d|j j |j |jdSsRnRos AES-128-CBC(RctbundletmanifesttBundleManifestR?RR9t image_archR:t kernel_idt ramdisk_idRltupdatet product_codestextendt image_namet account_idRkt image_digesttimage_digest_algorithmRWtsumtbundled_image_sizeRnRot enc_algorithmt image_parts(RNtdigesttpartinfoR((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytbuild_manifestBs,     c Cs8t|d#}|j|j|d|WdQXdS(Ntwt pretty_print(RfRtdump_manifest_to_str(RNRtfilenameRt manifest_file((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytdump_manifest_to_fileZs cCs.|j|jd|jd|jdd|S(NR1R-R4R(t dump_to_strR9(RNRR((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyR_s (RRRRRRN(smachineskernelsramdisk(t__name__t __module__RRItNonetargparsetSUPPRESSRRRtARGSRSRiRmRwRHRRRR(((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyR9sd       b "     tBundleUploadingMixincBseZedddddeddeddd d d)dd eeddddddejddeddddddeddedddddedddedddddd d!dd"dd#gZd$Z d%Z d&Z e d'Z d*d*d(ZRS(+s-bs--bucketRsBUCKET[/PREFIX]RRs)bucket to upload the bundle to (required)s--aclR s aws-exec-readRs public-readsec2-bundle-readsQcanned ACL policy to apply to the bundle (default: aws-exec-read)s--upload-policytdestt upload_policytPOLICYRs&upload policy to use for authorizations--upload-policy-fileRsMfile containing an upload policy to use for authorizations--upload-policy-signaturet SIGNATUREsYsignature for the upload policy (required when an 'upload policy is used)s --locationslocation constraint of the destination bucket (default: inferred from s3-location-constraint in configuration, or otherwise none)s--retrytretriesR't store_consttconstiis"retry failed uploads up to 5 timescCsm|jjdri|jjds3tdn|jjdsTtdnd|_d|_ndS(NRtkey_ids:-I/--access-key-id is required when using an upload policytupload_policy_signaturesA--upload-policy-signature is required when using an upload policy(R9R:RRRzt AUTH_CLASS(RN((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytconfigure_bundle_upload_auth}s cCsN|jdjd\}}}|rB|jd rB|d7}n|d|S(Ntbuckett/(R9t partitiontendswith(RNRt_RV((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytget_bundle_key_prefixs cCs|jjdr&|jjddS|jdjddd}y#tj|d|}|jWnltk r}|j dkr|jjd|t j|d|d |jjd }|jqnXdS( NRs6using an upload policy; not verifying bucket existenceRRiiiscreating bucket '%s'tlocation( R9R:R?RrtsplitRt from_otherRR RKR (RNRRRQ((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytensure_dest_bucket_existss! cKs"|jjdr|r$|GdGHn|jjdrMi|jdd6}ni}|j|tj|d|d|d|jjdpdd |jdd |jd d |jd |}nTtj|d|d|d|jjdpdd|jjdpdd||}|jdS(NRs uploading...tsecurity_tokensx-amz-security-tokentsourceRtacls aws-exec-readtPolicyt SignatureRtAWSAccessKeyIdRRit show_progress(R9R:RR RR R(RNRRRt putobj_kwargstpostobj_kwargsR((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytupload_bundle_files&     cKszyxztr|j}|tjj|j}|j|j|||dk rc|jn|dk r |j |q q WWnt k rdSXWd|j |dk r|j nXdS(N( RItrecvR=RCRbRRRtreleasetsendtEOFErrortclose(RNtpartinfo_in_mpconnt key_prefixtpartinfo_out_mpconntpart_write_semRRR((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytupload_bundle_partss         (s public-reads aws-exec-readsec2-bundle-readN(RRRRIRtbase64t b64encodeRRRRRRRRR(((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyRes.       tBundleDownloadingMixincBseZedddddeddddeedd d d dddd ed dd d ddddddedd dddddddjgZddZdZ dZ dZ dZ dZ RS(s-bs--bucketRsBUCKET[/PREFIX]RRRs\the bucket that contains the bundle, with an optional path prefix (required)s-ms --manifestRRslthe manifest's complete file name, not including any path that may be specified using -bs-ps--prefixRcCs|dS(Ns .manifest.xml((tx((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyR&ssYthe portion of the manifest's file name that precedes ".manifest.xml"s--local-manifesttlocal_manifestRsSuse a manifest on disk and ignore any that appear on the serverc Cs9|jjdrIt|jddtjjjj|jdd|S|j}t j }|j j d|t j|d|d|d|}y|jWnStk r}|jdkr|j jd d ttd j|nnX|j|jd tjjjj|d|SWdQXdS( NRRtprivkey_filenamesreading manifest from %sRyRRisfailed to fetch manifestR8s+manifest '{0}' does not exist on the serveri(R9R:RFRcRRRtread_from_filetget_manifest_s3pathRt TemporaryFileR?RrR RRR RKR@RIt ValueErrorRZRtseektread_from_fileobj(RNt s3_serviceRtmanifest_s3pathtmanifest_tempfileRRQ((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytfetch_manifests,     cCs;|jjdr3dj|jd|jdfSdSdS(NRRR(R9R:tjoinR(RN((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyR s!c CsL|j|}x|D]\}}tjj|tjj||_|jjd||jtj |d|d|d|jd|j j dt }|j }|j|||qW|j} | rHtjj|tjj| } |jjd| | tj |d|d| d| d|j j dt }|j | SdS(Nsdownloading part %s to %sRyRRRsdownloading manifest %s to %s(tmap_bundle_parts_to_s3pathsR=RCRRbRR?RrR RR9R:RRt(_BundleDownloadingMixin__check_part_sha1RR( RNRtdest_dirRtpartsRt part_s3pathRRJRt manifest_dest((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytdownload_bundle_to_dirs2          c Cs|j|}x||D]t\}}|jjd|tj|d|d|d|d|jjdt}|j}|j |||qWdS(Nsdownloading part %sRyRRR( RR?RrR RR9R:RRR( RNRtfileobjRRRRRRJ((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pytdownload_bundle_to_fileobj/s cCsJg}x=|jD]2}|j|dj|jd|jffqW|S(NRR(RtappendRR9R(RNRRR((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyR<s  'cCsb||d|jkr^|jjd|j||dtdj|j||dndS(Ntsha1sKrejecting download due to manifest SHA1 mismatch (expected: %s, actual: %s)sJdownloaded file {0} appears to be corrupt (expected SHA1: {0}, actual: {1}(t hexdigestR?terrort RuntimeErrorRZ(RNRRRJ((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyt__check_part_sha1Cs   N(RRRRIRRRRRRRRRR(((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyRs$      cCs^tjj|s-tdj||ntjj|sZtdj||ndS(Ns{0} file '{1}' does not exists{0} file '{1}' is not a file(R=RCRXRRZtisfile(Rtfiletype((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyRFNs   cCs8tjddtjdtj}|j|djS( Ntopenssltx509s -fingerprints-nooutR^tstdouti(RRs -fingerprints-noout(t subprocesstPopentPIPEt communicatetstrip(t cert_contenttpopen((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyt_get_cert_fingerprintWs (,RRRtos.pathR=RpRR]RtrequestbuilderRRtrequestbuilder.exceptionsRRLteuca2ools.bundle.manifestRcteuca2ools.bundle.utilteuca2ools.commands.argtypesRRRRt8euca2ools.commands.bootstrap.describeservicecertificatesRt!euca2ools.commands.s3.checkbucketRt"euca2ools.commands.s3.createbucketR teuca2ools.commands.s3.getobjectR t euca2ools.commands.s3.postobjectR teuca2ools.commands.s3.putobjectR teuca2ools.exceptionsR RgtobjectRRRRFR(((sD/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/mixins.pyts6           "-rw