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, platform=None,
44 ssh_access=False, last_update=None, fqdn=None, join_time=None, permission=None):
47 self.owner = (owner, owner_email)
51 self.join_time = time.time()
53 self.join_time = join_time
54 self.permission = permission
55 self.password = password
56 self.platform = platform
57 self.ssh_access = ssh_access
58 self.last_update = last_update
60 self.last_dead_mail = None
62 def __cmp__(self, other):
63 return cmp(self.name, other.name)
65 def dead_mail_sent(self):
66 self.last_dead_mail = int(time.time())
68 def update_platform(self, new_platform):
69 self.platform = new_platform
71 def update_owner(self, new_owner, new_owner_email):
74 self.owner_email = None
76 self.owner = (new_owner, new_owner_email)
79 class HostDatabase(object):
82 def createhost(self, name, platform=None, owner=None, owner_email=None, password=None, permission=None):
83 """Create a new host."""
84 raise NotImplementedError(self.createhost)
86 def deletehost(self, name):
88 raise NotImplementedError(self.deletehost)
91 """Retrieve an iterable over all hosts."""
92 raise NotImplementedError(self.hosts)
94 def dead_hosts(self, age):
95 dead_time = time.time() - age
96 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))
98 yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
101 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")
103 yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
105 def __getitem__(self, name):
106 """Find a host by name."""
107 raise NotImplementedError(self.host)
109 def create_rsync_secrets(self):
110 """Write out the rsyncd.secrets"""
111 yield "# rsyncd.secrets file\n"
112 yield "# automatically generated by textfiles.pl. DO NOT EDIT!\n\n"
114 for host in self.hosts():
116 yield "# %s, owner: %s <%s>\n" % (host.name, host.owner[0], host.owner[1])
118 yield "# %s, owner unknown\n" % (host.name,);
120 yield "%s:%s\n\n" % (host.name, host.password)
122 yield "# %s password is unknown\n\n" % host.name
124 def create_hosts_list(self):
125 """Write out the web/"""
127 for host in self.hosts():
128 yield "%s: %s\n" % (host.name, host.platform.encode("utf-8"))
134 class PlainTextHostDatabase(HostDatabase):
136 def __init__(self, hosts):
140 def from_file(cls, path):
145 (host, platform) = l.split(":", 1)
146 ret[host] = platform.strip().decode("utf-8")
152 for name, platform in self._hosts.iteritems():
153 yield Host(name, platform=platform)
155 def __getitem__(self, name):
157 return Host(name=name, platform=self._hosts[name])
159 raise NoSuchHost(name)