Lower the maximum block size used with rsync down to the maximum acceptable size...
[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,
44             platform=None, ssh_access=False, last_update=None, fqdn=None,
45             join_time=None, permission=None):
46         self.name = name
47         if owner:
48             self.owner = (owner, owner_email)
49         else:
50             self.owner = None
51         if join_time is None:
52             self.join_time = time.time()
53         else:
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
60         self.fqdn = fqdn
61         self.last_dead_mail = None
62
63     def __cmp__(self, other):
64         return cmp(self.name, other.name)
65
66     def dead_mail_sent(self):
67         self.last_dead_mail = int(time.time())
68
69     def update_platform(self, new_platform):
70         self.platform = new_platform
71
72     def update_owner(self, new_owner, new_owner_email):
73         if new_owner is None:
74             self.owner = None
75             self.owner_email = None
76         else:
77             self.owner = (new_owner, new_owner_email)
78
79
80 class HostDatabase(object):
81     """Host database."""
82
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)
86
87     def deletehost(self, name):
88         """Remove a host."""
89         raise NotImplementedError(self.deletehost)
90
91     def hosts(self):
92         """Retrieve an iterable over all hosts."""
93         raise NotImplementedError(self.hosts)
94
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))
98         for row in cursor:
99             yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
100
101     def host_ages(self):
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")
103         for row in cursor:
104             yield Host(row[0], owner=row[1], owner_email=row[2], last_update=row[3])
105
106     def __getitem__(self, name):
107         """Find a host by name."""
108         raise NotImplementedError(self.host)
109
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"
114
115         for host in self.hosts():
116             if host.owner:
117                 yield "# %s, owner: %s <%s>\n" % (host.name, host.owner[0], host.owner[1])
118             else:
119                 yield "# %s, owner unknown\n" % (host.name,);
120             if host.password:
121                 yield "%s:%s\n\n" % (host.name, host.password)
122             else:
123                 yield "# %s password is unknown\n\n" % host.name
124
125     def commit(self):
126         pass