]>
Commit | Line | Data |
---|---|---|
f6e6ff79 MT |
1 | #!/usr/bin/python |
2 | ||
c660ff59 | 3 | import datetime |
f6e6ff79 MT |
4 | import logging |
5 | import math | |
6 | import socket | |
c660ff59 MT |
7 | import time |
8 | import tornado.httpclient | |
9 | import urlparse | |
f6e6ff79 | 10 | |
2c909128 MT |
11 | from . import base |
12 | from . import logs | |
f6e6ff79 | 13 | |
c660ff59 MT |
14 | log = logging.getLogger("mirrors") |
15 | log.propagate = 1 | |
16 | ||
d3e7a9fb | 17 | from .decorators import lazy_property |
f6e6ff79 MT |
18 | |
19 | class Mirrors(base.Object): | |
c660ff59 MT |
20 | def __iter__(self): |
21 | res = self.db.query("SELECT * FROM mirrors \ | |
22 | WHERE deleted IS FALSE ORDER BY hostname") | |
f6e6ff79 | 23 | |
c660ff59 MT |
24 | mirrors = [] |
25 | for row in res: | |
26 | mirror = Mirror(self.backend, row.id, data=row) | |
f6e6ff79 MT |
27 | mirrors.append(mirror) |
28 | ||
c660ff59 | 29 | return iter(mirrors) |
f6e6ff79 | 30 | |
c660ff59 MT |
31 | def _get_mirror(self, query, *args): |
32 | res = self.db.get(query, *args) | |
33 | ||
34 | if res: | |
35 | return Mirror(self.backend, res.id, data=res) | |
f6e6ff79 | 36 | |
c660ff59 MT |
37 | def create(self, hostname, path="", owner=None, contact=None, user=None): |
38 | mirror = self._get_mirror("INSERT INTO mirrors(hostname, path, owner, contact) \ | |
39 | VALUES(%s, %s, %s, %s) RETURNING *", hostname, path, owner, contact) | |
f6e6ff79 | 40 | |
c660ff59 MT |
41 | # Log creation |
42 | mirror.log("created", user=user) | |
f6e6ff79 | 43 | |
c660ff59 | 44 | return mirror |
f6e6ff79 MT |
45 | |
46 | def get_random(self, limit=None): | |
47 | query = "SELECT id FROM mirrors WHERE status = 'enabled' ORDER BY RAND()" | |
48 | args = [] | |
49 | ||
50 | if limit: | |
51 | query += " LIMIT %s" | |
52 | args.append(limit) | |
53 | ||
54 | mirrors = [] | |
55 | for mirror in self.db.query(query, *args): | |
56 | mirror = Mirror(self.pakfire, mirror.id) | |
57 | mirrors.append(mirror) | |
58 | ||
59 | return mirrors | |
60 | ||
61 | def get_by_id(self, id): | |
c660ff59 | 62 | return self._get_mirror("SELECT * FROM mirrors WHERE id = %s", id) |
f6e6ff79 MT |
63 | |
64 | def get_by_hostname(self, hostname): | |
c660ff59 MT |
65 | return self._get_mirror("SELECT * FROM mirrors \ |
66 | WHERE hostname = %s AND deleted IS FALSE", hostname) | |
f6e6ff79 | 67 | |
c660ff59 MT |
68 | def get_for_location(self, address): |
69 | country_code = self.backend.geoip.guess_from_address(address) | |
f6e6ff79 | 70 | |
d3e7a9fb MT |
71 | # Cannot return any good mirrors if location is unknown |
72 | if not country_code: | |
73 | return [] | |
f6e6ff79 MT |
74 | |
75 | mirrors = [] | |
f6e6ff79 | 76 | |
d3e7a9fb MT |
77 | # Walk through all mirrors |
78 | for mirror in self.get_all(): | |
79 | if not mirror.enabled: | |
80 | continue | |
f6e6ff79 | 81 | |
d3e7a9fb MT |
82 | if mirror.country_code == country_code: |
83 | mirrors.append(mirror) | |
f6e6ff79 | 84 | |
d3e7a9fb | 85 | # XXX needs to search for nearby countries |
f6e6ff79 MT |
86 | |
87 | return mirrors | |
88 | ||
89 | def get_history(self, limit=None, offset=None, mirror=None, user=None): | |
90 | query = "SELECT * FROM mirrors_history" | |
91 | args = [] | |
92 | ||
93 | conditions = [] | |
94 | ||
95 | if mirror: | |
96 | conditions.append("mirror_id = %s") | |
97 | args.append(mirror.id) | |
98 | ||
99 | if user: | |
100 | conditions.append("user_id = %s") | |
101 | args.append(user.id) | |
102 | ||
103 | if conditions: | |
104 | query += " WHERE %s" % " AND ".join(conditions) | |
105 | ||
106 | query += " ORDER BY time DESC" | |
107 | ||
108 | if limit: | |
109 | if offset: | |
110 | query += " LIMIT %s,%s" | |
111 | args += [offset, limit,] | |
112 | else: | |
113 | query += " LIMIT %s" | |
114 | args += [limit,] | |
115 | ||
116 | entries = [] | |
117 | for entry in self.db.query(query, *args): | |
118 | entry = logs.MirrorLogEntry(self.pakfire, entry) | |
119 | entries.append(entry) | |
120 | ||
121 | return entries | |
122 | ||
c660ff59 MT |
123 | def check(self, **kwargs): |
124 | """ | |
125 | Runs the mirror check for all mirrors | |
126 | """ | |
127 | for mirror in self: | |
128 | with self.db.transaction(): | |
129 | mirror.check(**kwargs) | |
f6e6ff79 | 130 | |
f6e6ff79 | 131 | |
c660ff59 MT |
132 | class Mirror(base.DataObject): |
133 | table = "mirrors" | |
f6e6ff79 | 134 | |
c660ff59 MT |
135 | def __eq__(self, other): |
136 | if isinstance(other, self.__class__): | |
137 | return self.id == other.id | |
f6e6ff79 MT |
138 | |
139 | def log(self, action, user=None): | |
140 | user_id = None | |
141 | if user: | |
142 | user_id = user.id | |
143 | ||
144 | self.db.execute("INSERT INTO mirrors_history(mirror_id, action, user_id, time) \ | |
145 | VALUES(%s, %s, %s, NOW())", self.id, action, user_id) | |
146 | ||
f6e6ff79 | 147 | def set_hostname(self, hostname): |
c660ff59 | 148 | self._set_attribute("hostname", hostname) |
f6e6ff79 MT |
149 | |
150 | hostname = property(lambda self: self.data.hostname, set_hostname) | |
151 | ||
152 | @property | |
153 | def path(self): | |
154 | return self.data.path | |
155 | ||
156 | def set_path(self, path): | |
c660ff59 | 157 | self._set_attribute("path", path) |
f6e6ff79 MT |
158 | |
159 | path = property(lambda self: self.data.path, set_path) | |
160 | ||
161 | @property | |
162 | def url(self): | |
c660ff59 | 163 | return self.make_url() |
f6e6ff79 | 164 | |
c660ff59 MT |
165 | def make_url(self, path=""): |
166 | url = "http://%s%s" % (self.hostname, self.path) | |
f6e6ff79 | 167 | |
c660ff59 MT |
168 | if path.startswith("/"): |
169 | path = path[1:] | |
f6e6ff79 | 170 | |
c660ff59 | 171 | return urlparse.urljoin(url, path) |
f6e6ff79 | 172 | |
c660ff59 MT |
173 | def set_owner(self, owner): |
174 | self._set_attribute("owner", owner) | |
f6e6ff79 | 175 | |
c660ff59 | 176 | owner = property(lambda self: self.data.owner or "", set_owner) |
f6e6ff79 | 177 | |
c660ff59 MT |
178 | def set_contact(self, contact): |
179 | self._set_attribute("contact", contact) | |
f6e6ff79 | 180 | |
c660ff59 | 181 | contact = property(lambda self: self.data.contact or "", set_contact) |
f6e6ff79 | 182 | |
c660ff59 MT |
183 | def check(self, connect_timeout=10, request_timeout=10): |
184 | log.info("Running mirror check for %s" % self.hostname) | |
f6e6ff79 | 185 | |
c660ff59 | 186 | client = tornado.httpclient.HTTPClient() |
f6e6ff79 | 187 | |
c660ff59 MT |
188 | # Get URL for .timestamp |
189 | url = self.make_url(".timestamp") | |
190 | log.debug(" Fetching %s..." % url) | |
f6e6ff79 | 191 | |
c660ff59 MT |
192 | # Record start time |
193 | time_start = time.time() | |
f6e6ff79 | 194 | |
c660ff59 MT |
195 | http_status = None |
196 | last_sync_at = None | |
197 | status = "OK" | |
f6e6ff79 | 198 | |
c660ff59 | 199 | # XXX needs to catch connection resets, DNS errors, etc. |
f6e6ff79 | 200 | |
c660ff59 MT |
201 | try: |
202 | response = client.fetch(url, | |
203 | connect_timeout=connect_timeout, | |
204 | request_timeout=request_timeout) | |
f6e6ff79 | 205 | |
c660ff59 MT |
206 | # We expect the response to be an integer |
207 | # which holds the timestamp of the last sync | |
208 | # in seconds since epoch UTC | |
209 | try: | |
210 | timestamp = int(response.body) | |
211 | except ValueError: | |
212 | raise | |
213 | ||
214 | # Convert to datetime | |
215 | last_sync_at = datetime.datetime.utcfromtimestamp(timestamp) | |
216 | ||
217 | # Must have synced within 24 hours | |
218 | now = datetime.datetime.utcnow() | |
219 | if now - last_sync_at >= datetime.timedelta(hours=24): | |
220 | status = "OUTOFSYNC" | |
221 | ||
222 | except tornado.httpclient.HTTPError as e: | |
223 | http_status = e.code | |
224 | status = "ERROR" | |
225 | ||
226 | finally: | |
227 | response_time = time.time() - time_start | |
228 | ||
229 | # Log check | |
230 | self.db.execute("INSERT INTO mirrors_checks(mirror_id, response_time, \ | |
231 | http_status, last_sync_at, status) VALUES(%s, %s, %s, %s, %s)", | |
232 | self.id, response_time, http_status, last_sync_at, status) | |
233 | ||
234 | @lazy_property | |
235 | def last_check(self): | |
236 | res = self.db.get("SELECT * FROM mirrors_checks \ | |
237 | WHERE mirror_id = %s ORDER BY timestamp DESC LIMIT 1", self.id) | |
238 | ||
239 | return res | |
f6e6ff79 MT |
240 | |
241 | @property | |
c660ff59 MT |
242 | def status(self): |
243 | if self.last_check: | |
244 | return self.last_check.status | |
f6e6ff79 MT |
245 | |
246 | @property | |
c660ff59 MT |
247 | def average_response_time(self): |
248 | res = self.db.get("SELECT AVG(response_time) AS response_time \ | |
249 | FROM mirrors_checks WHERE mirror_id = %s \ | |
250 | AND timestamp >= NOW() - '24 hours'::interval", self.id) | |
251 | ||
252 | return res.response_time | |
f6e6ff79 MT |
253 | |
254 | @property | |
255 | def address(self): | |
256 | return socket.gethostbyname(self.hostname) | |
257 | ||
d3e7a9fb | 258 | @lazy_property |
f6e6ff79 | 259 | def country_code(self): |
d3e7a9fb | 260 | return self.backend.geoip.guess_from_address(self.address) or "UNKNOWN" |
f6e6ff79 MT |
261 | |
262 | def get_history(self, *args, **kwargs): | |
263 | kwargs["mirror"] = self | |
264 | ||
265 | return self.pakfire.mirrors.get_history(*args, **kwargs) |