b19566941fa177f78a650c5b67209de44d1f5767
[build-farm.git] / buildfarm / hostdb.py
1 #!/usr/bin/python
2
3 # Samba.org buildfarm
4 # Copyright (C) 2008 Andrew Bartlett <abartlet@samba.org>
5 # Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
6 #
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.
11 #
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.
16 #
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/>.
19 #
20
21 import time
22
23
24 class HostAlreadyExists(Exception):
25     """The specified host already exists."""
26
27     def __init__(self, name):
28         super(HostAlreadyExists, self).__init__()
29         self.name = name
30
31
32 class NoSuchHost(Exception):
33     """The specified host did not exist."""
34
35     def __init__(self, name):
36         super(NoSuchHost, self).__init__()
37         self.name = name
38
39
40 class Host(object):
41     """A host in the buildfarm."""
42
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):
45         self.name = name
46         if owner:
47             self.owner = (owner, owner_email)
48         else:
49             self.owner = None
50         if join_time is None:
51             self.join_time = time.time()
52         else:
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
59         self.fqdn = fqdn
60         self.last_dead_mail = None
61
62     def __cmp__(self, other):
63         return cmp(self.name, other.name)
64
65     def dead_mail_sent(self):
66         self.last_dead_mail = int(time.time())
67
68     def update_platform(self, new_platform):
69         self.platform = new_platform
70
71     def update_owner(self, new_owner, new_owner_email):
72         if new_owner is None:
73             self.owner = None
74             self.owner_email = None
75         else:
76             self.owner = (new_owner, new_owner_email)
77
78
79 class HostDatabase(object):
80     """Host database."""
81
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)
85
86     def deletehost(self, name):
87         """Remove a host."""
88         raise NotImplementedError(self.deletehost)
89
90     def hosts(self):
91         """Retrieve an iterable over all hosts."""
92         raise NotImplementedError(self.hosts)
93
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))
97         for row in cursor:
98             yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
99
100     def host_ages(self):
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")
102         for row in cursor:
103             yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
104
105     def __getitem__(self, name):
106         """Find a host by name."""
107         raise NotImplementedError(self.host)
108
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"
113
114         for host in self.hosts():
115             if host.owner:
116                 yield "# %s, owner: %s <%s>\n" % (host.name, host.owner[0], host.owner[1])
117             else:
118                 yield "# %s, owner unknown\n" % (host.name,);
119             if host.password:
120                 yield "%s:%s\n\n" % (host.name, host.password)
121             else:
122                 yield "# %s password is unknown\n\n" % host.name
123
124     def create_hosts_list(self):
125         """Write out the web/"""
126
127         for host in self.hosts():
128             yield "%s: %s\n" % (host.name, host.platform.encode("utf-8"))
129
130     def commit(self):
131         pass
132
133
134 class PlainTextHostDatabase(HostDatabase):
135
136     def __init__(self, hosts):
137         self._hosts = hosts
138
139     @classmethod
140     def from_file(cls, path):
141         ret = {}
142         f = open(path, 'r')
143         try:
144             for l in f:
145                 (host, platform) = l.split(":", 1)
146                 ret[host] = platform.strip().decode("utf-8")
147         finally:
148             f.close()
149         return cls(ret)
150
151     def hosts(self):
152         for name, platform in self._hosts.iteritems():
153             yield Host(name, platform=platform)
154
155     def __getitem__(self, name):
156         try:
157             return Host(name=name, platform=self._hosts[name])
158         except KeyError:
159             raise NoSuchHost(name)