oB]c@sdZddlmZddlmZejeZddgZdZ de fdYZ d e fd YZ ed Zd S( 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: password: activation-key: org: auto-attach: service-level: add-pool: enable-repo: disable-repo: rhsm-baseurl: server-hostname: i(tlog(tutiltfedoratrhelc Cst|d|}|js2|jd|dS|jsyi|j\}}|tk rnt|n|j}|stdn|j r|j dk r|j stdq |j jdn7|j r |j stdq |j jdn|jdk rxt|jtsBd} t| n|j|j} | sxtd j|jqxn|j} | std n|jd Wqtk r} |jt| |jd qXn |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_keystTruetSubscriptionErrort rhn_registert auto_attacht servicelevelt_set_service_levelRt_set_auto_attachtpoolst isinstancetlisttaddPooltformatt update_repost log_successtlog_warntstr( tnametcfgt_cloudRt_argstsmtverifyt verify_msgtcontt pool_failt return_statte((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pythandle1sH         R cBseZRS((t__name__t __module__(((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR fsRc BseZdddddddddd d g Zdd Zd Zd ZdZdZdZ dZ dZ dZ dZ dZdZdZRS(torgsactivation-keytusernametpasswords disable-repos enable-reposadd-pools rhsm-baseurlsserver-hostnames auto-attachs service-levelcCs*|dkrt}n||_||_|jjdi|_|jjd|_|jjd|_|jjd|_|jjd|_ |jjd|_ |jjd|_ |jjd|_ |jjd |_ |jjd |_|jjd |_|jjd |_dS( Ntrh_subscriptions rhsm-baseurlsserver-hostnamesadd-poolsactivation-keyR(R)R*s auto-attachs enable-repos disable-repos service-level(RtLOGRRtgettrhel_cfgt rhsm_baseurltserver_hostnameRtactivation_keyR(tuseridR*R t enable_repot disable_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.pyRscCs|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.pyRscCsxH|jD]=}||jkr dj|dj|j}t|fSq W|jdk rtj|jp{tj |j rd}t|fS|j dk r|j stj t |jrd}t|fSt dfS(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.pyRs  cCsH|jdk r|jdk rddj|jdj|jg}|jdk rp|jdj|jn|jdk r|jdj|jnyt|dtd}Wqt j k r}|j dkr|j d j|j ntSXn|jdk r|jdk rdd j|jd j|jg}|jdk rm|jdj|jn|jdk r|jdj|jnyt|dtd}Wqt j k r}|j dkr|j d j|j ntSXn|j d |j d tS|jddj}|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     cCsdddj|jg}yt|d}Wntjk r}|jjdkrxX|jjdD]"}|dkrl|j|qlqlWn|jdj|j j t SXx6|jdD]%}|dkr|j j |qqWtS(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.pyRs     cCsddg}yt|d}Wn-tjk rO}|jdj|tSXx6|jdD]%}|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.pyRs  cCs~g}g}dddg}t|d}|jjd}dddg}t|d}|jjd}||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 _getPoolsscCsddg}t|d}g}xF|jdD]5}d|kr2|j|jddjq2q2Wddg}t|d}g}xF|jdD]5}d|kr|j|jddjqqW||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(R5RFROt active_repostrepotinactive_repos((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyt _getRepos's  '  'cCs2t|dkr&|jjdtS|j\}}g}dg}xU|D]M}||kr||kr|jdj|qN|jdj|qNWt|dkr.|j|y:t ||jjddj |j dd tSWq.t j k r*}|jd j||tSXnd S( 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|ts\|jdtSt|ts||jdtSt|dkrt|dkr|jj dt S|j \}}g}g}x@|D]8}||kr|j dj |q|j |qWg}g} x@|D]8}||krS|j dj |q(| j |q(Wt|dkrxI|D]>} | |kr|jj d| q}|jdj | q}Wnt| dkrx$| D]} |jj d| qWnd g} t|dkr&| j|nt|dkrH| j|nyt| Wn-tjk r} |jd j | tSXt|dkr|jj d d j|jd dnt|dkr|jj dd j|jddnt 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\t enable_listtenable_list_failtrepoidt disable_listtdisable_list_failtfailRFR$((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyR]sn        $            cCst|jr|jp|jS(N(tboolR2R*R1(R5((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyRsN(R&R'R:RR6RRR RR RRRWR]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.pyRDsN(t__doc__t cloudinitRtloggingRt getLoggerR&R,tdistrosR%t ExceptionR tobjectRR<RD(((sG/usr/lib/python2.7/site-packages/cloudinit/config/cc_rh_subscription.pyt's  5H