
    th@              
           U d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ  ej                  e      Zdg d	ed
gdZe
ed<   dededededdf
dZ G d de      Z G d d      ZddZy)zDRed Hat Subscription: Register Red Hat Enterprise Linux based system    N)subputil)Cloud)Config)
MetaSchema)PER_INSTANCEcc_rh_subscription)fedorarhel	openeulerrh_subscription)iddistros	frequencyactivate_by_schema_keysmetanamecfgcloudargsreturnc                 v   t        |      }|j                         st        j                  d|        y |j	                         s	 |j                         \  }}|durt        |      |j                         }|st        d      |j                  r=|j                  1|j                         st        d      t        j                  d       n<|j                  r0|j                         st        d      t        j                  d       |j                  ht        |j                  t              sd}t        |      |j                  |j                        }	|	s$t        d	j!                  |j                              |j#                         }	|	st        d
      |j$                  r |j'                          |j)                          t        j+                  d       y t        j+                  d       y # t        $ r=}
t        j-                  t/        |
             t        j-                  d       Y d }
~
y d }
~
ww xY w)Nz%s: module not configured.Tz-Registration failed or did not run completelyzSetting of service_level failedz(Completed auto_attach with service levelzSetting auto_attach failedzCompleted auto_attachz"Pools must in the format of a listzUnable to attach pools {0}zUnable to add or remove reposz-rh_subscription plugin completed successfullyz4rh_subscription plugin did not complete successfullyzSystem is already registered)SubscriptionManageris_configuredLOGdebugis_registered_verify_keysSubscriptionErrorrhn_registerauto_attachservicelevel_set_service_level_set_auto_attachpools
isinstancelistaddPoolformatupdate_reposrelease_version_set_release_version_delete_packagemanager_cacheinfowarningstr)r   r   r   r   smverify
verify_msgcont	pool_failreturn_states              E/usr/lib/python3/dist-packages/cloudinit/config/cc_rh_subscription.pyhandler9      s   	S	!B		.5.	P!#!2FJT!'
33??$D'C  ~~"//"=,,.+,MNNIIHI**,+,HIIII56xx#!"((D1 DI+I66 jj2"+4;;BHHE  //+K'(GHH!!'')//1HHDE
 	/0	 ! 	PKKAKKNOO	Ps   FG2 2	H8;3H33H8c                       e Zd Zy)r   N)__name__
__module____qualname__     r8   r   r   T   s    r?   r   c                   Z    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)r   c                    |j                  di       | _        | j                  j                  d      | _        | j                  j                  d      | _        | j                  j                  d      | _        | j                  j                  d      | _        | j                  j                  d| j                  j                  d            | _        | j                  j                  d| j                  j                  d	            | _        | j                  j                  d
| j                  j                  d            | _        | j                  j                  d| j                  j                  d            | _	        | j                  j                  d| j                  j                  d            | _
        | j                  j                  d| j                  j                  d            | _        | j                  j                  d| j                  j                  d            | _        | j                  j                  d| j                  j                  d            | _        y )Nr   orgusernamepasswordr+   rhsm_baseurlzrhsm-baseurlserver_hostnamezserver-hostnameadd_poolzadd-poolactivation_keyzactivation-keyr!   zauto-attachenable_repozenable-repodisable_repozdisable-reposervice_levelzservice-level)getrhel_cfgrB   useridrD   r+   rE   rF   r%   rH   r!   rI   rJ   r"   )selfr   s     r8   __init__zSubscriptionManager.__init__Z   s    126==$$U+mm''
3))*5#}}001BC !MM--DMM--n=
  $}}00t}}001BC 
 ]]&&))*5

 #mm//dmm//0@A
  ==,,4==,,];
  ==,,4==,,];
 !MM--DMM--n=
 !MM--T]]..?
r?   c                 0   | j                   Dt        j                  | j                         s%t        j                  | j                         sd}d|fS | j                  :| j                   r(t        j                  t        | j                               rd}d|fS y)zq
        Checks that the keys in the rh_subscription dict from the user-data
        are what we expect.
        z8The key auto_attach must be a boolean value (True/False)FzqThe service_level key must be used in conjunction with the auto_attach key.  Please re-run with auto_attach: True)TN)r!   r   is_trueis_falser"   r0   )rO   not_boolno_autos      r8   r   z SubscriptionManager._verify_keys   s     (LL))*dmmD<L<L.M K  (?")!!t}}S9I9I5J'K$ 
 '>! r?   c                 T    dg}	 t        |       y# t        j                  $ r Y yw xY w)zg
        Checks if the system is already registered and returns
        True if so, else False
        identityFT)_sub_man_clir   ProcessExecutionError)rO   cmds     r8   r   z!SubscriptionManager.is_registered   s7    
 l	  )) 		s    ''c                 :   | j                   | j                  ddj                  | j                         dj                  | j                        g}| j                  *|j	                  dj                  | j                               | j
                  *|j	                  dj                  | j
                               	 t        |d      d	   }n| j                  | j                  ddj                  | j                        dj                  | j                        g}| j                  *|j	                  dj                  | j                               | j
                  *|j	                  dj                  | j
                               	 t        |d      d	   }n+t        j                  d       t        j                  d       y|j                  d      d   j!                         }t        j#                  d|       y# t        j                  $ r9}|j                  d
k(  r t        j                  d|j                         Y d}~yd}~ww xY w# t        j                  $ r9}|j                  d
k(  r t        j                  d|j                         Y d}~yd}~ww xY w)z
        Registers the system by userid and password or activation key
        and org.  Returns True when successful False when not.
        Nregisterz--activationkey={0}z	--org={0}z--baseurl={0}z--serverurl={0}T)logstring_valr    zRegistration failed due to: %sFz--username={0}z--password={0}z8Unable to register system due to incomplete information.z9Use either activationkey and org *or* userid and passwordzID:    z"Registered successfully with ID %s)rH   rB   r)   rE   appendrF   rX   r   rY   stdoutr   r/   stderrrN   rD   splitrstripr   )rO   rZ   
return_outr7   reg_ids        r8   r    z SubscriptionManager.rhn_register   s/    +$((2F %,,T-@-@A""488,C   ,

?11$2C2CDE##/

,33D4H4HIJ)#TB1E
 kk%DMM,E  ''4 ''6C   ,

?11$2C2CDE##/

,33D4H4HIJ)#TB1E
 KKJ KKK !!&)!,335		6?S -- 88r>KK @!((K2 -- 88r>KK @!((Ks0   =G? 
I ?I/IIJ!/JJc                    dddj                  | j                        g}	 t        |      d   }|j                  d      D ]  }|dk7  s	t        j                  |        y	# t        j                  $ r}|j
                  j                         dk7  r<|j
                  j                  d      D ]  }|dk7  s	t        j                  |        n.t        j                  d|j                  j                                Y d }~yd }~ww xY w)
Nattach--autoz--servicelevel={0}r   r^   
z)Setting the service level failed with: %sFT)r)   r"   rX   r   rY   ra   rd   rc   r   r/   rb   stripr   rO   rZ   re   r7   lines        r8   r#   z&SubscriptionManager._set_service_level   s     ''(9(9:
	%c*1-J $$T* 	 Drz		$	   )) 
	xx B&HHNN40 *DrzD)* ?HHNN$ 
	s   A   D3A C?4AC??Dc                     ddg}	 t        |      d   }|j                  d      D ]  }|dk7  s	t        j                  |        y# t        j                  $ r }t        j	                  d|       Y d }~yd }~ww xY w)	Nrh   ri   r   zauto_attach failed with: %sFrj   r^   T)rX   r   rY   r   r/   rc   r   rl   s        r8   r$   z$SubscriptionManager._set_auto_attach  s}    "	%c*1-J $$T* 	 Drz		$	   )) 	KK5q9	s   A A9A44A9c                     g }g }g d}t        |      d   }|j                         j                  d      }g d}t        |      d   }|j                         j                  d      }||fS )zh
        Gets the list pools for the active subscription and returns them
        in list form.
        )r'   z--available--pool-onlyr   rj   )r'   z
--consumedrp   )rX   rd   rc   )rO   	availableconsumedrZ   resultss        r8   	_getPoolszSubscriptionManager._getPools  ss    
 	 5s#A&^^%,,T2	 4s#A&NN$++D1(""r?   c                    ddg}t        |      d   }g }|j                  d      D ]8  }d|v s|j                  |j                  d      d   j                                : ddg}t        |      d   }g }|j                  d      D ]8  }d|v s|j                  |j                  d      d   j                                : ||fS )	zl
        Obtains the current list of active yum repositories and returns
        them in list form.
        reposz--list-enabledr   rj   zRepo ID::r_   z--list-disabled)rX   rc   r`   rk   )rO   rZ   re   active_reposrepoinactive_reposs         r8   	_getReposzSubscriptionManager._getRepos+  s     ()!#&q)
$$T* 	BDT!##TZZ_Q%7$>$>$@A	B )*!#&q)
$$T* 	DDT!%%tzz#q'9&@&@&BC	D ^++r?   c                     |st         j                  d       y| j                         \  }}g }dg}|D ]A  }||vr%||v r!|j                  dj	                  |             ,t         j                  d|       C t        |      dkD  rS|j                  |       	 t        |       t         j                  ddj                  |      j                  d	d
             yy# t        j                  $ r!}t         j                  d|       Y d}~yd}~ww xY w)zl
        Takes a list of subscription pools and "attaches" them to the
        current subscription
        zNo pools to attachTrh   z
--pool={0}zPool %s is not availabler   z/Attached the following pools to your system: %s, z--pool=r^   z"Unable to attach pool %s due to %sNF)r   r   rt   r`   r)   r/   lenextendrX   joinreplacer   rY   )rO   r%   pool_availablepool_consumed	pool_listrZ   poolr7   s           r8   r(   zSubscriptionManager.addPoolA  s     II*+(,(8%	j 	>DM)0F  !4!4T!:;6=		>
 y>AJJy!	S!		EYYy)229bA   -- @$Js   A C D,DDc                 .   | j                   }| j                  }|g }|g }t        |t              st        j                  d       yt        |t              st        j                  d       y|s|st        j                  d       y| j                         \  }}g }g }|D ]8  }||v r!|j                  dj                  |             (|j                  |       : g }g }	|D ]8  }||v r!|j                  dj                  |             (|	j                  |       : t        |      dkD  r8|D ]3  }
|
|v rt        j                  d	|
       t        j                  d
|
       5 t        |	      dkD  r|	D ]  }
t        j                  d|
        dg}t        |      dkD  r|j                  |       t        |      dkD  r|j                  |       	 t        |       t        |      dkD  r5t        j                  ddj                  |      j!                  dd             t        |      dkD  r5t        j                  ddj                  |      j!                  dd             y# t        j                  $ r }t        j                  d|       Y d}~yd}~ww xY w)z
        Takes a list of yum repo ids that need to be disabled or enabled; then
        it verifies if they are already enabled or disabled and finally
        executes the action to disable or enable
        Nz&Repo IDs must in the format of a list.Fz No repo IDs to enable or disableTz--enable={0}z--disable={0}r   zRepo %s is already enabledz Repo %s does not appear to existz.Repo %s not disabled because it is not enabledrv   zUnable to alter repos due to %szEnabled the following repos: %sr}   z	--enable=r^   z Disabled the following repos: %sz
--disable=)rI   rJ   r&   r'   r   r/   r   r{   r`   r)   r~   r   rX   r   rY   r   r   )rO   ereposdreposrx   rz   enable_listenable_list_failrepoiddisable_listdisable_list_failfailrZ   r7   s                r8   r*   z SubscriptionManager.update_reposa  sz    !!"">F>F&$'KK@A&$'KK@A II89'+~~'7$n 	0F'"">#8#8#@A ''/		0  	1F%##O$:$:6$BC!((0		1  1$( J<'II:DAKK BDIJ  !A%) 		Dd
 i|q JJ|${aJJ{#	
 {aII1;'00bA |q II2<(11,C  )) 	KK91=	s   I! !J4JJc                 d    t        | j                  xr | j                  xs | j                        S )N)boolrN   rD   rH   )rO   s    r8   r   z!SubscriptionManager.is_configured  s%    T[[2T]]Jt7J7JKKr?   c                     dd| j                    g}	 t        |       y# t        j                  $ r}t	        d|       |d}~ww xY w)z
        Execute "subscription-manager release --set=<release_version>"
        Raises Subscription error if the command fails
        releasez--set=z%Unable to set release_version using: N)r+   rX   r   rY   r   )rO   rZ   r7   s      r8   r,   z(SubscriptionManager._set_release_version  sY     F4#7#7"89:	)) 	#7u=	s    AAAc                     t         j                  d       	 t        j                  d       t        j                  d       y# t        $ r}t        d      |d}~ww xY w)zk
        Delete the package manager cache.
        Raises Subscription error if the deletion fails
        z"Deleting the package manager cachez/var/cache/dnfz/var/cache/yumz*Unable to delete the package manager cacheN)r   r   r   del_dir	Exceptionr   )rO   r7   s     r8   r-   z0SubscriptionManager._delete_packagemanager_cache  sS    
 			67	LL)*LL)* 	#<	s   *A 	AAAN)r;   r<   r=   rP   r   r   r    r#   r$   rt   r{   r(   r*   r   r,   r-   r>   r?   r8   r   r   X   sK    #
JBBH2
#(,,@RhLr?   r   c                 8    t        j                   dg| z   |      S )z
    Uses the preferred cloud-init subprocess def of subp.subp
    and runs subscription-manager.  Breaking this to a
    separate function for later use in mocking and unittests
    zsubscription-manager)	logstring)r   )rZ   r]   s     r8   rX   rX     s     99,-3}MMr?   )F)__doc__logging	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   	getLoggerr;   r   r   __annotations__r0   r'   r9   r   r   r   rX   r>   r?   r8   <module>r      s    K    ! # . +g! . 12	j 71 716 71% 71t 71 71t		 	z zzNr?   