4 # Copyright (C) 2008 Andrew Bartlett <abartlet@samba.org>
5 # Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
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/>.
24 class HostAlreadyExists(Exception):
25 """The specified host already exists."""
27 def __init__(self, name):
28 super(HostAlreadyExists, self).__init__()
32 class NoSuchHost(Exception):
33 """The specified host did not exist."""
35 def __init__(self, name):
36 super(NoSuchHost, self).__init__()
41 """A host in the buildfarm."""
43 def __init__(self, name, owner=None, owner_email=None, password=None,
44 platform=None, ssh_access=False, last_update=None, fqdn=None,
45 join_time=None, permission=None):
48 self.owner = (owner, owner_email)
52 self.join_time = time.time()
54 self.join_time = join_time
55 self.permission = permission
56 self.password = password
57 self.platform = platform
58 self.ssh_access = ssh_access
59 self.last_update = last_update
61 self.last_dead_mail = None
63 def __cmp__(self, other):
64 return cmp(self.name, other.name)
66 def dead_mail_sent(self):
67 self.last_dead_mail = int(time.time())
69 def update_platform(self, new_platform):
70 self.platform = new_platform
72 def update_owner(self, new_owner, new_owner_email):
75 self.owner_email = None
77 self.owner = (new_owner, new_owner_email)
80 class HostDatabase(object):
83 def createhost(self, name, platform=None, owner=None, owner_email=None, password=None, permission=None):
84 """Create a new host."""
85 raise NotImplementedError(self.createhost)
87 def deletehost(self, name):
89 raise NotImplementedError(self.deletehost)
92 """Retrieve an iterable over all hosts."""
93 raise NotImplementedError(self.hosts)
95 def dead_hosts(self, age):
96 dead_time = time.time() - age
97 cursor = self.store.execute("SELECT host.name AS host, host.owner AS owner, host.owner_email AS owner_email, MAX(age) AS last_update FROM host LEFT JOIN build ON ( host.name == build.host) WHERE ifnull(last_dead_mail, 0) < %d AND ifnull(join_time, 0) < %d GROUP BY host.name having ifnull(MAX(age),0) < %d" % (dead_time, dead_time, dead_time))
99 yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
102 cursor = self.store.execute("SELECT host.name AS host, host.owner AS owner, host.owner_email AS owner_email, MAX(age) AS last_update FROM host LEFT JOIN build ON ( host.name == build.host) GROUP BY host.name ORDER BY age")
104 yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
106 def __getitem__(self, name):
107 """Find a host by name."""
108 raise NotImplementedError(self.host)
110 def create_rsync_secrets(self):
111 """Write out the rsyncd.secrets"""
112 yield "# rsyncd.secrets file\n"
113 yield "# automatically generated by textfiles.pl. DO NOT EDIT!\n\n"
115 for host in self.hosts():
117 yield "# %s, owner: %s <%s>\n" % (host.name, host.owner[0], host.owner[1])
119 yield "# %s, owner unknown\n" % (host.name,);
121 yield "%s:%s\n\n" % (host.name, host.password)
123 yield "# %s password is unknown\n\n" % host.name