
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
`3                 @   s^  d dl mZ d dl mZ d dlmZm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 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 d	Zed
ZedZedZedZedZedZedZedZedZ edZ!edZ"edZ#G dd dej$Z%G dd dej&j'Z(dd Z)G dd de*Z+dd Z,d d! Z-dS )"    )absolute_import)unicode_literals)_loggerN)copy)PRIO_PLUGINCONFIG)ustr)up2dateErrorsz_spacewalk.jsonzCRed Hat Satellite or Spacewalk based repositories will be disabled.z@Red Hat Satellite or Spacewalk channel support will be disabled.zLThere was an error communicating with Red Hat Satellite or Spacewalk server.zIThis system is not registered with Red Hat Satellite or Spacewalk server.z.This system is not subscribed to any channels.zSystemId could not be acquired.z%You can use rhn_register to register.zLThis system is receiving updates from Red Hat Satellite or Spacewalk server.zFor security reasons packages from Red Hat Satellite or Spacewalk based repositories can be verified only with locally installed gpg keys. GPG key '%s' has been rejected.z.Package profile information could not be sent.zIMissing required login information for Red Hat Satellite or Spacewalk: %sz>Spacewalk plugin has to be run under with the root privileges.c                   sR   e Zd ZdZ fddZdd ZdddZd	d
 Zdd Zdd Z	dd Z
  ZS )	SpacewalkZ	spacewalkc                s   t t| j|| || _|| _tjj| jjj	t
| _d| _i | _t| jj| _| j| j| _d| jj kr| jjd}x |D ]\}}| jj||t q|W tjj stjt d| j_| jjsd S tjd | j  d S )NFmainzinitialized Spacewalk plugin)superr
   __init__basecliospathjoinconfZ
persistdirSTORED_CHANNELS_NAMEstored_channels_pathconnected_to_spacewalkup2date_cfgr   Zread_configparsersectionsitems
_set_valuer   dnfutilZ	am_i_rootr   warningMUST_BE_ROOTenableddebugactivate_channels)selfr   r   optionskeyvalue)	__class__ /usr/lib/python3.6/spacewalk.pyr   :   s(    


zSpacewalk.__init__c             C   s   | j jsd S d| jj_d S )NT)r   r    r   ZdemandsZ	root_user)r#   r(   r(   r)   configQ   s    zSpacewalk.configTc          )   C   sd  i }d }d}d }d }| j  }|s(|}nvtjj | _t| j}| jd f}ytjj| jj	d}W n2 t
jk
r } ztjdtt| d S d }~X nX |stjdtt | ji  d S ytjj| jj	d}	W n t
jk
 r } ztjdtt| d S d }~X nX t
jk
r,   tjdtt | ji  d S  t
jk
rT   tjdtttt d S X d| _tjt x,|	D ]$}
|
d rlt|
j ||
d	 < qlW | j| | j j!}x|j D ]\}}|j"|}d }|r|j"d}t#| j}|| j$j% kr| j$j|}x |D ]\}}|j&||t' qW t(|| j j||j	|||||j)|j*d
	}|j+| qW tj,| d S )Nr   ZuseNoSSLForPackages)timeoutz%s
%s
%sz%s
%sz%s %s
%s
%sTversionlabel)	r   proxyr+   	sslcacert
force_httpcached_version
login_infogpgcheckr    )-_read_channels_fileup2date_clientr*   ZinitUp2dateConfigr   get_ssl_ca_certZup2dateAuthZgetLoginInfor   r+   r	   RhnServerExceptionr   errorCOMMUNICATION_ERRORRHN_DISABLEDNOT_REGISTERED_ERROR_write_channels_fileZ
rhnChannelZgetChannelDetailsZCommunicationErrorZNoChannelsErrorNOT_SUBSCRIBED_ERRORCHANNELS_DISABLEDZNoSystemIdErrorNO_SYSTEM_ID_ERRORUSE_RHNREGISTERr   infoUPDATES_FROM_SPACEWALKdictr   r   reposgetr   r   r   r   r   SpacewalkRepor3   r    addr!   )r#   Z
networkingZenabled_channelsr/   r0   Z	proxy_urlr2   Zcached_channelseZsvrChannelschannelrD   Z
channel_idZchannel_dictZcached_channelr1   r   r$   r%   r&   repor(   r(   r)   r"   V   s~    










zSpacewalk.activate_channelsc             C   s   | j jsdS | jsdS | jd dkr8| j }tjj| ytjj| j j	d W n4 t
jk
r } ztjdtt| W Y dd}~X nX dS )z, Update system's profile after transaction. NZwriteChangesToLog   )r+   z%s
%s
%s)r   r    r   r   _make_package_deltar5   ZrhnPackageInfoZlogDeltaPackagesZupdatePackageProfiler+   r	   r7   r   r8   r9   PROFILE_NOT_SENT)r#   deltarH   r(   r(   r)   transaction   s    zSpacewalk.transactionc             C   s   y.t | jd}|j }tj|}|S Q R X W nX ttfk
rb } z|jtjkrR W Y d d }~X n& tj	j
k
r } zW Y d d }~X nX i S )Nr)openr   readjsonloadsFileNotFoundErrorIOErrorerrnoENOENTdecoderZJSONDecodeError)r#   channels_fileZcontentZchannelsrH   r(   r(   r)   r4      s    
zSpacewalk._read_channels_filec             C   sf   y,t | jd}tj||dd W d Q R X W n4 ttfk
r` } z|jtjkrP W Y d d }~X nX d S )Nw   )indent)rQ   r   rS   dumprU   rV   rW   rX   )r#   varrZ   rH   r(   r(   r)   r<      s    zSpacewalk._write_channels_filec             C   s.   dd | j jjD dd | j jjD d}|S )Nc             S   s$   g | ]}|j |j|j|j|jfqS r(   )namer,   releaseepocharch).0pr(   r(   r)   
<listcomp>   s   z1Spacewalk._make_package_delta.<locals>.<listcomp>c             S   s$   g | ]}|j |j|j|j|jfqS r(   )r`   r,   ra   rb   rc   )rd   re   r(   r(   r)   rf      s   )ZaddedZremoved)r   rO   Zinstall_setZ
remove_set)r#   rN   r(   r(   r)   rL      s
    zSpacewalk._make_package_delta)T)__name__
__module____qualname__r`   r   r*   r"   rO   r4   r<   rL   __classcell__r(   r(   )r'   r)   r
   6   s   
Mr
   c                   sD   e Zd ZdZdddddgZ fddZd	d
 Zd fdd	Z  ZS )rF   zB
    Repository object for Spacewalk. Uses up2date libraries.
    zX-RHN-Server-IdzX-RHN-Auth-User-Idz
X-RHN-AuthzX-RHN-Auth-Server-TimezX-RHN-Auth-Expire-Offsetc                sT  t t jt|d |jd t|d  _ fdd|d D  _|jd _|jd _yt	|d	  _
W n< tk
r } z tjttjj| g  _
W Y d d }~X nX |d
 |jdkrd _|jd _d _d _d _d _|jd _|jd _|jd _|jdr$ j  n j  t drP j }|rP j| d S )Nr-   r   r`   c                s   g | ]}|d   j  qS )z	/GET-REQ/)id)rd   url)r#   r(   r)   rf      s    z*SpacewalkRepo.__init__.<locals>.<listcomp>rl   r/   r.   Zgpg_key_urlr,   r1   rK   r2   r   r+   r3   r0   r    set_http_headers) r   rF   r   r   rE   r`   Zbaseurlr/   r.   get_gpg_key_urlsZgpgkeyInvalidGpgKeyLocationr   r   GPG_KEY_REJECTEDr   Zi18nZucdZmetadata_expirer2   Z	keepaliveZ	bandwidthZretriesZthrottler+   r3   r0   enabledisablehasattrcreate_http_headersrm   )r#   rI   ZoptsrH   http_headers)r'   )r#   r)   r      s8    
zSpacewalkRepo.__init__c             C   s|   g }xb| j D ]X}|| jkr.t| }tjj|| j| dkrL|jd|  q|jd|| j| f  qW | jsx|jd |S )N z*%s: 
X-libcurl-Empty-Header-Workaround: *z%s: %sz.X-RHN-Transport-Capability: follow-redirects=3)Nrv   )needed_headersr2   MISSING_HEADERr   ErrorZ	RepoErrorappendr0   )r#   ru   headerr8   r(   r(   r)   rt     s    

z!SpacewalkRepo.create_http_headersTc                s0   t t| j||}| j }|r,|jtj| |S )N)r   rF   _handle_new_remotert   ZsetoptlibrepoZLRO_HTTPHEADER)r#   ZdestdirZmirror_setupZhandleru   )r'   r(   r)   r|     s
    z SpacewalkRepo._handle_new_remote)T)	rg   rh   ri   __doc__rw   r   rt   r|   rj   r(   r(   )r'   r)   rF      s   %rF   c             C   s*   | j  }x|D ]}t|st|qW |S )a  
    Parse the key urls and validate them.

    key_url_string is a space seperated list of gpg key urls that must be
    located in /etc/pkg/rpm-gpg/.
    Return a list of strings containing the key urls.
    Raises InvalidGpgKeyLocation if any of the key urls are invalid.
    )splitis_valid_gpg_key_urlro   )Zkey_url_stringZkey_urlskey_urlr(   r(   r)   rn      s
    	
rn   c               @   s   e Zd ZdS )ro   N)rg   rh   ri   r(   r(   r(   r)   ro   /  s   ro   c             C   sP   | j d}t|dkrdS |\}}|j dkr2dS tjj|}|jdsLdS dS )Nz://   Ffilez/etc/pki/rpm-gpg/T)r   lenlowerr   r   normpath
startswith)r   Zproto_splitprotor   r(   r(   r)   r   2  s    

r   c             C   s4   d| ko| d st | d }t|tkr0|d S |S )NZ	sslCACertr   )ZBadSslCaCertConfigtypelist)r   Zca_certsr(   r(   r)   r6   @  s    r6   ).Z
__future__r   r   Zdnfpluginscorer   r   r   Zdnf.exceptionsrW   rS   r}   r   r   Zdnf.conf.configr   Zup2date_client.up2dateAuthr5   Zup2date_client.configZup2date_client.rhnChannelZup2date_client.rhnPackageInfoZrhn.i18nr   r	   r   r:   r>   r9   r;   r=   r?   r@   rB   rp   rM   rx   r   ZPluginr
   rJ   ZReporF   rn   	Exceptionro   r   r6   r(   r(   r(   r)   <module>   sH    M