4 # Copyright (C) 2008 Andrew Bartlett <abartlet@samba.org>
5 # Copyright (C) 2008 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/>.
29 my ($class, $filename) = @_;
31 my $dbh = DBI->connect("dbi:SQLite:$filename", "", "", {RaiseError => 1, PrintError => 0,
32 ShowErrorStatement => 1, AutoCommit => 0}) or return undef;
34 my $self = { filename => $filename, dbh => $dbh };
43 $self->{dbh}->do("CREATE TABLE host ( name text, owner text, owner_email text, password text, ssh_access int, fqdn text, platform text, permission text, last_dead_mail int, join_time int );");
45 $self->{dbh}->do("CREATE UNIQUE INDEX unique_hostname ON host (name);");
47 $self->{dbh}->do("CREATE TABLE build ( id integer primary key autoincrement, tree text, revision text, host text, compiler text, checksum text, age int, status text, commit_revision text);");
48 $self->{dbh}->do("CREATE UNIQUE INDEX unique_checksum ON build (checksum);");
50 $self->{dbh}->do("CREATE TABLE test_run ( build int, test text, result text, output text);");
51 $self->{dbh}->commit();
54 local $self->{dbh}->{RaiseError} = 0;
55 $self->{dbh}->rollback();
56 print "DB Failure: $@";
62 sub createhost($$$$$$)
64 my ($self, $name, $platform, $owner, $owner_email, $password, $permission) = @_;
65 my $sth = $self->{dbh}->prepare("INSERT INTO host (name, platform, owner, owner_email, password, permission, join_time) VALUES (?,?,?,?,?,?,?)");
68 $sth->execute($name, $platform, $owner, $owner_email, $password, $permission, time());
69 $self->{dbh}->commit();
72 local $self->{dbh}->{RaiseError} = 0;
73 $self->{dbh}->rollback();
74 print "DB Failure: $@";
82 my ($self, $name) = @_;
84 my $sth = $self->{dbh}->prepare("DELETE FROM host WHERE name = ?");
87 $ret = $sth->execute($name);
88 $self->{dbh}->commit();
91 local $self->{dbh}->{RaiseError} = 0;
92 $self->{dbh}->rollback();
93 print "DB Failure: $@";
104 return $self->{dbh}->selectall_arrayref("SELECT * FROM host ORDER BY name", { Slice => {} });
109 my ($self, $age) = @_;
110 my $dead_age = time() - $age;
111 return $self->{dbh}->selectall_arrayref("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) < $dead_age AND ifnull(join_time, 0) < $dead_age GROUP BY host.name having ifnull(MAX(age),0) < $dead_age", { Slice => {} });
117 return $self->{dbh}->selectall_arrayref("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", { Slice => {} });
120 sub sent_dead_mail($$)
122 my ($self, $host) = @_;
125 $changed = $self->{dbh}->do("UPDATE host SET last_dead_mail = ? WHERE name = ?", undef,
127 $self->{dbh}->commit();
130 local $self->{dbh}->{RaiseError} = 0;
131 $self->{dbh}->rollback();
132 print "DB Failure: $@";
136 return ($changed == 1);
141 my ($self, $name) = @_;
143 my $hosts = $self->hosts();
146 return $_ if ($_->{name} eq $name);
152 sub update_platform($$$)
154 my ($self, $name, $new_platform) = @_;
158 $changed = $self->{dbh}->do("UPDATE host SET platform = ? WHERE name = ?", undef,
159 ($new_platform, $name));
160 $self->{dbh}->commit();
163 local $self->{dbh}->{RaiseError} = 0;
164 $self->{dbh}->rollback();
165 print "DB Failure: $@";
169 return ($changed == 1);
172 sub update_owner($$$$)
174 my ($self, $name, $new_owner, $new_owner_email) = @_;
178 $changed = $self->{dbh}->do("UPDATE host SET owner = ?, owner_email = ? WHERE name = ?",
179 undef, ($new_owner, $new_owner_email, $name));
180 $self->{dbh}->commit();
183 local $self->{dbh}->{RaiseError} = 0;
184 $self->{dbh}->rollback();
188 return ($changed == 1);
191 # Write out the rsyncd.secrets
192 sub create_rsync_secrets($)
196 my $hosts = $db->hosts();
200 $res .= "# rsyncd.secrets file\n";
201 $res .= "# automatically generated by textfiles.pl. DO NOT EDIT!\n\n";
204 $res .= "# $_->{name}";
206 $res .= ", owner: $_->{owner} <$_->{owner_email}>\n";
208 $res .= ", owner unknown\n";
210 if ($_->{password}) {
211 $res .= "$_->{name}:$_->{password}\n\n";
213 $res .= "# $->{name} password is unknown\n\n";
221 sub create_hosts_list($)
227 my $hosts = $self->hosts();
230 $res .= "$_->{name}: $_->{platform}\n";