]>
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
, base64
=0):
68 c
.execute("SELECT value FROM %(table)s WHERE key = '%(key)s'" \
69 % { "table" : "config",
72 self
.data
= c
.fetchone()[0]
83 c
.execute("SELECT date FROM %(table)s WHERE key = '%(key)s'" \
84 % { "table" : "config",
87 self
.date
= float("%s" % c
.fetchone()[0])
91 return self
.date
or float(0)
95 value
= base64
.b64decode(value
)
99 sql
= "INSERT INTO %(table)s(key, value, date) VALUES('%(key)s', '%(value)s', '%(date)s')" \
100 % { "table" : "config",
103 "date" : time
.time(), }
106 sql
= "UPDATE %(table)s SET value='%(value)s', date='%(date)s' WHERE key='%(key)s'" \
107 % { "table" : "config",
110 "date" : time
.time(), }
116 class DurationsConfig
:
117 def __init__(self
, db
):
120 def get(self
, sort
=0):
122 c
.execute("SELECT duration FROM durations")
124 for value
in c
.fetchall():
125 value
= int("%s" % value
)
126 if value
< 900: # 15min
133 def set(self
, value
):
136 c
.execute("INSERT INTO %(table)s(duration) VALUES('%(value)s')" \
137 % { "table" : "durations",
144 durations
= self
.get()
145 if not len(durations
):
147 for value
in durations
:
149 avg
= sum / len(durations
)
152 def get_eta(self
, timestamp
):
156 eta
= int(timestamp
) + avg
157 return time
.ctime(eta
)
159 class DistccConfig(DatabaseConfig
):
160 def __init__(self
, db
, key
, hostname
, jobs
):
161 DatabaseConfig
.__init
__(self
, db
, key
)
162 self
.hostname
= hostname
166 if not self
.ping() or self
.get() == "0":
168 return "%s:%s/%s,lzo \t# %s" % \
169 (socket
.gethostbyname(self
.hostname
), self
.get(), self
.jobs
or "4", self
.hostname
)
172 if not self
.hostname
:
174 return not os
.system("ping -c1 -w1 %s &>/dev/null" % self
.hostname
)
177 return os
.popen("distcc --version").readlines()
180 def __init__(self
, path
, filetype
):
181 self
.filename
= os
.path
.join(path
, config
["path"][filetype
])
183 # Create the file if not existant
184 if not os
.access(self
.filename
, os
.R_OK
):
185 f
= open(self
.filename
, "w")
191 f
= open(self
.filename
)
196 return ret
or ["Log is empty."]
200 def set(self
, lines
):
201 f
= open(self
.filename
, "w")
202 for line
in base64
.b64decode(lines
).split("\n"):
203 f
.write("%s\n" % line
.rstrip("\n"))
207 def __init__(self
, config
, uuid
):
210 self
.path
= os
.path
.join(self
.config
['path']['db'], self
.uuid
)
212 if not os
.access(self
.path
, os
.R_OK
):
218 self
.db
= Database(self
.path
)
220 self
.hostname
= DatabaseConfig(self
.db
, "hostname")
221 self
.state
= DatabaseConfig(self
.db
, "state")
222 self
.package
= DatabaseConfig(self
.db
, "package")
223 self
.target
= DatabaseConfig(self
.db
, "target")
225 self
.duration
= DurationsConfig(self
.db
)
226 self
.jobs
= DatabaseConfig(self
.db
, "jobs")
227 self
.distcc
= DistccConfig(self
.db
, "distcc", self
.hostname(), self
.jobs())
228 self
.cpu
= DatabaseConfig(self
.db
, "cpu", base64
=1)
229 self
.machine
= DatabaseConfig(self
.db
, "machine")
231 self
.log
= FileConfig(self
.path
, "log")
233 # If host was longer than 3 days in state compiling we set it as idle.
234 if self
.state() == "compiling" and \
235 (time
.time() - self
.state
.time()) > 3*24*60*60:
236 self
.state
.set("idle")
238 # If host is idle and distcc is not disabled we set it as distcc host.
239 if self
.state() == "idle" and self
.distcc() != "0":
240 self
.state
.set("distcc")
242 # If host is longer than 24h in error state we set it as distcc host.
243 if self
.state() == "error" and \
244 (time
.time() - self
.state
.time()) > 24*60*60:
245 self
.state
.set("distcc")
247 # If host was longer than two weeks in distcc state we set it as unknown.
248 if self
.state() == "error" and \
249 (time
.time() - self
.state
.time()) > 2*7*24*60*60:
250 self
.state
.set("unknown")
252 # If host was longer than four weels in distcc state we delete it.
253 if self
.state() == "unknown" and \
254 (time
.time() - self
.state
.time()) > 4*7*24*60*60:
256 shutil
.rmtree(self
.path
)
258 def set(self
, key
, value
):
259 eval("self.%s.set(\"%s\")" % (key
, value
,))
262 return eval("self.%s.get()" % (key
,))
264 def getAllBuilders(age
=0):
266 for uuid
in os
.listdir(config
["path"]["db"]):
267 if uuid
== "empty.txt": continue
268 builder
= Builder(config
, uuid
)
269 # If there was no activity since "age" days -> continue...
270 if age
and (time
.time() - builder
.state
.time()) > age
*24*60*60:
272 builders
.append(builder
)