]>
git.ipfire.org Git - ipfire.org.git/blob - build/builder.py
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
28 from pysqlite2
import dbapi2
as sqlite
32 from constants
import config
35 def __init__(self
, path
):
36 self
.db
= sqlite
.connect(os
.path
.join(path
, config
["db_name"]))
39 create table if not exists config(key, value, date);
40 create table if not exists durations(duration);
52 return self
.db
.cursor()
58 def __init__(self
, db
, key
):
67 c
.execute("SELECT value FROM %(table)s WHERE key = '%(key)s'" \
68 % { "table" : "config",
71 self
.data
= c
.fetchone()[0]
82 c
.execute("SELECT date FROM %(table)s WHERE key = '%(key)s'" \
83 % { "table" : "config",
86 self
.date
= float("%s" % c
.fetchone()[0])
90 return self
.date
or float(0)
96 sql
= "INSERT INTO %(table)s(key, value, date) VALUES('%(key)s', '%(value)s', '%(date)s')" \
97 % { "table" : "config",
100 "date" : time
.time(), }
103 sql
= "UPDATE %(table)s SET value='%(value)s', date='%(date)s' WHERE key='%(key)s'" \
104 % { "table" : "config",
107 "date" : time
.time(), }
113 class DurationsConfig
:
114 def __init__(self
, db
):
117 def get(self
, sort
=0):
119 c
.execute("SELECT duration FROM durations")
121 for value
in c
.fetchall():
122 value
= int("%s" % value
)
123 if value
< 900: # 15min
130 def set(self
, value
):
133 c
.execute("INSERT INTO %(table)s(duration) VALUES('%(value)s')" \
134 % { "table" : "durations",
141 durations
= self
.get()
142 if not len(durations
):
144 for value
in durations
:
146 avg
= sum / len(durations
)
149 def get_eta(self
, timestamp
):
153 eta
= int(timestamp
) + avg
154 return time
.ctime(eta
)
156 class DistccConfig(DatabaseConfig
):
157 def __init__(self
, db
, key
, hostname
, jobs
):
158 DatabaseConfig
.__init
__(self
, db
, key
)
159 self
.hostname
= hostname
163 if not self
.ping() or self
.get() == "0":
165 return "%s:%s/%s,lzo \t# %s" % \
166 (socket
.gethostbyname(self
.hostname
), self
.get(), self
.jobs
or "4", self
.hostname
)
169 if not self
.hostname
:
171 return not os
.system("ping -c1 -w1 %s &>/dev/null" % self
.hostname
)
174 return os
.popen("distcc --version").readlines()
177 def __init__(self
, path
, filetype
):
178 self
.filename
= os
.path
.join(path
, config
["path"][filetype
])
180 # Create the file if not existant
181 if not os
.access(self
.filename
, os
.R_OK
):
182 f
= open(self
.filename
, "w")
188 f
= open(self
.filename
)
193 return ret
or ["Log is empty."]
197 def set(self
, lines
):
198 f
= open(self
.filename
, "w")
199 for line
in base64
.b64decode(lines
).split("\n"):
200 f
.write("%s\n" % line
.rstrip("\n"))
204 def __init__(self
, config
, uuid
):
207 self
.path
= os
.path
.join(self
.config
['path']['db'], self
.uuid
)
209 if not os
.access(self
.path
, os
.R_OK
):
215 self
.db
= Database(self
.path
)
217 self
.hostname
= DatabaseConfig(self
.db
, "hostname")
218 self
.state
= DatabaseConfig(self
.db
, "state")
219 self
.package
= DatabaseConfig(self
.db
, "package")
220 self
.target
= DatabaseConfig(self
.db
, "target")
222 self
.duration
= DurationsConfig(self
.db
)
223 self
.jobs
= DatabaseConfig(self
.db
, "jobs")
224 self
.distcc
= DistccConfig(self
.db
, "distcc", self
.hostname(), self
.jobs())
226 self
.log
= FileConfig(self
.path
, "log")
228 # If host was longer than 3 days in state compiling we set it as idle.
229 if self
.state() == "compiling" and \
230 (time
.time() - self
.state
.time()) > 3*24*60*60:
231 self
.state
.set("idle")
233 # If host is idle and distcc is not disabled we set it as distcc host.
234 if self
.state() == "idle" and self
.distcc() != "0":
235 self
.state
.set("distcc")
237 # If host is longer than 24h in error state we set it as distcc host.
238 if self
.state() == "error" and \
239 (time
.time() - self
.state
.time()) > 24*60*60:
240 self
.state
.set("distcc")
242 # If host was longer than two weeks in distcc state we set it as unknown.
243 if self
.state() == "error" and \
244 (time
.time() - self
.state
.time()) > 2*7*24*60*60:
245 self
.state
.set("unknown")
247 # If host was longer than four weels in distcc state we delete it.
248 if self
.state() == "unknown" and \
249 (time
.time() - self
.state
.time()) > 4*7*24*60*60:
251 shutil
.rmtree(self
.path
)
253 def set(self
, key
, value
):
254 eval("self.%s.set(\"%s\")" % (key
, value
,))
257 return eval("self.%s.get()" % (key
,))
259 def getAllBuilders(age
=0):
261 for uuid
in os
.listdir(config
["path"]["db"]):
262 if uuid
== "empty.txt": continue
263 builder
= Builder(config
, uuid
)
264 # If there was no activity since "age" days -> continue...
265 if age
and (time
.time() - builder
.state
.time()) > age
*24*60*60:
267 builders
.append(builder
)