
    W'FgzS                        S SK r S SKrS SKrSSKJr  SSKJrJrJrJr  SSK	J
r
  SSKJr  SSKJr  S rS	 rS
 rS r " S S5      rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S S jr!S r"S S jr#S r$g)!    N   )cli)failfail_conflictingprint_stdoutprint_stderr)Guest)log)xmlutilc                 D    U S-   n[         R                  R                  U5        [         R                  R                  5         [         R                  R                  5       R                  5       R                  5       nUS;   a  gUS;   a  g[        [        S5      5        M  )Nz (y/n): )yyesT)nnoFzPlease enter 'yes' or 'no'.)
sysstdoutwriteflushstdinreadlinelowerstripr   _)msgprintmsginps      +/usr/share/virt-manager/virtinst/virtxml.pyprompt_yes_or_nor      s~    
#

"

ii  "((*002,K89:     c                     [         R                  " XSS5      nU(       a  [        R                  " SU5        U$ [        R                  " S5        U$ )NzOriginal XMLzAltered XMLzXML diff:
%sz(No XML diff, didn't generate any change.)r   diffr
   debug)origxmlnewxmlr!   s      r   get_diffr%   %   s@    <<GD		/4( K 			<=Kr   c                     Uc  g [         R                  " U5      nUR                  5       (       a   U R                  UR                  5       5        g g N)r   parse_os_variantget_nameset_os_name)guest
os_variantosdatas      r   set_os_variantr.   /   sB    !!*-F&//+, r   c                 b    [         R                  " USS9n[        XS9R                  5       nXB:H  $ )NT)inactiveparsexml)r   get_xmldescr	   get_xml)conndomainoriginal_xmlrawxmlnew_xmls        r   defined_xml_is_unchangedr:   8   s.    __Vd3FD*224G""r   c                   ^    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
Srg	)
ActionB   z_
Helper class tracking one pair of
    XML ACTION (ex. --edit) and
    XML OPTION (ex. --disk)
c                 4    Xl         X l        X0l        X@l        g r'   )action_nameselectorparserclassparservalue)selfr?   r@   rA   rB   s        r   __init__Action.__init__H   s    & &&r   c                      U R                   S:H  $ )Neditr?   rC   s    r   is_editAction.is_editR   s    6))r   c                      U R                   S:H  $ )N
add-devicerH   rI   s    r   is_add_deviceAction.is_add_deviceV   s    <//r   c                      U R                   S:H  $ )Nremove-devicerH   rI   s    r   is_remove_deviceAction.is_remove_deviceZ   s    ?22r   c                      U R                   S:H  $ )N	build-xmlrH   rI   s    r   is_build_xmlAction.is_build_xml^   s    ;..r   )r?   rA   rB   r@   N)__name__
__module____qualname____firstlineno____doc__rD   propertyrJ   rN   rR   rV   __static_attributes__ r   r   r<   r<   B   s\    
' * * 0 0 3 3 / /r   r<   c                    UR                   bo  U R                  (       a  [        [        S5      5        U R                  (       a  [        [        S5      5        U R
                  (       a  [        [        S5      5        U R                  R                  (       dJ  U R
                  (       a9  [        [        S5      R                  U R                  R                  5       S95        [        U5      nU R                  R                  U5      (       d  U R                  (       a9  [        [        S5      R                  U R                  R                  5       S95        U R                  (       a9  [        [        S5      R                  U R                  R                  5       S95        UR                  (       aV  U R                  R                  (       d:  [        [        S5      R                  U R                  R                  5       S95        g g g )	Nz2--os-variant/--osinfo is not supported with --editz;--os-variant/--osinfo is not supported with --remove-devicez7--os-variant/--osinfo is not supported with --build-xmlz(--build-xml not supported for {cli_flag}cli_flagz*Cannot use --remove-device with {cli_flag}z'Cannot use --add-device with {cli_flag}z)Don't know how to --update for {cli_flag})r,   rJ   r   r   rR   rV   rA   guest_propnameformatcli_flag_namer	   prop_is_listrN   update)actionr5   options
stub_guests       r   validate_actionrk   c   s   %>>GHI""PQRLMN,,1D1DQ9:AA((668 B : 	; tJ**:66""?@GG ,,::< H > ?<=DD ,,::< E > ? ~~f00??Q:;BB((668 C : 	; @~r   c                    / n/ SQnU H:  nUR                  SS5      n[        X5      nUS;  d  M(  UR                  X545        M<     [        U5      S:X  a:  [	        [        S5      SR                  U Vs/ s H  nSU-   PM
     sn5      -  5        [        U5      S	:  a=  [	        [        S
5      SR                  U Vs/ s H  nSUS   -   PM     sn5      -  5        US   u  pxXx4$ s  snf s  snf )N)rG   rM   rQ   rU   -r   )Fr   zOne of %s must be specified.z, --r   zConflicting options %s)replacegetattrappendlenr   r   join)	ri   
collisionsactionsclinameoptnamevaluecr?   r@   s	            r   check_action_collisionr{   ~   s    JBG//#s+)#w./	  :!Q-.YY'2'Qq'234 	5
:Q'(YYZ8ZqtZ89: 	; 'qMK   3 9s   4C&=C+c                    / n[         R                   H3  n[        XR                  5      nU(       d  M!  UR	                  X#45        M5     [        U5      S:X  a  [        [        S5      5        [        U5      S:w  a8  [        [        S5      U Vs/ s H  oDS   R                  5       PM     sn-  5        US   u  p%X%4$ s  snf )Nr   zNo change specified.r   zCOnly one change operation may be specified (conflicting options %s))	r   VIRT_PARSERSrq   cli_arg_namerr   rs   r   r   re   )ri   ru   rA   ry   rz   rB   s         r   check_xmlopt_collisionr      s    J''!9!9:5{23 (
 :!Q%&'
:!Q * +.89j!""$j9: 	;  *!}K## :s   B>
c                 j    [        U5      u  p#[        U5      u  pE[        XEX#5      n[        X`U5        U$ r'   )r   r{   r<   rk   )r5   ri   rA   rB   r?   r@   rh   s          r   parse_actionr      s;      6g>K 37;KK;DFF'*Mr   c           	         [         R                  " UR                  U 5      5      nS nUc  SnONUR                  5       (       d.  UR	                  S5      (       a#  USS  R                  5       (       a  [        U5      nUb  US:X  a  [        [        S5      U-  5        U(       d/  [        [        S5      R                  UR                  5       S95        [        U5      [        U5      :  aE  [        [        SS[        U5      5      R                  U[        U5      UR                  5       S	95        US:  a  US-  nXE   nU$ US
:X  a  US S  nU$ U" X S9nUR                  5       nU(       d  [        [        S5      SU< SU< 3-  5        U$ )Nr   rm   r   zInvalid --edit option '%s'z&No {cli_flag} objects found in the XMLra   zR'--edit {number}' requested but there's only {maxnum} {cli_flag} object in the XMLzU'--edit {number}' requested but there are only {maxnum} {cli_flag} objects in the XML)numbermaxnumrb   all)r+   z No matching objects found for %sro    )r   listifylookup_propisdigit
startswithintr   r   rd   re   rs   absngettextlookup_child_from_option_string)r+   r?   editvalrA   objlistidxinst	parserobjs           r   _find_objects_to_editr      s   ook55e<=G
C
//




S
!
!gabk&9&9&;&;'l
!8/07:;;<CC$224 D 6 7w<#c(" BCg,	( )/3w<$224 )/ )6	7 71HC| K 
E	qz K  5	88:56*G46 7 Kr   c           	         U R                   nU R                  nU R                  nUR                  (       a  [	        USXB5      nOEUnU(       a<  US:w  a6  US:w  a0  [        [        S5      R                  UUR                  5       S95        / n[        R                  " U5       H  nU[        R                  " XUUS9-  nM     U$ )NrG   1r   zM'--edit {option}' doesn't make sense with {cli_flag}, just use empty '--edit')optionrb   )editinst)rA   rB   r@   rc   r   r   r   rd   re   r   r   r   
run_parser)rh   r+   rA   rB   r@   r   devsr   s           r   action_editr      s    $$K$$KH!!$UF%-< SX-> 9 ::@&"'557 ;A ;9:
 DOOD)u;(02 	2 * Kr   c                     U R                   nU R                  n[        X5        U(       a  U H  nUR                  U5        M     UnU$ [        R
                  " XU5      nU H  nUR                  U5        M     U$ r'   )rA   rB   r.   
add_devicer   r   set_defaults)rh   r+   r,   
input_devsrA   rB   devr   s           r   action_add_devicer      sx    $$K$$K5%CS!  K	 ~~e+>CU#  Kr   c                     U R                   nU R                  S   n[        USX25      n[        R                  " U5      nU H  nUR                  U5        M     U$ )Nrn   rQ   )rA   rB   r   r   r   remove_devicerh   r+   rA   rB   r   r   s         r   action_remove_devicer     sY    $$K$$R(K "D??4 DC  Kr   c                     U R                   nU R                  n[        R                  " XU5      nU H  nUR	                  U5        M     U$ r'   )rA   rB   r   r   r   r   s         r   action_build_xmlr     sE    $$K$$K>>%k:D Kr   c                    U R                   (       a  [        XUR                  U5      $ U R                  (       a  [	        X5      $ U R
                  (       a  [        X5      $ [        R                  " SU R                  -  5      e)Nz7perform_action() incorrectly called with action_name=%s)
rN   r   r,   rR   r   rJ   r   r   DevErrorr?   )rh   r+   ri   r   s       r   perform_actionr   '  sj     0B0BJOO#F22~~6))


A	 r   c                     [        U SS 5      S:w  a  g [        R                  " SU 5        U R                  [        R
                  " 5       5        g )NDEVICE_TYPEdiskzDoing setup for disk=%s)rq   r
   r"   build_storager   	get_meter)r   s    r   setup_devicer   3  s9    sM4(F2II'-cmmo&r   c                 ,   U(       a'  [        [        S5      UR                  -  5      (       d  gUR                  (       a  U H  n[	        U5        M     U R                  UR                  5       5      n[        [        S5      UR                  -  5        U$ )Nz!Define '%s' with the changed XML?Fz!Domain '%s' defined successfully.)r   r   namerN   r   	defineXMLr4   r   )r5   inactive_xmlobjr   rh   confirmr   doms          r   define_changesr   ;  s}    569M9MMO OC  ..002
3C67/:N:NNOJr   c                    U(       a'  [        [        S5      UR                  -  5      (       d  gUR                  (       a  U H  n[	        U5        M      U R                  UR                  5       5      n[        [        S5      UR                  -  5        U$ ! [        R                   a.  n[        [        S5      UR                  US.-  5         S nAg S nAff = f)Nz Start '%s' with the changed XML?F!Domain '%s' started successfully..Failed starting domain '%(domain)s': %(error)sr6   error)r   r   r   rN   r   	createXMLr4   r   libvirtlibvirtErrorr   )r5   xmlobjr   rh   r   r   r   es           r   start_domain_transientr   J  s    45CE EC 	nnV^^-. 	Q:;fkkIJ
  Q?@!;;D  	 	s   B C+$CCc                 >   UR                   (       a"  [        S5      n[        S5      n[        S5      nOUR                  (       a"  [        S5      n[        S5      n[        S5      nOVUR                  (       a"  [        S5      n[        S5      n[        S	5      nO#[        R
                  " S
UR                  -  5      eU GH  nUR                  5       nU(       a(  UUU R                  5       S.-  n	[        U	5      (       d  MC  UR                   (       a  [        U5         UR                   (       a!  U R                  U[        R                  5        OcUR                  (       a!  U R                  U[        R                  5        O1UR                  (       a   U R                  U[        R                  5        [)        U5        U(       d  GM
  [)        S5        GM     g ! [        R                    a.  n
S["        R$                  ;  a  ['        USU
0-  5         S n
A
NcS n
A
ff = f)Nz7%(xml)s

Hotplug this device to the guest '%(domain)s'?zDevice hotplug successful.z*Error attempting device hotplug: %(error)sz;%(xml)s

Hotunplug this device from the guest '%(domain)s'?zDevice hotunplug successful.z,Error attempting device hotunplug: %(error)sz7%(xml)s

Update this device for the guest '%(domain)s'?zDevice update successful.z)Error attempting device update: %(error)sz2update_changes() incorrectly called with action=%s)xmlr6   $VIRTXML_TESTSUITE_UPDATE_IGNORE_FAILr    )rN   r   rR   rJ   r   r   r?   r4   r   r   r   attachDeviceFlagsr   VIR_DOMAIN_AFFECT_LIVEdetachDeviceFlagsupdateDeviceFlagsr   osenvironr   r   )r6   r   rh   r   msg_confirmmsg_successmsg_failr   r   r   r   s              r   update_changesr   `  s    ( )45AB		 	  ( )67CD	 ( )34@AD""#$ 	$ kkm ++-! C $C((		.##((g.L.LM((((g.L.LM((g.L.LM
 	[!77 * ## 	.5RZZGX!,-	.s$   1G1G1GH.$HHc                 z   U R                  5       nU R                  U R                  US9n[        X%X5      nUR                  5       n[	        XG5      nU(       d  [
        R                  " [        S5      5        UR                  (       a  U(       a  [        U5        Xe4$ UR                  (       a  [        U5        Xe4$ )z
Perform requested XML edits locally, but don't submit them to libvirt.
Optionally perform any XML printing per user request

:returns: (list of device objects, altered xmlobj)
)r5   r2   zENo XML diff was generated. The requested changes will have no effect.)r4   	__class__r5   r   r%   r
   warningr   
print_diffr   	print_xml)	orig_xmlobjri   rh   r   r#   r   r   r$   r!   s	            r   prepare_changesr     s     !!#G""(8(87"KF&'>D^^FG$DA + , 	-  < 
		V<r   c                     [         R                  " S[        S5      SS9n [         R                  " U S5        U R	                  SS[        S5      S	9  U R                  [        S
5      5      nUR	                  SSS[        S5      S9  UR	                  SS[        S5      S9  UR	                  SS[        S5      S9  UR	                  SS[        S5      S9  U R                  [        S5      5      nUR	                  SS[        S5      S9  UR                  5       nUR	                  SS[        S5      S9  UR	                  SSS[        S5      S 9  UR                  S S!9  UR	                  S"S[        S#5      S9  UR	                  S$S[        S%5      S9  UR	                  S&S[        S'5      S9  UR	                  S(S[        S)5      S9  [         R                  " U S*S+9  U R                  [        S,5      5      n[         R                  R                  5         UR	                  S-S[         R                  R                  [        S.5      S/9  [         R                  R                  5         UR	                  S0S[         R                  R                  [        S15      S/9  U R                  [        S25      5      n[         R                  " USS39  [         R                  " U5        [         R                   " U5        [         R"                  " U5        [         R$                  " U5        [         R&                  " USS39  [         R(                  " U5        [         R*                  " U5        [         R,                  " U5        [         R.                  " U5        U R                  [        S45      5      n[         R0                  " US*S*S*S59  [         R2                  " U 5        U R5                  5       $ )6Nz%(prog)s [options]z,Edit libvirt XML using command line options.T)introspection_epilogvirt-xmlr6   ?zDomain name, id, or uuid)nargshelpzXML actionsz--editrn   zEdit VM XML. Examples:
--edit --disk ...     (edit first disk device)
--edit 2 --disk ...   (edit second disk device)
--edit all --disk ... (edit all disk devices)
--edit target=hda --disk ... (edit disk 'hda')
)r   defaultr   z--remove-device
store_truezRemove specified device. Examples:
--remove-device --disk 1 (remove first disk)
--remove-device --disk all (remove all disks)
--remove-device --disk /some/path)rh   r   z--add-devicez6Add specified device. Example:
--add-device --disk ...z--build-xmlzWOutput built device XML. Domain is optional but recommended to ensure optimal defaults.zOutput options--updatezApply changes to the running VM.
With --add-device, this is a hotplug operation.
With --remove-device, this is a hotunplug operation.
With --edit, this is an update device operation.z--definezKForce defining the domain. Only required if a --print option was specified.z--no-definedefinestore_falsezForce not defining the domain.)destrh   r   )r   --startzStart the domain.z--print-diffz/Only print the requested change, in diff formatz--print-xmlz3Only print the requested change, in full XML formatz	--confirmz/Require confirmation before saving any results.F)virtinstallzConversion optionsz--convert-to-q35z-Convert an existing VM from PC/i440FX to Q35.)r   constr   z--convert-to-vnczXConvert an existing VM to use VNC graphics. This removes any remnants of Spice graphics.zXML options)editexamplezMiscellaneous Options)promptprintxmldryrun)r   setupParserr   add_connect_optionadd_argumentadd_argument_groupadd_mutually_exclusive_groupr   add_os_variant_optionParserConvertToQ35registerVirtCLIParserOPTSTR_EMPTYParserConvertToVNCadd_disk_optionadd_net_optionadd_gfx_optionadd_metadata_optionadd_memory_optionvcpu_cli_optionsadd_xml_optionadd_guest_xml_optionsadd_boot_optionsadd_device_optionsadd_misc_optionsautocomplete
parse_args)parseractgoutgdefine_gconvgmiscs          r   r  r    s   __	
89!#F
 6:.
)*  , $$Q}%56Dhc2 ; <  = 	' , -  .
 	n\ " #  $ 	mL 9 :  ; $$Q'7%89Dj B C  D
 002H*\  "9  :  ; -h} !AB  D&i01  3n\@A  CmLDE  Gk,@A  C f%8$$Q';%<=D##%(,,>?  A ##%(,, > ?  @
 	!!!M"23At,qqA!-qa 1$$Q'>%?@DeeEJVr   c                 "
   [         R                  " 5         [        5       nUR                  (       d3  UR                  (       d"  UR
                  (       d  UR                  (       a  SUl        [         R                  " SUR                  UR                  5        [         R                  " U5      (       a  g[         R                  " U5      (       a  gS Ul        UR                  (       d  UR                  (       d  [        R                  R                   (       d  [        R                  R#                  5       (       dn  UR                  (       a  [%        ['        S5      5        UR(                  (       a  [%        ['        S5      5        [        R                  R+                  5       Ul        O[%        ['        S5      5        UR                  (       dO  UR
                  (       d>  UR                  (       a  UR,                  (       d  SUl        OUR,                  c  SUl        UR                  (       a  UR                  (       d  SUl        [         R.                  " UR0                  U 5      n [3        X5      nS nS nS nUR                  (       a$  [         R4                  " XR                  5      u  p5nO[7        XR                  S9n[9        U5      nUR:                  (       aB  [=        X%5      nU H0  n[?        [@        RB                  " URE                  5       5      5        M2     gS n	Sn
UR(                  (       a  UR(                  (       a  URF                  (       a  [I        S	S
5        U(       a'  [K        XAU5      u  p[M        X9X!R                  5        Sn
O[N        RP                  " ['        S5      5        UR,                  (       d  gURE                  5       n[K        XQX)S9u  pUR,                  (       d)  URF                  (       a  [S        XUX!R                  5        g[U        XXUR                  5      nU(       d  gURF                  (       a3   URW                  5         [?        ['        S5      UR\                  -  5        gU(       a  U
(       d  [?        ['        S5      5        g[_        XU5      (       a  [N        RP                  " ['        S5      5        g! [X        RZ                   a.  n[%        ['        S5      UR\                  US.-  5         S nANS nAff = f)NFr   r   z%Can't use --confirm with stdin input.z$Can't use --update with stdin input.zA domain must be specifiedTr1   r   r   z0The VM is not running, --update is inapplicable.)r   r   r   r   z?Changes will take effect after the domain is fully powered off.zhXML did not change after domain define. You may have changed a value that libvirt is setting by default.)0r   earlyLoggingr  r   r   r   	build_xmlquietsetupLoggingr"   check_option_introspectioncheck_osinfo_liststdinxmlr6   r   r   closedisattyr   r   rg   readr   getConnectionconnectr   get_domain_and_guestr	   boolrV   r   r   r   unindent_device_xmlr4   startr   r   r   r
   r   r   r   creater   r   r   r:   )r5   ri   rh   r6   active_xmlobjr   vm_is_running
built_devsr   r   performed_updatedummyr7   r   xmlobj_to_definer   r   s                    r   mainr"    s   lG7,,g//Z>
%%g..
W%%G>>'"3"3yy		(8(8(:(:Q>?@~~Q=>?"yy~~/G/01 W%7%7>>$(!~~%!%w00!W__d3D$(FFMO~~141I1I..2".  /?/?@'M%f>
C44S[[]CD  J~~>>gmmZ3 /!F!4J6vG#KKDEG~~"**,L,fED>>=="44#)??<
w8C}}	JJL 	Q:;$))* 	+  
/OP	R 	 
"$\	:	:A G H 	I  ## 	CD.33H   	s   S T $T		Tc                  R    [         R                  " [        5       5        g ! [         a*  n [         R                  " U R                  5         S n A g S n A f[
         a,    [        R                  " SSS9  [        [        S5      5         g [         a  n[        U5         S nAg S nAff = f)Nr   T)exc_infozAborted at user request)r   exitr"  
SystemExitcodeKeyboardInterruptr
   r"   r   r   	Exceptionr   )sys_emain_es     r   runclir,  |  sn      3		"t$Q012 Vs&   ! 
B& A6B&	B&B!!B&r'   )%r   r   r   r   r   r   r   r   r   r+   r	   loggerr
   r   r   r%   r.   r:   r<   rk   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r"  r,  r_   r   r   <module>r.     s    
 
   C C   ;-#/ /B;6!($$*+\.$
	',0f>Stm`	r   