/
spacewalk-resolver.py
executable file
·141 lines (119 loc) · 4.82 KB
/
spacewalk-resolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
#
# Copyright (c) 2010-2013 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
# ZYpp URL resolver plugin for Spacewalk-like servers
# Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
#
import sys
import os
import re
import logging
import traceback
sys.path.append("/usr/share/rhn/")
from up2date_client import rhnChannel
from up2date_client import up2dateAuth
from up2date_client import up2dateErrors
from inspect import getargspec
# for testing add the relative path to the load path
if "spacewalk-resolver.py" in sys.argv[0]:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '../python'))
from zypp_plugin import Plugin
CONF = "/etc/zypp/zypp.conf"
class SpacewalkResolverPlugin(Plugin):
""" Pure exception handling """
def RESOLVEURL(self, headers, body):
try:
self.doRESOLVEURL(headers, body)
except up2dateErrors.Error as e:
self.answer("ERROR", {}, str(e))
except:
self.answer("ERROR", {}, traceback.format_exc())
""" RESOLVEURL action """
def doRESOLVEURL(self, headers, body):
spacewalk_auth_headers = ['X-RHN-Server-Id',
'X-RHN-Auth-User-Id',
'X-RHN-Auth',
'X-RHN-Auth-Server-Time',
'X-RHN-Auth-Expire-Offset']
if not os.geteuid() == 0:
# you can't access auth data if you are not root
self.answer("ERROR", {}, "Can't access managed repositores without root access")
return
if 'channel' not in headers:
self.answer("ERROR", {}, "Missing argument channel")
return
if 'server' in headers:
server = int(headers['server'])
else:
server = 0
# do we have spacewalk-client-tools with timeout option?
args = getargspec(rhnChannel.getChannelDetails)[0]
timeout = self._getTimeout()
self.auth_headers = {}
if 'timeout' in args:
login_info = up2dateAuth.getLoginInfo(timeout=timeout)
else:
login_info = up2dateAuth.getLoginInfo()
for k,v in list(login_info.items()):
if k in spacewalk_auth_headers:
self.auth_headers[k] = v
if 'timeout' in args:
details = rhnChannel.getChannelDetails(timeout=timeout)
else:
details = rhnChannel.getChannelDetails();
self.channel = None
for channel in details:
if channel['label'] == headers['channel']:
self.channel = channel
if not self.channel:
self.answer("ERROR", {}, "Can't retrieve information for channel %s" % headers['channel'])
return
proxystr = ""
if rhnChannel.config.cfg['enableProxy'] == 1:
proxy_config = rhnChannel.config.getProxySetting()
if proxy_config:
(proxy_host, proxy_port) = proxy_config.split(':')
proxystr = "&proxy=%s&proxyport=%s" % (proxy_host, proxy_port)
# url is a list, use the one provided by the given server
if type(self.channel['url']) == type([]):
self.channel['url'] = self.channel['url'][server]
timeoutstr = ""
if timeout:
timeoutstr = "&timeout=%d" % timeout
url = "%s/GET-REQ/%s?head_requests=no%s%s" % (self.channel['url'],
self.channel['label'],
proxystr,
timeoutstr)
self.answer("RESOLVEDURL", self.auth_headers, url)
def _getTimeout(self):
""" read timeout from config"""
timeout = None
if os.path.exists(CONF):
c = open(CONF, "r")
for line in c:
if line[0] == '#':
continue
match = re.match('\s*download.transfer_timeout\s*=\s*(\d+)', line)
if match:
timeout = int(match.group(1))
break
c.close()
return timeout
plugin = SpacewalkResolverPlugin()
plugin.main()