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