1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import string
20
21 from spacewalk.common import rhnFlags
22 from spacewalk.common.rhnLog import log_debug
23 from spacewalk.server import rhnSQL, apacheAuth
24
25
27 """ Stores the route the client took to get to hosted or the Satellite """
28
29 log_debug(5, server_id)
30
31
32
33
34 h = rhnSQL.prepare("""
35 select position,
36 proxy_server_id,
37 hostname
38 from rhnServerPath
39 where server_id = :server_id
40 order by position
41 """)
42 h.execute(server_id=server_id)
43 oldRoute = h.fetchall_dict() or []
44 newRoute = []
45
46
47 oldRouteTuples = []
48 for oldRouteDict in oldRoute:
49 oldRouteTuples.append((str(oldRouteDict['proxy_server_id']), oldRouteDict['hostname']))
50
51
52
53 if rhnFlags.test('X-RHN-Proxy-Auth'):
54 tokens = string.split(rhnFlags.get('X-RHN-Proxy-Auth'), ',')
55 tokens = [token for token in tokens if token]
56
57 log_debug(4, "route tokens", tokens)
58
59
60 for token in tokens:
61 token, hostname = apacheAuth.splitProxyAuthToken(token)
62 if hostname is None:
63 log_debug(3, "NOTE: Spacewalk Proxy v1.1 detected - route tracking is unsupported")
64 newRoute = []
65 break
66 newRoute.append((token[0], hostname))
67
68 log_debug(4, "newRoute", newRoute)
69
70 if oldRouteTuples == newRoute:
71
72
73 return
74
75 if oldRouteTuples:
76
77 log_debug(8, 'blow away route-info for %s' % server_id)
78 h = rhnSQL.prepare("""
79 delete from rhnServerPath where server_id = :server_id
80 """)
81 h.execute(server_id=server_id)
82
83 if not newRoute:
84 log_debug(3, "No new route to add")
85 rhnSQL.commit()
86 return
87
88 log_debug(8, 'adding route-info entries: %s - %s' % (server_id, newRoute))
89
90 h = rhnSQL.prepare("""
91 insert into rhnServerPath
92 (server_id, proxy_server_id, position, hostname)
93 values (:server_id, :proxy_server_id, :position, :hostname)
94 """)
95 server_ids = []
96 proxy_ids = []
97 proxy_hostnames = []
98 positions = []
99 counter = 0
100 for p in newRoute:
101 proxy_id, proxy_hostname = p[:2]
102 proxy_ids.append(proxy_id)
103 proxy_hostnames.append(proxy_hostname)
104 server_ids.append(server_id)
105 positions.append(counter)
106 counter = counter + 1
107
108 log_debug(5, server_ids, proxy_ids, positions,
109 proxy_hostnames)
110 h.executemany(server_id=server_ids, proxy_server_id=proxy_ids,
111 position=positions, hostname=proxy_hostnames)
112
113 rhnSQL.commit()
114