]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/mirrors.py
10 from .decorators
import lazy_property
12 class Mirrors(base
.Object
):
16 for mirror
in self
.db
.query("SELECT id FROM mirrors \
17 WHERE NOT status = 'deleted' ORDER BY hostname"):
18 mirror
= Mirror(self
.pakfire
, mirror
.id)
19 mirrors
.append(mirror
)
23 def count(self
, status
=None):
24 query
= "SELECT COUNT(*) AS count FROM mirrors"
28 query
+= " WHERE status = %s"
31 query
= self
.db
.get(query
, *args
)
35 def get_random(self
, limit
=None):
36 query
= "SELECT id FROM mirrors WHERE status = 'enabled' ORDER BY RAND()"
44 for mirror
in self
.db
.query(query
, *args
):
45 mirror
= Mirror(self
.pakfire
, mirror
.id)
46 mirrors
.append(mirror
)
50 def get_by_id(self
, id):
51 mirror
= self
.db
.get("SELECT id FROM mirrors WHERE id = %s", id)
55 return Mirror(self
.pakfire
, mirror
.id)
57 def get_by_hostname(self
, hostname
):
58 mirror
= self
.db
.get("SELECT id FROM mirrors WHERE NOT status = 'deleted' \
59 AND hostname = %s", hostname
)
64 return Mirror(self
.pakfire
, mirror
.id)
66 def get_for_location(self
, addr
):
67 country_code
= self
.backend
.geoip
.guess_from_address(addr
)
69 # Cannot return any good mirrors if location is unknown
75 # Walk through all mirrors
76 for mirror
in self
.get_all():
77 if not mirror
.enabled
:
80 if mirror
.country_code
== country_code
:
81 mirrors
.append(mirror
)
83 # XXX needs to search for nearby countries
87 def get_history(self
, limit
=None, offset
=None, mirror
=None, user
=None):
88 query
= "SELECT * FROM mirrors_history"
94 conditions
.append("mirror_id = %s")
95 args
.append(mirror
.id)
98 conditions
.append("user_id = %s")
102 query
+= " WHERE %s" % " AND ".join(conditions
)
104 query
+= " ORDER BY time DESC"
108 query
+= " LIMIT %s,%s"
109 args
+= [offset
, limit
,]
115 for entry
in self
.db
.query(query
, *args
):
116 entry
= logs
.MirrorLogEntry(self
.pakfire
, entry
)
117 entries
.append(entry
)
122 class Mirror(base
.Object
):
123 def __init__(self
, pakfire
, id):
124 base
.Object
.__init
__(self
, pakfire
)
130 self
._location
= None
132 def __cmp__(self
, other
):
133 return cmp(self
.id, other
.id)
136 def create(cls
, pakfire
, hostname
, path
="", owner
=None, contact
=None, user
=None):
137 id = pakfire
.db
.execute("INSERT INTO mirrors(hostname, path, owner, contact) \
138 VALUES(%s, %s, %s, %s)", hostname
, path
, owner
, contact
)
140 mirror
= cls(pakfire
, id)
141 mirror
.log("created", user
=user
)
145 def log(self
, action
, user
=None):
150 self
.db
.execute("INSERT INTO mirrors_history(mirror_id, action, user_id, time) \
151 VALUES(%s, %s, %s, NOW())", self
.id, action
, user_id
)
155 if self
._data
is None:
157 self
.db
.get("SELECT * FROM mirrors WHERE id = %s", self
.id)
161 def set_status(self
, status
, user
=None):
162 assert status
in ("enabled", "disabled", "deleted")
164 if self
.status
== status
:
167 self
.db
.execute("UPDATE mirrors SET status = %s WHERE id = %s",
171 self
._data
["status"] = status
173 # Log the status change.
174 self
.log(status
, user
=user
)
176 def set_hostname(self
, hostname
):
177 if self
.hostname
== hostname
:
180 self
.db
.execute("UPDATE mirrors SET hostname = %s WHERE id = %s",
184 self
._data
["hostname"] = hostname
186 hostname
= property(lambda self
: self
.data
.hostname
, set_hostname
)
190 return self
.data
.path
192 def set_path(self
, path
):
193 if self
.path
== path
:
196 self
.db
.execute("UPDATE mirrors SET path = %s WHERE id = %s",
200 self
._data
["path"] = path
202 path
= property(lambda self
: self
.data
.path
, set_path
)
206 ret
= "http://%s" % self
.hostname
211 if not self
.path
.startswith("/"):
214 if self
.path
.endswith("/"):
221 def set_owner(self
, owner
):
222 if self
.owner
== owner
:
225 self
.db
.execute("UPDATE mirrors SET owner = %s WHERE id = %s",
229 self
._data
["owner"] = owner
231 owner
= property(lambda self
: self
.data
.owner
or "", set_owner
)
233 def set_contact(self
, contact
):
234 if self
.contact
== contact
:
237 self
.db
.execute("UPDATE mirrors SET contact = %s WHERE id = %s",
241 self
._data
["contact"] = contact
243 contact
= property(lambda self
: self
.data
.contact
or "", set_contact
)
247 return self
.data
.status
251 return self
.status
== "enabled"
254 def check_status(self
):
255 return self
.data
.check_status
258 def last_check(self
):
259 return self
.data
.last_check
263 return socket
.gethostbyname(self
.hostname
)
266 def country_code(self
):
267 return self
.backend
.geoip
.guess_from_address(self
.address
) or "UNKNOWN"
269 def get_history(self
, *args
, **kwargs
):
270 kwargs
["mirror"] = self
272 return self
.pakfire
.mirrors
.get_history(*args
, **kwargs
)