]>
git.ipfire.org Git - ipfire.org.git/blob - www/webapp/backend/mirrors.py
6 import tornado
.httpclient
8 from databases
import Databases
9 from geoip
import GeoIP
10 from misc
import Singleton
12 class Mirrors(object):
13 __metaclass__
= Singleton
17 return Databases().webapp
20 return [Mirror(m
.id) for m
in self
.db
.query("SELECT id FROM mirrors ORDER BY state")]
23 for mirror
in self
.list():
29 def get_by_hostname(self
, hostname
):
30 mirror
= self
.db
.get("SELECT id FROM mirrors WHERE hostname=%s", hostname
)
32 return Mirror(mirror
.id)
34 def get_with_file(self
, filename
):
35 return [Mirror(m
.mirror
) for m
in \
36 self
.db
.query("SELECT mirror FROM mirror_files WHERE filename=%s", filename
)]
40 def __init__(self
, id):
47 return Databases().webapp
50 self
._info
= self
.db
.get("SELECT * FROM mirrors WHERE id=%s", self
.id)
51 self
._info
["url"] = self
.generate_url()
53 def generate_url(self
):
54 url
= "http://%s" % self
.hostname
55 if not self
.path
.startswith("/"):
57 url
+= "%s" % self
.path
58 if not self
.path
.endswith("/"):
62 def __getattr__(self
, key
):
64 return self
._info
[key
]
66 raise AttributeError(key
)
70 return socket
.gethostbyname(self
.hostname
)
73 def country_code(self
):
74 return GeoIP().get_country(self
.address
).lower() or "unknown"
78 filelist
= self
.db
.query("SELECT filename FROM mirror_files WHERE mirror=%s ORDER BY filename", self
.id)
79 return [f
.filename
for f
in filelist
]
81 def set_state(self
, state
):
82 logging
.info("Setting state of %s to %s" % (self
.hostname
, state
))
84 if self
.state
== state
:
87 self
.db
.execute("UPDATE mirrors SET state=%s WHERE id=%s",
90 # Reload changed settings
94 logging
.info("Running check for mirror %s" % self
.hostname
)
96 self
.check_timestamp()
99 def check_state(self
):
100 logging
.debug("Checking state of mirror %s" % self
.id)
102 if self
.disabled
== "Y":
103 self
.set_state("DOWN")
105 time_diff
= time
.time() - self
.last_update
106 if time_diff
> 3*24*60*60: # XXX get this into Settings
107 self
.set_state("DOWN")
108 elif time_diff
> 6*60*60:
109 self
.set_state("OUTOFSYNC")
113 def check_timestamp(self
):
114 if self
.releases
== "N":
117 http
= tornado
.httpclient
.AsyncHTTPClient()
119 http
.fetch(self
.url
+ ".timestamp",
120 headers
={"Pragma" : "no-cache", },
121 callback
=self
.__check
_timestamp
_response
)
123 def __check_timestamp_response(self
, response
):
125 logging
.debug("Error getting timestamp from %s" % self
.hostname
)
129 timestamp
= int(response
.body
.strip())
133 self
.db
.execute("UPDATE mirrors SET last_update=%s WHERE id=%s",
136 # Reload changed settings
141 logging
.info("Successfully updated timestamp from %s" % self
.hostname
)
143 def check_filelist(self
):
144 if self
.releases
== "N":
147 http
= tornado
.httpclient
.AsyncHTTPClient()
149 http
.fetch(self
.url
+ ".filelist",
150 headers
={"Pragma" : "no-cache", },
151 callback
=self
.__check
_filelist
_response
)
153 def __check_filelist_response(self
, response
):
155 logging
.debug("Error getting timestamp from %s" % self
.hostname
)
158 self
.db
.execute("DELETE FROM mirror_files WHERE mirror=%s", self
.id)
160 for file in response
.body
.splitlines():
161 self
.db
.execute("INSERT INTO mirror_files(mirror, filename) VALUES(%s, %s)",
164 logging
.info("Successfully updated mirror filelist from %s" % self
.hostname
)
167 if __name__
== "__main__":
170 for mirror
in m
.list():
171 print mirror
.hostname
, mirror
.country_code