
    Ce                     ,   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZmZmZmZmZmZmZmZmZmZ e
j        dk    r	ddlmZmZ nd Zd Z G d	 d
          ZdZdZ	 e  n# e!$ r e"Z Y nw xY w G d de#          Z$ G d de$          Z% G d de$          Z& G d de$          Z' G d de$          Z( G d de$          Z) G d de$          Z* G d de#          Z+ G d de,          Z- G d d e-          Z. G d! d"e-          Z/ G d# d$e-          Z0 G d% d&e-          Z1 G d' d(e-          Z2 G d) d*e2          Z3 G d+ d,e-          Z4 G d- d.e-          Z5 G d/ d0e-          Z6 G d1 d2e-          Z7 G d3 d4e-          Z8 G d5 d6e-          Z9 G d7 d8e-          Z: G d9 d:e-          Z; G d; d<e-          Z< G d= d>e-          Z= G d? d@e-          Z> G dA dBe,          Z?dC Z@dD ZAdEeee"ee"ef         f                  dFee?         fdGZBdHe"dIe"dFee"ee"ee?         f         f         fdJZCeeDeEeFe"ee"e"f         f         ZG	 dmdLe"dMeDdNeDdFeeG         fdOZH	 dmdPee"         dQee?         dNeDdFeEfdRZIeeDeEeFe"ee"e"f         ee"         f         ZJee"eJf         ZKdSe?dPeeJ         dTeKfdUZL	 	 dndPee"         dQee?         dVeEdNeeD         dFeKf
dWZM	 dmdXee"ee"ef         f         dPee"         dYeeD         dFeKfdZZNd[ee"         dFee"ee"         f         fd\ZO G d] d^ejP                  ZQd_eeegeeEeRe"f         f         dPed`edFeeEeRe"f         fdaZSdPed`edFeeEeRe"f         fdbZT	 	 	 	 	 dodeee"ee"         f         dfee"         dgeeR         dheeE         dYeeD         dFeeEeRe"f         fdiZU	 	 	 	 	 	 dpdeee"ee"         f         djee"         dkeeK         dgeeR         dheeE         dYeeD         dFeeEeRe"f         fdlZVdS )qa`  
Types and routines used by the ceph CLI as well as the RESTful
interface.  These have to do with querying the daemons for
command-description information, validating user command input against
those descriptions, and submitting the command to the appropriate
daemon.

Copyright (C) 2013 Inktank Storage, Inc.

LGPL-2.1 or LGPL-3.0.  See file COPYING.
    N)abc)
castAnyCallableDictGenericListOptionalSequenceTupleUnion)      )get_args
get_originc                 :    | t           u r| S t          | dd          S )N__args__ )r   getattrtps    //usr/lib/python3/dist-packages/ceph_argparse.pyr   r      s"    ==I2z2...    c                 $    t          | dd           S )N
__origin__)r   r   s    r   r   r   %   s    r<...r   c                   &    e Zd ZdZdZdZdZdZdZdS )Flag         r          N)	__name__
__module____qualname__	NOFORWARDOBSOLETE
DEPRECATEDMGRPOLLHIDDENr   r   r   r   r   *   s,        IHJCDFFFr   r   z--([^=]+)=(.+)z	--([^=]+)c                       e Zd ZdZdS )ArgumentErrorz(
    Something wrong with arguments
    Nr#   r$   r%   __doc__r   r   r   r-   r-   <             	Dr   r-   c                       e Zd ZdZdS )ArgumentNumberz-
    Wrong number of a repeated argument
    Nr.   r   r   r   r2   r2   C   r0   r   r2   c                       e Zd ZdZdS )ArgumentFormatz)
    Argument value has wrong format
    Nr.   r   r   r   r4   r4   J   r0   r   r4   c                       e Zd ZdZdS )ArgumentMissingz-
    Argument value missing in a command
    Nr.   r   r   r   r6   r6   Q   r0   r   r6   c                       e Zd ZdZdS )ArgumentValidzS
    Argument value is otherwise invalid (doesn't match choices, for instance)
    Nr.   r   r   r   r8   r8   X   r0   r   r8   c                       e Zd ZdZdS )ArgumentTooFewz~
    Fewer arguments than descriptors in signature; may mean to continue
    the search, so gets a special exception type
    Nr.   r   r   r   r:   r:   _   s           r   r:   c                       e Zd ZdZdS )ArgumentPrefixzM
    Special for mismatched prefix; less severe, don't report by default
    Nr.   r   r   r   r<   r<   f   r0   r   r<   c                       e Zd ZdZdS )
JsonFormatz8
    some syntactic or semantic issue with the JSON
    Nr.   r   r   r   r>   r>   m   r0   r   r>   c                       e Zd ZdZd ZddZd Zd Zd Zd Z	e
d	             Ze
dd            Zd Ze
d             Ze
d             ZdS )CephArgtypea  
    Base class for all Ceph argument types

    Instantiating an object sets any validation parameters
    (allowable strings, numeric ranges, etc.).  The 'valid'
    method validates a string against that initialized instance,
    throwing ArgumentError if there's a problem.
    c                     dS )z~
        set any per-instance validation parameters here
        from kwargs (fixed string sets, integer ranges, etc)
        Nr   )selfkwargss     r   __init__zCephArgtype.__init__}   s	    
 	r   Fc                     || _         dS )as  
        Run validation against given string s (generally one word);
        partial means to accept partial string matches (begins-with).
        If cool, set self.val to the value that should be returned
        (a copy of the input string, or a numeric or boolean interpretation
        thereof, for example)
        if not, throw ArgumentError(msg-as-to-why)
        N)valrB   spartials      r   validzCephArgtype.valid   s     r   c                     | j         j        S )a"  
        return string representation of description of type.  Note,
        this is not a representation of the actual value.  Subclasses
        probably also override __str__() to give a more user-friendly
        'name/type' description for use in command format help messages.
        )	__class__r#   rB   s    r   __repr__zCephArgtype.__repr__   s     ~&&r   c                 @    d                     | j        j                  S )a  
        where __repr__ (ideally) returns a string that could be used to
        reproduce the object, __str__ returns one you'd like to see in
        print messages.  Use __str__ to format the argtype descriptor
        as it would be useful in a command usage message.
        <{0}>)formatrL   r#   rM   s    r   __str__zCephArgtype.__str__   s     ~~dn5666r   c                     |S Nr   rB   vs     r   __call__zCephArgtype.__call__   s    r   c                     g S rT   r   rB   rH   s     r   completezCephArgtype.complete   s    	r   c                    t          |           }t          |           }|t          j        t          t          t
          fv r=t          |          dk    sJ d|d<   t                              |d         ||          S |t          u rt          |          dk    sJ |d         }|
                    |          t          |          k    sJ d|  d            t          t          |                    |d<   t                              |||          S t          |           t          u rNt          |          dk    rt          d |d                   sJ t                              |d         |d	|          S t          d
|  d| d          )Nr   Nnr   )
positionalzall elements in z should be identicalr   Tunknown type '': '')r   r   r   r   r	   listlenr@   
to_argdescr   countstrr   
isinstance
ValueError)r   attrsr^   	orig_type	type_argsinner_tps         r   _compound_type_to_argdescz%CephArgtype._compound_type_to_argdesc   s    rNN	RLL	xt<<<y>>Q&&&&E#J)))A,*)UUU%y>>Q&&&& |H??8,,I>>>;2;;; ?>>S^^,,E#J))(Ej)QQQ^^u$$y>>Q&&:dIaL+I+I&&&)))A,tZPPP>b>>e>>>???r   Tc                 "   |rd|d<   |sd|d<   t           t          t          t          t          t
          t          t          i}	  ||                                          |          S # t          $ r t          | t                    r|                     |          cY S t          | t                    r?t          | t          j                  r%t!          |                               |          cY S t                              | ||          cY S w xY w)Nfalsereqr^   r   )rf   
CephStringintCephIntfloat	CephFloatboolCephBoolargdescKeyErrorrg   r@   type
issubclassenumEnumCephChoicesrm   )r   ri   has_defaultr^   CEPH_ARG_TYPESs        r   rd   zCephArgtype.to_argdesc   s    	#"E%L 	*")E,9(	
	T%>"%''//666 	T 	T 	T"k** Tzz%(((((B%% T*R*C*C T"b)))11%88888"<<R
SSSSS	Ts   "A% %4DAD/DDc                     t          |           j        |d<   d                    d |                                D                       S )Nrz   ,c              3   *   K   | ]\  }}| d | V  dS )=Nr   ).0krV   s      r   	<genexpr>z&CephArgtype.argdesc.<locals>.<genexpr>   s0      ==tq!1

q

======r   )rz   r#   joinitems)rB   ri   s     r   rx   zCephArgtype.argdesc   s=    T

+fxx==u{{}}======r   c                    t          |           }t          |           |t          j        t          t          t
          fv r|d S fd|D             S |t          u rt          fd|D                       S t          |           t          u rLt                    dk    rt          d d                   sJ t                              d         |          S t          d|  d| d          )	Nc                 R    g | ]#}t                               d          |          $S )r   r@   cast_tor   erk   s     r   
<listcomp>z6CephArgtype._cast_to_compound_type.<locals>.<listcomp>   s-    DDDQK''	!a88DDDr   c              3   Z   K   | ]%}t                               d          |          V  &dS )r   Nr   r   s     r   r   z5CephArgtype._cast_to_compound_type.<locals>.<genexpr>   s7      II!,,Yq\1==IIIIIIr   r   r   r   r_   r`   ra   )r   r   r   r   r	   rb   r   tupler   rc   rg   r@   r   rh   )r   rV   rj   rk   s      @r   _cast_to_compound_typez"CephArgtype._cast_to_compound_type   s    rNN	RLL	xt<<<ytDDDD!DDDD%IIIIqIIIIII^^u$$y>>Q&&:dIaL+I+I&&&&&y|Q777:b::a:::;;;r   c                     t           t          t          t          f}| |v r | |          S t	          | t
                    r%t          | t          j                  r | |          S t          
                    | |          S rT   )rf   rr   rt   rv   rg   rz   r{   r|   r}   r@   r   )r   rV   PYTHON_TYPESs      r   r   zCephArgtype.cast_to   sy     	
 2a55LD!! 	=jTY&?&? 	=2a55L55b!<<<r   NF)FT)r#   r$   r%   r/   rD   rJ   rN   rR   rW   rZ   staticmethodrm   rd   rx   r   r   r   r   r   r@   r@   t   s          	 	 	 	' ' '7 7 7     @ @ \@, T T T \T*> > > < < \<  = = \= = =r   r@   c                   8     e Zd ZdZddZd	dZd Z fdZ xZS )
rs   zo
    range-limited integers, [+|-][0-9]+ or 0x[0-9a-f]+
    range: list of 1 or 2 ints, [min] or [min,max]
     c                     |dk    rt                      | _        d S d |                    d          D             | _        d S )Nr   c                 ,    g | ]}t          |          S r   )rr   r   xs     r   r   z$CephInt.__init__.<locals>.<listcomp>  s    ;;;Q#a&&;;;r   |rb   rangesplitrB   r   s     r   rD   zCephInt.__init__   s?    B;;DJJJ;;%++c*:*:;;;DJJJr   Fc                    	 t          |d          }n0# t          $ r# t          d                    |                    w xY wt	          | j                  dk    r<|| j        d         k     s|| j        d         k    rt          | d| j                   nBt	          | j                  dk    r*|| j        d         k     rt          | d| j                   || _        d S )Nr   z{0} doesn't represent an intr   r    not in range )rr   rh   r8   rQ   rc   r   rF   rB   rH   rI   rF   s       r   rJ   zCephInt.valid  s    	Ja))CC 	J 	J 	J > E Ea H HIII	Jtz??aTZ]""cDJqM&9&9#s$F$F$*$F$FGGG ':__!!TZ]""#s$F$F$*$F$FGGGs	    -A c                 (   d}t          | j                  dk    r d                    | j        d                   }t          | j                  dk    r,d                    | j        d         | j        d                   }d                    |          S )Nr   r   [{0}-]r   r   	[{0}-{1}]z<int{0}>rc   r   rQ   rB   rs     r   rR   zCephInt.__str__  sy    tz??a
1..Atz??a""4:a=$*Q-@@A  ###r   c                     | j         r'd                    d | j         D                       |d<   t                                          |          S )Nr   c              3   4   K   | ]}t          |          V  d S rT   rf   r   rV   s     r   r   z"CephInt.argdesc.<locals>.<genexpr>  (      %A%Ac!ff%A%A%A%A%A%Ar   r   r   r   superrx   rB   ri   rL   s     r   rx   zCephInt.argdesc  J    : 	B XX%A%Adj%A%A%AAAE'Nwwu%%%r   r   r   	r#   r$   r%   r/   rD   rJ   rR   rx   __classcell__rL   s   @r   rs   rs      sy         < < < <   $ $ $& & & & & & & & &r   rs   c                   8     e Zd ZdZddZd	dZd Z fdZ xZS )
ru   zX
    range-limited float type
    range: list of 1 or 2 floats, [min] or [min, max]
    r   c                     |dk    rt                      | _        d S d |                    d          D             | _        d S )Nr   c                 ,    g | ]}t          |          S r   )rt   r   s     r   r   z&CephFloat.__init__.<locals>.<listcomp>)  s    ===q%((===r   r   r   r   s     r   rD   zCephFloat.__init__%  s?    B;;DJJJ==EKK,<,<===DJJJr   Fc                    	 t          |          }n0# t          $ r# t          d                    |                    w xY wt	          | j                  dk    r<|| j        d         k     s|| j        d         k    rt          | d| j                   nBt	          | j                  dk    r*|| j        d         k     rt          | d| j                   || _        d S )Nz{0} doesn't represent a floatr   r   r   r   )rt   rh   r8   rQ   rc   r   rF   r   s       r   rJ   zCephFloat.valid+  s    	K((CC 	K 	K 	K ? F Fq I IJJJ	Ktz??aTZ]""cDJqM&9&9#s$F$F$*$F$FGGG ':__!!TZ]""#s$F$F$*$F$FGGGs    -?c                 (   d}t          | j                  dk    r d                    | j        d                   }t          | j                  dk    r,d                    | j        d         | j        d                   }d                    |          S )Nr   r   r   r   r   r   z
<float{0}>r   r   s     r   rR   zCephFloat.__str__8  sy    tz??a
1..Atz??a""4:a=$*Q-@@A""1%%%r   c                     | j         r'd                    d | j         D                       |d<   t                                          |          S )Nr   c              3   4   K   | ]}t          |          V  d S rT   r   r   s     r   r   z$CephFloat.argdesc.<locals>.<genexpr>B  r   r   r   r   r   s     r   rx   zCephFloat.argdesc@  r   r   r   r   r   r   s   @r   ru   ru      sy         > > > >   & & && & & & & & & & &r   ru   c                   d     e Zd ZdZddZddededdfd	Zdefd
Zde	e         fdZ
 fdZ xZS )rq   zN
    String; pretty generic.  goodchars is a RE char class of valid chars
    r   c                     ddl m} 	 t          j                   n5# t          j        $ r# t          d                                        w xY w| _        t          fd|D                       | _	        d S )Nr   )	printablez%CephString(): "{0}" is not a valid REc                 >    g | ]}t          j        |          |S r   )rematch)r   c	goodcharss     r   r   z'CephString.__init__.<locals>.<listcomp>S  s*    <<<1RXi%;%;<Q<<<r   )
stringr   r   compileerrorrh   rQ   r   	frozensetgoodset)rB   r   r   s    ` r   rD   zCephString.__init__J  s    $$$$$$	0Jy!!!!x 	0 	0 	0D#VI..0 0 0	0 # <<<<	<<<
 
s	    2AFrH   rI   returnNc                     t          |          }| j        rI|| j        k    s>t          d                    d                    || j        z
            |                    || _        d S )Nzinvalid chars {0} in {1}r   )setr   r4   rQ   r   rF   )rB   rH   rI   ssets       r   rJ   zCephString.validV  si    1vv< 	J 4 4 !;!'t|0C(D(Da!H!HJ J Jr   c                 x    d}| j         r|d                    | j                   z  }d                    |          S )Nr   z(goodchars {0})z<string{0}>)r   rQ   )rB   bs     r   rR   zCephString.__str__]  s@    > 	:"))$.999A##A&&&r   c                     |dk    rg S |gS )Nr   r   rY   s     r   rZ   zCephString.completec  s    77I3Jr   c                 h    | j         r
| j         |d<   t                                          |          S )Nr   )r   r   rx   r   s     r   rx   zCephString.argdesci  s/    > 	0!%E+wwu%%%r   r   r   )r#   r$   r%   r/   rD   rf   rv   rJ   rR   r	   rZ   rx   r   r   s   @r   rq   rq   F  s         

 

 

 

 s T d    ' ' ' ' 'T#Y    & & & & & & & & &r   rq   c                   4    e Zd ZdZd	dededdfdZdefdZdS )
CephSocketpathzB
    Admin socket path; check that it's readable and S_ISSOCK
    FrH   rI   r   Nc                     t          j        |          j        }t          j        |          s"t	          d                    |                    || _        d S )Nzsocket path {0} is not a socket)osstatst_modeS_ISSOCKr8   rQ   rF   )rB   rH   rI   modes       r   rJ   zCephSocketpath.valids  sM    wqzz!}T"" 	M A H H K KLLLr   c                     dS )Nz<admin-socket-path>r   rM   s    r   rR   zCephSocketpath.__str__y  s    $$r   r   r#   r$   r%   r/   rf   rv   rJ   rR   r   r   r   r   r   o  sg          s T d    % % % % % % %r   r   c                        e Zd ZdZddZd ZdS )
CephIPAddrz2
    IP address (v4 or v6) with optional port
    Fc                 \   d}d }|                     d          r|dd          }d}nw|                     d          r|dd          }d}nU|                     d          r|dd          }d}n3|                     d          rd}n|                    d          d	k    rd}|dk    r|                    d
          }|d	k    rM|d |         }||dz   d          }t          |          dk    r"t          d                    |                    n|}d }	 t          j        t
          j        |           n.# t          $ r# t          d                    |                    w xY w|                     d          r|                    d          }|d	k    r"t          d                    |                    ||dz            d
k    r=	 ||dz            }n0# t          $ r# t          d                    |                    w xY w|d|         }n|}d }	 t          j        t
          j        |           n0# t          $ r# t          d                    |                    w xY w|5t          |          dk    r"t          d                    |                    || _        || _        || _        d S )N   zv1:r   r    zv2:zany:[.:r   i  z{0}: invalid IPv4 portz{0}: invalid IPv4 address]z{0} missing terminating ]r   z{0}: bad port numberz{0} not valid IPv6 addressz{0} not a valid port number)
startswithfindrr   r8   rQ   socket	inet_ptonAF_INETOSErrorr4   rh   AF_INET6rF   addrport)rB   rH   rI   rz   pr   aends           r   rJ   zCephIPAddr.valid  s   << 	!""ADD\\%   		!""ADD\\&!! 	!""ADD\\# 	DDVVC[[BD19966#;;DrzzeteHdQhiiLq66E>>'(@(G(G(J(JKKK " K 3333 K K K#$?$F$Fq$I$IJJJK ||C   ffSkk"99()D)K)KA)N)NOOOS1W:$$NcAgJ% N N N+,B,I,I!,L,LMMMNaeHL !4444 L L L#$@$G$G$J$JKKKL=SVVe^^ = D DQ G GHHH				s$   D2 2-EG -G=H0 0-Ic                     dS )Nz<IPaddr[:port]>r   rM   s    r   rR   zCephIPAddr.__str__  s      r   Nr   r#   r$   r%   r/   rJ   rR   r   r   r   r   r   }  sB         7 7 7 7r! ! ! ! !r   r   c                   >     e Zd ZdZd	dededdf fdZdefdZ xZS )
CephEntityAddrz4
    EntityAddress, that is, IP address[/nonce]
    FrH   rI   r   Nc                    d }d|v r=|                     d          \  }}|                    d          r|d d         }|dz  }n|}t          | j        |                               |           |rNd }	 t          |          }n# t          $ r Y nw xY w||dk     r#t          d                    ||                    || _	        d S )N/r   r   r   z.{0}: invalid entity, nonce {1} not integer > 0)
r   endswithr   rL   rJ   rr   rh   r8   rQ   rF   )rB   rH   rI   nonceip	nonce_intrL   s         r   rJ   zCephEntityAddr.valid  s    !88IB~~c"" crc
c	Bdnd##))"--- 
	IJJ		    IMM#DF1e$$   s   4B 
BBc                     dS )Nz<EntityAddr>r   rM   s    r   rR   zCephEntityAddr.__str__      ~r   r   )	r#   r$   r%   r/   rf   rv   rJ   rR   r   r   s   @r   r   r     s|          s T d      .        r   r   c                       e Zd ZdZdefdZdS )CephPoolnamez(
    Pool name; very little utility
    r   c                     dS )Nz
<poolname>r   rM   s    r   rR   zCephPoolname.__str__  s    |r   Nr#   r$   r%   r/   rf   rR   r   r   r   r  r    s9               r   r  c                       e Zd ZdZdefdZdS )CephObjectnamez
    Object name.  Maybe should be combined with Pool name as they're always
    present in pairs, and then could be checked for presence
    r   c                     dS )Nz<objectname>r   rM   s    r   rR   zCephObjectname.__str__  r   r   Nr  r   r   r   r  r    s9               r   r  c                        e Zd ZdZddZd ZdS )CephPgidz@
    pgid, in form N.xxx (N = pool number, xxx = hex pgnum)
    Fc                    |                     d          dk    rt          d          |                    dd          \  }}	 t          |          }n0# t          $ r# t          d                    |                    w xY w|dk     r"t          d                    |                    	 t          |d          }n0# t          $ r# t          d	                    |                    w xY w|| _        d S )
Nr   r   zpgid has no .r   zpool {0} not integerr   zpool {0} < 0r!   zpgnum {0} not hex integer)r   r4   r   rr   rh   rQ   rF   )rB   rH   rI   poolid_spgnum_spoolidpgnums          r   rJ   zCephPgid.valid  s   66#;;" 111GGCOO'	H]]FF 	H 	H 	H !7!>!>v!F!FGGG	HA:: !6!6v!>!>???	L$$EE 	L 	L 	L !<!C!CE!J!JKKK	Ls   A -B ,B= =-C*c                     dS )Nz<pgid>r   rM   s    r   rR   zCephPgid.__str__      xr   Nr   r   r   r   r   r  r    sA                 r   r  c                   (    e Zd ZdZddZd	dZd ZdS )
CephNamez
    Name (type.id) where:
    type is osd|mon|client|mds
    id is a base10 int, if type == osd, or a string otherwise

    Also accept '*'
    r   Nc                 "    d | _         d | _        d S rT   nametypenameidrM   s    r   rD   zCephName.__init__      '+%)r   Fc                    |dk    r	|| _         d S |dk    rd| _        || _         d S |dk    rd| _        || _         d S |                    d          dk    r"t          d                    |                    |                    dd          \  }}|dvrt          d	|z             |d
k    r8|dk    r2	 t          |           n!# t          $ r t          d| d          w xY w|| _        || _         || _	        d S )N*mgrmonr   r   zCephName: no . in {0}r   )osdr  clientmdsr  unknown type r  osd id  not integer)
rF   r  r   r4   rQ   r   r8   rr   rh   r  )rB   rH   rI   tis        r   rJ   zCephName.valid  s,   88DHF%ZZ!DMDHF%ZZ!DMDHF66#;;" !8!?!?!B!BCCC773??DAq>>>#Oa$7888Ezz88HA% H H H,-Fq-F-F-FGGGHDMs   3C C!c                     dS )Nz<name (type.id)>r   rM   s    r   rR   zCephName.__str__.  s    !!r   r   Nr   r#   r$   r%   r/   rD   rJ   rR   r   r   r   r  r    sU         * * * *   8" " " " "r   r  c                   &    e Zd ZdZd ZddZd ZdS )CephOsdNamezv
    Like CephName, but specific to osds: allow <id> alone

    osd.<id>, or <id>, or *, where id is a base10 int
    c                 "    d | _         d | _        d S rT   r  rM   s    r   rD   zCephOsdName.__init__8  r  r   Fc                    |dk    r	|| _         d S |                    d          dk    r2|                    dd          \  }}|dk    rt          d|z             nd}|}	 t	          |          }n!# t
          $ r t          d| d          w xY w|d	k     rt          d| d
          || _        || _        || _         d S )Nr  r   r   r   r  r  r  r   r   z is less than 0)	rF   r   r   r8   rr   rh   r4   r  r  )rB   rH   rI   r!  r"  rV   s         r   rJ   zCephOsdName.valid<  s    88DHF66#;;"773??DAqEzz#Oa$7888  AA	<AAA 	< 	< 	< !:1!:!:!:;;;	<q55 !=1!=!=!=>>>s    A0 0Bc                     dS )Nz<osdname (id|osd.id)>r   rM   s    r   rR   zCephOsdName.__str__Q  s    &&r   Nr   r%  r   r   r   r'  r'  2  sP         
* * *   *' ' ' ' 'r   r'  c                   D     e Zd ZdZddZddZd Zd Zd	 Z fd
Z	 xZ
S )r~   z9
    Set of string literals; init with valid choices
    r   Nc                     |                     d          | _        || _        | j        %t          d | j        D                       | _        d S d S )Nr   c              3   $   K   | ]}|j         V  d S rT   )value)r   r   s     r   r   z'CephChoices.__init__.<locals>.<genexpr>]  s$      ;;A;;;;;;r   )r   stringsr|   rb   )rB   r/  r   rC   s       r   rD   zCephChoices.__init__Y  sP    }}S))	9 ;;;;;;;DLLL ! r   Fc                    |s5|| j         vr#t          d                    ||                     || _        d S | j         D ]!}|                    |          r
|| _         d S "t          d                    ||                     )Nz{0} not in {1})r/  r8   rQ   rF   r   )rB   rH   rI   r!  s       r   rJ   zCephChoices.valid_  s     	$$#$4$;$;At$D$DEEEDHF  	 	A||A  ,55a>>???r   c                     t          | j                  dk    r d                    | j        d                   S d                    d                    | j                            S )Nr   z{0}r   r   )rc   r/  rQ   r   rM   s    r   rR   zCephChoices.__str__n  sO    t|!!<<Q000<< 6 6777r   c                 .    | j         |S | j         |         S rT   )r|   rU   s     r   rW   zCephChoices.__call__t  s    9H9Q<r   c                 .    fd| j         D             }|S )Nc                 >    g | ]}|                               |S r   )r   )r   tokenrH   s     r   r   z(CephChoices.complete.<locals>.<listcomp>{  s,    LLLu8H8H8K8KLULLLr   )r/  )rB   rH   	all_elemss    ` r   rZ   zCephChoices.completez  s$    LLLLLLL	r   c                     d                     | j                  |d<   t                                          |          S )Nr   r/  )r   r/  r   rx   r   s     r   rx   zCephChoices.argdesc~  s1    88DL11iwwu%%%r   )r   Nr   )r#   r$   r%   r/   rD   rJ   rR   rW   rZ   rx   r   r   s   @r   r~   r~   U  s         < < < <@ @ @ @8 8 8       & & & & & & & & &r   r~   c                   (    e Zd ZdZddZd	dZd ZdS )
rw   z
    A boolean argument, values may be case insensitive 'true', 'false', '0',
    '1'.  In keyword form, value may be left off (implies true).
    r   c                 :    |                     d          | _        d S )Nr   )r   r/  )rB   r/  rC   s      r   rD   zCephBool.__init__  s    }}S))r   Fc                     |                                 }|dv r	d| _        d S |dv r	d| _        d S t          d                    |                    )N)true1T)ro   0Fz{0} not one of 'true', 'false')lowerrF   r8   rQ   )rB   rH   rI   
lower_cases       r   rJ   zCephBool.valid  sX    WWYY
&&DHHH>))DHHH @ G G J JKKKr   c                     dS )Nz<bool>r   rM   s    r   rR   zCephBool.__str__  r  r   Nr   r   r%  r   r   r   rw   rw     sY         * * * *L L L L    r   rw   c                   &    e Zd ZdZddZd Zd ZdS )CephFilepathz
    Openable file
    Fc                     t           j                            |          }t          j        |t           j                  s|                     |           || _        d S rT   )r   pathabspathaccessR_OK_validate_writable_filerF   rG   s      r   rJ   zCephFilepath.valid  sJ    GOOAyBG$$ 	,((+++r   c                    t           j                            |          rt           j                            |          rCt          j        |t           j                  s"t          d                    |                    d S t          d                    |                    t           j                            |          }t          j        |t           j                  s"t          d                    |                    d S )Nz{0} is not writablez{0} is not filezcannot create file in {0})	r   rD  existsisfilerF  W_OKr8   rQ   dirname)rB   fnamerM  s      r   rH  z$CephFilepath._validate_writable_file  s    7>>%   		Qw~~e$$ Ey00 M'(=(D(DU(K(KLLLM M $$5$<$<U$C$CDDDgooe,,G9Wbg.. Q#$?$F$Fw$O$OPPPQ Qr   c                     dS )Nz<outfilename>r   rM   s    r   rR   zCephFilepath.__str__  s    r   Nr   )r#   r$   r%   r/   rJ   rH  rR   r   r   r   rB  rB    sS            
Q 
Q 
Q    r   rB  c                        e Zd ZdZddZd ZdS )CephFragmentz
    'Fragment' ??? XXX
    Fc                 ,   |                     d          dk    r"t          d                    |                    |                    d          \  }}|                    d          s"t          d                    |                    	 t          |           n0# t          $ r# t          d                    |                    w xY w	 t          |           n0# t          $ r# t          d                    |                    w xY w|| _        d S )Nr   r   z	{0}: no /0xz{0} not a hex integerzcan't convert {0} to integer)r   r4   rQ   r   r   rr   rh   rF   )rB   rH   rI   rF   bitss        r   rJ   zCephFragment.valid  s!   66#;;" !3!3A!6!6777GGCLL	T~~d## 	F !8!?!?!D!DEEE	NHHHH 	N 	N 	N !@!G!G!L!LMMM	N	OIIII 	O 	O 	O !@!G!G!M!MNNN	Os   B -C	C -D
c                     dS )Nz <CephFS fragment ID (0xvvv/bbb)>r   rM   s    r   rR   zCephFragment.__str__  s    11r   Nr   r   r   r   r   rQ  rQ    sA            "2 2 2 2 2r   rQ  c                   4    e Zd ZdZd	dededdfdZdefdZdS )
CephUUIDz+
    CephUUID: pretty self-explanatory
    FrH   rI   r   Nc                     	 t          j        |           n5# t          $ r(}t          d                    ||                    d }~ww xY w|| _        d S )Nzinvalid UUID {0}: {1})uuidUUID	Exceptionr4   rQ   rF   )rB   rH   rI   r   s       r   rJ   zCephUUID.valid  sd    	GIaLLLL 	G 	G 	G !8!?!?1!E!EFFF	Gs    
A	#AA	c                     dS )Nz<uuid>r   rM   s    r   rR   zCephUUID.__str__  r  r   r   r   r   r   r   rW  rW    sg          s T d          r   rW  c                   ^    e Zd ZdZddeddfdZdded	eddfd
ZdefdZde	e         fdZ
dS )
CephPrefixzD
    CephPrefix: magic type for "all the first n fixed strings"
    r   prefixr   Nc                     || _         d S rT   r_  )rB   r_  s     r   rD   zCephPrefix.__init__  s    r   FrH   rI   c                    	 t          |          }t          |t                    r|                    d          }n[# t          $ r# t          d                    |                    t          $ r# t          d                    |                    w xY w|r$| j        	                    |          r	|| _
        d S n|| j        k    r	|| _
        d S t          d                    |                    )Nasciizno match for {0})rf   rg   bytesdecodeUnicodeEncodeErrorr<   rQ   UnicodeDecodeErrorr_  r   rF   rG   s      r   rJ   zCephPrefix.valid  s   		?AA!U## & HHW%%! 	@ 	@ 	@ !4!;!;A!>!>???! 	? 	? 	? !3!:!:1!=!=>>>	?  	{%%a((  DK/66q99:::s
   9< ABc                     | j         S rT   ra  rM   s    r   rR   zCephPrefix.__str__  s
    {r   c                 p    | j                             |          r| j                             d          gS g S )N )r_  r   rstriprY   s     r   rZ   zCephPrefix.complete  s8    ;!!!$$ 	K&&s++,,Ir   r   r   )r#   r$   r%   r/   rf   rD   rv   rJ   rR   r	   rZ   r   r   r   r^  r^    s          s D    ; ;s ;T ;d ; ; ; ;.    T#Y      r   r^  c                   4    e Zd ZdZdddZd Zd Zd	 Zd
 ZdS )rx   a  
    argdesc(typename, name='name', n=numallowed|N,
            req=False, positional=True,
            helptext=helptext, **kwargs (type-specific))

    validation rules:
    typename: type(**kwargs) will be constructed
    later, type.valid(w) will be called with a word in that position

    name is used for parse errors and for constructing JSON output
    n is a numeric literal or 'n|N', meaning "at least one, but maybe more"
    req=False means the argument need not be present in the list
    positional=False means the argument name must be specified, e.g. "--myoption value"
    helptext is the associated help for the command
    anything else are arguments to pass to the type constructor.

    self.instance is an instance of type t constructed with typeargs.

    valid() will later be called with input to validate against it,
    and will store the validated value in self.instance.val for extraction.
    Nr   Tr   c                 f   t          |t                    r$t          | _        d|i| _        d| _        d| _        n&|| _        || _        |dv | _        |dv | _        |s|rJ || _        |dv | _        | j        rd| _	        nt          |          | _	        d| _         | j        di | j        | _        d S )Nr_  T)TTruer;  )r]   r\   r   r   r   )rg   
basestringr^  r!  typeargsrp   r^   namer\   r]   rr   numseeninstance)rB   r!  rq  r]   rp   r^   rC   s          r   rD   zargdesc.__init__  s    a$$ 	DF%qMDMDH"DOODF"DM44DH(,BBDO 	z/6 	DFFVVDF////r   c                    dt          | j                  z   dz   }g d}| j                                        D ]G\  }}|                    d          s||v r|dk    r	| j        rd}|d                    ||          z  }H| j                                        D ]\  }}|d                    ||          z  }|d d         d	z   S )
Nzargdesc(z, )r\   rp  rs  r!  __r]   r\   z	{0}={1}, ))rf   r!  __dict__r   r   r\   rQ   rp  )rB   r   	internalsr   rV   s        r   rN   zargdesc.__repr__2  s    TV$t+666	m))++ 	. 	.FQ||D!! .Q)^^ 888A[''1---m))++ 	* 	*FQ##Aq)))AA"v|r   c                 :   | j         t          k    rt          | j        j                  dk    s| j         t
          k    rt          | j                  }n9d                    | j        t          | j                            }| j	        r|dz  }| j
        sd|z   dz   }|S )Nr   z{0}({1})...r   r   )r!  r~   rc   rs  r/  r^  rf   rQ   rq  r\   rp   rY   s     r   rR   zargdesc.__str__A  s    V{""s4=+@'A'AQ'F'Fv##DM""AA!!$)S-?-?@@Av U
x 	a#Ar   c                 D   | j         rj| j        t          k    r0d                    | j                            dd                    }n| j        t          k    rt          | j                  }n| j        t          k    r@| j        dk    r!d| j         dt          | j                   d}nd| j         d	| j         d
}n| j        t          k    rd}n| j        t          k    rd}np| j        t          k    rd                    | j                  }nE| j        t          k    rd                    | j                  }nd                    | j                  }|}| j        r|dz  }| j        sd|z   dz   }n-| j        t          k    r/d                    | j                            dd                    }n| j        t          k    rt          | j                  }n| j        t          k    r!d| j         dt          | j                   d}n{| j        t          k    rd| j         d}n_| j        t          k    rd| j         d}nC| j        t          k    rd| j         d}n'| j        t          k    rd| j         d}nd| j         d}|}| j        r|dz  }| j        sd|z   dz   }|S )zo
        like str(), but omit parameter names (except for CephString,
        which really needs them)
        z--{0}_-rQ   --z {}<r   >z<id|osd.id>z	<type.id>z	<{0}:int>z<{0}:float>rP   r{  r   r   z <id|osd.id>z
 <type.id>z <int>z <float>z <value>)r^   r!  rw   rQ   rq  replacer^  rf   rs  r~   r'  r  rs   ru   r\   rp   )rB   chunkrH   s      r   helpstrzargdesc.helpstrM  s   
 ? 5	"v!!ty'8'8c'B'BCC:%%DM**;&&9(( FEEs4=/A/AEEEEE<	<<DM<<<EE;&& &8## $7""#**49559$$%,,TY77ty11Av U
8 "!GcM v!!ty'8'8c'B'BCC:%%DM**;&&ATYAA3t}+=+=AAA;&&4TY4448##2TY2227"".TY...9$$0TY0000TY000Av U
8 "!GcMr   c                 6    | j                             |          S rT   )rs  rZ   rY   s     r   rZ   zargdesc.complete  s    }%%a(((r   )Nr   TTr$  )	r#   r$   r%   r/   rD   rN   rR   r  rZ   r   r   r   rx   rx     st         *0 0 0 0 02  
 
 
< < <|) ) ) ) )r   rx   c                 @    d                     d | D                       S )zQ
    Return string representation of sig useful for syntax reference in help
    rj  c                 6    g | ]}|                                 S r   )r  )r   ds     r   r   zconcise_sig.<locals>.<listcomp>  s     ...QQYY[[...r   )r   sigs    r   concise_sigr    s%     88..#...///r   c                 ,    t          | d                   S )z
    sort descriptors by prefixes, defined as the concatenation of all simple
    strings in the descriptor; this works out to just the leading strings.
    r  )r  )shs    r   descsort_keyr    s    
 r%y!!!r   r  r   c                 <   g }d}| D ]}|dz  }t          |t                    rt          }|d|d}nd|vr$d                    |           }t	          |          |d         t                      v rZt                      |d                  }t          |t                    s*d                    |d                   }t	          |          n*d                    |d                   }t	          |          t                      }|                                D ]\  }}|dvr|||<   |	                    t          |f|                    d	d
          |                    dd          |                    dd          |                    dd          d|           |S )z
    parse a single descriptor (array of strings or dicts) into a
    dict of function descriptor/validators (objects of CephXXX type)

    :returns: list of ``argdesc``
    r   r   r_  )rz   rq  r_  rz   zJSON descriptor {0} has no typezunknown type {0})rz   rq  r]   rp   r^   rq  Nr]   rp   Tr^   )rq  r]   rp   r^   )rg   ro  r^  rQ   r>   globalsrz   dictr   appendrx   get)	r  newsigargnumdescr!  rH   rC   keyrF   s	            r   parse_funcsigr    s    FF ) )!dJ'' 	$Ax4@@DD T!!5<<SAA mm# F|wyy((IId6l+!!T** (*11$v,??A$Q--'( '--d6l;; mm#

 	" 	"HCDDD!sga (#'88FD#9#9 $a 0 0"&((5$"7"7)-,)E)E	( (
 !'( ( 	) 	) 	) 	) Mr   rH   consumerc                    	 t          j        |           }nC# t          $ r6}t          d                    | |          t
          j                   |d}~ww xY wi }|                                D ]Y\  }}d|vr$d                    |          } t          |           d|v r||d         vr<t          |d                   |d<   |||<   Z|S )a<  
    A function signature is mostly an array of argdesc; it's represented
    in JSON as
    {
      "cmd001": {"sig":[ "type": type, "name": name, "n": num, "req":true|false <other param>], "help":helptext, "module":modulename, "perm":perms, "avail":availability}
       .
       .
       .
      ]

    A set of sigs is in an dict mapped by a unique number:
    {
      "cmd1": {
         "sig": ["type.. ], "help":helptext...
      }
      "cmd2"{
         "sig": [.. ], "help":helptext...
      }
    }

    Parse the string s and return a dict of dicts, keyed by opcode;
    each dict contains 'sig' with the array of descriptors, and 'help'
    with the helptext, 'module' with the module name, 'perm' with a
    string representing required permissions in that module to execute
    this command (and also whether it is a read or write command from
    the cluster state perspective), and 'avail' as a hint for
    whether the command should be advertised by CLI, REST, or both.
    If avail does not contain 'consumer', don't include the command
    in the returned dict.
    zCouldn't parse JSON {0}: {1}fileNr  z JSON descriptor {0} has no 'sig'avail)
jsonloadsr[  printrQ   sysstderrr   r>   r  )rH   r  overallr   sigdictcmdtagcmds          r   parse_json_funcsigsr    s    @*Q--   ,33Aq99
KKKK G}}  299&AAAQ--c>>s7|++"3u:..E
Ns    
A1AAFwordis_kwargrI   c                    g }|p|j         t          u}|j        rU|rS|                     d          D ]<}|j                            ||           |                    |j        j                   =n:|j                            | |           |                    |j        j                   |xj        dz  c_        |j        r|j        dz   |_	        |S )a+  
    validate_one(word, desc, is_kwarg, partial=False)

    validate word against the constructed instance of the type
    in desc.  May raise exception.  If it returns false (and doesn't
    raise an exception), desc.instance.val will
    contain the validated value (in the appropriate type).
    r   r   )
r!  rq   r\   r   rs  rJ   r  rF   rr  r]   )r  r  r  rI   vals	allow_csvparts          r   validate_oner    s     D4DF*4Iv ') 'JJsOO 	+ 	+DMg...KK)****	+ 	D'***DM%&&&LLALLv "!Kr   args	signaturec           	         | dd         }t          j        |          }d}|D ]}d|_        |j        |j        k     r|s|c S |                    d          }	 t          ||d|ot          |          dk               d}n# t          $ r d}Y nw xY w|s"|j        s|	                    d|           n|c S |j        |j        k     |j        r|dz  }|S )z
    matchnum(s, signature, partial=False)

    Returns number of arguments matched in s against signature.
    Can be used to determine most-likely command for full or partial
    matches (partial applies to string matches).
    Nr   FTr   )
copydeepcopyrr  r]   popr  rc   r-   rp   insert)	r  r  rI   wordsmysigmatchcntr  r  rJ   s	            r   matchnumr    s!    GEM)$$EH  lTV##  99Q<<D T40MSZZ1_NNN      $x $LLD))) $OOO/ lTV##0 8 	MHOs   'B  BBr  r  c                    | j         r,| j        |v r|| j        xx         |z  cc<   dS ||| j        <   dS | j        t          k    r-| j        |v r$|| j        xx         d| j        j        z   z  cc<   dS | j        j        || j        <   dS )a  
    Store argument described by, and held in, thanks to valid(),
    desc into the dictionary d, keyed by desc.name.  Three cases:

    1) desc.N is set: use args for arg value in "d", desc.instance.val
       only contains the last parsed arg in the "args" list
    2) prefix: multiple args are joined with ' ' into one d{} item
    3) single prefix or other arg: store as simple value

    Used in validate() below.
    rj  N)r\   rq  r!  r^  rs  rF   )r  r  r  s      r   	store_argr  O  s     v )9>>diLLLD LLLLLAdiLLL
&J

TY!^^	$)dm/// }($)r   flagsc                    t          j        |           }t          j        |          }t          d |D                       }d}t                      }d}	t          d |D                       }
|o|d         dk    }|D ]L}d|_        |j        |j        k     r1|r|                    d          }nd}|dg fv r|j        sn|j        |v rn|r&d}t          j
        t          |          }|rD|                                \  }}|                    dd          }|
                    |d          }nt          j
        t          |          }|r}|                    d          }|                    dd          }|
                    |d          }|r:|j        t$          k    rd	}n't          |          r|                    d          }nd}|r)t'          ||d
          } |dz  }t)          || |           n|j        st-          d| d          |rc|                    d          rN|j        t0          k    p|dk    p|dk    p|dk    p|dk    p|}|s"t-          d                    |                    |dg fv r|j        r|j        r9|j        dk     r.|r|c S t7          d                    |j        |                    |j        s||j        |j        k     rl|r|c S |j        dk    r-|j        dk    r"t9          d                    |                    t7          d                    |j        ||j                            n|J 	 t'          ||d          } nD# t:          $ r7}|j        s||g}	|                    d|           Y d}~n=|r	|cY d}~c S  d}~ww xY w|dz  }t)          || |           d}	|j        |j        k     1N||k     rt?          d          |rL|sJ|	r)tA          |	d         d|	d         tB          j"                   t;          dtG          |          z             |tH          j%        z  rd|d<   |tH          j&        z  rd
|d<   |S )a  
    validate(args, signature, flags=0, partial=False)

    args is a list of strings representing a possible
    command input following format of signature.  Runs a validation; no
    exception means it's OK.  Return a dict containing all arguments keyed
    by their descriptor name, with duplicate args per name accumulated
    into a list (or space-separated value for CephPrefix).

    Mismatches of prefix are non-fatal, as this probably just means the
    search hasn't hit the correct command.  Mismatches of non-prefix
    arguments are treated as fatal, and an exception raised.

    This matching is modified if partial is set: allow partial matching
    (with partial dict returned); in this case, there are no exceptions
    raised.
    c                      g | ]}|j         	|S r   rp   r   r  s     r   r   zvalidate.<locals>.<listcomp>  s    888dtx8T888r   r   Nc              3   H   K   | ]}|j         t          k    |j        |fV  d S rT   )r!  r^  rq  r  s     r   r   zvalidate.<locals>.<genexpr>  s5      II4DFj4H4Hdi4H4H4H4HIIr   
injectargsr~  r}  r   r;  TzUnexpected argument 'ra   r  z--yes-i-really-mean-itz--yes-i-really-really-mean-itz(--yes-i-really-really-mean-it-not-fakingz--forcezUnexpected argument '{0}'z#saw {0} of {1}, expected at least 1zmissing required parameter {0}zsaw {0} of {1}, expected {2}Fznot enough arguments givenznot valid: r  zunused arguments: )mon-mgrr   targetpoll)'r  r  rc   r  rr  r]   r  rp   rq  r   r   KWARG_EQUALSgroupsr  r  KWARG_SPACEgroupr!  rw   r  r  r^   r8   r   r~   rQ   r\   r2   r6   r-   r  r:   r  r  r  rf   r   r)   r*   )r  r  r  rI   myargsr  	reqsiglenr  r  save_exceptionarg_descs_by_namer  r  myarg
kwarg_desckwarg_matchkwarg_kkwarg_v	key_matchis_valuer   s                        r   validater  i  sw   , ]4  FM)$$E88e88899IHvvAN 	II5IIIII 
 5F1I5J  F" F"lTV## '-zz!}} r
""x 9>>  '!
 !h|U;; 2'2'9'9';';$GW &ooc377G!2!6!6w!E!EJJ !#e < <I  2"+//!"4"4
 #*//#s";";%6%:%:7D%I%I
% 2)|x77*0!$V 2*0**Q-- .2
 'TBBDMHj$222? F#$DE$D$D$DEEE
   ))$//    6[0 & $<<& $CC& !$NN& !I-	&
 &     
 ((C(J(J) )      
 r
""tx"6 dlQ.. ! (=t|T22    DL46$9$9 ! |q((TVq[[-<CCDII   )6t|T46::    $$$#E477    x 
 ',QZNMM!U+++EEEE  !  MHdD!$$$!NG lTV##L )9::: @g @ 	X.#]N14ECJWWWW03v;;>???tx &%(ty &	 Hs*   
M
N&!NNNNNr  verbosec           
      N   |r1t          dd                    |          z   t          j                   d}i }d}g }|                                 D ]}|                    dd          }|t          j        z  r(|d         }	t          ||	d	
          }
|
t          j
        |          k    r|
t          ||	d
          k    r|
dz  }
|
|k     r||r=t          d                    |
|t          |	                    t          j                   |
|k    r|
}|g}|                    |           d }t          ||          }|rMt          dt          j                   t          j        t          j                                      |           d}|D ]}|d         }		 t#          ||	|                    dd                    }|} n# t$          $ r Y At&          $ r"}|}t)          |          dk    r|}Y d}~ nUd}~wt*          $ r. |r)t          dt          |	          t          j                   Y t,          $ r}|}|}Y d}~ nd}~ww xY w|rO|sLt          d|t          j                   t          t          |	          d|d         t          j                   nd |D             }|dd         }t          d                    t)          |                    t          j                   |D ]0}t          t          |d                   t          j                   1|S )as  
    Parse positional arguments into a parameter dict, according to
    the command descriptions.

    Writes advice about nearly-matching commands ``sys.stderr`` if
    the arguments do not match any command.

    :param sigdict: A command description dictionary, as returned
                    from Ceph daemons by the get_command_descriptions
                    command.
    :param args: List of strings, should match one of the command
                 signatures in ``sigdict``

    :returns: A dict of parsed parameters (including ``prefix``),
              or an empty dict if the args did not match any signature
    zvalidate_command: rj  r  Ng        r  r   r  TrI   Fg      ?zbetter match: {0} > {1}: {2} c                 N    | d         }t          t          d |                    S )Nr  c                     | j         S rT   r  r  s    r   <lambda>z1validate_command.<locals>.grade.<locals>.<lambda>a  s    37 r   )summap)r  sigss     r   gradezvalidate_command.<locals>.grade^  s'    5z3**D11222r   )r  zbestcmds_sorted: )stream)r  r   zNot enough args supplied for zInvalid command:z: helpc                 r    g | ]4}|                     d d          t          j        t          j        z  z  2|5S )r  r   )r  r   r(   r+   )r   r   s     r   r   z$validate_command.<locals>.<listcomp>  sO     P P P!55!,,$+0MNPA P P Pr   
   z,no valid command found; {0} closest matches:)r  r   r  r  valuesr  r   r'   r  mathfloorrQ   r  r  sortedpprintPrettyPrinterr  r<   r6   rc   r:   r-   )r  r  r  found
valid_dictbest_match_cntbestcmdsr  r  r  matchedr  bestcmds_sortedexr   s                  r   validate_commandr  )  s   &  F"SXXd^^3#*EEEE&*EJ N%'H~~ ! !##4=  	%j!$T:::tz.1111xc599999sNG^## 	 188S)9)9 J        ^##$NuHHOOC    
3 3 3
 X5111O H!
3333CJ///66GGG"&B  %j	!$3777A3F3FGGGJEE 	 	 	 D 	 	 	B8}}!!EEEEE 	9 	9 	9
  95!#&&SZ9 9 9 9 	 	 	B EEEEEE	  < 	H$bsz::::+c""D#f+CJGGGGP Px P P PCRC=<CCCMMRR:	 	 	 	 	< 	<C+c%j))
;;;;;s0   (G		
I	IG;;8I5	I>II	childargsc                    t          ddddg          }	 t          | |d          }t          |          dk    r@t                      }|                    |d                    |j        J |j        |j        fS n# t          $ r Y nw xY wt          dd	d
dg          }	 t          | |d          }t          |          dk    r#|d	         }t          |t                    sJ d|fS n# t          $ r Y nw xY wt          |           dk    rU| d         dk    rIt                      }|                    | d                    |j        J |j        J |j        |j        fS t          dd	d
dg          }	 t          | |d          }t          |          dk    r#|d	         }t          |t                    sJ d|fS n# t          $ r Y nw xY wdS )a%  
    Using a minimal validation, figure out whether the command
    should be sent to a monitor or an osd.  We do this before even
    asking for the 'real' set of command signatures, so we can ask the
    right daemon.
    Returns ('osd', osdid), ('pg', pgid), ('mgr', '') or ('mon', '')
    tellr  r  )rq  rz   Tr  r   Npgidr  pgr   r   r  r   )
r  r  rc   r  rJ   r  r  r-   rg   rf   )r  r  r  rq  r  s        r   find_cmd_targetr    sT    (J!G!GH
I
IC.id;;;
 z??a::D JJz(+,,,=,,,=$+--       &*!E!EF
G
GC	id;;;
 z??af%DdC(((((:	      4 9~~ilf44zz 	

9Q<   }((({&&&}dk))

CCD
E
ECid;;;
 z??af%DdC(((((:       9s5   A< <
B	B	!C* *
C76C77G   
GGc                       e Zd Zd Zd ZdS )RadosThreadc                 |    || _         || _        || _        d | _        t          j                            |            d S rT   )r  rC   func	exception	threadingThreadrD   )rB   r  r  rC   s       r   rD   zRadosThread.__init__  s;    		!!$'''''r   c                 ~    	  | j         | j        i | j        | _        d S # t          $ r}|| _        Y d }~d S d }~ww xY wrT   )r  r  rC   retvalr[  r  )rB   r   s     r   runzRadosThread.run  sY    	#$)TY>$+>>DKKK 	 	 	DNNNNNNN	s     
<7<N)r#   r$   r%   rD   r  r   r   r   r  r    s2        ( ( (    r   r  r  rC   c                 4   |                     dd          }|dk    s|d}t          | g|R i |}d|_        |                                 |                    |           |                                rt          d          |j        r|j        |j        S )Ntimeoutr   iQ Tr  z	timed out)	r  r  daemonstartr   is_aliver[  r  r  )r  r  rC   r  r!  s        r   run_in_threadr
    s    jjA&&G!||w D*4***6**A
 AHGGIIIFF7F 	zz|| $$$	
 kxr   c                      	 	 t          | i |S # t          $ r-}dt          |          v rdt          |          v rY d }~? d }~ww xY w)NTget_command_descriptionszobject in state configuring)send_commandr[  rf   )r  rC   r   s      r   send_command_retryr    sp    
		0000 	 	 	 +c!ff44-Q77	s    
A"A AAr  r   r  r  inbufr  c                    	 |d         dk    ri|d         }|J |r/t          d                    ||          t          j                   t	          | j        t          |          |||          \  }}}	n|d         dk    r|d	}
t          |          dk    r|d         |d         }
|r6t          d
                    ||d         |
          t          j                   t	          | j        ||||
          \  }}}	n|d         dk    rU|r5t          d                    ||d                   t          j                   t	          | j        |||          \  }}}	n*|d         dk    r|d         }|rt          j
        |          }||d<   nt          |          }t          j        |          }|r/t          d                    ||          t          j                   t	          | j        ||||          \  }}}	n|d         dk    r|r5t          d                    ||d                   t          j                   t          |          dk     s|d         d	k    rt	          | j        |||          \  }}}	nt	          | j        ||||d                   \  }}}	n|d         dk    r|d         }|r/t          d                    ||          t          j                   	 ddlm} n# t"          $ r t%          d          w xY w ||           }|                                 |                    |||          \  }}}	|                                 n(t-          d                    |d                             nK# t.          $ r>}t1          |t2                    s#t%          d                    ||                     d}~ww xY w|||	fS )a  
    Send a command to a daemon using librados's
    mon_command, osd_command, mgr_command, or pg_command.  Any bulk input data
    comes in inbuf.

    Returns (ret, outbuf, outs); ret is the return code, outbuf is
    the outbl "bulk useful output" buffer, and outs is any status
    or error message (intended for stderr).

    If target is osd.N, send command to that osd (except for pgid cmds)
    r   r  r   Nzsubmit {0} to osd.{1}r  r  r  r   zsubmit {0} to {1} name {2})r  r  r  zsubmit {0} to {1}r  r  )r  zsubmit {0} for pgid {1}r  z
{0} to {1}r   r  zsubmit {0} to mds.{1})	LibCephFSz1CephFS unavailable, have you installed libcephfs?)
rados_instzBad target type '{0}'"{0}": exception {1})r  rQ   r  r  r
  osd_commandrr   rc   mgr_commandr  r  r  dumps
pg_commandmon_commandcephfsr  ImportErrorRuntimeErrorinitmds_commandshutdownr8   r[  rg   r-   )clusterr  r  r  r  r  osdidretoutbufoutsrq  r  cmddictmds_specr  
filesystemr   s                    r   r  r  "  s   "N!91IE$$$ '-44S%@@:' ' ' ' -#SZZeW!N !N !NC AY%D6{{Q6!9#8ay '299#vay$OO:' ' ' ' -#S%!O !O !OC AY)## ')00fQi@@:' ' ' ' -#S%!B !B !BC AY$!9D  **S//"&D///*W%%C '/66sDAA:' ' ' ' -"D#ug!G !G !GC AY% 'l))#vay99:' ' ' '6{{Q&)r//$1'eW%F %F %F!VTT %2'eWVTUY%X %X %X!VTTAY%ayH '-44S(CC:' ' ' 'X,,,,,,, X X X"#VWWWX #g666JOO&&xe<< C!!!! 7 > >vay I IJJJ   !]++ 	5<<S!DDEEE	 s1   K+N .K5 4N 5LA;N 
O9OOr_  argdictc                    i }|r|                     d|i           |rF|                     |           d|v r-t          t          t          t          f         |d                   }	 |d         dk    rkt	                      } dj        | }	d|v r(t          t          |                    d                    }		 |                    |	           d|j        f}n#  Y nxY wt          | |t          j        |          |||          \  }
}}nK# t          $ r>}t          |t                    s#t          d                    ||                     d}~ww xY w|
||fS )a  
    Serialize a command and up a JSON command and send it with send_command() above.
    Prefix may be supplied separately or in argdict.  Any bulk input
    data comes in inbuf.

    If target is osd.N, send command to that osd (except for pgid cmds)

    :param cluster: ``rados.Rados`` instance
    :param prefix: String to inject into command arguments as 'prefix'
    :param argdict: Command arguments
    r_  r  r   r  z{0}.{1}r  N)updater   r   rf   r  rQ   r  rJ   r  r  r  r  r[  rg   r-   r  )r  r  r_  r'  r  r  r  r$  osdtarg	osdtargetr!  r"  r#  r   s                 r   json_commandr,    s   $  G +&)*** >ww%S/78+<==F!9jjG(	(&1I7"" gkk(&;&;<<	i(((0.w/5tz'7J7J/4gwH HVTT    !]++ 	5<<WaHHIII	 s1   %AD 6C D C/D 
E9E

Er   )r   F)r  Nr   r   F)r  NNr   r   F)Wr/   r  r|   r  r  r   r  r   r   r   r  r  rY  collectionsr   typingr   r   r   r   r   r	   r
   r   r   r   version_infor   r   r   r  r  ro  	NameErrorrf   r[  r-   r2   r4   r6   r8   r:   r<   r>   objectr@   rs   ru   rq   r   r   r   r  r  r  r  r'  r~   rw   rB  rQ  rW  r^  rx   r  r  r  r  rv   rr   rt   ArgValTr  r  ValidatedArgValidatedArgsr  r  r  r  r   r  rd  r
  r  r  r,  r   r   r   <module>r5     s
  
 
     				  				   



            ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ]v+++++++++/ / // / /
         JJ   JJJ	 	 	 	 	I 	 	 		 	 	 	 	] 	 	 		 	 	 	 	] 	 	 		 	 	 	 	m 	 	 		 	 	 	 	M 	 	 	    ]   	 	 	 	 	] 	 	 		 	 	 	 	 	 	 	B= B= B= B= B=& B= B= B=J$& $& $& $& $&k $& $& $&N#& #& #& #& #& #& #& #&L&& && && && && && && &&R% % % % %[ % % %>! >! >! >! >! >! >! >!B    Z   >    ;       [       {   0)" )" )" )" )"{ )" )" )"X '  '  '  '  '+  '  '  'F+& +& +& +& +&+ +& +& +&\    {   *    ;   62 2 2 2 2; 2 2 22    {   % % % % % % % %PI) I) I) I) I)f I) I) I)X0 0 0" " ")xc4S>&9 :; )W ) ) ) )X23 2"%2*.sDd7m9K4L/L*M2 2 2 2j c5#uS#X6
7
 "' s   ,0=   < #) )49 )W))'*) ) ) )X T3s38_c]# $ S,&')G )8L#9 )m ) ) ) )8 ',} }49 } )}} tn} 2?} } } }D 05l ld3S#X#67 l9l&tnl9Fl l l l^HtCy HU33E-F H H H HV    )"   #sU3s?-C!CD (+05c5#o0F   Dc S U3s?5K     6A&**-*++0a asHSM12asma !a #3-	a
 #4.a
 6;3s?5Ka a a aJ 6A)-48*-*++02 2sHSM122!#2 #=12 !	2
 #3-2 #4.2 6;3s?5K2 2 2 2 2 2s   =B   B
	B
