�
oB�]c@s�dZddlmZddlmZeje�ZddgZd�Z de
fd��YZd efd
��YZ
ed�ZdS(
s"
RedHat Subscription
-------------------
**Summary:** register red hat enterprise linux based system
Register a RedHat system either by username and password *or* activation and
org. Following a sucessful registration, you can auto-attach subscriptions, set
the service level, add subscriptions based on pool id, enable/disable yum
repositories based on repo id, and alter the rhsm_baseurl and server-hostname
in ``/etc/rhsm/rhs.conf``. For more details, see the ``Register RedHat
Subscription`` example config.
**Internal name:** ``cc_rh_subscription``
**Module frequency:** per instance
**Supported distros:** rhel, fedora
**Config keys**::
rh_subscription:
username: <username>
password: <password>
activation-key: <activation key>
org: <org number>
auto-attach: <true/false>
service-level: <service level>
add-pool: <list of pool ids>
enable-repo: <list of yum repo ids>
disable-repo: <list of yum repo ids>
rhsm-baseurl: <url>
server-hostname: <hostname>
i����(tlog(tutiltfedoratrhelcCs�t|d|�}|j�s2|jd|�dS|j�s�yi|j�\}}|tk rnt|��n|j�}|s�td��n|j r�|j
dk r�|j�s�td��q|jjd�n7|j r|j
�s�td��q|jjd�n|jdk rxt|jt�sBd} t| ��n|j|j�}
|
sxtd j|j���qxn|j�}
|
s�td
��n|jd�Wq�tk
r�}|jt|��|jd�q�Xn
|jd
�dS(NRs%s: module not configured.s-Registration failed or did not run completelysSetting of service-level faileds(Completed auto-attach with service levelsSetting auto-attach failedsCompleted auto-attachs"Pools must in the format of a listsUnable to attach pools {0}sUnable to add or remove reposs-rh_subscription plugin completed successfullys4rh_subscription plugin did not complete successfullysSystem is already registered(tSubscriptionManagert
is_configuredtdebugtNonet
is_registeredt_verify_keystTruetSubscriptionErrortrhn_registertauto_attachtservicelevelt_set_service_levelRt_set_auto_attachtpoolst
isinstancetlisttaddPooltformattupdate_repostlog_successtlog_warntstr(tnametcfgt_cloudRt_argstsmtverifyt
verify_msgtcontt pool_failtreturn_statte((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pythandle1sH RcBseZRS((t__name__t
__module__(((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyRfsRcBs�eZdddddddddd d
gZdd�Zd�Zd
�Zd�Zd�Zd�Z d�Z
d�Zd�Zd�Z
d�Zd�Zd�ZRS(torgsactivation-keytusernametpasswordsdisable-reposenable-reposadd-poolsrhsm-baseurlsserver-hostnamesauto-attachs
service-levelcCs*|dkrt}n||_||_|jjdi�|_|jjd�|_|jjd�|_|jjd�|_|jjd�|_ |jjd�|_
|jjd�|_|jjd�|_|jjd �|_
|jjd
�|_|jjd�|_|jjd�|_dS(
Ntrh_subscriptionsrhsm-baseurlsserver-hostnamesadd-poolsactivation-keyR(R)R*sauto-attachsenable-reposdisable-repos
service-level(RtLOGRRtgettrhel_cfgtrhsm_baseurltserver_hostnameRtactivation_keyR(tuseridR*R
tenable_repotdisable_repoR(tselfRR((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyt__init__ps cCs|jj|�dS(s>Simple wrapper for logging info messages. Useful for unittestsN(Rtinfo(R5tmsg((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR�scCs|jj|�dS(sASimple wrapper for logging warning messages. Useful for unittestsN(Rtwarning(R5R8((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR�scCs�xH|jD]=}||jkr
dj|dj|j��}t|fSq
W|jdk r�tj|j�p{tj |j�r�d}t|fS|j
dk r�|js�tj t|j��r�d}t|fStdfS(sq
Checks that the keys in the rh_subscription dict from the user-data
are what we expect.
s?{0} is not a valid key for rh_subscription. Valid keys are: {1}s, s8The key auto-attach must be a boolean value (True/False sqThe service-level key must be used in conjunction with the auto-attach key. Please re-run with auto-attach: TrueN(
R.t
valid_rh_keysRtjointFalseR
RRtis_truetis_falseRRR
(R5tktbad_keytnot_booltno_auto((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR �s
cCs3dg}yt|�Wntjk
r.tSXtS(sg
Checks if the system is already registered and returns
True if so, else False
tidentity(t_sub_man_cliRtProcessExecutionErrorR<R
(R5tcmd((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR�s cCsH|jdk r�|jdk r�ddj|j�dj|j�g}|jdk rp|jdj|j��n|jdk r�|jdj|j��nyt|dt�d}Wqt j
k
r�}|jdkr�|jd j|j
��ntSXn|jdk r�|jdk r�dd
j|j�dj|j�g}|jdk rm|jdj|j��n|jdk r�|jdj|j��nyt|dt�d}Wqt j
k
r�}|jdkr�|jd j|j
��ntSXn|jd�|jd
�tS|jd�dj�}|jjd|�tS(s�
Registers the system by userid and password or activation key
and org. Returns True when successful False when not.
tregisters--activationkey={0}s --org={0}s
--baseurl={0}s--serverurl={0}t
logstring_valitsRegistration failed due to: {0}s--username={0}s--password={0}s8Unable to register system due to incomplete information.s9Use either activationkey and org *or* userid and passwordsID: is"Registered successfully with ID %sN(R1RR(RR/tappendR0RDR
RREtstdoutRtstderrR<R2R*tsplittrstripRR(R5RFt
return_outR$treg_id((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR�sD
cCs�dddj|j�g}yt|�d}Wn�tjk
r�}|jj�dkr�xX|jjd�D]"}|dkrl|j|�qlqlWn|jdj|j j
���tSXx6|jd�D]%}|dkr�|jj
|�q�q�WtS(Ntattachs--autos--servicelevel={0}iRIs
s*Setting the service level failed with: {0}(RRRDRRERKRNRMRRLtstripR<RRR
(R5RFROR$tline((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR�s cCs�ddg}yt|�d}Wn-tjk
rO}|jdj|��tSXx6|jd�D]%}|dkr`|jj|�q`q`Wt S(NRQs--autoisAuto-attach failed with: {0}s
RI(
RDRRERRR<RMRRR
(R5RFROR$RS((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyRscCs~g}g}dddg}t|�d}|j�jd�}dddg}t|�d}|j�jd�}||fS(sh
Gets the list pools for the active subscription and returns them
in list form.
Rs--availables--pool-onlyis
s
--consumed(RDRNRM(R5t availabletconsumedRFtresults((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyt _getPoolsscCs�ddg}t|�d}g}xF|jd�D]5}d|kr2|j|jd�dj��q2q2Wddg}t|�d}g}xF|jd�D]5}d|kr�|j|jd�dj��q�q�W||fS( sl
Obtains the current list of active yum repositories and returns
them in list form.
treposs--list-enabledis
sRepo ID:t:is--list-disabled(RDRMRJRR(R5RFROtactive_repostrepotinactive_repos((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyt _getRepos's''cCs2t|�dkr&|jjd�tS|j�\}}g}dg}xU|D]M}||kr�||kr�|jdj|��qN|jdj|��qNWt|�dkr.|j|�y:t |�|jjddj
|�jdd ��tSWq.tj
k
r*}|jd
j||��tSXndS(sl
Takes a list of subscription pools and "attaches" them to the
current subscription
isNo pools to attachRQs
--pool={0}sPool {0} is not availables/Attached the following pools to your system: %ss, s--pool=RIs$Unable to attach pool {0} due to {1}N(tlenRRR
RWRJRRtextendRDR;treplaceRRER<(R5Rtpool_availablet
pool_consumedt pool_listRFtpoolR$((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR=s,
c
Cs|j}|j}|dkr'g}n|dkr<g}nt|t�s\|jd�tSt|t�s||jd�tSt|�dkr�t|�dkr�|jj d�t
S|j�\}}g}g}x@|D]8}||kr|jdj
|��q�|j|�q�Wg}g} x@|D]8}||krS|jdj
|��q(| j|�q(Wt|�dkr�xI|D]>}
|
|kr�|jj d|
�q}|jdj
|
��q}Wnt| �dkr�x$| D]}
|jj d|
�q�Wnd g}t|�dkr&|j|�nt|�dkrH|j|�nyt|�Wn-tjk
r�}|jd
j
|��tSXt|�dkr�|jj ddj|�jd
d��nt|�dkr|jj ddj|�jdd��nt
S(s�
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
s&Repo IDs must in the format of a list.is No repo IDs to enable or disables--enable={0}s
--disable={0}sRepo %s is already enableds!Repo {0} does not appear to exists.Repo %s not disabled because it is not enabledRXs Unable to alter repos due to {0}sEnabled the following repos: %ss, s --enable=RIs Disabled the following repos: %ss
--disable=N(R3R4RRRRR<R^RRR
R]RJRR_RDRRER;R`(
R5terepostdreposRZR\tenable_listtenable_list_failtrepoidtdisable_listtdisable_list_failtfailRFR$((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR]sn
$
cCst|jr|jp|j�S(N(tboolR2R*R1(R5((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR�sN(R&R'R:RR6RRR RRRRRWR]RRR(((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyRjs" > PcCstjdg|d|�S(s�
Uses the prefered cloud-init subprocess def of util.subp
and runs subscription-manager. Breaking this to a
separate function for later use in mocking and unittests
ssubscription-managert logstring(Rtsubp(RFRH((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyRD�sN(t__doc__t cloudinitRtloggingRt getLoggerR&R,tdistrosR%t ExceptionRtobjectRR<RD(((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyt<module>'s 5�H |