]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/mirrors.py
11 def __init__(self
, pakfire
):
12 self
.pakfire
= pakfire
14 self
.db
= self
.pakfire
.geoip_db
16 def __encode_ip(self
, addr
):
17 # We get a tuple if there were proxy headers.
18 addr
= addr
.split(", ")
22 # ip is calculated as described in http://dev.maxmind.com/geoip/csv
24 a1
, a2
, a3
, a4
= addr
.split(".")
33 return (16777216 * a1
) + (65536 * a2
) + (256 * a3
) + a4
35 def get_all(self
, addr
):
36 addr
= self
.__encode
_ip
(addr
)
39 SELECT * FROM locations \
40 JOIN addresses ON addresses.location = locations.id \
42 %s BETWEEN addresses.start_ip_num AND addresses.end_ip_num \
45 # If location was not determinable
46 if ret
and ret
.latitude
== 0 and ret
.longitude
== 0:
52 class Mirrors(base
.Object
):
56 for mirror
in self
.db
.query("SELECT id FROM mirrors \
57 WHERE NOT status = 'deleted' ORDER BY hostname"):
58 mirror
= Mirror(self
.pakfire
, mirror
.id)
59 mirrors
.append(mirror
)
63 def count(self
, status
=None):
64 query
= "SELECT COUNT(*) AS count FROM mirrors"
68 query
+= " WHERE status = %s"
71 query
= self
.db
.get(query
, *args
)
75 def get_random(self
, limit
=None):
76 query
= "SELECT id FROM mirrors WHERE status = 'enabled' ORDER BY RAND()"
84 for mirror
in self
.db
.query(query
, *args
):
85 mirror
= Mirror(self
.pakfire
, mirror
.id)
86 mirrors
.append(mirror
)
90 def get_by_id(self
, id):
91 mirror
= self
.db
.get("SELECT id FROM mirrors WHERE id = %s", id)
95 return Mirror(self
.pakfire
, mirror
.id)
97 def get_by_hostname(self
, hostname
):
98 mirror
= self
.db
.get("SELECT id FROM mirrors WHERE NOT status = 'deleted' \
99 AND hostname = %s", hostname
)
104 return Mirror(self
.pakfire
, mirror
.id)
106 def get_for_location(self
, addr
):
109 # Get all mirrors in here.
110 _mirrors
= self
.get_all()
113 while len(mirrors
) <= 2 and distance
<= 270:
114 for mirror
in _mirrors
:
115 if not mirror
.enabled
:
118 if mirror
in mirrors
:
121 # Cannot calc the distance for mirrors when their location is unknown.
122 if mirror
.location
is None:
125 if mirror
.distance_to(addr
) <= distance
:
126 mirrors
.append(mirror
)
132 def get_history(self
, limit
=None, offset
=None, mirror
=None, user
=None):
133 query
= "SELECT * FROM mirrors_history"
139 conditions
.append("mirror_id = %s")
140 args
.append(mirror
.id)
143 conditions
.append("user_id = %s")
147 query
+= " WHERE %s" % " AND ".join(conditions
)
149 query
+= " ORDER BY time DESC"
153 query
+= " LIMIT %s,%s"
154 args
+= [offset
, limit
,]
160 for entry
in self
.db
.query(query
, *args
):
161 entry
= logs
.MirrorLogEntry(self
.pakfire
, entry
)
162 entries
.append(entry
)
167 class Mirror(base
.Object
):
168 def __init__(self
, pakfire
, id):
169 base
.Object
.__init
__(self
, pakfire
)
175 self
._location
= None
177 def __cmp__(self
, other
):
178 return cmp(self
.id, other
.id)
181 def create(cls
, pakfire
, hostname
, path
="", owner
=None, contact
=None, user
=None):
182 id = pakfire
.db
.execute("INSERT INTO mirrors(hostname, path, owner, contact) \
183 VALUES(%s, %s, %s, %s)", hostname
, path
, owner
, contact
)
185 mirror
= cls(pakfire
, id)
186 mirror
.log("created", user
=user
)
190 def log(self
, action
, user
=None):
195 self
.db
.execute("INSERT INTO mirrors_history(mirror_id, action, user_id, time) \
196 VALUES(%s, %s, %s, NOW())", self
.id, action
, user_id
)
200 if self
._data
is None:
202 self
.db
.get("SELECT * FROM mirrors WHERE id = %s", self
.id)
206 def set_status(self
, status
, user
=None):
207 assert status
in ("enabled", "disabled", "deleted")
209 if self
.status
== status
:
212 self
.db
.execute("UPDATE mirrors SET status = %s WHERE id = %s",
216 self
._data
["status"] = status
218 # Log the status change.
219 self
.log(status
, user
=user
)
221 def set_hostname(self
, hostname
):
222 if self
.hostname
== hostname
:
225 self
.db
.execute("UPDATE mirrors SET hostname = %s WHERE id = %s",
229 self
._data
["hostname"] = hostname
231 hostname
= property(lambda self
: self
.data
.hostname
, set_hostname
)
235 return self
.data
.path
237 def set_path(self
, path
):
238 if self
.path
== path
:
241 self
.db
.execute("UPDATE mirrors SET path = %s WHERE id = %s",
245 self
._data
["path"] = path
247 path
= property(lambda self
: self
.data
.path
, set_path
)
251 ret
= "http://%s" % self
.hostname
256 if not self
.path
.startswith("/"):
259 if self
.path
.endswith("/"):
266 def set_owner(self
, owner
):
267 if self
.owner
== owner
:
270 self
.db
.execute("UPDATE mirrors SET owner = %s WHERE id = %s",
274 self
._data
["owner"] = owner
276 owner
= property(lambda self
: self
.data
.owner
or "", set_owner
)
278 def set_contact(self
, contact
):
279 if self
.contact
== contact
:
282 self
.db
.execute("UPDATE mirrors SET contact = %s WHERE id = %s",
286 self
._data
["contact"] = contact
288 contact
= property(lambda self
: self
.data
.contact
or "", set_contact
)
292 return self
.data
.status
296 return self
.status
== "enabled"
299 def check_status(self
):
300 return self
.data
.check_status
303 def last_check(self
):
304 return self
.data
.last_check
308 return socket
.gethostbyname(self
.hostname
)
312 if self
._location
is None:
313 self
._location
= self
.geoip
.get_all(self
.address
)
315 return self
._location
318 def country_code(self
):
320 return self
.location
.country_code
327 return self
.location
.latitude
334 return self
.location
.longitude
338 def distance_to(self
, addr
):
339 location
= self
.geoip
.get_all(addr
)
343 #if location.country_code.lower() in self.prefer_for_countries:
347 self
.latitude
- location
.latitude
,
348 self
.longitude
- location
.longitude
352 for i
in distance_vector
:
355 return math
.sqrt(distance
)
357 def get_history(self
, *args
, **kwargs
):
358 kwargs
["mirror"] = self
360 return self
.pakfire
.mirrors
.get_history(*args
, **kwargs
)