Current File : //usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyc
�
��Xc@s�ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZddlm
Z
ddlmZmZddlmZmZddlZddlZddlmZmZddlmZmZmZddlmZdd	lm Z d
ddd
ddgZ!ej"j#edd�Z$ej"j#edd�Z%de
eefd��YZ&d�Z'd�Z(e)d�Z*d�Z+d�Z,d�Z-dS(i����N(tArgtMutuallyExclusiveArgList(tBaseCommand(t
ArgumentErrortClientError(tFileTransferProgressBarMixintRegionConfigurableMixin(t	Euca2oolst
SYSCONFDIR(tdelimited_listtfilesizetmanifest_block_device_mappings(tBootstrapRequest(tBundleImagetbtrfstext2text3text4tjfstxfss
bundle-voltexcludestfstabtBundleVolumec#Bs�eZeZdZdzZedddd�eddd	d
dd�edd
dddd{�eddd	dded�dd�eddd	dded�dd�edd d	d!ded"d#dd$�ed%d&d'dd(�ed)d&d'dd*�e	ed+d,d-d&d'dd.�ed/d,d-d&d0dd1��ed2d3d	d
d"d4dd5�ed6d7dd|dd;�ed<d	d=dd>�e	ed?d	d=dd@�edAd&d'ddB��edCd	d=ddD�edEdFd	d=ddG�edHdId	d=ddJ�edKd	d=ddL�edMdNd	dOddP�edQd	dRddS�edTd	dRddU�edVdWd}ddX�edYdWd}dej�edZdWd}dej�e
jjed[d\d	d]dedd^�ed_d	d`ded�d"gdda�edbded"dcdej�edddde�dej�edfddg�dej�gZdh�Zdi�Zdj�Zd}dk�Zdl�Zdm�Zdn�Zdo�Zdp�Zdq�Zdr�Zds�Zdt�Zdu�Zdv�Z dw�Z!dx�Z"dy�Z#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
--destinationtmetavartDIRs�location 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-inherittstore_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--scripttFILEs�location 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--privatekeys�file 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-sizei�s	--enc-keycCs
t|d�S(Ni(tint(ts((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt<lambda>�ss--enc-ivcCs
t|d�S(Ni(R4(R5((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR6�scCs1tj�dkr!td��n|jjd�sBtd��n|jddd�t�}|jjd�r}|j�n|jjd	�s�t	|�|jd	<|jd	s�|j
jd
|�tdj|���n|j
j
d|jd	�n|jjd
�r-|jjd�r-d|jd
<ndS(Nismust be superusertarchsargument -r/--arch is requireds	/dev/nullt
image_sizeiR*t	partitions?could not determine the partition table type for root device %ssdcould not determine the type of partition table to use for disk {0}; specify one with -P/--partitions"discovered partition table type %sRtgenerate_fstabs
/etc/fstab(tostgeteuidtRuntimeErrortargstgetRt#_BundleVolume__build_bundle_commandt_get_root_devicet*_BundleVolume__populate_args_from_metadatat_get_partition_table_typetlogtwarntformattinfo(tselftroot_device((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt	configure�s(	

&cCs�|jjd�r"|jd}ntjjdd�}tjj||jjd�pXd�}tj	ddd|�}|j
||jd�}z0|j||�}z�y�|j�}|j
d|d|g�|j||�|j|�|j|�|jjd	�rG|jd	|g}|jjd
t|��tj|�nWn7tk
r�|jjd�d}tj|IJ�nXWdtjd
�|j|�tj|�X|j||�Wd|j||�X|j|�}	|	j �}
tj!|�|
S(Ntdestinationtprefixsbundle-timagestarget-tdirtsizes	--excludetscriptsrunning user script ``%s''s received ^C; skipping to cleanupsf

Cleaning up after ^C
WARNING:  pressing ^C again will result in the need for manual device cleanup

g�������?("R>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.pyRk�sF'








cCs"x|dD]}dG|GHqWdS(NisWrote manifest((RHRutmanifest_filename((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pytprint_result�scsDd}t�fd�|D��}tj�d|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	<genexpr>�sRMR8t
image_typetmachine(sprefixsdestinationsarchRxRyRzR{R|R}R~RR�R�R�R�R�R�R�R�R�R�R�(tdictR
t
from_other(RHtimage_filenameR8tbundle_argstbundle_args_dict((RHsJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__build_bundle_command�s
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?tstatus_codettexttNone(RHRTturltresponse((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__read_metadata_value�s
cCsE|j|�}|rAg|j�D]}|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_list�s,cCsfi}|jd�s"|d7}n|j|�}x.|D]&}|r8|j||�||<q8q8W|S(NR,(tendswitht!_BundleVolume__read_metadata_listR�(RHRTtmetadatatkeysR�((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__read_metadata_dicts

cCs�y�|jjd�sH|jd�|jd<|jjd|jd�n|jjd�s�|jd�|jd<|jjd|jd�n|jjd�s�|jd�|jd<|jdr�|jjd	d
j|jd��q�n|jjd�s�i|jd<xL|jd�pij�D]/\}}|j	d
�s&||jd|<q&q&Wx:|jdj�D]"\}}|jjd||�qmWnWn#t
jjk
r�t
d��nXdS(ss
        Populate missing/empty values in self.args using info obtained
        from the metadata service.
        R|s	kernel-idsinherited kernel: %sR}s
ramdisk-idsinherited ramdisk: %sRs
product-codessinherited product codes: %sR$R~sblock-device-mappingtebss%inherited block device mapping: %s=%ssRmetadata service is absent or unresponsive; use --no-inherit to proceed without itN(R>R?R�RDRGR�RUt!_BundleVolume__read_metadata_dicttitemst
startswithR�t
exceptionstTimeoutR(RHR�tval((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__populate_args_from_metadatas0

	 c	Cstjdddj|�dddjt|�d�g�|jdd	kr|d }tjdd|ddj|�g�ns|jddkr�tjddddddd|g�tjdddddd|g�tjdd|g�n|j|�}tjj	|�st
�|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_imageR;RTtexiststAssertionError(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�}z�xb|jj�D]Q}|j�}|d
 ddgkr`|d
}|j	d
�r�dj
|�Sq`q`W|jjd�td��Wd|j
�XnS|jjd|�tjdd|dgd	tj�}|j
�\}}|j�SdS(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>RDR�R`tPopentPIPER�t	readlinestsplitR�RFterrorR=tcommunicatetstrip(	RHRMR�R�t
line_splitRoR�tloopdevt_((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__map_disk_imageFs&
cCs�t�}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|�tjdg�tjd�tjd|g�dS(Ns
unmounting %stsyncg�������?tumount(RDRGR`RaReRf(RHRo((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__unmount_filesystemrs
cCs/g}|jd�r/|jd|df�n|jd�rX|jd|df�n|jd�dkr}|jd�n|r+|jd�dkr�dg|}nJ|jd�dkr�d
g|}n%|jd�dkr�dg|}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[R�RDRGR_R`Ra(RHRoRptoptionsRr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__update_filesystem_idsxs$
cCs~tjdg�|jddkrK|jjd|�ddd|g}n"|jjd	|�d
d|g}tj|�dS(NR�R9R-R.sunmapping partitioned image %sR�s-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_image�scCs@g}x3|jjd�pgD]}|jd|g�qWx3|jjd�pTgD]}|jd|g�qUW|jjd�s�xgt�D]Y\}}}|tkr�|jjd|||�|jdtjj	|d�g�q�q�Wn|jjd�r<tjj
t�r<|jjd	t�|jd
tg�n|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_TYPESRDR�R;RTRUtisfilet
EXCLUDES_FILE(RHR>R�R�RoRntfstype((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__get_exclude_and_include_args�s
,%c
Cs�|jjd�pd}|jd�s4|d7}n|jd�sP|d7}ndg}|jjd�r�tjddgdtj�}|j�\}}|jd�d	p�d
j�d}|dkr�|j	d
�q�|j	d�n
|j	d�ddg||||g}	|j
jdt|	��dGHtj|	�}|j
�|jdkr�|j
jd|j�dGHdg||||g}	tj|	�}|j
�n|jdkr�|j
jd|j�tj|jd��ndS(NtvolumeR,s
-rHlpogDtSR�trsyncs	--versionR�sversion 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?R�R`R�R�R�R9R�R�RDRGR_twaitt
returncodeR�tCalledProcessError(
RHR)Rqtsourcet
rsync_optsR�toutR�t
rsync_versionRr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__copy_to_target_dir�s<

	#



cCs�tjj|dd�}tjj|�rW|d}|jjd|�t||�n|jjd�r�|jj	d|jd�tt
|�nC|jjd�r�|jj	d|jd�t|jd|�ndS(NtetcRs.baks$backing up original fstab file as %sR:sgenerating fstab file from %ssusing fstab file %s(R;RTRUR�RDR�t_copy_with_xattrsR>R?RGtFSTAB_TEMPLATE_FILE(RHRntfstab_filenamet	fstab_bak((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__insert_fstab�s
cCs�|jjd�r�tjj|ddd�}tjj|�rl|d}|jjd|�t||�n|jj	d|jd�t|jd|�ndS(Ntgrub_configtboottgrubsmenu.lsts.baks+backing up original grub1 config file as %ssusing grub1 config file %s(
R>R?R;RTRUR�RDR�R�RG(RHRnt
grub_filenamet	grub_back((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt__insert_grub_config�s	
(RR(RRRR R!R"(smbrsgptR/N($t__name__t
__module__RtSUITEtDESCRIPTIONt
REGION_ENVVARRR	R4RR�targparsetSUPPRESSRt
AUTH_CLASStARGSRR
RJRkRwR@R�R�R�RBRXR�RYRgRiRjRZR\R]R^(((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR7s�							0				
	!								%	c	Cs�i}td��F}x<|D]4}|jdd�\}}}}||f||<qWWdQXg}x6|j�D](\}\}}|j|||f�qmW|S(Ns/proc/mountsi(topenR�R�R�R�(tfilesystems_dicttmountsR�RoRnR�R�tfilesystems_list((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR��s	
cCs�tjddd|gdtj�}i}xz|jD]o}|j�jd�\}}}|dkrq||d<q4|dkr�||d	<q4|d
kr4||d<q4q4W|j�|jdkr�tj|jd��n|S(
Ntblkids-otexportR�t=tLABELR�tTYPER#tUUIDR�i(	R`R�R�R�R�R9R�R�R�(RoRRpR�R�R�R�((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR�s


cCs|ddkrM|ddkr-|d }qM|ddkrM|d }qMn|r_tj}nd}tjddd|d	gd
tjd|�}|j�\}}xa|j�D]S}|jd�r�|jd
�}|ddkr�dS|ddkr�dSdSq�q�WdS(Ni����t
0123456789i����tdtpR�s-ms-sR�R�Rds/dev/t:itmsdosR-R.R/(R`R�R�R�R�R�R�R�(RoR�tstderr_destR�R�R�R�t	line_bits((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyRCs&
cCscd}xAt�D]6\}}}|dkrtjj|�r|}qqW|s_td��n|S(NR,sno / filesystem found(R�R�R;RTR�tKeyError(RIRoRnR�((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyRA4s
cCsdjd�|D��S(NR�css|]}tj|�VqdS(N(tpipestquote(R�targ((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pys	<genexpr>Bs(RU(Rr((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR_AscCsHytjdd||g�Wn$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`RaR�tshutiltcopy2(R�R)((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyR�Es(.Rtos.pathR;RR"R`RcRVRetrequestbuilderRRtrequestbuilder.commandRtrequestbuilder.exceptionsRRtrequestbuilder.mixinsRRR�RQteuca2ools.commandsRRteuca2ools.commands.argtypesR	R
Rteuca2ools.commands.bootstrapRt%euca2ools.commands.bundle.bundleimageR
R�RTRUR�R�RR�R�tFalseRCRAR_R�(((sJ/usr/lib/python2.7/site-packages/euca2ools/commands/bundle/bundlevolume.pyt<module>s:	��